From 5334e90bad5f75dfbf113cb1943d81e53067dcf8 Mon Sep 17 00:00:00 2001 From: Ulli Hafner Date: Sun, 29 May 2022 22:11:55 +0200 Subject: [PATCH] Add test that verifies some ArchUnit rules. --- .../hm/hafner/util/ArchitectureRulesTest.java | 51 +++++++++++++++++++ .../edu/hm/hafner/util/ArchitectureTest.java | 15 +++++- 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 src/test/java/edu/hm/hafner/util/ArchitectureRulesTest.java diff --git a/src/test/java/edu/hm/hafner/util/ArchitectureRulesTest.java b/src/test/java/edu/hm/hafner/util/ArchitectureRulesTest.java new file mode 100644 index 00000000..a2106754 --- /dev/null +++ b/src/test/java/edu/hm/hafner/util/ArchitectureRulesTest.java @@ -0,0 +1,51 @@ +package edu.hm.hafner.util; + +import org.junit.jupiter.api.Test; + +import com.tngtech.archunit.core.domain.JavaClasses; +import com.tngtech.archunit.core.importer.ClassFileImporter; + +import static org.assertj.core.api.Assertions.*; + +/** + * Verifies the architecture rules in {@link ArchitectureRules}. + * + * @author Ullrich Hafner + */ +class ArchitectureRulesTest { + @Test + void shouldVerifyNoPublicTestClassesRule() { + JavaClasses violatedClasses = importClasses(NoPublicTestElementsViolatedTest.class); + assertThatExceptionOfType(AssertionError.class).isThrownBy( + () -> ArchitectureRules.NO_PUBLIC_TEST_CLASSES.check(violatedClasses)); + JavaClasses passedClasses = new ClassFileImporter().importClasses(NoPublicTestElementsPassedTest.class); + assertThatNoException().isThrownBy( + () -> ArchitectureRules.NO_PUBLIC_TEST_CLASSES.check(passedClasses)); + } + + @Test + void shouldVerifyNoPublicTestMethodsRule() { + JavaClasses violatedClasses = importClasses(NoPublicTestElementsViolatedTest.class); + assertThatExceptionOfType(AssertionError.class).isThrownBy( + () -> ArchitectureRules.ONLY_PACKAGE_PRIVATE_TEST_METHODS.check(violatedClasses)); + JavaClasses passedClasses = new ClassFileImporter().importClasses(NoPublicTestElementsPassedTest.class); + assertThatNoException().isThrownBy( + () -> ArchitectureRules.ONLY_PACKAGE_PRIVATE_TEST_METHODS.check(passedClasses)); + } + + private JavaClasses importClasses(final Class... classes) { + return new ClassFileImporter().importClasses(classes); + } + + public static class NoPublicTestElementsViolatedTest { + @Test + public void shouldFail() { + } + } + + static class NoPublicTestElementsPassedTest { + @Test + void shouldPass() { + } + } +} diff --git a/src/test/java/edu/hm/hafner/util/ArchitectureTest.java b/src/test/java/edu/hm/hafner/util/ArchitectureTest.java index c39d6611..3abfa2a4 100644 --- a/src/test/java/edu/hm/hafner/util/ArchitectureTest.java +++ b/src/test/java/edu/hm/hafner/util/ArchitectureTest.java @@ -1,16 +1,20 @@ package edu.hm.hafner.util; +import com.tngtech.archunit.core.importer.ImportOption; +import com.tngtech.archunit.core.importer.Location; import com.tngtech.archunit.junit.AnalyzeClasses; import com.tngtech.archunit.junit.ArchTest; import com.tngtech.archunit.lang.ArchRule; +import edu.hm.hafner.util.ArchitectureTest.DoNotIncludeRulesUnderTest; + /** * Checks the architecture of this module. * * @author Ullrich Hafner */ @SuppressWarnings("hideutilityclassconstructor") -@AnalyzeClasses(packages = "edu.hm.hafner") +@AnalyzeClasses(packages = "edu.hm.hafner", importOptions = DoNotIncludeRulesUnderTest.class) class ArchitectureTest { @ArchTest static final ArchRule NO_PUBLIC_TEST_CLASSES = ArchitectureRules.NO_PUBLIC_TEST_CLASSES; @@ -35,4 +39,13 @@ class ArchitectureTest { @ArchTest static final ArchRule NO_EXCEPTIONS_WITH_NO_ARG_CONSTRUCTOR = ArchitectureRules.NO_EXCEPTIONS_WITH_NO_ARG_CONSTRUCTOR; + + static final class DoNotIncludeRulesUnderTest implements ImportOption { + DoNotIncludeRulesUnderTest() { + } + + public boolean includes(final Location location) { + return !location.contains(ArchitectureRulesTest.class.getSimpleName()); + } + } }