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.
This commit is contained in:
@@ -340,13 +340,16 @@ public class ApplicationModules implements Iterable<ApplicationModule> {
|
||||
* modules.
|
||||
*
|
||||
* @param className must not be {@literal null} or empty.
|
||||
* @return
|
||||
*/
|
||||
public boolean withinRootPackages(String className) {
|
||||
|
||||
Assert.hasText(className, "Class name must not be null or empty!");
|
||||
|
||||
return rootPackages.stream().anyMatch(it -> it.contains(className));
|
||||
var candidate = PackageName.ofType(className);
|
||||
|
||||
return rootPackages.stream()
|
||||
.map(JavaPackage::getPackageName)
|
||||
.anyMatch(candidate::equals);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -293,9 +293,9 @@ public class JavaPackage implements DescribedIterable<JavaClass>, Comparable<Jav
|
||||
* Returns the name of the package.
|
||||
*
|
||||
* @return will never be {@literal null}.
|
||||
* @since 1.3
|
||||
* @since 1.4, package protected since 1.3
|
||||
*/
|
||||
PackageName getPackageName() {
|
||||
public PackageName getPackageName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
@@ -30,9 +30,9 @@ import org.springframework.util.ClassUtils;
|
||||
* last.
|
||||
*
|
||||
* @author Oliver Drotbohm
|
||||
* @since 1.2
|
||||
* @since 1.4, previously package private since 1.2
|
||||
*/
|
||||
class PackageName implements Comparable<PackageName> {
|
||||
public class PackageName implements Comparable<PackageName> {
|
||||
|
||||
private static final Map<String, PackageName> PACKAGE_NAMES = new HashMap<>();
|
||||
|
||||
@@ -68,8 +68,9 @@ class PackageName implements Comparable<PackageName> {
|
||||
*
|
||||
* @param fullyQualifiedName must not be {@literal null} or empty.
|
||||
* @return will never be {@literal null}.
|
||||
* @since 1.4
|
||||
*/
|
||||
static PackageName ofType(String fullyQualifiedName) {
|
||||
public static PackageName ofType(String fullyQualifiedName) {
|
||||
|
||||
Assert.notNull(fullyQualifiedName, "Type name must not be null!");
|
||||
|
||||
@@ -191,8 +192,9 @@ class PackageName implements Comparable<PackageName> {
|
||||
* sub-package of it.
|
||||
*
|
||||
* @param reference must not be {@literal null}.
|
||||
* @since 1.4
|
||||
*/
|
||||
boolean contains(PackageName reference) {
|
||||
public boolean contains(PackageName reference) {
|
||||
|
||||
Assert.notNull(reference, "Reference package name must not be null!");
|
||||
|
||||
|
||||
@@ -36,6 +36,7 @@ import org.springframework.modulith.core.ApplicationModule;
|
||||
import org.springframework.modulith.core.ApplicationModules;
|
||||
import org.springframework.modulith.core.ApplicationModulesFactory;
|
||||
import org.springframework.modulith.core.JavaPackage;
|
||||
import org.springframework.modulith.core.PackageName;
|
||||
import org.springframework.modulith.test.ApplicationModuleTest.BootstrapMode;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -134,8 +135,7 @@ public class ModuleTestExecution implements Iterable<ApplicationModule> {
|
||||
|
||||
public boolean includes(String className) {
|
||||
|
||||
var result = modules.withinRootPackages(className) //
|
||||
|| basePackages.get().stream().anyMatch(it -> it.contains(className));
|
||||
var result = isLocatedInRootPackageOrContainedInBasePackages(className);
|
||||
|
||||
if (result) {
|
||||
LOGGER.trace("Including class {}.", className);
|
||||
@@ -239,6 +239,17 @@ public class ModuleTestExecution implements Iterable<ApplicationModule> {
|
||||
return Objects.hash(key);
|
||||
}
|
||||
|
||||
private boolean isLocatedInRootPackageOrContainedInBasePackages(String className) {
|
||||
|
||||
if (modules.withinRootPackages(className)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
var candidate = PackageName.ofType(className);
|
||||
|
||||
return basePackages.get().stream().map(JavaPackage::getPackageName).anyMatch(it -> it.contains(candidate));
|
||||
}
|
||||
|
||||
private static Stream<ApplicationModule> getExtraModules(ApplicationModuleTest annotation,
|
||||
ApplicationModules modules) {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user