|
23 | 23 | import java.util.Objects;
|
24 | 24 | import java.util.Optional;
|
25 | 25 | import java.util.function.Supplier;
|
| 26 | +import java.util.stream.Collectors; |
26 | 27 | import java.util.stream.Stream;
|
27 | 28 |
|
28 | 29 | import org.slf4j.Logger;
|
|
34 | 35 | import org.springframework.modulith.core.ApplicationModules;
|
35 | 36 | import org.springframework.modulith.core.JavaPackage;
|
36 | 37 | import org.springframework.modulith.test.ApplicationModuleTest.BootstrapMode;
|
| 38 | +import org.springframework.util.ObjectUtils; |
37 | 39 | import org.springframework.util.StringUtils;
|
38 | 40 | import org.springframework.util.function.SingletonSupplier;
|
39 | 41 |
|
40 | 42 | /**
|
41 | 43 | * @author Oliver Drotbohm
|
| 44 | + * @author Lukas Dohmen |
42 | 45 | */
|
43 | 46 | public class ModuleTestExecution implements Iterable<ApplicationModule> {
|
44 | 47 |
|
@@ -95,21 +98,14 @@ public static Supplier<ModuleTestExecution> of(Class<?> type) {
|
95 | 98 |
|
96 | 99 | var annotation = AnnotatedElementUtils.findMergedAnnotation(type, ApplicationModuleTest.class);
|
97 | 100 | var packageName = type.getPackage().getName();
|
98 |
| - var optionalModulithType = findSpringBootApplicationByClasses(annotation); |
99 |
| - |
100 |
| - var modulithType = optionalModulithType.orElseGet(() -> MODULITH_TYPES.computeIfAbsent(type, |
101 |
| - it -> new AnnotatedClassFinder(SpringBootApplication.class).findFromPackage(packageName))); |
102 |
| - var modules = ApplicationModules.of(modulithType); |
103 |
| - |
| 101 | + var modules = ApplicationModules.of(findSpringBootApplicationByClasses(annotation, type)); |
104 | 102 | var moduleName = annotation.module();
|
105 |
| - ApplicationModule module; |
106 |
| - if (StringUtils.hasText(moduleName)) { |
107 |
| - module = modules.getModuleByName(moduleName).orElseThrow( // |
108 |
| - () -> new IllegalStateException(String.format("Unable to find module %s!", moduleName))); |
109 |
| - } else { |
110 |
| - module = modules.getModuleForPackage(packageName).orElseThrow( // |
111 |
| - () -> new IllegalStateException(String.format("Package %s is not part of any module!", packageName))); |
112 |
| - } |
| 103 | + |
| 104 | + var module = StringUtils.hasText(moduleName) |
| 105 | + ? modules.getModuleByName(moduleName).orElseThrow( // |
| 106 | + () -> new IllegalStateException(String.format("Unable to find module %s!", moduleName))) |
| 107 | + : modules.getModuleForPackage(packageName).orElseThrow( // |
| 108 | + () -> new IllegalStateException(String.format("Package %s is not part of any module!", packageName))); |
113 | 109 |
|
114 | 110 | return EXECUTIONS.computeIfAbsent(new Key(module.getBasePackage().getName(), annotation),
|
115 | 111 | it -> new ModuleTestExecution(annotation, modules, module));
|
@@ -240,15 +236,22 @@ private static Stream<ApplicationModule> getExtraModules(ApplicationModuleTest a
|
240 | 236 | .flatMap(Optional::stream);
|
241 | 237 | }
|
242 | 238 |
|
243 |
| - private static Optional<Class<?>> findSpringBootApplicationByClasses(ApplicationModuleTest annotation) { |
244 |
| - for (Class<?> clazz : annotation.classes()) { |
245 |
| - Class<?> modulithType = MODULITH_TYPES.computeIfAbsent(clazz, |
246 |
| - it -> new AnnotatedClassFinder(SpringBootApplication.class).findFromPackage(clazz.getPackageName())); |
247 |
| - if (modulithType != null) { |
248 |
| - return Optional.of(modulithType); |
249 |
| - } |
250 |
| - } |
251 |
| - return Optional.empty(); |
| 239 | + private static Class<?> findSpringBootApplicationByClasses(ApplicationModuleTest annotation, |
| 240 | + Class<?> testClass) { |
| 241 | + |
| 242 | + var types = ObjectUtils.addObjectToArray(annotation.classes(), testClass); |
| 243 | + |
| 244 | + return Arrays.stream(types) |
| 245 | + .<Class<?>> map(ModuleTestExecution::lookupSpringBootApplicationAnnotation) |
| 246 | + .findFirst() |
| 247 | + .orElseThrow(() -> new IllegalStateException("Couldn't find @SpringBootApplication traversing %s." |
| 248 | + .formatted(Arrays.stream(types).map(Class::getName).collect(Collectors.joining(", "))))); |
| 249 | + } |
| 250 | + |
| 251 | + private static Class<?> lookupSpringBootApplicationAnnotation(Class<?> clazz) { |
| 252 | + |
| 253 | + return MODULITH_TYPES.computeIfAbsent(clazz, |
| 254 | + it -> new AnnotatedClassFinder(SpringBootApplication.class).findFromClass(clazz)); |
252 | 255 | }
|
253 | 256 |
|
254 | 257 | private static record Key(String moduleBasePackage, ApplicationModuleTest annotation) {}
|
|
0 commit comments