Skip to content

Commit

Permalink
Ignore casting string on driver (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
koxudaxi authored Mar 18, 2021
1 parent e93498d commit d244bbc
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.koxudaxi.localDataApi
import java.sql.Connection
import java.sql.DriverManager
import java.util.*
import java.util.Properties


class ConnectionManager {
Expand All @@ -12,9 +13,22 @@ class ConnectionManager {
IntRange(1, transactionIdLength).map { transactionIdCharacters.random() }.joinToString("")

fun createConnection(
url: String, user: String, password: String?, database: String?, schema: String?,
url: String,
user: String,
password: String?,
database: String?,
schema: String?,
jdbcOptions: Map<String, String>,
): Connection {
return DriverManager.getConnection(url + (database ?: ""), user, password)
val props = Properties()
props.setProperty("user", user)
if (password is String) {
props.setProperty("password", password)
}
jdbcOptions.entries.forEach {
props.setProperty(it.key, it.value)
}
return DriverManager.getConnection(url + (database ?: ""), props)
.apply {
this.autoCommit = false
if (schema is String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ fun setup() {
resourceArn,
env["MYSQL_HOST"] ?: "127.0.0.1",
env["MYSQL_PORT"]?.toInt() ?: 3306,
emptyMap()
)
SecretManager.INSTANCE.setSecret(
env["MYSQL_USER"] ?: "root",
Expand All @@ -117,6 +118,7 @@ fun setup() {
resourceArn,
env["POSTGRES_HOST"] ?: "127.0.0.1",
env["POSTGRES_PORT"]?.toInt() ?: 5432,
mapOf("stringtype" to "unspecified")
)
SecretManager.INSTANCE.setSecret(
env["POSTGRES_USER"] ?: "postgres",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Resource(
private val connectionManager = ConnectionManager.INSTANCE

val connection = if (transactionId == null) {
connectionManager.createConnection(config.url, userName, password, database, schema)
connectionManager.createConnection(config.url, userName, password, database, schema, config.jdbcOptions)
} else {
connectionManager.getConnection(transactionId)
}
Expand Down Expand Up @@ -42,6 +42,7 @@ class Resource(
val resourceArn: String,
val host: String?,
val port: Int?,
val jdbcOptions: Map<String, String>
) {
val url get() = if (host is String) "jdbc:${jdbcName}://${host}:${port}/" else "jdbc:${jdbcName}"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ class ResourceManager {
resourceArn: String,
host: String?,
port: Int?,
jdbcOptions: Map<String,String>
) {
resourceConfigs[resourceArn] = Resource.Config(jdbcName, resourceArn, host, port)
resourceConfigs[resourceArn] = Resource.Config(jdbcName, resourceArn, host, port, jdbcOptions)
}

fun getResource(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class ApplicationTest {
@Before
fun setUp() {
val jdbcName = "h2:./test;MODE=MySQL"
ResourceManager.INSTANCE.setResource(jdbcName, dummyResourceArn, null, null)
ResourceManager.INSTANCE.setResource(jdbcName, dummyResourceArn, null, null, emptyMap())
SecretManager.INSTANCE.setSecret(username, password, dummySecretArn)
ConnectionManager.INSTANCE.createConnection("jdbc:${jdbcName}", username, password, null, null)
ConnectionManager.INSTANCE.createConnection("jdbc:${jdbcName}", username, password, null, null, emptyMap())
.createStatement()
.execute("DROP ALL OBJECTS")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class ConnectionManagerTest {
@Before
fun setUp() {
val jdbcName = "h2:./test;MODE=MySQL"
ResourceManager.INSTANCE.setResource(jdbcName, dummyResourceArn, null, null)
ResourceManager.INSTANCE.setResource(jdbcName, dummyResourceArn, null, null, emptyMap())
SecretManager.INSTANCE.setSecret(username, password, dummySecretArn)
ConnectionManager.INSTANCE.createConnection("jdbc:${jdbcName}", username, password, null, null)
ConnectionManager.INSTANCE.createConnection("jdbc:${jdbcName}", username, password, null, null, emptyMap())
.createStatement()
.execute("DROP ALL OBJECTS")
}
Expand All @@ -33,7 +33,7 @@ class ConnectionManagerTest {
fun testCreateConnection() {
val jdbcName = "h2:./test;MODE=MySQL;INIT=CREATE SCHEMA IF NOT EXISTS TEST"
val connectionManager =
ConnectionManager.INSTANCE.createConnection("jdbc:${jdbcName}", username, password, null, "TEST")
ConnectionManager.INSTANCE.createConnection("jdbc:${jdbcName}", username, password, null, "TEST", mapOf("timeout" to "10"))
assertEquals("TEST", connectionManager.schema)
assertEquals(false, connectionManager.autoCommit)
}
Expand All @@ -60,17 +60,17 @@ class ConnectionManagerTest {
val connectionManager = spyk<ConnectionManager>()
val connection = mockk<Connection>(relaxed = true)
mockkStatic(DriverManager::class)
every { DriverManager.getConnection("jdbc:mysql://127.0.0.1/", "username", "password") } returns connection
connectionManager.createConnection("jdbc:mysql://127.0.0.1/", "username", "password", null, null)
every { DriverManager.getConnection("jdbc:mysql://127.0.0.1/", any()) } returns connection
connectionManager.createConnection("jdbc:mysql://127.0.0.1/", "username", null, null, null, emptyMap())
}

@Test
fun testGetConnectionWithDatabase() {
val connectionManager = spyk<ConnectionManager>()
val connection = mockk<Connection>(relaxed = true)
mockkStatic(DriverManager::class)
every { DriverManager.getConnection("jdbc:mysql://127.0.0.1/test", "username", "password") } returns connection
connectionManager.createConnection("jdbc:mysql://127.0.0.1/", "username", "password", "test", "testSchema")
every { DriverManager.getConnection("jdbc:mysql://127.0.0.1/test", any()) } returns connection
connectionManager.createConnection("jdbc:mysql://127.0.0.1/", "username", "password", "test", "testSchema", emptyMap())
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class LocalDataApiTest {
val resource = mockk<Resource>(relaxed = true)
mockkStatic(Resource::class)
every {
Resource(Resource.Config("mysql", "abc", "localhost", 1234), "user", "pass", null, null, "xyz")
Resource(Resource.Config("mysql", "abc", "localhost", 1234, emptyMap()), "user", "pass", null, null, "xyz")
} returns resource

val env = mapOf(
Expand Down Expand Up @@ -78,7 +78,7 @@ class LocalDataApiTest {
val resource = mockk<Resource>(relaxed = true)
mockkStatic(Resource::class)
every {
Resource(Resource.Config("mysql", "arn:aws:rds:us-east-1:123456789012:cluster:dummy", "127.0.0.1", 3306),
Resource(Resource.Config("mysql", "arn:aws:rds:us-east-1:123456789012:cluster:dummy", "127.0.0.1", 3306, emptyMap()),
"root",
"example",
null,
Expand Down Expand Up @@ -107,7 +107,7 @@ class LocalDataApiTest {
val resource = mockk<Resource>(relaxed = true)
mockkStatic(Resource::class)
every {
Resource(Resource.Config("postgresql", "abc", "localhost", 1234), "user", "pass", null, null, "xyz")
Resource(Resource.Config("postgresql", "abc", "localhost", 1234, mapOf("stringtype" to "unspecified")), "user", "pass", null, null, "xyz")
} returns resource

val env = mapOf(
Expand Down Expand Up @@ -140,7 +140,7 @@ class LocalDataApiTest {
Resource(Resource.Config("postgresql",
"arn:aws:rds:us-east-1:123456789012:cluster:dummy",
"127.0.0.1",
5432), "postgres", "example", null, null, "xyz")
5432, mapOf("stringtype" to "unspecified")), "postgres", "example", null, null, "xyz")
} returns resource

val env = mapOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ResourceManagerTest {
@Before
fun setUp() {
val jdbcName = "h2:./test;MODE=MySQL"
ResourceManager.INSTANCE.setResource(jdbcName, dummyResourceArn, null, null)
ResourceManager.INSTANCE.setResource(jdbcName, dummyResourceArn, null, null, emptyMap())
SecretManager.INSTANCE.setSecret(username, password, dummySecretArn)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package com.koxudaxi.localDataApi

import kotlin.test.*
import io.mockk.*
import org.junit.After
import org.junit.Before

class ResourceTest {
@Test
fun testResourceConfig() {
val config = Resource.Config("mysql", "abc", "localhost", 3306)
val config = Resource.Config("mysql", "abc", "localhost", 3306, emptyMap())
assertEquals("jdbc:mysql://localhost:3306/", config.url)
assertEquals(config.jdbcName, "mysql")
assertEquals(config.resourceArn, "abc")
assertEquals(config.host, "localhost")
assertEquals(config.port, 3306)
assertEquals(config.jdbcOptions, emptyMap())
}
}

0 comments on commit d244bbc

Please sign in to comment.