|
| 1 | +/* |
| 2 | + * Copyright 2009-2024 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 org.apache.ibatis.transaction.jdbc; |
| 17 | + |
| 18 | +import org.apache.ibatis.session.TransactionIsolationLevel; |
| 19 | +import org.apache.ibatis.transaction.Transaction; |
| 20 | +import org.apache.ibatis.transaction.TransactionFactory; |
| 21 | +import org.apache.ibatis.transaction.TransactionFactoryTest; |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.mockito.Mock; |
| 25 | + |
| 26 | +import javax.sql.DataSource; |
| 27 | +import java.sql.Connection; |
| 28 | +import java.sql.SQLException; |
| 29 | +import java.util.Properties; |
| 30 | + |
| 31 | +import static org.junit.jupiter.api.Assertions.*; |
| 32 | +import static org.mockito.Mockito.verify; |
| 33 | +import static org.mockito.Mockito.when; |
| 34 | + |
| 35 | +/** |
| 36 | + * @author <a href="[email protected]">mawen12</a> |
| 37 | + * @see JdbcTransactionFactory |
| 38 | + */ |
| 39 | +class JdbcTransactionFactoryUnitTest extends TransactionFactoryTest { |
| 40 | + |
| 41 | + @Mock |
| 42 | + private Properties properties; |
| 43 | + |
| 44 | + @Mock |
| 45 | + private Connection connection; |
| 46 | + |
| 47 | + @Mock |
| 48 | + private DataSource dataSource; |
| 49 | + |
| 50 | + private TransactionFactory transactionFactory; |
| 51 | + |
| 52 | + @BeforeEach |
| 53 | + void setup() { |
| 54 | + this.transactionFactory = new JdbcTransactionFactory(); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + @Override |
| 59 | + public void shouldSetProperties() throws Exception { |
| 60 | + when(properties.getProperty("skipSetAutoCommitOnClose")).thenReturn("true"); |
| 61 | + |
| 62 | + transactionFactory.setProperties(properties); |
| 63 | + |
| 64 | + assertTrue((Boolean) getValue(transactionFactory.getClass().getDeclaredField("skipSetAutoCommitOnClose"), transactionFactory)); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + @Override |
| 69 | + public void shouldNewTransactionWithConnection() throws SQLException { |
| 70 | + Transaction result = transactionFactory.newTransaction(connection); |
| 71 | + |
| 72 | + assertNotNull(result); |
| 73 | + assertInstanceOf(JdbcTransaction.class, result); |
| 74 | + assertEquals(connection, result.getConnection()); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + @Override |
| 79 | + public void shouldNewTransactionWithDataSource() throws Exception { |
| 80 | + when(dataSource.getConnection()).thenReturn(connection); |
| 81 | + |
| 82 | + Transaction result = transactionFactory.newTransaction(dataSource, TransactionIsolationLevel.READ_COMMITTED, false); |
| 83 | + |
| 84 | + assertNotNull(result); |
| 85 | + assertInstanceOf(JdbcTransaction.class, result); |
| 86 | + assertEquals(connection, result.getConnection()); |
| 87 | + verify(connection).setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); |
| 88 | + |
| 89 | + assertEquals(dataSource, getValue(result.getClass().getDeclaredField("dataSource"), result)); |
| 90 | + assertEquals(TransactionIsolationLevel.READ_COMMITTED, getValue(result.getClass().getDeclaredField("level"), result)); |
| 91 | + assertEquals(false, getValue(result.getClass().getDeclaredField("autoCommit"), result)); |
| 92 | + assertEquals(false, getValue(result.getClass().getDeclaredField("skipSetAutoCommitOnClose"), result)); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void shouldNewTransactionWithDataSourceAndCustomProperties() throws Exception { |
| 97 | + when(dataSource.getConnection()).thenReturn(connection); |
| 98 | + when(properties.getProperty("skipSetAutoCommitOnClose")).thenReturn("true"); |
| 99 | + |
| 100 | + transactionFactory.setProperties(properties); |
| 101 | + Transaction result = transactionFactory.newTransaction(dataSource, TransactionIsolationLevel.READ_COMMITTED, true); |
| 102 | + |
| 103 | + assertNotNull(result); |
| 104 | + assertInstanceOf(JdbcTransaction.class, result); |
| 105 | + assertEquals(connection, result.getConnection()); |
| 106 | + verify(connection).setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); |
| 107 | + |
| 108 | + assertEquals(dataSource, getValue(result.getClass().getDeclaredField("dataSource"), result)); |
| 109 | + assertEquals(TransactionIsolationLevel.READ_COMMITTED, getValue(result.getClass().getDeclaredField("level"), result)); |
| 110 | + assertEquals(true, getValue(result.getClass().getDeclaredField("autoCommit"), result)); |
| 111 | + assertEquals(true, getValue(result.getClass().getDeclaredField("skipSetAutoCommitOnClose"), result)); |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments