From c192a7dd141e65b73088daac57ab818fb9067f03 Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Fri, 7 Oct 2022 17:22:21 +0200 Subject: [PATCH] GH-42 - Upgrade to ArchUnit 1.0. --- pom.xml | 2 +- .../modulith/model/ApplicationModule.java | 6 +-- .../modulith/model/ApplicationModules.java | 22 ++++++---- .../model/ArchitecturallyEvidentType.java | 42 ++++++++++--------- .../modulith/model/Classes.java | 9 ++-- .../modulith/model/SpringBean.java | 2 +- .../springframework/modulith/model/Types.java | 2 +- .../modulith/model/JavaPackageUnitTests.java | 5 +-- 8 files changed, 49 insertions(+), 41 deletions(-) diff --git a/pom.xml b/pom.xml index a3c84718..cfe06a3a 100644 --- a/pom.xml +++ b/pom.xml @@ -26,7 +26,7 @@ - 0.23.1 + 1.0.0 3.4.0 2021.2.4 UTF-8 diff --git a/spring-modulith-core/src/main/java/org/springframework/modulith/model/ApplicationModule.java b/spring-modulith-core/src/main/java/org/springframework/modulith/model/ApplicationModule.java index 98e4b5ff..83d1c5fb 100644 --- a/spring-modulith-core/src/main/java/org/springframework/modulith/model/ApplicationModule.java +++ b/spring-modulith-core/src/main/java/org/springframework/modulith/model/ApplicationModule.java @@ -772,7 +772,7 @@ public class ApplicationModule { DependencyType.USES_COMPONENT); this.member = member; - this.isConfigurationClass = isConfiguration().apply(origin); + this.isConfigurationClass = isConfiguration().test(origin); } /* @@ -887,8 +887,8 @@ public class ApplicationModule { } public static DependencyType forCodeUnit(JavaCodeUnit codeUnit) { - return Types.isAnnotatedWith(SpringTypes.AT_EVENT_LISTENER).apply(codeUnit) // - || Types.isAnnotatedWith(JMoleculesTypes.AT_DOMAIN_EVENT_HANDLER).apply(codeUnit) // + return Types.isAnnotatedWith(SpringTypes.AT_EVENT_LISTENER).test(codeUnit) // + || Types.isAnnotatedWith(JMoleculesTypes.AT_DOMAIN_EVENT_HANDLER).test(codeUnit) // ? EVENT_LISTENER : DEFAULT; } diff --git a/spring-modulith-core/src/main/java/org/springframework/modulith/model/ApplicationModules.java b/spring-modulith-core/src/main/java/org/springframework/modulith/model/ApplicationModules.java index 95eedbeb..230822ad 100644 --- a/spring-modulith-core/src/main/java/org/springframework/modulith/model/ApplicationModules.java +++ b/spring-modulith-core/src/main/java/org/springframework/modulith/model/ApplicationModules.java @@ -59,7 +59,8 @@ public class ApplicationModules implements Iterable { static { - List loadFactories = SpringFactoriesLoader.loadFactories(ApplicationModuleDetectionStrategy.class, + List loadFactories = SpringFactoriesLoader.loadFactories( + ApplicationModuleDetectionStrategy.class, ApplicationModules.class.getClassLoader()); if (loadFactories.size() > 1) { @@ -69,7 +70,8 @@ public class ApplicationModules implements Iterable { loadFactories)); } - DETECTION_STRATEGY = loadFactories.isEmpty() ? ApplicationModuleDetectionStrategies.DIRECT_SUB_PACKAGES : loadFactories.get(0); + DETECTION_STRATEGY = loadFactories.isEmpty() ? ApplicationModuleDetectionStrategies.DIRECT_SUB_PACKAGES + : loadFactories.get(0); } private final ModulithMetadata metadata; @@ -80,7 +82,8 @@ public class ApplicationModules implements Iterable { private boolean verified; - private ApplicationModules(ModulithMetadata metadata, Collection packages, DescribedPredicate ignored, + private ApplicationModules(ModulithMetadata metadata, Collection packages, + DescribedPredicate ignored, boolean useFullyQualifiedModuleNames) { this.metadata = metadata; @@ -105,8 +108,8 @@ public class ApplicationModules implements Iterable { } /** - * Creates a new {@link ApplicationModules} relative to the given modulith type. Will inspect the {@link Modulith} annotation on - * the class given for advanced customizations of the module setup. + * Creates a new {@link ApplicationModules} relative to the given modulith type. Will inspect the {@link Modulith} + * annotation on the class given for advanced customizations of the module setup. * * @param modulithType must not be {@literal null}. * @return @@ -116,9 +119,10 @@ public class ApplicationModules implements Iterable { } /** - * Creates a new {@link ApplicationModules} relative to the given modulith type, a {@link ApplicationModuleDetectionStrategy} and a - * {@link DescribedPredicate} which types and packages to ignore. Will inspect the {@link Modulith} and - * {@link Modulithic} annotations on the class given for advanced customizations of the module setup. + * Creates a new {@link ApplicationModules} relative to the given modulith type, a + * {@link ApplicationModuleDetectionStrategy} and a {@link DescribedPredicate} which types and packages to ignore. + * Will inspect the {@link Modulith} and {@link Modulithic} annotations on the class given for advanced + * customizations of the module setup. * * @param modulithType must not be {@literal null}. * @param detection must not be {@literal null}. @@ -371,7 +375,7 @@ public class ApplicationModules implements Iterable { return Arrays.stream(names) // .map(it -> withoutModule(it)) // - .reduce(DescribedPredicate.alwaysFalse(), DescribedPredicate::or, (__, right) -> right); + .reduce(DescribedPredicate.alwaysFalse(), (left, right) -> left.or(right), (__, right) -> right); } public static DescribedPredicate withoutModule(String name) { diff --git a/spring-modulith-core/src/main/java/org/springframework/modulith/model/ArchitecturallyEvidentType.java b/spring-modulith-core/src/main/java/org/springframework/modulith/model/ArchitecturallyEvidentType.java index bd467b9c..96305fff 100644 --- a/spring-modulith-core/src/main/java/org/springframework/modulith/model/ArchitecturallyEvidentType.java +++ b/spring-modulith-core/src/main/java/org/springframework/modulith/model/ArchitecturallyEvidentType.java @@ -60,8 +60,8 @@ public abstract class ArchitecturallyEvidentType { private final @Getter JavaClass type; /** - * Creates a new {@link ArchitecturallyEvidentType} for the given {@link JavaType} and {@link Classes} of - * Spring components. + * Creates a new {@link ArchitecturallyEvidentType} for the given {@link JavaType} and {@link Classes} of Spring + * components. * * @param type must not be {@literal null}. * @param beanTypes must not be {@literal null}. @@ -102,7 +102,7 @@ public abstract class ArchitecturallyEvidentType { * @return */ boolean isEntity() { - return isJpaEntity().apply(getType()); + return isJpaEntity().test(getType()); } /** @@ -181,8 +181,8 @@ public abstract class ArchitecturallyEvidentType { * Methods (meta-)annotated with @EventListener. */ private static final Predicate IS_ANNOTATED_EVENT_LISTENER = it -> // - Types.isAnnotatedWith(SpringTypes.AT_EVENT_LISTENER).apply(it) // - || Types.isAnnotatedWith(SpringTypes.AT_TX_EVENT_LISTENER).apply(it); + Types.isAnnotatedWith(SpringTypes.AT_EVENT_LISTENER).test(it) // + || Types.isAnnotatedWith(SpringTypes.AT_TX_EVENT_LISTENER).test(it); /** * {@code ApplicationListener.onApplicationEvent(…)} @@ -214,7 +214,7 @@ public abstract class ArchitecturallyEvidentType { */ @Override public boolean isRepository() { - return Types.isAnnotatedWith(SpringTypes.AT_REPOSITORY).apply(getType()); + return Types.isAnnotatedWith(SpringTypes.AT_REPOSITORY).test(getType()); } /* @@ -223,7 +223,7 @@ public abstract class ArchitecturallyEvidentType { */ @Override public boolean isService() { - return Types.isAnnotatedWith(SpringTypes.AT_SERVICE).apply(getType()); + return Types.isAnnotatedWith(SpringTypes.AT_SERVICE).test(getType()); } /* @@ -232,7 +232,7 @@ public abstract class ArchitecturallyEvidentType { */ @Override public boolean isController() { - return Types.isAnnotatedWith(SpringTypes.AT_CONTROLLER).apply(getType()); + return Types.isAnnotatedWith(SpringTypes.AT_CONTROLLER).test(getType()); } /* @@ -250,7 +250,7 @@ public abstract class ArchitecturallyEvidentType { */ @Override public boolean isConfigurationProperties() { - return Types.isAnnotatedWith(SpringTypes.AT_CONFIGURATION_PROPERTIES).apply(getType()); + return Types.isAnnotatedWith(SpringTypes.AT_CONFIGURATION_PROPERTIES).test(getType()); } /* @@ -329,7 +329,7 @@ public abstract class ArchitecturallyEvidentType { */ @Override public boolean isRepository() { - return SpringDataTypes.isSpringDataRepository().apply(getType()); + return SpringDataTypes.isSpringDataRepository().test(getType()); } /* @@ -338,14 +338,15 @@ public abstract class ArchitecturallyEvidentType { */ @Override public boolean isController() { - return Types.isAnnotatedWith("org.springframework.data.rest.webmvc.BasePathAwareController").apply(getType()); + return Types.isAnnotatedWith("org.springframework.data.rest.webmvc.BasePathAwareController") + .test(getType()); } } static class JMoleculesArchitecturallyEvidentType extends ArchitecturallyEvidentType { private static final Predicate IS_ANNOTATED_EVENT_LISTENER = Types - .isAnnotatedWith(JMoleculesTypes.AT_DOMAIN_EVENT_HANDLER)::apply; + .isAnnotatedWith(JMoleculesTypes.AT_DOMAIN_EVENT_HANDLER)::test; JMoleculesArchitecturallyEvidentType(JavaClass type) { super(type); @@ -360,7 +361,7 @@ public abstract class ArchitecturallyEvidentType { JavaClass type = getType(); - return Types.isAnnotatedWith(org.jmolecules.ddd.annotation.Entity.class).apply(type) || // + return Types.isAnnotatedWith(org.jmolecules.ddd.annotation.Entity.class).test(type) || // type.isAssignableTo(org.jmolecules.ddd.types.Entity.class); } @@ -373,7 +374,7 @@ public abstract class ArchitecturallyEvidentType { JavaClass type = getType(); - return Types.isAnnotatedWith(org.jmolecules.ddd.annotation.AggregateRoot.class).apply(type) // + return Types.isAnnotatedWith(org.jmolecules.ddd.annotation.AggregateRoot.class).test(type) // || type.isAssignableTo(org.jmolecules.ddd.types.AggregateRoot.class); } @@ -386,7 +387,7 @@ public abstract class ArchitecturallyEvidentType { JavaClass type = getType(); - return Types.isAnnotatedWith(org.jmolecules.ddd.annotation.Repository.class).apply(type) + return Types.isAnnotatedWith(org.jmolecules.ddd.annotation.Repository.class).test(type) || type.isAssignableTo(org.jmolecules.ddd.types.Repository.class); } @@ -399,7 +400,7 @@ public abstract class ArchitecturallyEvidentType { JavaClass type = getType(); - return Types.isAnnotatedWith(org.jmolecules.ddd.annotation.Service.class).apply(type); + return Types.isAnnotatedWith(org.jmolecules.ddd.annotation.Service.class).test(type); } /* @@ -414,15 +415,18 @@ public abstract class ArchitecturallyEvidentType { static class DelegatingType extends ArchitecturallyEvidentType { - private final Supplier isAggregateRoot, isRepository, isEntity, isService, isController, isEventListener, + private final Supplier isAggregateRoot, isRepository, isEntity, isService, isController, + isEventListener, isConfigurationProperties; private final Supplier> referenceTypes; private final Supplier> referenceMethods; DelegatingType(JavaClass type, Supplier isAggregateRoot, Supplier isRepository, Supplier isEntity, Supplier isService, - Supplier isController, Supplier isEventListener, Supplier isConfigurationProperties, - Supplier> referenceTypes, Supplier> referenceMethods) { + Supplier isController, Supplier isEventListener, + Supplier isConfigurationProperties, + Supplier> referenceTypes, + Supplier> referenceMethods) { super(type); diff --git a/spring-modulith-core/src/main/java/org/springframework/modulith/model/Classes.java b/spring-modulith-core/src/main/java/org/springframework/modulith/model/Classes.java index 6690f896..a2748911 100644 --- a/spring-modulith-core/src/main/java/org/springframework/modulith/model/Classes.java +++ b/spring-modulith-core/src/main/java/org/springframework/modulith/model/Classes.java @@ -108,7 +108,7 @@ public class Classes implements DescribedIterable { Assert.notNull(predicate, "Predicate must not be null!"); return classes.stream() // - .filter((Predicate) it -> predicate.apply(it)) // + .filter((Predicate) it -> predicate.test(it)) // .collect(Collectors.collectingAndThen(Collectors.toList(), Classes::new)); } @@ -166,7 +166,8 @@ public class Classes implements DescribedIterable { return classes.stream() // .filter(it -> it.isEquivalentTo(type)) // .findFirst() // - .orElseThrow(() -> new IllegalArgumentException(String.format("No JavaClass found for type %s!", type))); + .orElseThrow( + () -> new IllegalArgumentException(String.format("No JavaClass found for type %s!", type))); } /* @@ -227,10 +228,10 @@ public class Classes implements DescribedIterable { /* * (non-Javadoc) - * @see com.tngtech.archunit.base.DescribedPredicate#apply(java.lang.Object) + * @see java.util.function.Predicate#test(java.lang.Object) */ @Override - public boolean apply(@Nullable JavaClass input) { + public boolean test(@Nullable JavaClass input) { return input != null && reference.getName().equals(input.getName()); } } diff --git a/spring-modulith-core/src/main/java/org/springframework/modulith/model/SpringBean.java b/spring-modulith-core/src/main/java/org/springframework/modulith/model/SpringBean.java index a9a466e6..9c71f182 100644 --- a/spring-modulith-core/src/main/java/org/springframework/modulith/model/SpringBean.java +++ b/spring-modulith-core/src/main/java/org/springframework/modulith/model/SpringBean.java @@ -69,7 +69,7 @@ public class SpringBean { } public boolean isAnnotatedWith(Class type) { - return Types.isAnnotatedWith(type).apply(this.type); + return Types.isAnnotatedWith(type).test(this.type); } public ArchitecturallyEvidentType toArchitecturallyEvidentType() { diff --git a/spring-modulith-core/src/main/java/org/springframework/modulith/model/Types.java b/spring-modulith-core/src/main/java/org/springframework/modulith/model/Types.java index a4539589..5625d1ca 100644 --- a/spring-modulith-core/src/main/java/org/springframework/modulith/model/Types.java +++ b/spring-modulith-core/src/main/java/org/springframework/modulith/model/Types.java @@ -125,7 +125,7 @@ class Types { } static boolean isAtBeanMethod(JavaMethod method) { - return isAnnotatedWith(SpringTypes.AT_BEAN).apply(method); + return isAnnotatedWith(SpringTypes.AT_BEAN).test(method); } } diff --git a/spring-modulith-integration-test/src/test/java/org/springframework/modulith/model/JavaPackageUnitTests.java b/spring-modulith-integration-test/src/test/java/org/springframework/modulith/model/JavaPackageUnitTests.java index 4a83b78a..ec5a6205 100644 --- a/spring-modulith-integration-test/src/test/java/org/springframework/modulith/model/JavaPackageUnitTests.java +++ b/spring-modulith-integration-test/src/test/java/org/springframework/modulith/model/JavaPackageUnitTests.java @@ -22,15 +22,14 @@ import org.junit.jupiter.api.Test; import com.tngtech.archunit.core.domain.JavaClasses; import com.tngtech.archunit.core.importer.ClassFileImporter; import com.tngtech.archunit.core.importer.ImportOption; -import com.tngtech.archunit.core.importer.ImportOptions; /** * @author Oliver Drotbohm */ class JavaPackageUnitTests { - static final ImportOptions NO_TESTS = new ImportOptions().with(new ImportOption.DoNotIncludeTests()); - static final JavaClasses ALL_CLASSES = new ClassFileImporter(NO_TESTS) // + static final JavaClasses ALL_CLASSES = new ClassFileImporter() // + .withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS) .importPackages("com.acme.myproject"); @Test