-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support DatabaseSetup & DatabaseTeardown annotations on all levels of…
… class hierachy
- Loading branch information
Showing
4 changed files
with
138 additions
and
2 deletions.
There are no files selected for viewing
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
34 changes: 34 additions & 0 deletions
34
spring-boot-test-dbunit/src/main/kotlin/io/camassia/spring/dbunit/api/NestedClassUtil.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,34 @@ | ||
package io.camassia.spring.dbunit.api | ||
|
||
import org.slf4j.LoggerFactory | ||
|
||
object NestedClassUtil { | ||
|
||
private val log = LoggerFactory.getLogger(NestedClassUtil::class.java) | ||
|
||
fun getHierarchy(clazz: Class<*>): List<Class<*>> { | ||
val hierarchy = mutableListOf<Class<*>>() | ||
val builder = StringBuilder() | ||
val className = clazz.name | ||
for (i in className.indices) { | ||
val char = className[i] | ||
if (char == '$' || i == className.length) { | ||
safelyFetchClass(builder.toString())?.also { | ||
hierarchy.add(it) | ||
} | ||
} | ||
builder.append(char) | ||
} | ||
safelyFetchClass(builder.toString())?.also { | ||
hierarchy.add(it) | ||
} | ||
return hierarchy | ||
} | ||
|
||
private fun safelyFetchClass(name: String): Class<*>? = try { | ||
Class.forName(name) | ||
} catch (throwable: ClassNotFoundException) { | ||
log.warn("Invalid classname [$name]. Please avoid using '$' within class names.") | ||
null | ||
} | ||
} |
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
60 changes: 60 additions & 0 deletions
60
spring-boot-test-dbunit/src/test/kotlin/io/camassia/spring/dbunit/api/NestedClassUtilTest.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,60 @@ | ||
package io.camassia.spring.dbunit.api | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Nested | ||
import org.junit.jupiter.api.Test | ||
|
||
internal class NestedClassUtilTest { | ||
|
||
@Nested | ||
inner class GetHierarchy { | ||
@Test | ||
fun shouldExtractZeroLevelHierarchy() { | ||
val hierarchy = NestedClassUtil.getHierarchy(NestedClassUtilTest::class.java) | ||
|
||
assertThat(hierarchy).hasSize(1).containsExactly(NestedClassUtilTest::class.java) | ||
} | ||
|
||
@Test | ||
fun shouldExtractOneLevelHierarchy() { | ||
val hierarchy = NestedClassUtil.getHierarchy(LevelOne::class.java) | ||
|
||
assertThat(hierarchy).hasSize(2).containsExactly( | ||
NestedClassUtilTest::class.java, | ||
LevelOne::class.java | ||
) | ||
} | ||
|
||
@Test | ||
fun shouldExtractTwoLevelHierarchy() { | ||
val hierarchy = NestedClassUtil.getHierarchy(LevelOne.LevelTwo::class.java) | ||
|
||
assertThat(hierarchy).hasSize(3).containsExactly( | ||
NestedClassUtilTest::class.java, | ||
LevelOne::class.java, | ||
LevelOne.LevelTwo::class.java | ||
) | ||
} | ||
|
||
@Test | ||
fun shouldExtractOneLevelHierarchy_withSpecialName() { | ||
val hierarchy = NestedClassUtil.getHierarchy(`Special $ name`::class.java) | ||
|
||
assertThat(hierarchy).hasSize(2).containsExactly( | ||
NestedClassUtilTest::class.java, | ||
`Special $ name`::class.java | ||
) | ||
} | ||
} | ||
|
||
|
||
|
||
@Nested | ||
inner class LevelOne { | ||
@Nested | ||
inner class LevelTwo | ||
} | ||
|
||
@Nested | ||
inner class `Special $ name` | ||
} |