GH-610 - Additional modulith packages are now considered for application classes.

This commit is contained in:
Oliver Drotbohm
2024-05-20 17:23:15 +02:00
parent f1ae4af0fb
commit 2c1b2aa371
11 changed files with 187 additions and 5 deletions

View File

@@ -261,7 +261,6 @@ public class ApplicationModules implements Iterable<ApplicationModule> {
* 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) {
@@ -271,6 +270,18 @@ public class ApplicationModules implements Iterable<ApplicationModule> {
.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.

View File

@@ -40,6 +40,13 @@
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-starter-test</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>

View File

@@ -63,7 +63,9 @@ public class ApplicationModulesRuntime implements Supplier<ApplicationModules> {
* @return
*/
public boolean isApplicationClass(Class<?> type) {
return runtime.isApplicationClass(type);
return runtime.isApplicationClass(type)
|| modules.get().contains(type);
}
/**

View File

@@ -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 {}

View File

@@ -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 {}

View File

@@ -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 {}

View File

@@ -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-610
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());
}
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1 @@
spring.main.banner-mode=OFF

View File

@@ -9,6 +9,6 @@
</root>
<logger name="org.springframework.modulith" level="DEBUG"/>
<logger name="example" level="INFO" />
<logger name="example" level="WARN" />
</configuration>

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.modulith.test;
import java.util.ArrayList;
import java.util.List;
import org.springframework.modulith.core.ApplicationModules;
@@ -37,7 +38,19 @@ public class TestApplicationModules {
* @return
*/
public static ApplicationModules of(String basePackage) {
return new ApplicationModules(ModulithMetadata.of(basePackage), List.of(basePackage),
DescribedPredicate.alwaysFalse(), false, new ImportOption.OnlyIncludeTests()) {};
return of(ModulithMetadata.of(basePackage), basePackage);
}
public static ApplicationModules of(Class<?> type) {
return of(ModulithMetadata.of(type), type.getPackageName());
}
private static ApplicationModules of(ModulithMetadata metadata, String basePackage) {
var packages = new ArrayList<>(List.of(basePackage));
packages.addAll(metadata.getAdditionalPackages());
return new ApplicationModules(metadata, packages, DescribedPredicate.alwaysFalse(), false,
new ImportOption.OnlyIncludeTests()) {};
}
}