Skip to content

Commit 04c1203

Browse files
committed
GH-587 - Additional modulith packages are now considered for application classes.
1 parent a7145c3 commit 04c1203

File tree

11 files changed

+178
-4
lines changed

11 files changed

+178
-4
lines changed

spring-modulith-core/src/main/java/org/springframework/modulith/core/ApplicationModules.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,6 @@ public Set<ApplicationModule> getSharedModules() {
275275
* Returns whether the given {@link JavaClass} is contained within the {@link ApplicationModules}.
276276
*
277277
* @param type must not be {@literal null}.
278-
* @return
279278
*/
280279
public boolean contains(JavaClass type) {
281280

@@ -285,6 +284,18 @@ public boolean contains(JavaClass type) {
285284
.anyMatch(module -> module.contains(type));
286285
}
287286

287+
/**
288+
* Returns whether the given {@link Class} is contained within the {@link ApplicationModules}.
289+
*
290+
* @param type must not be {@literal null}.
291+
*/
292+
public boolean contains(Class<?> type) {
293+
294+
Assert.notNull(type, "Type must not be null!");
295+
296+
return allClasses.contain(type) && contains(allClasses.get(type));
297+
}
298+
288299
/**
289300
* Returns whether the given type is contained in one of the root packages (not including sub-packages) of the
290301
* modules.

spring-modulith-runtime/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@
4040
<optional>true</optional>
4141
</dependency>
4242

43+
<dependency>
44+
<groupId>org.springframework.modulith</groupId>
45+
<artifactId>spring-modulith-starter-test</artifactId>
46+
<version>${project.version}</version>
47+
<scope>test</scope>
48+
</dependency>
49+
4350
<dependency>
4451
<groupId>org.springframework.boot</groupId>
4552
<artifactId>spring-boot-starter-test</artifactId>

spring-modulith-runtime/src/main/java/org/springframework/modulith/runtime/ApplicationModulesRuntime.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ public ApplicationModules get() {
6363
* @return
6464
*/
6565
public boolean isApplicationClass(Class<?> type) {
66-
return runtime.isApplicationClass(type);
66+
67+
return runtime.isApplicationClass(type)
68+
|| modules.get().contains(type);
6769
}
6870

6971
/**
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package different.moduleB;
17+
18+
/**
19+
* @author Oliver Drotbohm
20+
*/
21+
public class ModuleBType {}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example;
17+
18+
import org.springframework.boot.autoconfigure.SpringBootApplication;
19+
import org.springframework.modulith.Modulithic;
20+
21+
/**
22+
* @author Oliver Drotbohm
23+
*/
24+
@Modulithic(additionalPackages = "different")
25+
@SpringBootApplication
26+
public class SampleApplication {}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example.moduleA;
17+
18+
/**
19+
* @author Oliver Drotbohm
20+
*/
21+
public class ModuleAType {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.modulith.runtime;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import different.moduleB.ModuleBType;
21+
import example.SampleApplication;
22+
import example.moduleA.ModuleAType;
23+
24+
import java.util.stream.Stream;
25+
26+
import org.junit.jupiter.api.Test;
27+
import org.springframework.boot.SpringApplication;
28+
import org.springframework.modulith.core.ApplicationModules;
29+
import org.springframework.modulith.runtime.autoconfigure.TestSpringBootApplicationRuntime;
30+
import org.springframework.modulith.test.TestApplicationModules;
31+
32+
/**
33+
* Integration tests for {@link ApplicationModulesRuntime}.
34+
*
35+
* @author Oliver Drotbohm
36+
*/
37+
public class ApplicationModulesRuntimeIntegrationTests {
38+
39+
ApplicationModules modules = TestApplicationModules.of(SampleApplication.class);
40+
41+
@Test // GH-587
42+
void detectsTypeInAdditionalPackageAsApplicationType() {
43+
44+
var context = SpringApplication.run(SampleApplication.class);
45+
var applicationRuntime = new TestSpringBootApplicationRuntime(context);
46+
47+
var runtime = new ApplicationModulesRuntime(() -> modules, applicationRuntime);
48+
49+
Stream.of(ModuleAType.class, ModuleBType.class)
50+
.forEach(it -> assertThat(runtime.isApplicationClass(it)).isTrue());
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.modulith.runtime.autoconfigure;
17+
18+
import org.springframework.context.ApplicationContext;
19+
20+
/**
21+
* @author Oliver Drotbohm
22+
*/
23+
public class TestSpringBootApplicationRuntime extends SpringBootApplicationRuntime {
24+
25+
public TestSpringBootApplicationRuntime(ApplicationContext context) {
26+
super(context);
27+
}
28+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
spring.main.banner-mode=OFF

spring-modulith-runtime/src/test/resources/logback.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
</root>
1010

1111
<logger name="org.springframework.modulith" level="DEBUG"/>
12-
<logger name="example" level="INFO" />
12+
<logger name="example" level="WARN" />
1313

1414
</configuration>

0 commit comments

Comments
 (0)