Files
hlaeja-test/src/main/kotlin/ltd/hlaeja/test/container/PostgresInitializer.kt
Swordsteel 6aad7e3d63 update postgres test container
- update README.md
- update PostgresContainer
  - add TestExecutionListeners
  - remove ExtendWith
- update PostgresInitializer
  - cleanup
  - use properties for script and container
  - add afterTestClass
  - add beforeTestClass
  - extend TestExecutionListener
- remove PostgresExtension
- add debug logging to PostgresExecutor
- add ContainerUtils
- add dependencies
- extract function from PostgresExtension to PostgresExecutor
2025-03-10 19:48:44 +01:00

68 lines
2.6 KiB
Kotlin

package ltd.hlaeja.test.container
import io.github.oshai.kotlinlogging.KotlinLogging
import ltd.hlaeja.test.util.getProperty
import ltd.hlaeja.test.util.isResourceFile
import org.springframework.boot.test.util.TestPropertyValues
import org.springframework.context.ApplicationContextInitializer
import org.springframework.context.ConfigurableApplicationContext
import org.springframework.test.context.TestContext
import org.springframework.test.context.TestExecutionListener
import org.testcontainers.containers.PostgreSQLContainer
private val log = KotlinLogging.logger {}
@Suppress("unused")
class PostgresInitializer : ApplicationContextInitializer<ConfigurableApplicationContext>, TestExecutionListener {
companion object {
const val SCRIPT_INIT = "container.postgres.init"
const val SCRIPT_BEFORE = "container.postgres.before"
const val SCRIPT_AFTER = "container.postgres.after"
const val POSTGRES_VERSION = "container.postgres.version"
const val POSTGRES_LATEST = "postgres:latest"
}
override fun initialize(
context: ConfigurableApplicationContext,
) {
postgres(context).apply {
TestPropertyValues.of(
"spring.r2dbc.url=r2dbc:pool:postgresql://$host:$firstMappedPort/$databaseName",
"spring.r2dbc.username=$username",
"spring.r2dbc.password=$password",
).applyTo(context)
}
}
override fun beforeTestClass(
context: TestContext,
) {
context.testClass
.also { log.debug { "Starting execution before class: ${it.simpleName}" } }
.getAnnotation(PostgresContainer::class.java) ?: return
context.getProperty(SCRIPT_BEFORE)
?.let { context.applicationContext.getBean(PostgresExecutor::class.java).executeSqlFile(it) }
}
override fun afterTestClass(
context: TestContext,
) {
context.testClass
.also { log.debug { "Starting execution after class: ${it.simpleName}" } }
.getAnnotation(PostgresContainer::class.java) ?: return
context.getProperty(SCRIPT_AFTER)
?.let { context.applicationContext.getBean(PostgresExecutor::class.java).executeSqlFile(it) }
}
private fun postgres(
context: ConfigurableApplicationContext,
): PostgreSQLContainer<*> = PostgreSQLContainer(context.getProperty(POSTGRES_VERSION, POSTGRES_LATEST)).apply {
context.getProperty(SCRIPT_INIT)
?.isResourceFile()
?.let { lala -> withInitScript(lala.path) }
?: log.error { "Postgres init script not found" }
start()
}
}