Skip to content

Commit

Permalink
Fix db credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
e5l committed Aug 31, 2023
1 parent bad2d2f commit 8262554
Show file tree
Hide file tree
Showing 16 changed files with 72 additions and 87 deletions.
1 change: 1 addition & 0 deletions buildScripts/docker/docker-compose-postgres.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ services:
ports:
- "3004:5432"
environment:
POSTGRES_USER: "root"
POSTGRES_PASSWORD: "Exposed_password_1!"
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ fun Project.testDb(name: String, block: TestDb.() -> Unit) {
val testTask = tasks.register<Test>("test${db.name.capitalized()}") {
description = "Runs tests using ${db.name} database"
group = "verification"
systemProperties["exposed.test.container"] = db.container
systemProperties["exposed.test.dialects"] = db.dialects.joinToString(",") { it.toUpperCase() }
outputs.cacheIf { false }
ignoreFailures = true

if (!db.withContainer) return@register
dependsOn(rootProject.tasks.getByName("${db.container}ComposeUp"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ abstract class Statement<out T>(val type: StatementType, val targets: List<Table
transaction.interceptors.forEach { it.afterStatementPrepared(transaction, statement) }
val result = try {
statement.executeInternal(transaction)
} catch (e: SQLException) {
throw ExposedSQLException(e, contexts, transaction)
} catch (cause: SQLException) {
throw ExposedSQLException(cause, contexts, transaction)
}
transaction.currentStatement = null
transaction.executedStatements.add(statement)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import org.jetbrains.exposed.sql.transactions.transactionManager
import org.jetbrains.exposed.sql.vendors.H2Dialect
import org.jetbrains.exposed.sql.vendors.MysqlDialect
import org.junit.Assume
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import org.junit.runners.Parameterized.Parameters
import java.lang.IllegalStateException
import java.math.BigDecimal
import java.sql.Connection
import java.sql.SQLException
Expand All @@ -19,8 +23,7 @@ import kotlin.reflect.KMutableProperty1
import kotlin.reflect.full.declaredMemberProperties

val TEST_DIALECTS: HashSet<String> = System.getProperty(
"exposed.test.dialects",
""
"exposed.test.dialects", ""
).split(",").mapTo(HashSet()) { it.trim().uppercase() }

enum class TestDB(
Expand All @@ -38,80 +41,50 @@ enum class TestDB(
H2_MYSQL({ "jdbc:h2:mem:mysql;MODE=MySQL;DB_CLOSE_DELAY=-1" }, "org.h2.Driver", beforeConnection = {
Mode::class.declaredMemberProperties.firstOrNull { it.name == "convertInsertNullToZero" }?.let { field ->
val mode = Mode.getInstance("MySQL")
@Suppress("UNCHECKED_CAST")
(field as KMutableProperty1<Mode, Boolean>).set(mode, false)
@Suppress("UNCHECKED_CAST") (field as KMutableProperty1<Mode, Boolean>).set(mode, false)
}
}),
H2_MARIADB(
{ "jdbc:h2:mem:mariadb;MODE=MariaDB;DATABASE_TO_LOWER=TRUE;DB_CLOSE_DELAY=-1" },
"org.h2.Driver",
user = "root",
pass = "root"
{ "jdbc:h2:mem:mariadb;MODE=MariaDB;DATABASE_TO_LOWER=TRUE;DB_CLOSE_DELAY=-1" }, "org.h2.Driver", pass = "root"
),
H2_PSQL(
{ "jdbc:h2:mem:psql;MODE=PostgreSQL;DATABASE_TO_LOWER=TRUE;DEFAULT_NULL_ORDERING=HIGH;DB_CLOSE_DELAY=-1" },
"org.h2.Driver"
{ "jdbc:h2:mem:psql;MODE=PostgreSQL;DATABASE_TO_LOWER=TRUE;DEFAULT_NULL_ORDERING=HIGH;DB_CLOSE_DELAY=-1" }, "org.h2.Driver"
),
H2_ORACLE(
{ "jdbc:h2:mem:oracle;MODE=Oracle;DATABASE_TO_LOWER=TRUE;DEFAULT_NULL_ORDERING=HIGH;DB_CLOSE_DELAY=-1" },
"org.h2.Driver"
{ "jdbc:h2:mem:oracle;MODE=Oracle;DATABASE_TO_LOWER=TRUE;DEFAULT_NULL_ORDERING=HIGH;DB_CLOSE_DELAY=-1" }, "org.h2.Driver"
),
H2_SQLSERVER({ "jdbc:h2:mem:sqlserver;MODE=MSSQLServer;DB_CLOSE_DELAY=-1" }, "org.h2.Driver"),
SQLITE({ "jdbc:sqlite:file:test?mode=memory&cache=shared" }, "org.sqlite.JDBC"),
MYSQL(
connection = {
"jdbc:mysql://127.0.0.1:3001/testdb?useSSL=false&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull"
},
user = "root",
pass = "root",
driver = "com.mysql.jdbc.Driver"
),
POSTGRESQL(
{ "jdbc:postgresql://127.0.0.1:3004/&user=postgres&password=&lc_messages=en_US.UTF-8" },
"org.postgresql.Driver",
beforeConnection = { },
afterTestFinished = { }
}, driver = "com.mysql.jdbc.Driver"
),
POSTGRESQL({ "jdbc:postgresql://127.0.0.1:3004/&user=postgres&password=&lc_messages=en_US.UTF-8" },
"org.postgresql.Driver",
beforeConnection = { },
afterTestFinished = { }),
POSTGRESQLNG(
{ "jdbc:pgsql://127.0.0.1:3004/&user=postgres&password=&lc_messages=en_US.UTF-8" },
"com.impossibl.postgres.jdbc.PGDriver",
user = "postgres",
),
ORACLE(driver = "oracle.jdbc.OracleDriver", user = "ExposedTest", pass = "12345", connection = {
"jdbc:oracle:thin:@//127.0.0.1:3003/XEPDB1"
ORACLE(driver = "oracle.jdbc.OracleDriver", user = "sys as sysdba", pass = "Oracle18", connection = {
"jdbc:oracle:thin:@127.0.0.1:3003:XE"
}, beforeConnection = {
Locale.setDefault(Locale.ENGLISH)
val tmp = Database.connect(
ORACLE.connection(),
user = "sys as sysdba",
password = "Oracle18",
driver = ORACLE.driver
)
transaction(Connection.TRANSACTION_READ_COMMITTED, db = tmp) {
repetitionAttempts = 1
try {
exec("DROP USER ExposedTest CASCADE")
} catch (cause: Throwable) { // ignore
exposedLogger.warn("Exception on deleting ExposedTest user: $cause")
}
exec("CREATE USER ExposedTest ACCOUNT UNLOCK IDENTIFIED BY 12345")
exec("grant all privileges to ExposedTest")
}
Unit
}),
SQLSERVER(
{
"jdbc:sqlserver://127.0.0.1:3005"
},
"com.microsoft.sqlserver.jdbc.SQLServerDriver",
"SA",
"yourStrong(!)Password"
),
MARIADB(
{
"jdbc:mariadb://127.0.0.1:3000/testdb"
},
"org.mariadb.jdbc.Driver"
}, "org.mariadb.jdbc.Driver"
);

var db: Database? = null
Expand All @@ -127,7 +100,7 @@ enum class TestDB(
companion object {
val allH2TestDB = listOf(H2, H2_MYSQL, H2_PSQL, H2_MARIADB, H2_ORACLE, H2_SQLSERVER)
val mySqlRelatedDB = listOf(MYSQL, MARIADB, H2_MYSQL, H2_MARIADB)
fun enabledInTests(): Set<TestDB> {
fun enabledDialects(): Set<TestDB> {
return values().filterTo(enumSetOf()) { it.name in TEST_DIALECTS }
}
}
Expand Down Expand Up @@ -169,6 +142,7 @@ private val registeredOnShutdown = HashSet<TestDB>()

internal var currentTestDB by nullableTransactionScope<TestDB>()

@RunWith(Parameterized::class)
abstract class DatabaseTestsBase {
init {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
Expand All @@ -180,17 +154,31 @@ abstract class DatabaseTestsBase {
}
}

companion object {
@Parameters(name = "container: {0}, dialect: {1}")
@JvmStatic
fun data(): Collection<Array<Any>> {
val container = System.getProperty("exposed.test.container")
return TestDB.enabledDialects().map { arrayOf(container, it) }
}
}


@Parameterized.Parameter(0)
lateinit var container: String

@Parameterized.Parameter(1)
lateinit var dialect: Any

fun withDb(dbSettings: TestDB, statement: Transaction.(TestDB) -> Unit) {
Assume.assumeTrue(dbSettings in TestDB.enabledInTests())
Assume.assumeTrue(dbSettings in TestDB.enabledDialects())

if (dbSettings !in registeredOnShutdown) {
dbSettings.beforeConnection()
Runtime.getRuntime().addShutdownHook(
thread(false) {
dbSettings.afterTestFinished()
registeredOnShutdown.remove(dbSettings)
}
)
Runtime.getRuntime().addShutdownHook(thread(false) {
dbSettings.afterTestFinished()
registeredOnShutdown.remove(dbSettings)
})
registeredOnShutdown += dbSettings
dbSettings.db = dbSettings.connect()
}
Expand All @@ -206,26 +194,25 @@ abstract class DatabaseTestsBase {
} catch (cause: SQLException) {
throw cause
} catch (cause: Throwable) {
throw Exception("Failed on ${dbSettings.name}", cause)
throw IllegalStateException("Failed on ${dbSettings.name}", cause)
}
}

fun withDb(db: List<TestDB>? = null, excludeSettings: List<TestDB> = emptyList(), statement: Transaction.(TestDB) -> Unit) {
val enabledInTests = TestDB.enabledInTests()
val enabledInTests = TestDB.enabledDialects()
val toTest = db?.intersect(enabledInTests) ?: (enabledInTests - excludeSettings)
Assume.assumeTrue(toTest.isNotEmpty())
toTest.forEach { dbSettings ->
@Suppress("TooGenericExceptionCaught")
try {
@Suppress("TooGenericExceptionCaught") try {
withDb(dbSettings, statement)
} catch (e: Exception) {
throw AssertionError("Failed on ${dbSettings.name}", e)
} catch (cause: Throwable) {
throw AssertionError("Failed on ${dbSettings.name}", cause)
}
}
}

fun withTables(excludeSettings: List<TestDB>, vararg tables: Table, statement: Transaction.(TestDB) -> Unit) {
val toTest = TestDB.enabledInTests() - excludeSettings
val toTest = TestDB.enabledDialects() - excludeSettings
Assume.assumeTrue(toTest.isNotEmpty())
toTest.forEach { testDB ->
withDb(testDB) {
Expand All @@ -250,7 +237,7 @@ abstract class DatabaseTestsBase {
}

fun withSchemas(excludeSettings: List<TestDB>, vararg schemas: Schema, statement: Transaction.() -> Unit) {
val toTest = TestDB.enabledInTests() - excludeSettings
val toTest = TestDB.enabledDialects() - excludeSettings
Assume.assumeTrue(toTest.isNotEmpty())
toTest.forEach { testDB ->
withDb(testDB) {
Expand Down Expand Up @@ -292,10 +279,6 @@ abstract class DatabaseTestsBase {
fun Transaction.isOldMySql(version: String = "8.0") = currentDialectTest is MysqlDialect && !db.isVersionCovers(BigDecimal(version))

protected fun prepareSchemaForTest(schemaName: String): Schema = Schema(
schemaName,
defaultTablespace = "USERS",
temporaryTablespace = "TEMP ",
quota = "20M",
on = "USERS"
schemaName, defaultTablespace = "USERS", temporaryTablespace = "TEMP ", quota = "20M", on = "USERS"
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class City(id: EntityID<Int>) : IntEntity(id) {
}

fun main() {
Assume.assumeTrue(TestDB.H2 in TestDB.enabledInTests())
Assume.assumeTrue(TestDB.H2 in TestDB.enabledDialects())
Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver", user = "root", password = "")

transaction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ object Cities : Table() {
}

fun main() {
Assume.assumeTrue(TestDB.H2 in TestDB.enabledInTests())
Assume.assumeTrue(TestDB.H2 in TestDB.enabledDialects())
Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver", user = "root", password = "")

transaction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ConnectionPoolTests {

@Test
fun testSuspendTransactionsExceedingPoolSize() {
Assume.assumeTrue(TestDB.H2 in TestDB.enabledInTests())
Assume.assumeTrue(TestDB.H2 in TestDB.enabledDialects())
transaction(db = hikariDB1) {
SchemaUtils.create(TestTable)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class EntityReferenceCacheTest : DatabaseTestsBase() {
}

private fun executeOnH2(vararg tables: Table, body: () -> Unit) {
Assume.assumeTrue(TestDB.H2 in TestDB.enabledInTests())
Assume.assumeTrue(TestDB.H2 in TestDB.enabledDialects())
var testWasStarted = false
transaction(db) {
SchemaUtils.create(*tables)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class MultiDatabaseEntityTest {

@Before
fun before() {
Assume.assumeTrue(TestDB.H2 in TestDB.enabledInTests())
Assume.assumeTrue(TestDB.H2 in TestDB.enabledDialects())
if (TransactionManager.isInitialized()) {
currentDB = TransactionManager.currentOrNull()?.db
}
Expand All @@ -51,7 +51,7 @@ class MultiDatabaseEntityTest {

@After
fun after() {
if (TestDB.H2 in TestDB.enabledInTests()) {
if (TestDB.H2 in TestDB.enabledDialects()) {
TransactionManager.resetCurrent(currentDB?.transactionManager)
transaction(db1) {
SchemaUtils.drop(EntityTestsData.XTable, EntityTestsData.YTable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class MultiDatabaseTest {

@Before
fun before() {
Assume.assumeTrue(TestDB.H2 in TestDB.enabledInTests())
Assume.assumeTrue(TestDB.H2 in TestDB.enabledDialects())
if (TransactionManager.isInitialized()) {
currentDB = TransactionManager.currentOrNull()?.db
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.TestDB
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.vendors.OracleDialect
import org.jetbrains.exposed.sql.vendors.SQLServerDialect
import org.jetbrains.exposed.sql.vendors.currentDialect
import org.junit.Assume
Expand Down Expand Up @@ -119,7 +118,7 @@ class SchemaTests : DatabaseTestsBase() {

@Test
fun `test default schema`() {
Assume.assumeTrue(TestDB.H2 in TestDB.enabledInTests())
Assume.assumeTrue(TestDB.H2 in TestDB.enabledDialects())
val schema = Schema("schema")
TestDB.H2.connect()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class ConnectionExceptions {
`_transaction repetition works even if rollback throws exception`(::ExceptionOnRollbackConnection)
}
private fun `_transaction repetition works even if rollback throws exception`(connectionDecorator: (Connection) -> ConnectionSpy) {
Assume.assumeTrue(TestDB.H2 in TestDB.enabledInTests())
Assume.assumeTrue(TestDB.H2 in TestDB.enabledDialects())
Class.forName(TestDB.H2.driver).newInstance()

val wrappingDataSource = WrappingDataSource(TestDB.H2, connectionDecorator)
Expand Down Expand Up @@ -180,7 +180,7 @@ class ConnectionExceptions {
`_transaction repetition works when commit throws exception`(::ExceptionOnCommitConnection)
}
private fun `_transaction repetition works when commit throws exception`(connectionDecorator: (Connection) -> ConnectionSpy) {
Assume.assumeTrue(TestDB.H2 in TestDB.enabledInTests())
Assume.assumeTrue(TestDB.H2 in TestDB.enabledDialects())
Class.forName(TestDB.H2.driver).newInstance()

val wrappingDataSource = WrappingDataSource(TestDB.H2, connectionDecorator)
Expand All @@ -205,7 +205,7 @@ class ConnectionExceptions {
`_transaction throws exception if all commits throws exception`(::ExceptionOnCommitConnection)
}
private fun `_transaction throws exception if all commits throws exception`(connectionDecorator: (Connection) -> ConnectionSpy) {
Assume.assumeTrue(TestDB.H2 in TestDB.enabledInTests())
Assume.assumeTrue(TestDB.H2 in TestDB.enabledDialects())
Class.forName(TestDB.H2.driver).newInstance()

val wrappingDataSource = WrappingDataSource(TestDB.H2, connectionDecorator)
Expand Down Expand Up @@ -270,7 +270,7 @@ class ConnectionExceptions {
class ThreadLocalManagerTest : DatabaseTestsBase() {
@Test
fun testReconnection() {
Assume.assumeTrue(TestDB.MYSQL in TestDB.enabledInTests())
Assume.assumeTrue(TestDB.MYSQL in TestDB.enabledDialects())

var secondThreadTm: TransactionManager? = null
val db1 = TestDB.MYSQL.connect()
Expand Down Expand Up @@ -384,7 +384,7 @@ class TransactionIsolationTest : DatabaseTestsBase() {
class TransactionManagerResetTest {
@Test
fun `test closeAndUnregister with next Database-connect works fine`() {
Assume.assumeTrue(TestDB.H2 in TestDB.enabledInTests())
Assume.assumeTrue(TestDB.H2 in TestDB.enabledDialects())
val initialManager = TransactionManager.manager
val db1 = TestDB.H2.connect()
val db1TransactionManager = TransactionManager.managerFor(db1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ class InsertTests : DatabaseTestsBase() {
val testTable = object : IntIdTable("TestRollback") {
val foo = integer("foo").check { it greater 0 }
}
val dbToTest = TestDB.enabledInTests() - setOfNotNull(
val dbToTest = TestDB.enabledDialects() - setOfNotNull(
TestDB.SQLITE,
TestDB.MYSQL.takeIf { System.getProperty("exposed.test.mysql8.port") == null }
)
Expand Down Expand Up @@ -499,7 +499,7 @@ class InsertTests : DatabaseTestsBase() {
val testTable = object : IntIdTable("TestRollback") {
val foo = integer("foo").check { it greater 0 }
}
val dbToTest = TestDB.enabledInTests() - setOfNotNull(
val dbToTest = TestDB.enabledDialects() - setOfNotNull(
TestDB.SQLITE,
TestDB.MYSQL.takeIf { System.getProperty("exposed.test.mysql8.port") == null }
)
Expand Down
Loading

0 comments on commit 8262554

Please sign in to comment.