Skip to content

Commit 8df778b

Browse files
committed
GH-202 - Support for bean instances located in test sources in @ApplicationModuleTest.
Prior to this commit, the TypeExcludeFilter registered by @ApplicationModuleTest decided whether to include a type based on the ApplicationModules instance and the content of the modules' backing JavaPackage instances. Those in turn always consider the classes scanned by ArchUnit to decide whether they include a type or not. As an ApplicationModules instance is set up to only consider production code, any type located in the test sources was disregarded from component scanning. The checks for package inclusion for a test execution have now been revamped to consider the sole package names when filtering types for inclusion.
1 parent a771ecf commit 8df778b

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -340,13 +340,16 @@ public boolean contains(Class<?> type) {
340340
* modules.
341341
*
342342
* @param className must not be {@literal null} or empty.
343-
* @return
344343
*/
345344
public boolean withinRootPackages(String className) {
346345

347346
Assert.hasText(className, "Class name must not be null or empty!");
348347

349-
return rootPackages.stream().anyMatch(it -> it.contains(className));
348+
var candidate = PackageName.ofType(className);
349+
350+
return rootPackages.stream()
351+
.map(JavaPackage::getPackageName)
352+
.anyMatch(candidate::equals);
350353
}
351354

352355
/**

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,9 @@ public <A extends Annotation> Optional<A> getAnnotation(Class<A> annotationType)
293293
* Returns the name of the package.
294294
*
295295
* @return will never be {@literal null}.
296-
* @since 1.3
296+
* @since 1.4, package protected since 1.3
297297
*/
298-
PackageName getPackageName() {
298+
public PackageName getPackageName() {
299299
return name;
300300
}
301301

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
* last.
3131
*
3232
* @author Oliver Drotbohm
33-
* @since 1.2
33+
* @since 1.4, previously package private since 1.2
3434
*/
35-
class PackageName implements Comparable<PackageName> {
35+
public class PackageName implements Comparable<PackageName> {
3636

3737
private static final Map<String, PackageName> PACKAGE_NAMES = new HashMap<>();
3838

@@ -68,8 +68,9 @@ private PackageName(String name, String[] segments) {
6868
*
6969
* @param fullyQualifiedName must not be {@literal null} or empty.
7070
* @return will never be {@literal null}.
71+
* @since 1.4
7172
*/
72-
static PackageName ofType(String fullyQualifiedName) {
73+
public static PackageName ofType(String fullyQualifiedName) {
7374

7475
Assert.notNull(fullyQualifiedName, "Type name must not be null!");
7576

@@ -191,8 +192,9 @@ boolean isDirectParentOf(PackageName reference) {
191192
* sub-package of it.
192193
*
193194
* @param reference must not be {@literal null}.
195+
* @since 1.4
194196
*/
195-
boolean contains(PackageName reference) {
197+
public boolean contains(PackageName reference) {
196198

197199
Assert.notNull(reference, "Reference package name must not be null!");
198200

spring-modulith-test/src/main/java/org/springframework/modulith/test/ModuleTestExecution.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.modulith.core.ApplicationModules;
3737
import org.springframework.modulith.core.ApplicationModulesFactory;
3838
import org.springframework.modulith.core.JavaPackage;
39+
import org.springframework.modulith.core.PackageName;
3940
import org.springframework.modulith.test.ApplicationModuleTest.BootstrapMode;
4041
import org.springframework.util.ObjectUtils;
4142
import org.springframework.util.StringUtils;
@@ -134,8 +135,7 @@ public Stream<String> getBasePackages() {
134135

135136
public boolean includes(String className) {
136137

137-
var result = modules.withinRootPackages(className) //
138-
|| basePackages.get().stream().anyMatch(it -> it.contains(className));
138+
var result = isLocatedInRootPackageOrContainedInBasePackages(className);
139139

140140
if (result) {
141141
LOGGER.trace("Including class {}.", className);
@@ -239,6 +239,17 @@ public int hashCode() {
239239
return Objects.hash(key);
240240
}
241241

242+
private boolean isLocatedInRootPackageOrContainedInBasePackages(String className) {
243+
244+
if (modules.withinRootPackages(className)) {
245+
return true;
246+
}
247+
248+
var candidate = PackageName.ofType(className);
249+
250+
return basePackages.get().stream().map(JavaPackage::getPackageName).anyMatch(it -> it.contains(candidate));
251+
}
252+
242253
private static Stream<ApplicationModule> getExtraModules(ApplicationModuleTest annotation,
243254
ApplicationModules modules) {
244255

0 commit comments

Comments
 (0)