|
| 1 | +/* |
| 2 | + * Copyright 2016-2021 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 | + * http://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.canonical |
| 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.ExecutorType |
| 23 | +import org.apache.ibatis.session.SqlSession |
| 24 | +import org.apache.ibatis.session.SqlSessionFactoryBuilder |
| 25 | +import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory |
| 26 | +import org.assertj.core.api.Assertions.assertThat |
| 27 | +import org.junit.jupiter.api.Test |
| 28 | +import java.io.InputStreamReader |
| 29 | +import java.sql.DriverManager |
| 30 | + |
| 31 | +class GeneratedAlwaysTest { |
| 32 | + private fun newSession(executorType: ExecutorType = ExecutorType.REUSE): SqlSession { |
| 33 | + Class.forName(JDBC_DRIVER) |
| 34 | + val script = javaClass.getResourceAsStream("/examples/kotlin/spring/CreateGeneratedAlwaysDB.sql") |
| 35 | + DriverManager.getConnection(JDBC_URL, "sa", "").use { connection -> |
| 36 | + val sr = ScriptRunner(connection) |
| 37 | + sr.setLogWriter(null) |
| 38 | + sr.runScript(InputStreamReader(script)) |
| 39 | + } |
| 40 | + |
| 41 | + val ds = UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", "") |
| 42 | + val environment = Environment("test", JdbcTransactionFactory(), ds) |
| 43 | + val config = Configuration(environment) |
| 44 | + config.addMapper(GeneratedAlwaysMapper::class.java) |
| 45 | + return SqlSessionFactoryBuilder().build(config).openSession(executorType) |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + fun testInsertSingleRecord() { |
| 50 | + newSession().use { session -> |
| 51 | + val mapper = session.getMapper(GeneratedAlwaysMapper::class.java) |
| 52 | + |
| 53 | + val record = GeneratedAlwaysRecord( |
| 54 | + firstName = "Fred", |
| 55 | + lastName = "Flintstone" |
| 56 | + ) |
| 57 | + |
| 58 | + val rows = mapper.insert(record) |
| 59 | + |
| 60 | + assertThat(rows).isEqualTo(1) |
| 61 | + assertThat(record.id).isEqualTo(22) |
| 62 | + assertThat(record.fullName).isEqualTo("Fred Flintstone") |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + fun testInsertMultiple() { |
| 68 | + newSession().use { session -> |
| 69 | + val mapper = session.getMapper(GeneratedAlwaysMapper::class.java) |
| 70 | + |
| 71 | + val record1 = GeneratedAlwaysRecord( |
| 72 | + firstName = "Fred", |
| 73 | + lastName = "Flintstone" |
| 74 | + ) |
| 75 | + |
| 76 | + val record2 = GeneratedAlwaysRecord( |
| 77 | + firstName = "Barney", |
| 78 | + lastName = "Rubble" |
| 79 | + ) |
| 80 | + |
| 81 | + val rows = mapper.insertMultiple(listOf(record1, record2)) |
| 82 | + |
| 83 | + assertThat(rows).isEqualTo(2) |
| 84 | + assertThat(record1.id).isEqualTo(22) |
| 85 | + assertThat(record1.fullName).isEqualTo("Fred Flintstone") |
| 86 | + assertThat(record2.id).isEqualTo(23) |
| 87 | + assertThat(record2.fullName).isEqualTo("Barney Rubble") |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + companion object { |
| 92 | + const val JDBC_URL = "jdbc:hsqldb:mem:aname" |
| 93 | + const val JDBC_DRIVER = "org.hsqldb.jdbcDriver" |
| 94 | + } |
| 95 | +} |
0 commit comments