-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from momosetkn/feat/custom-komapper-jdbc-chang…
…e-unit-test custom-komapper-jdbc-change unit-test
- Loading branch information
Showing
6 changed files
with
382 additions
and
3 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
29 changes: 27 additions & 2 deletions
29
...c-change/src/main/kotlin/momosetkn/liquibase/kotlin/change/LiquibaseKomapperJdbcConfig.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 |
---|---|---|
@@ -1,13 +1,38 @@ | ||
package momosetkn.liquibase.kotlin.change | ||
|
||
import org.komapper.jdbc.JdbcDatabase | ||
import org.komapper.jdbc.JdbcDialect | ||
import org.komapper.jdbc.JdbcDialects | ||
import org.komapper.jdbc.spi.JdbcDialectFactory | ||
import java.util.ServiceLoader | ||
|
||
object LiquibaseKomapperJdbcConfig { | ||
var provideJdbcDatabase: ( | ||
javaxSqlDataSource: javax.sql.DataSource, | ||
liquibaseDatabaseShortName: String | ||
) -> JdbcDatabase = { javaxSqlDataSource, liquibaseDatabaseShortName -> | ||
JdbcDatabase(javaxSqlDataSource, JdbcDialects.get(liquibaseDatabaseShortName)) | ||
) -> JdbcDatabase = ::defaultProvideJdbcDatabase | ||
|
||
fun defaultProvideJdbcDatabase( | ||
javaxSqlDataSource: javax.sql.DataSource, | ||
liquibaseDatabaseShortName: String | ||
): JdbcDatabase { | ||
return JdbcDatabase(javaxSqlDataSource, getJdbcDialect(liquibaseDatabaseShortName)) | ||
} | ||
|
||
private fun getJdbcDialect(liquibaseDatabaseShortName: String): JdbcDialect { | ||
return runCatching { | ||
JdbcDialects.get(liquibaseDatabaseShortName) | ||
}.fold( | ||
onSuccess = { it }, | ||
onFailure = { | ||
val loader = ServiceLoader.load(JdbcDialectFactory::class.java) | ||
val factory = loader.singleOrNull() | ||
checkNotNull(factory) { | ||
@Suppress("MaxLineLength") | ||
"I could not find the `org.komapper.jdbc.JdbcDialects` that should be used. Please set the `${this::class.qualifiedName}.provideJdbcDatabase`." | ||
} | ||
factory.create() | ||
} | ||
) | ||
} | ||
} |
249 changes: 249 additions & 0 deletions
249
...ange/src/test/kotlin/momosetkn/liquibase/kotlin/change/LiquibaseKomapperJdbcConfigSpec.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,249 @@ | ||
package momosetkn.liquibase.kotlin.change | ||
|
||
import io.kotest.assertions.throwables.shouldThrow | ||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.matchers.shouldBe | ||
import io.kotest.matchers.types.shouldBeInstanceOf | ||
import liquibase.database.core.CockroachDatabase | ||
import liquibase.database.core.DB2Database | ||
import liquibase.database.core.Db2zDatabase | ||
import liquibase.database.core.DerbyDatabase | ||
import liquibase.database.core.EnterpriseDBDatabase | ||
import liquibase.database.core.FirebirdDatabase | ||
import liquibase.database.core.H2Database | ||
import liquibase.database.core.HsqlDatabase | ||
import liquibase.database.core.InformixDatabase | ||
import liquibase.database.core.Ingres9Database | ||
import liquibase.database.core.MSSQLDatabase | ||
import liquibase.database.core.MariaDBDatabase | ||
import liquibase.database.core.MySQLDatabase | ||
import liquibase.database.core.OracleDatabase | ||
import liquibase.database.core.PostgresDatabase | ||
import liquibase.database.core.SQLiteDatabase | ||
import liquibase.database.core.SnowflakeDatabase | ||
import liquibase.database.core.SybaseASADatabase | ||
import liquibase.database.core.SybaseDatabase | ||
import org.komapper.dialect.h2.jdbc.H2JdbcDialect | ||
import org.komapper.dialect.mariadb.jdbc.MariaDbJdbcDialect | ||
import org.komapper.dialect.mysql.jdbc.MySqlJdbcDialect | ||
import org.komapper.dialect.oracle.jdbc.OracleJdbcDialect | ||
import org.komapper.dialect.postgresql.jdbc.PostgreSqlJdbcDialect | ||
import org.komapper.jdbc.JdbcDatabase | ||
import java.io.PrintWriter | ||
import java.lang.IllegalStateException | ||
import java.sql.Connection | ||
import java.util.logging.Logger | ||
import kotlin.reflect.KClass | ||
import liquibase.database.Database as LiquibaseDatabase | ||
|
||
class LiquibaseKomapperJdbcConfigSpec : FunSpec({ | ||
fun subject(clazz: KClass<out LiquibaseDatabase>): JdbcDatabase { | ||
val liquibaseDatabase = clazz.constructors.find { it.parameters.isEmpty() }!!.call() | ||
return LiquibaseKomapperJdbcConfig.defaultProvideJdbcDatabase( | ||
javaxSqlDataSource = MockDataSource, | ||
liquibaseDatabaseShortName = liquibaseDatabase.shortName | ||
) | ||
} | ||
|
||
@Suppress("MaxLineLength") | ||
val errorMessage = "I could not find the `org.komapper.jdbc.JdbcDialects` that should be used. Please set the `momosetkn.liquibase.kotlin.change.LiquibaseKomapperJdbcConfig.provideJdbcDatabase`." | ||
|
||
context("CockroachDatabase") { | ||
test("not supported") { | ||
val exception = shouldThrow<IllegalStateException> { | ||
subject(CockroachDatabase::class) | ||
} | ||
exception.message shouldBe errorMessage | ||
} | ||
} | ||
|
||
context("DB2Database") { | ||
test("not supported") { | ||
val exception = shouldThrow<IllegalStateException> { | ||
subject(DB2Database::class) | ||
} | ||
exception.message shouldBe errorMessage | ||
} | ||
} | ||
|
||
context("Db2zDatabase") { | ||
test("not supported") { | ||
val exception = shouldThrow<IllegalStateException> { | ||
subject(Db2zDatabase::class) | ||
} | ||
exception.message shouldBe errorMessage | ||
} | ||
} | ||
|
||
context("DerbyDatabase") { | ||
test("not supported") { | ||
val exception = shouldThrow<IllegalStateException> { | ||
subject(DerbyDatabase::class) | ||
} | ||
exception.message shouldBe errorMessage | ||
} | ||
} | ||
|
||
context("EnterpriseDBDatabase") { | ||
test("not supported") { | ||
val exception = shouldThrow<IllegalStateException> { | ||
subject(EnterpriseDBDatabase::class) | ||
} | ||
exception.message shouldBe errorMessage | ||
} | ||
} | ||
|
||
context("FirebirdDatabase") { | ||
test("not supported") { | ||
val exception = shouldThrow<IllegalStateException> { | ||
subject(FirebirdDatabase::class) | ||
} | ||
exception.message shouldBe errorMessage | ||
} | ||
} | ||
|
||
context("H2Database") { | ||
test("supported") { | ||
val actual = subject(H2Database::class) | ||
actual.config.dialect.shouldBeInstanceOf<H2JdbcDialect>() | ||
} | ||
} | ||
|
||
context("HsqlDatabase") { | ||
test("not supported") { | ||
val exception = shouldThrow<IllegalStateException> { | ||
subject(HsqlDatabase::class) | ||
} | ||
exception.message shouldBe errorMessage | ||
} | ||
} | ||
|
||
context("InformixDatabase") { | ||
test("not supported") { | ||
val exception = shouldThrow<IllegalStateException> { | ||
subject(InformixDatabase::class) | ||
} | ||
exception.message shouldBe errorMessage | ||
} | ||
} | ||
|
||
context("Ingres9Database") { | ||
test("not supported") { | ||
val exception = shouldThrow<IllegalStateException> { | ||
subject(Ingres9Database::class) | ||
} | ||
exception.message shouldBe errorMessage | ||
} | ||
} | ||
|
||
context("MariaDBDatabase") { | ||
test("supported") { | ||
val actual = subject(MariaDBDatabase::class) | ||
actual.config.dialect.shouldBeInstanceOf<MariaDbJdbcDialect>() | ||
} | ||
} | ||
|
||
context("MSSQLDatabase") { | ||
test("not supported") { | ||
val exception = shouldThrow<IllegalStateException> { | ||
subject(MSSQLDatabase::class) | ||
} | ||
exception.message shouldBe errorMessage | ||
} | ||
} | ||
|
||
context("MySQLDatabase") { | ||
test("supported") { | ||
val actual = subject(MySQLDatabase::class) | ||
actual.config.dialect.shouldBeInstanceOf<MySqlJdbcDialect>() | ||
} | ||
} | ||
|
||
context("OracleDatabase") { | ||
test("supported") { | ||
val actual = subject(OracleDatabase::class) | ||
actual.config.dialect.shouldBeInstanceOf<OracleJdbcDialect>() | ||
} | ||
} | ||
|
||
context("PostgresDatabase") { | ||
test("supported") { | ||
val actual = subject(PostgresDatabase::class) | ||
actual.config.dialect.shouldBeInstanceOf<PostgreSqlJdbcDialect>() | ||
} | ||
} | ||
|
||
context("SnowflakeDatabase") { | ||
test("not supported") { | ||
val exception = shouldThrow<IllegalStateException> { | ||
subject(SnowflakeDatabase::class) | ||
} | ||
exception.message shouldBe errorMessage | ||
} | ||
} | ||
|
||
context("SQLiteDatabase") { | ||
test("not supported") { | ||
val exception = shouldThrow<IllegalStateException> { | ||
subject(SQLiteDatabase::class) | ||
} | ||
exception.message shouldBe errorMessage | ||
} | ||
} | ||
|
||
context("SybaseASADatabase") { | ||
test("not supported") { | ||
val exception = shouldThrow<IllegalStateException> { | ||
subject(SybaseASADatabase::class) | ||
} | ||
exception.message shouldBe errorMessage | ||
} | ||
} | ||
|
||
context("SybaseDatabase") { | ||
test("not supported") { | ||
val exception = shouldThrow<IllegalStateException> { | ||
subject(SybaseDatabase::class) | ||
} | ||
exception.message shouldBe errorMessage | ||
} | ||
} | ||
}) | ||
|
||
val MockDataSource = object : javax.sql.DataSource { | ||
override fun getLogWriter(): PrintWriter { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun setLogWriter(out: PrintWriter?) { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun setLoginTimeout(seconds: Int) { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun getLoginTimeout(): Int { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun getParentLogger(): Logger { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun <T : Any?> unwrap(iface: Class<T>?): T { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun isWrapperFor(iface: Class<*>?): Boolean { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun getConnection(): Connection { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun getConnection(username: String?, password: String?): Connection { | ||
TODO("Not yet implemented") | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
liquibase-kotlin.custom-komapper-jdbc-change-unit-test/build.gradle.kts
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,29 @@ | ||
val liquibaseVersion = rootProject.properties["liquibaseVersion"] as String | ||
val kotestVersion = rootProject.properties["kotestVersion"] as String | ||
val slf4jVersion = rootProject.properties["slf4jVersion"] as String | ||
val komapperVersion = rootProject.properties["komapperVersion"] as String | ||
|
||
dependencies { | ||
implementation(project(":custom-komapper-jdbc-change")) | ||
|
||
// log | ||
implementation("org.slf4j:slf4j-api:$slf4jVersion") | ||
implementation("org.apache.logging.log4j:log4j-slf4j2-impl:2.24.1") | ||
implementation("org.apache.logging.log4j:log4j-api-kotlin:1.5.0") | ||
|
||
// komapper | ||
implementation("org.komapper:komapper-dialect-postgresql-jdbc:$komapperVersion") | ||
|
||
// test | ||
testImplementation(kotlin("test")) | ||
testImplementation("io.kotest:kotest-framework-engine-jvm:$kotestVersion") | ||
testImplementation("io.kotest:kotest-runner-junit5:$kotestVersion") | ||
|
||
// db-migration | ||
implementation("org.liquibase:liquibase-core:$liquibaseVersion") | ||
} | ||
|
||
tasks.test { | ||
useJUnitPlatform() | ||
systemProperty("kotest.framework.classpath.scanning.config.disable", "true") | ||
} |
Oops, something went wrong.