|
| 1 | +/* |
| 2 | + * Copyright 2016-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package examples.kotlin.mybatis3 |
| 17 | + |
| 18 | +import org.apache.ibatis.datasource.unpooled.UnpooledDataSource |
| 19 | +import org.apache.ibatis.jdbc.ScriptRunner |
| 20 | +import org.apache.ibatis.mapping.Environment |
| 21 | +import org.apache.ibatis.session.Configuration |
| 22 | +import org.apache.ibatis.session.SqlSessionFactory |
| 23 | +import org.apache.ibatis.session.SqlSessionFactoryBuilder |
| 24 | +import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory |
| 25 | +import java.io.InputStreamReader |
| 26 | +import javax.sql.DataSource |
| 27 | +import kotlin.reflect.KClass |
| 28 | + |
| 29 | +class TestUtils { |
| 30 | + companion object { |
| 31 | + const val JDBC_URL = "jdbc:hsqldb:mem:aname" |
| 32 | + const val JDBC_DRIVER = "org.hsqldb.jdbcDriver" |
| 33 | + |
| 34 | + fun buildSqlSessionFactory(configurator: FactoryConfig.() -> Unit): SqlSessionFactory { |
| 35 | + val factoryConfig = FactoryConfig().apply(configurator) |
| 36 | + |
| 37 | + val dataSource = factoryConfig.dataSource?:defaultDataSource() |
| 38 | + |
| 39 | + factoryConfig.initializationScript?.let { |
| 40 | + val script = TestUtils::class.java.getResourceAsStream(it) |
| 41 | + dataSource.getConnection("sa", "").use { connection -> |
| 42 | + val sr = ScriptRunner(connection) |
| 43 | + sr.setLogWriter(null) |
| 44 | + sr.runScript(InputStreamReader(script!!)) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + val environment = Environment("test", JdbcTransactionFactory(), dataSource) |
| 49 | + with(Configuration(environment)) { |
| 50 | + factoryConfig.typeHandlers.map(KClass<*>::java).forEach(typeHandlerRegistry::register) |
| 51 | + factoryConfig.mappers.map(KClass<*>::java).forEach { addMapper(it) } |
| 52 | + return SqlSessionFactoryBuilder().build(this) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private fun defaultDataSource() = UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", "") |
| 57 | + } |
| 58 | + |
| 59 | + class FactoryConfig { |
| 60 | + internal var initializationScript: String? = null |
| 61 | + internal val mappers = mutableListOf<KClass<*>>() |
| 62 | + internal val typeHandlers = mutableListOf<KClass<*>>() |
| 63 | + internal var dataSource: DataSource? = null |
| 64 | + |
| 65 | + fun withInitializationScript(initializationScript: String) { |
| 66 | + this.initializationScript = initializationScript |
| 67 | + } |
| 68 | + |
| 69 | + fun withMapper(mapper: KClass<*>) { |
| 70 | + this.mappers.add(mapper) |
| 71 | + } |
| 72 | + |
| 73 | + fun withTypeHandler(typeHandler: KClass<*>) { |
| 74 | + this.typeHandlers.add(typeHandler) |
| 75 | + } |
| 76 | + |
| 77 | + fun withDataSource(dataSource: DataSource) { |
| 78 | + this.dataSource = dataSource |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments