CORDA-4034 Rebuild AuthDBTests so H2 shuts down correctly (#6718)

* CORDA-4034 Rebuild AuthDBTests so H2 shuts down correctly
This commit is contained in:
Ross Nicoll 2020-09-16 16:44:31 +01:00 committed by GitHub
parent 6ab9557061
commit cc14c8e53a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -22,6 +22,7 @@ import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import java.sql.Connection
import java.sql.Statement
import java.util.*
import javax.sql.DataSource
@ -33,7 +34,7 @@ import kotlin.test.assertFailsWith
*/
@RunWith(Parameterized::class)
class AuthDBTests : NodeBasedTest() {
private lateinit var node: NodeWithInfo
private var node: NodeWithInfo? = null
private lateinit var client: CordaRPCClient
private lateinit var db: UsersDB
@ -93,8 +94,9 @@ class AuthDBTests : NodeBasedTest() {
)
)
node = startNode(ALICE_NAME, rpcUsers = emptyList(), configOverrides = securityConfig)
client = CordaRPCClient(node.node.configuration.rpcOptions.address)
node = startNode(ALICE_NAME, rpcUsers = emptyList(), configOverrides = securityConfig).also { node ->
client = CordaRPCClient(node.node.configuration.rpcOptions.address)
}
}
@Test
@ -215,7 +217,7 @@ class AuthDBTests : NodeBasedTest() {
@After
fun tearDown() {
node.node.stop()
node?.node?.stop()
db.close()
}
@ -229,7 +231,7 @@ private data class RoleAndPermissions(val role: String, val permissions: List<St
* Manage in-memory DB mocking a users database with the schema expected by Node's security manager
*/
private class UsersDB(name: String, users: List<UserAndRoles> = emptyList(), roleAndPermissions: List<RoleAndPermissions> = emptyList()) : AutoCloseable {
val jdbcUrl = "jdbc:h2:mem:$name;DB_CLOSE_DELAY=-1"
val jdbcUrl = "jdbc:h2:mem:$name"
companion object {
const val DB_CREATE_SCHEMA = """
@ -270,36 +272,34 @@ private class UsersDB(name: String, users: List<UserAndRoles> = emptyList(), rol
}
}
private val dataSource: DataSource
private val connection: Connection
private inline fun session(statement: (Statement) -> Unit) {
dataSource.connection.use {
it.autoCommit = false
it.createStatement().use(statement)
it.commit()
}
connection.createStatement().use(statement)
connection.commit()
}
init {
dataSource = DataSourceFactory.createDataSource(Properties().apply {
require(users.map { it.username }.toSet().size == users.size) {
"Duplicate username in input"
}
connection = DataSourceFactory.createDataSource(Properties().apply {
put("dataSourceClassName", "org.h2.jdbcx.JdbcDataSource")
put("dataSource.url", jdbcUrl)
}, false)
.connection
.apply {
autoCommit = false
}
session {
it.execute(DB_CREATE_SCHEMA)
}
require(users.map { it.username }.toSet().size == users.size) {
"Duplicate username in input"
}
users.forEach { insert(it) }
roleAndPermissions.forEach { insert(it) }
}
override fun close() {
dataSource.connection.use {
it.createStatement().use {
it.execute("DROP ALL OBJECTS")
}
}
// Close the connection, at which point the database will shut down
connection.close()
}
}