Skip to content

Commit

Permalink
only load tests if dependent classes are on the classpath (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
klu2 authored Jul 17, 2022
1 parent 439f77b commit 259ec44
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
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)

}
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
}



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
}

}

0 comments on commit 259ec44

Please sign in to comment.