Skip to content

Commit

Permalink
Support DatabaseSetup & DatabaseTeardown annotations on all levels of…
Browse files Browse the repository at this point in the history
… class hierachy
  • Loading branch information
ed0906 committed Feb 1, 2023
1 parent 7c49972 commit 455486b
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,47 @@ class DatabaseSetupAnnotationTest : RepositoryTest() {
}
}

@Nested
@DatabaseSetup(tables = [
Table("demo1",
Row(Cell("id", "1"), Cell("name", "Test1"))
)
])
inner class WithMultipleLevelsOfAnnotations {
@Nested
@DatabaseSetup(tables = [
Table("demo1",
Row(Cell("id", "2"), Cell("name", "Test2"))
)
], operation = DatabaseOperation.INSERT)
inner class LevelTwo {
@Nested
@DatabaseSetup(tables = [
Table("demo1",
Row(Cell("id", "3"), Cell("name", "Test3"))
)
], operation = DatabaseOperation.INSERT)
inner class LevelTwo {
@Test
@DatabaseSetup(tables = [
Table("demo1",
Row(Cell("id", "4"), Cell("name", "Test4"))
)
], operation = DatabaseOperation.INSERT)
fun `should combine multiple levels of annotations`() {
val result = selectAllFrom("demo1")
assertThat(result).hasSize(4)
assertThat(result).containsExactly(
1L to "Test1",
2L to "Test2",
3L to "Test3",
4L to "Test4",
)
}
}
}
}

@TestConfiguration
class DemoTestConfiguration {

Expand All @@ -373,4 +414,4 @@ class DatabaseSetupAnnotationTest : RepositoryTest() {
@Bean
fun defaults() = TableDefaults("demo1", io.camassia.spring.dbunit.api.dataset.Cell("name", "default-name"))
}
}
}
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.camassia.spring.dbunit.api.wiring

import io.camassia.spring.dbunit.api.DatabaseTester
import io.camassia.spring.dbunit.api.DbUnitException
import io.camassia.spring.dbunit.api.NestedClassUtil
import io.camassia.spring.dbunit.api.annotations.DatabaseSetup
import io.camassia.spring.dbunit.api.annotations.DatabaseTeardown
import io.camassia.spring.dbunit.api.customization.DatabaseOperation
Expand Down Expand Up @@ -93,7 +94,7 @@ class DatabaseSetupAndTeardownTestExecutionListener : TestExecutionListener, Ord
}
)

private fun TestContext.annotations() = (this.testClass.annotations + this.testMethod.annotations)
private fun TestContext.annotations(): List<Annotation> = NestedClassUtil.getHierarchy(this.testClass).flatMap { it.annotations.toList() } + this.testMethod.annotations.toList()
private fun TestContext.dbUnit() = applicationContext.getBean(DatabaseTester::class.java)
private fun String.toArray() = this.takeIf { it.isNotEmpty() }?.let { arrayOf(it) } ?: emptyArray()
}
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`
}

0 comments on commit 455486b

Please sign in to comment.