From a294b509343936a19acf638a503948af579294cf Mon Sep 17 00:00:00 2001 From: Joe Barnett Date: Fri, 9 Jun 2023 16:16:19 -0700 Subject: [PATCH] Allow for non-postgres usage in DAOTestBase Still default to postgres, but allow overrides of the testcontainers container and the jooq dialect --- parent-pom/pom.xml | 7 ++++ testing/pom.xml | 5 +++ .../com/trib3/testing/db/DAOTestBase.kt | 33 ++++++++++++++----- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/parent-pom/pom.xml b/parent-pom/pom.xml index 0d86675a5..857feae3f 100644 --- a/parent-pom/pom.xml +++ b/parent-pom/pom.xml @@ -1138,6 +1138,12 @@ ${version.testcontainers} test + + org.testcontainers + jdbc + ${version.testcontainers} + test + @@ -1336,6 +1342,7 @@ git-commit-id-maven-plugin ${version.maven.git} + true true ${project.build.outputDirectory}/${project.groupId}/${project.artifactId}.git.properties diff --git a/testing/pom.xml b/testing/pom.xml index 47968710c..1218677fc 100644 --- a/testing/pom.xml +++ b/testing/pom.xml @@ -114,6 +114,11 @@ postgresql compile + + org.testcontainers + jdbc + compile + org.postgresql postgresql diff --git a/testing/src/main/kotlin/com/trib3/testing/db/DAOTestBase.kt b/testing/src/main/kotlin/com/trib3/testing/db/DAOTestBase.kt index 44f6c2857..c647ff273 100644 --- a/testing/src/main/kotlin/com/trib3/testing/db/DAOTestBase.kt +++ b/testing/src/main/kotlin/com/trib3/testing/db/DAOTestBase.kt @@ -6,6 +6,7 @@ import org.flywaydb.core.api.configuration.FluentConfiguration import org.jooq.DSLContext import org.jooq.SQLDialect import org.jooq.impl.DSL +import org.testcontainers.containers.JdbcDatabaseContainer import org.testcontainers.containers.PostgreSQLContainer import org.testng.annotations.AfterClass import org.testng.annotations.BeforeClass @@ -18,7 +19,7 @@ import javax.sql.DataSource open class DAOTestBase { lateinit var dataSource: DataSource lateinit var ctx: DSLContext - private lateinit var postgres: PostgreSQLContainer + private lateinit var database: JdbcDatabaseContainer<*> private var inited: Boolean = false /** @@ -30,6 +31,22 @@ open class DAOTestBase { return Flyway.configure().dataSource(dataSource) } + /** + * By default create a postgres container, but subclasses + * can override for other implementations + */ + open fun getDatabaseContainer(): JdbcDatabaseContainer<*> { + return PostgreSQLContainer("postgres:13.4") + } + + /** + * By default configure a POSTGRES dialect, but subclasses + * can override for other dialects + */ + open fun getJooqDialect(): SQLDialect { + return SQLDialect.POSTGRES + } + /** * By default configure autoCommit to false since * we do so in DbConfig, and don't want DAO tests @@ -44,22 +61,22 @@ open class DAOTestBase { open fun setUp() { if (!inited) { inited = true - postgres = PostgreSQLContainer("postgres:13.4") - postgres.start() + database = getDatabaseContainer() + database.start() dataSource = HikariDataSource().apply { - jdbcUrl = postgres.jdbcUrl - username = postgres.username - password = postgres.password + jdbcUrl = database.jdbcUrl + username = database.username + password = database.password configureDataSource(this) } - ctx = DSL.using(dataSource, SQLDialect.POSTGRES) + ctx = DSL.using(dataSource, getJooqDialect()) getFlywayConfiguration().load().migrate() } } @AfterClass open fun tearDown() { - postgres.stop() + database.stop() (dataSource as HikariDataSource).close() } }