Skip to content

Commit

Permalink
User testcontainers for MySQL instead of mysql-connector-mxj
Browse files Browse the repository at this point in the history
The mxj library is flaky and doesn't work on various platforms due to
being outdated.
  • Loading branch information
KushalP committed Jun 15, 2020
1 parent 56863e0 commit aee667d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 27 deletions.
2 changes: 1 addition & 1 deletion exposed-java-time/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ dependencies {
testImplementation(kotlin("test-junit"))

testImplementation("com.opentable.components", "otj-pg-embedded", "0.12.0")
testImplementation("mysql", "mysql-connector-mxj", Versions.mysqlMxj)
testImplementation("org.xerial", "sqlite-jdbc", Versions.sqlLite3)
testImplementation("com.h2database", "h2", Versions.h2)
testRuntimeOnly("org.testcontainers", "testcontainers", "1.14.3")

when (dialect) {
"mariadb" -> testImplementation("org.mariadb.jdbc", "mariadb-java-client", Versions.mariaDB)
Expand Down
2 changes: 1 addition & 1 deletion exposed-jodatime/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ dependencies {
testImplementation(kotlin("test-junit"))

testImplementation("com.opentable.components", "otj-pg-embedded", "0.12.0")
testImplementation("mysql", "mysql-connector-mxj", Versions.mysqlMxj)
testImplementation("org.xerial", "sqlite-jdbc", Versions.sqlLite3)
testImplementation("com.h2database", "h2", Versions.h2)
testRuntimeOnly("org.testcontainers", "testcontainers", "1.14.3")

when (dialect) {
"mariadb" -> testImplementation("org.mariadb.jdbc", "mariadb-java-client", Versions.mariaDB)
Expand Down
2 changes: 1 addition & 1 deletion exposed-money/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ dependencies {
testImplementation(kotlin("test-junit"))

testImplementation("com.opentable.components", "otj-pg-embedded", "0.12.0")
testImplementation("mysql", "mysql-connector-mxj", "5.0.12")
testImplementation("org.xerial", "sqlite-jdbc", "3.23.1")
testImplementation("com.h2database", "h2", "1.4.199")
testImplementation("org.javamoney", "moneta", "1.3")
testRuntimeOnly("org.testcontainers", "testcontainers", "1.14.3")

when (dialect) {
"mariadb" -> testImplementation("org.mariadb.jdbc", "mariadb-java-client", "2.4.1")
Expand Down
3 changes: 2 additions & 1 deletion exposed-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ dependencies {
implementation("org.jetbrains.kotlinx","kotlinx-coroutines-debug", Versions.kotlinCoroutines)

implementation("com.opentable.components", "otj-pg-embedded", "0.12.0")
implementation("mysql", "mysql-connector-mxj", Versions.mysqlMxj)
implementation("org.xerial", "sqlite-jdbc", Versions.sqlLite3)
implementation("com.h2database", "h2", Versions.h2)
implementation("org.testcontainers", "testcontainers", "1.14.3")
implementation("org.testcontainers", "mysql", "1.14.3")

when (dialect) {
"mariadb" -> implementation("org.mariadb.jdbc", "mariadb-java-client", Versions.mariaDB)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package org.jetbrains.exposed.sql.tests

import com.mysql.management.MysqldResource
import com.mysql.management.driverlaunched.MysqldResourceNotFoundException
import com.mysql.management.driverlaunched.ServerLauncherSocketFactory
import com.mysql.management.util.Files
import com.opentable.db.postgres.embedded.EmbeddedPostgres
import org.h2.engine.Mode
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.transactions.transactionManager
import org.testcontainers.containers.MySQLContainer
import java.sql.Connection
import java.util.*
import kotlin.concurrent.thread
Expand All @@ -21,25 +17,14 @@ enum class TestDB(val connection: () -> String, val driver: String, val user: St
Mode.getInstance("MySQL").convertInsertNullToZero = false
}),
SQLITE({"jdbc:sqlite:file:test?mode=memory&cache=shared"}, "org.sqlite.JDBC"),
MYSQL({
val host = System.getProperty("exposed.test.mysql.host") ?: System.getProperty("exposed.test.mysql8.host")
val port = System.getProperty("exposed.test.mysql.port") ?: System.getProperty("exposed.test.mysql8.port")
host?.let { dockerHost ->
"jdbc:mysql://$dockerHost:$port/testdb?useSSL=false&characterEncoding=UTF-8"
} ?: "jdbc:mysql:mxj://localhost:12345/testdb1?createDatabaseIfNotExist=true&characterEncoding=UTF-8&server.initialize-user=false&user=root&password="
},
MYSQL(
connection = { "${mySQLProcess.jdbcUrl}?createDatabaseIfNotExist=true&characterEncoding=UTF-8&useSSL=false" },
user = mySQLProcess.username,
pass = mySQLProcess.password,
driver = "com.mysql.jdbc.Driver",
beforeConnection = { System.setProperty(Files.USE_TEST_DIR, java.lang.Boolean.TRUE!!.toString()); Files().cleanTestDir(); Unit },
afterTestFinished = {
try {
val baseDir = com.mysql.management.util.Files().tmp(MysqldResource.MYSQL_C_MXJ)
ServerLauncherSocketFactory.shutdown(baseDir, null)
} catch (e: MysqldResourceNotFoundException) {
exposedLogger.warn(e.message, e)
} finally {
Files().cleanTestDir()
}
}),
beforeConnection = { mySQLProcess },
afterTestFinished = { mySQLProcess.close() }
),
POSTGRESQL({"jdbc:postgresql://localhost:12346/template1?user=postgres&password=&lc_messages=en_US.UTF-8"}, "org.postgresql.Driver",
beforeConnection = { postgresSQLProcess }, afterTestFinished = { postgresSQLProcess.close() }),
POSTGRESQLNG({"jdbc:pgsql://localhost:12346/template1?user=postgres&password="}, "com.impossibl.postgres.jdbc.PGDriver",
Expand Down Expand Up @@ -95,6 +80,15 @@ private val postgresSQLProcess by lazy {
.setPort(12346).start()
}

// MySQLContainer has to be extended, otherwise it leads to Kotlin compiler issues: https://github.com/testcontainers/testcontainers-java/issues/318
internal class SpecifiedMySQLContainer(val image: String) : MySQLContainer<SpecifiedMySQLContainer>(image)

private val mySQLProcess by lazy {
SpecifiedMySQLContainer(image = "mysql:5").withDatabaseName("testdb").withExposedPorts().apply {
start()
}
}

abstract class DatabaseTestsBase {
init {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
Expand Down

0 comments on commit aee667d

Please sign in to comment.