From 259ec446e38a81fca41d19496b1ae8d1adca7cc6 Mon Sep 17 00:00:00 2001 From: Klaus Lehner <172195+klu2@users.noreply.github.com> Date: Sun, 17 Jul 2022 10:50:43 +0200 Subject: [PATCH] only load tests if dependent classes are on the classpath (#3) --- .../cleancode/archunit/CleanCodeRuleSets.kt | 16 +++++++++---- .../cleancode/archunit/utils/ClassUtils.kt | 21 +++++++++++++++++ .../archunit/utils/ClassUtilsTest.kt | 23 +++++++++++++++++++ 3 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 src/test/kotlin/io/cloudflight/cleancode/archunit/utils/ClassUtilsTest.kt diff --git a/src/main/kotlin/io/cloudflight/cleancode/archunit/CleanCodeRuleSets.kt b/src/main/kotlin/io/cloudflight/cleancode/archunit/CleanCodeRuleSets.kt index 5c4b9e2..270456a 100644 --- a/src/main/kotlin/io/cloudflight/cleancode/archunit/CleanCodeRuleSets.kt +++ b/src/main/kotlin/io/cloudflight/cleancode/archunit/CleanCodeRuleSets.kt @@ -1,20 +1,26 @@ package io.cloudflight.cleancode.archunit import com.tngtech.archunit.junit.ArchTest -import com.tngtech.archunit.junit.ArchTests -import io.cloudflight.cleancode.archunit.rules.jpa.JpaRuleSet import io.cloudflight.cleancode.archunit.rules.jdk.JdkRuleSet +import io.cloudflight.cleancode.archunit.rules.jpa.JpaRuleSet import io.cloudflight.cleancode.archunit.rules.logging.LoggingRuleSet +import io.cloudflight.cleancode.archunit.utils.ruleSetOf class CleanCodeRuleSets { @ArchTest - val jpa = ArchTests.`in`(JpaRuleSet::class.java) + val jpa = ruleSetOf( + JpaRuleSet::class.java, + requiredClasses = arrayOf( + "javax.persistence.Entity", + "javax.validation.constraints.NotNull" + ) + ) @ArchTest - val logging = ArchTests.`in`(LoggingRuleSet::class.java) + val logging = ruleSetOf(LoggingRuleSet::class.java) @ArchTest - val jdk = ArchTests.`in`(JdkRuleSet::class.java) + val jdk = ruleSetOf(JdkRuleSet::class.java) } diff --git a/src/main/kotlin/io/cloudflight/cleancode/archunit/utils/ClassUtils.kt b/src/main/kotlin/io/cloudflight/cleancode/archunit/utils/ClassUtils.kt index 9607dc3..9ae9173 100644 --- a/src/main/kotlin/io/cloudflight/cleancode/archunit/utils/ClassUtils.kt +++ b/src/main/kotlin/io/cloudflight/cleancode/archunit/utils/ClassUtils.kt @@ -1,10 +1,31 @@ package io.cloudflight.cleancode.archunit.utils +import com.tngtech.archunit.junit.ArchTests + fun Class<*>.isKotlinClass(): Boolean { return this.declaredAnnotations.any { it.annotationClass == Metadata::class } } +internal fun ruleSetOf(clazz: Class<*>, vararg requiredClasses: String): ArchTests { + return if (classesExistOnClasspath(*requiredClasses)) { + ArchTests.`in`(clazz) + } else { + ArchTests.`in`(Object::class.java) + } +} + +internal fun classesExistOnClasspath(vararg classes: String): Boolean { + classes.forEach { + try { + Class.forName(it) + } catch (err: ClassNotFoundException) { + return false + } + } + return true +} + diff --git a/src/test/kotlin/io/cloudflight/cleancode/archunit/utils/ClassUtilsTest.kt b/src/test/kotlin/io/cloudflight/cleancode/archunit/utils/ClassUtilsTest.kt new file mode 100644 index 0000000..a41f262 --- /dev/null +++ b/src/test/kotlin/io/cloudflight/cleancode/archunit/utils/ClassUtilsTest.kt @@ -0,0 +1,23 @@ +package io.cloudflight.cleancode.archunit.utils + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class ClassUtilsTest { + + @Test + fun existOnClassPath() { + assertThat(classesExistOnClasspath("java.lang.Object")).isTrue + } + + @Test + fun dontExistOnClassPath() { + assertThat(classesExistOnClasspath("io.cloudflight.Foo")).isFalse + } + + @Test + fun dontExistOnClassPath2() { + assertThat(classesExistOnClasspath("java.lang.Object", "io.cloudflight.Foo")).isFalse + } + +} \ No newline at end of file