From 3b2b9cf95c157ea294155068f728af139bd884cc Mon Sep 17 00:00:00 2001 From: Ulli Hafner Date: Fri, 11 Mar 2022 09:34:59 +0100 Subject: [PATCH 1/2] Add some more forbidden testing packages. --- .../edu/hm/hafner/util/ArchitectureRules.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/test/java/edu/hm/hafner/util/ArchitectureRules.java b/src/test/java/edu/hm/hafner/util/ArchitectureRules.java index b33725ee..b72e8b63 100644 --- a/src/test/java/edu/hm/hafner/util/ArchitectureRules.java +++ b/src/test/java/edu/hm/hafner/util/ArchitectureRules.java @@ -25,10 +25,6 @@ * @author Ullrich Hafner */ public final class ArchitectureRules { - private ArchitectureRules() { - // prevents instantiation - } - /** Never create exception without any context. */ public static final ArchRule NO_EXCEPTIONS_WITH_NO_ARG_CONSTRUCTOR = noClasses().should().callConstructorWhere(new ExceptionHasNoContext()); @@ -40,7 +36,7 @@ private ArchitectureRules() { .and().doNotHaveModifier(JavaModifier.ABSTRACT) .should().bePublic(); - /** Junit 5 test methods should be package private. */ + /** Junit 5 test methods should not be public. */ public static final ArchRule ONLY_PACKAGE_PRIVATE_TEST_METHODS = methods().that().areAnnotatedWith(Test.class) .or().areAnnotatedWith(ParameterizedTest.class) @@ -68,8 +64,13 @@ private ArchitectureRules() { "org.apache.commons.lang..", "org.joda.time..", "javax.xml.bind..", + "net.jcip.annotations..", "javax.annotation..", - "net.jcip.annotations..")); + "junit..", + "org.hamcrest..", + "com.google.common..", + "org.junit" + )); /** Prevents that classes use visible but forbidden API. */ public static final ArchRule NO_FORBIDDEN_ANNOTATION_USED = @@ -81,12 +82,16 @@ private ArchitectureRules() { .should().callCodeUnitWhere(new TargetIsForbiddenClass( "org.junit.jupiter.api.Assertions", "org.junit.Assert")); - /** Ensures that the {@code readResolve} methods are protected so sub classes can call the parent method. */ + /** Ensures that the {@code readResolve} methods are protected so subclasses can call the parent method. */ public static final ArchRule READ_RESOLVE_SHOULD_BE_PROTECTED = methods().that().haveName("readResolve").and().haveRawReturnType(Object.class) .should().beDeclaredInClassesThat().implement(Serializable.class) .andShould().beProtected(); + private ArchitectureRules() { + // prevents instantiation + } + /** * Matches if a call from outside the defining class uses a method or constructor annotated with {@link * VisibleForTesting}. There are two exceptions: From c20de0bf7ff5474ab63738db1db0b8b7efa433f4 Mon Sep 17 00:00:00 2001 From: Ulli Hafner Date: Fri, 11 Mar 2022 12:12:31 +0100 Subject: [PATCH 2/2] Make list of no-arg exception constructor calls configurable. --- .../edu/hm/hafner/util/ArchitectureRules.java | 36 +++++++++++++++---- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/src/test/java/edu/hm/hafner/util/ArchitectureRules.java b/src/test/java/edu/hm/hafner/util/ArchitectureRules.java index b72e8b63..cc79ded8 100644 --- a/src/test/java/edu/hm/hafner/util/ArchitectureRules.java +++ b/src/test/java/edu/hm/hafner/util/ArchitectureRules.java @@ -2,6 +2,7 @@ import java.io.Serializable; import java.util.Arrays; +import java.util.List; import org.apache.commons.lang3.StringUtils; import org.junit.jupiter.api.Test; @@ -10,6 +11,7 @@ import com.tngtech.archunit.base.DescribedPredicate; import com.tngtech.archunit.core.domain.AccessTarget.ConstructorCallTarget; import com.tngtech.archunit.core.domain.JavaCall; +import com.tngtech.archunit.core.domain.JavaClass; import com.tngtech.archunit.core.domain.JavaConstructorCall; import com.tngtech.archunit.core.domain.JavaModifier; import com.tngtech.archunit.core.domain.properties.CanBeAnnotated; @@ -27,7 +29,8 @@ public final class ArchitectureRules { /** Never create exception without any context. */ public static final ArchRule NO_EXCEPTIONS_WITH_NO_ARG_CONSTRUCTOR = - noClasses().should().callConstructorWhere(new ExceptionHasNoContext()); + noClasses().that().haveSimpleNameNotContaining("Benchmark") + .should().callConstructorWhere(new ExceptionHasNoContext()); /** Junit 5 test classes should not be public. */ public static final ArchRule NO_PUBLIC_TEST_CLASSES = @@ -70,7 +73,7 @@ public final class ArchitectureRules { "org.hamcrest..", "com.google.common..", "org.junit" - )); + )); /** Prevents that classes use visible but forbidden API. */ public static final ArchRule NO_FORBIDDEN_ANNOTATION_USED = @@ -93,8 +96,8 @@ private ArchitectureRules() { } /** - * Matches if a call from outside the defining class uses a method or constructor annotated with {@link - * VisibleForTesting}. There are two exceptions: + * Matches if a call from outside the defining class uses a method or constructor annotated with + * {@link VisibleForTesting}. There are two exceptions: *