GH-613 - Add SPI to support external ApplicationModuleSource contributions.

We now expose ApplicationModuleSourceFactory as Spring Factories-based SPI interface to further contribute ApplicationModuleSource instances either from a provided root package subject for module detection through a (potentially customized) ApplicationModuleDetectionStrategy or by explicitly listing particular module base packages.
This commit is contained in:
Oliver Drotbohm
2024-09-04 08:51:19 +02:00
parent b370d5a3f2
commit 1062f53bfa
19 changed files with 697 additions and 78 deletions

View File

@@ -242,6 +242,25 @@ class ApplicationModulesIntegrationTest {
assertThatIllegalArgumentException().isThrownBy(() -> ApplicationModules.of("non.existant"));
}
@Test // GH-613
void detectsContributedApplicationModules() {
var location = ApplicationModuleSourceContributions.LOCATION;
ApplicationModuleSourceContributions.LOCATION = "META-INF/spring-test.factories";
try {
var modules = org.springframework.modulith.test.TestUtils.createApplicationModules(Application.class);
assertThat(modules.getModuleByName("detected")).isNotEmpty();
assertThat(modules.getModuleByName("enumerated")).isNotEmpty();
} finally {
ApplicationModuleSourceContributions.LOCATION = location;
}
}
private static void verifyNamedInterfaces(NamedInterfaces interfaces, String name, Class<?>... types) {
Stream.of(types).forEach(type -> {

View File

@@ -23,6 +23,7 @@ import org.springframework.boot.test.context.SpringBootContextLoader;
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.modulith.core.ApplicationModules;
import org.springframework.test.context.BootstrapContext;
import org.springframework.test.context.CacheAwareContextLoaderDelegate;
import org.springframework.test.context.ContextLoadException;
@@ -30,11 +31,25 @@ import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate;
import org.springframework.test.context.support.DefaultBootstrapContext;
import com.tngtech.archunit.base.DescribedPredicate;
import com.tngtech.archunit.core.domain.JavaClass;
/**
* @author Oliver Drotbohm
*/
public class TestUtils {
/**
* Creates a fresh {@link ApplicationModules} instance no matter the caching state.
*
* @param type must not be {@literal null}.
* @return will never be {@literal null}.
* @since 1.3
*/
public static ApplicationModules createApplicationModules(Class<?> type) {
return ApplicationModules.of(type, cacheInvalidator());
}
public static void assertDependencyMissing(Class<?> testClass, Class<?> expectedMissingDependency) {
CacheAwareContextLoaderDelegate delegate = new DefaultCacheAwareContextLoaderDelegate();
@@ -72,4 +87,15 @@ public class TestUtils {
private static RuntimeException asRuntimeException(Throwable o_O) {
return o_O instanceof RuntimeException ? (RuntimeException) o_O : new RuntimeException(o_O);
}
private static DescribedPredicate<JavaClass> cacheInvalidator() {
return new DescribedPredicate<JavaClass>("invalidator") {
@Override
public boolean test(JavaClass t) {
return false;
}
};
}
}