diff --git a/spring-modulith-core/src/main/java/org/springframework/modulith/core/ApplicationModules.java b/spring-modulith-core/src/main/java/org/springframework/modulith/core/ApplicationModules.java index 0a2e644d..8625d1c2 100644 --- a/spring-modulith-core/src/main/java/org/springframework/modulith/core/ApplicationModules.java +++ b/spring-modulith-core/src/main/java/org/springframework/modulith/core/ApplicationModules.java @@ -275,7 +275,6 @@ public class ApplicationModules implements Iterable { * Returns whether the given {@link JavaClass} is contained within the {@link ApplicationModules}. * * @param type must not be {@literal null}. - * @return */ public boolean contains(JavaClass type) { @@ -285,6 +284,18 @@ public class ApplicationModules implements Iterable { .anyMatch(module -> module.contains(type)); } + /** + * Returns whether the given {@link Class} is contained within the {@link ApplicationModules}. + * + * @param type must not be {@literal null}. + */ + public boolean contains(Class type) { + + Assert.notNull(type, "Type must not be null!"); + + return allClasses.contain(type) && contains(allClasses.get(type)); + } + /** * Returns whether the given type is contained in one of the root packages (not including sub-packages) of the * modules. diff --git a/spring-modulith-runtime/pom.xml b/spring-modulith-runtime/pom.xml index e7cd1422..0e6f6f06 100644 --- a/spring-modulith-runtime/pom.xml +++ b/spring-modulith-runtime/pom.xml @@ -40,6 +40,13 @@ true + + org.springframework.modulith + spring-modulith-starter-test + ${project.version} + test + + org.springframework.boot spring-boot-starter-test diff --git a/spring-modulith-runtime/src/main/java/org/springframework/modulith/runtime/ApplicationModulesRuntime.java b/spring-modulith-runtime/src/main/java/org/springframework/modulith/runtime/ApplicationModulesRuntime.java index 0943c3fa..629f158a 100644 --- a/spring-modulith-runtime/src/main/java/org/springframework/modulith/runtime/ApplicationModulesRuntime.java +++ b/spring-modulith-runtime/src/main/java/org/springframework/modulith/runtime/ApplicationModulesRuntime.java @@ -63,7 +63,9 @@ public class ApplicationModulesRuntime implements Supplier { * @return */ public boolean isApplicationClass(Class type) { - return runtime.isApplicationClass(type); + + return runtime.isApplicationClass(type) + || modules.get().contains(type); } /** diff --git a/spring-modulith-runtime/src/test/java/different/moduleB/ModuleBType.java b/spring-modulith-runtime/src/test/java/different/moduleB/ModuleBType.java new file mode 100644 index 00000000..f9bfb1f4 --- /dev/null +++ b/spring-modulith-runtime/src/test/java/different/moduleB/ModuleBType.java @@ -0,0 +1,21 @@ +/* + * Copyright 2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package different.moduleB; + +/** + * @author Oliver Drotbohm + */ +public class ModuleBType {} diff --git a/spring-modulith-runtime/src/test/java/example/SampleApplication.java b/spring-modulith-runtime/src/test/java/example/SampleApplication.java new file mode 100644 index 00000000..ea9d9bc4 --- /dev/null +++ b/spring-modulith-runtime/src/test/java/example/SampleApplication.java @@ -0,0 +1,26 @@ +/* + * Copyright 2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package example; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.modulith.Modulithic; + +/** + * @author Oliver Drotbohm + */ +@Modulithic(additionalPackages = "different") +@SpringBootApplication +public class SampleApplication {} diff --git a/spring-modulith-runtime/src/test/java/example/moduleA/ModuleAType.java b/spring-modulith-runtime/src/test/java/example/moduleA/ModuleAType.java new file mode 100644 index 00000000..c5350b4d --- /dev/null +++ b/spring-modulith-runtime/src/test/java/example/moduleA/ModuleAType.java @@ -0,0 +1,21 @@ +/* + * Copyright 2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package example.moduleA; + +/** + * @author Oliver Drotbohm + */ +public class ModuleAType {} diff --git a/spring-modulith-runtime/src/test/java/org/springframework/modulith/runtime/ApplicationModulesRuntimeIntegrationTests.java b/spring-modulith-runtime/src/test/java/org/springframework/modulith/runtime/ApplicationModulesRuntimeIntegrationTests.java new file mode 100644 index 00000000..c615c4c3 --- /dev/null +++ b/spring-modulith-runtime/src/test/java/org/springframework/modulith/runtime/ApplicationModulesRuntimeIntegrationTests.java @@ -0,0 +1,52 @@ +/* + * Copyright 2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.modulith.runtime; + +import static org.assertj.core.api.Assertions.*; + +import different.moduleB.ModuleBType; +import example.SampleApplication; +import example.moduleA.ModuleAType; + +import java.util.stream.Stream; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.SpringApplication; +import org.springframework.modulith.core.ApplicationModules; +import org.springframework.modulith.runtime.autoconfigure.TestSpringBootApplicationRuntime; +import org.springframework.modulith.test.TestApplicationModules; + +/** + * Integration tests for {@link ApplicationModulesRuntime}. + * + * @author Oliver Drotbohm + */ +public class ApplicationModulesRuntimeIntegrationTests { + + ApplicationModules modules = TestApplicationModules.of(SampleApplication.class); + + @Test // GH-587 + void detectsTypeInAdditionalPackageAsApplicationType() { + + var context = SpringApplication.run(SampleApplication.class); + var applicationRuntime = new TestSpringBootApplicationRuntime(context); + + var runtime = new ApplicationModulesRuntime(() -> modules, applicationRuntime); + + Stream.of(ModuleAType.class, ModuleBType.class) + .forEach(it -> assertThat(runtime.isApplicationClass(it)).isTrue()); + } +} diff --git a/spring-modulith-runtime/src/test/java/org/springframework/modulith/runtime/autoconfigure/TestSpringBootApplicationRuntime.java b/spring-modulith-runtime/src/test/java/org/springframework/modulith/runtime/autoconfigure/TestSpringBootApplicationRuntime.java new file mode 100644 index 00000000..78e871e0 --- /dev/null +++ b/spring-modulith-runtime/src/test/java/org/springframework/modulith/runtime/autoconfigure/TestSpringBootApplicationRuntime.java @@ -0,0 +1,28 @@ +/* + * Copyright 2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.modulith.runtime.autoconfigure; + +import org.springframework.context.ApplicationContext; + +/** + * @author Oliver Drotbohm + */ +public class TestSpringBootApplicationRuntime extends SpringBootApplicationRuntime { + + public TestSpringBootApplicationRuntime(ApplicationContext context) { + super(context); + } +} diff --git a/spring-modulith-runtime/src/test/resources/application.properties b/spring-modulith-runtime/src/test/resources/application.properties new file mode 100644 index 00000000..b92adaf4 --- /dev/null +++ b/spring-modulith-runtime/src/test/resources/application.properties @@ -0,0 +1 @@ +spring.main.banner-mode=OFF diff --git a/spring-modulith-runtime/src/test/resources/logback.xml b/spring-modulith-runtime/src/test/resources/logback.xml index 0f35da97..993299ee 100644 --- a/spring-modulith-runtime/src/test/resources/logback.xml +++ b/spring-modulith-runtime/src/test/resources/logback.xml @@ -9,6 +9,6 @@ - + diff --git a/spring-modulith-test/src/main/java/org/springframework/modulith/test/TestApplicationModules.java b/spring-modulith-test/src/main/java/org/springframework/modulith/test/TestApplicationModules.java index 8b4566ac..07e3ed14 100644 --- a/spring-modulith-test/src/main/java/org/springframework/modulith/test/TestApplicationModules.java +++ b/spring-modulith-test/src/main/java/org/springframework/modulith/test/TestApplicationModules.java @@ -15,6 +15,7 @@ */ package org.springframework.modulith.test; +import java.util.ArrayList; import java.util.List; import org.springframework.modulith.core.ApplicationModules; @@ -53,7 +54,11 @@ public class TestApplicationModules { } private static ApplicationModules of(ModulithMetadata metadata, String basePackage) { - return new ApplicationModules(metadata, List.of(basePackage), DescribedPredicate.alwaysFalse(), false, + + var packages = new ArrayList<>(List.of(basePackage)); + packages.addAll(metadata.getAdditionalPackages()); + + return new ApplicationModules(metadata, packages, DescribedPredicate.alwaysFalse(), false, new ImportOption.OnlyIncludeTests()) {}; }