Skip to content

Commit

Permalink
Merge pull request #56 from momosetkn/feat/custom-komapper-jdbc-chang…
Browse files Browse the repository at this point in the history
…e-unit-test

custom-komapper-jdbc-change unit-test
  • Loading branch information
momosetkn authored Oct 5, 2024
2 parents 7c168b9 + 621bfd0 commit 4c0ddb6
Show file tree
Hide file tree
Showing 6 changed files with 382 additions and 3 deletions.
8 changes: 7 additions & 1 deletion custom-komapper-jdbc-change/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ val liquibaseVersion = rootProject.properties["liquibaseVersion"] as String
val kotestVersion = rootProject.properties["kotestVersion"] as String
val liquibaseKotlinVersion = rootProject.properties["liquibaseKotlinVersion"] as String
val kotlinVersion = rootProject.properties["kotlinVersion"] as String
val komapperVersion = "3.1.0"
val komapperVersion = rootProject.properties["komapperVersion"] as String

dependencies {
implementation(project(":dsl"))
Expand All @@ -18,6 +18,12 @@ dependencies {
// test
testImplementation("io.kotest:kotest-framework-engine-jvm:$kotestVersion")
testImplementation("io.kotest:kotest-runner-junit5:$kotestVersion")
testImplementation("org.komapper:komapper-dialect-h2-jdbc:$komapperVersion")
testImplementation("org.komapper:komapper-dialect-mariadb-jdbc:$komapperVersion")
testImplementation("org.komapper:komapper-dialect-mysql-jdbc:$komapperVersion")
testImplementation("org.komapper:komapper-dialect-oracle-jdbc:$komapperVersion")
testImplementation("org.komapper:komapper-dialect-postgresql-jdbc:$komapperVersion")
testImplementation("org.komapper:komapper-dialect-sqlserver-jdbc:$komapperVersion")
}

tasks.test {
Expand Down
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()
}
)
}
}
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")
}
}
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")
}
Loading

0 comments on commit 4c0ddb6

Please sign in to comment.