-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
only load tests if dependent classes are on the classpath (#3)
- Loading branch information
Showing
3 changed files
with
55 additions
and
5 deletions.
There are no files selected for viewing
16 changes: 11 additions & 5 deletions
16
src/main/kotlin/io/cloudflight/cleancode/archunit/CleanCodeRuleSets.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/io/cloudflight/cleancode/archunit/utils/ClassUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
|
||
|
||
|
23 changes: 23 additions & 0 deletions
23
src/test/kotlin/io/cloudflight/cleancode/archunit/utils/ClassUtilsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
|
||
} |