1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.transaction.jdbc;
17
18 import static org.junit.jupiter.api.Assertions.assertEquals;
19 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
20 import static org.junit.jupiter.api.Assertions.assertNotNull;
21 import static org.junit.jupiter.api.Assertions.assertTrue;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24
25 import java.sql.Connection;
26 import java.sql.SQLException;
27 import java.util.Properties;
28
29 import javax.sql.DataSource;
30
31 import org.apache.ibatis.session.TransactionIsolationLevel;
32 import org.apache.ibatis.transaction.Transaction;
33 import org.apache.ibatis.transaction.TransactionFactory;
34 import org.apache.ibatis.transaction.TransactionFactoryBase;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37 import org.mockito.Mock;
38
39
40
41
42
43
44 class JdbcTransactionFactoryUnitTest extends TransactionFactoryBase {
45
46 @Mock
47 private Properties properties;
48
49 @Mock
50 private Connection connection;
51
52 @Mock
53 private DataSource dataSource;
54
55 private TransactionFactory transactionFactory;
56
57 @BeforeEach
58 void setup() {
59 this.transactionFactory = new JdbcTransactionFactory();
60 }
61
62 @Test
63 @Override
64 public void shouldSetProperties() throws Exception {
65 when(properties.getProperty("skipSetAutoCommitOnClose")).thenReturn("true");
66
67 transactionFactory.setProperties(properties);
68
69 assertTrue((Boolean) getValue(transactionFactory.getClass().getDeclaredField("skipSetAutoCommitOnClose"),
70 transactionFactory));
71 }
72
73 @Test
74 @Override
75 public void shouldNewTransactionWithConnection() throws SQLException {
76 Transaction result = transactionFactory.newTransaction(connection);
77
78 assertNotNull(result);
79 assertInstanceOf(JdbcTransaction.class, result);
80 assertEquals(connection, result.getConnection());
81 }
82
83 @Test
84 @Override
85 public void shouldNewTransactionWithDataSource() throws Exception {
86 when(dataSource.getConnection()).thenReturn(connection);
87
88 Transaction result = transactionFactory.newTransaction(dataSource, TransactionIsolationLevel.READ_COMMITTED, false);
89
90 assertNotNull(result);
91 assertInstanceOf(JdbcTransaction.class, result);
92 assertEquals(connection, result.getConnection());
93 verify(connection).setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
94
95 assertEquals(dataSource, getValue(result.getClass().getDeclaredField("dataSource"), result));
96 assertEquals(TransactionIsolationLevel.READ_COMMITTED,
97 getValue(result.getClass().getDeclaredField("level"), result));
98 assertEquals(false, getValue(result.getClass().getDeclaredField("autoCommit"), result));
99 assertEquals(false, getValue(result.getClass().getDeclaredField("skipSetAutoCommitOnClose"), result));
100 }
101
102 @Test
103 void shouldNewTransactionWithDataSourceAndCustomProperties() throws Exception {
104 when(dataSource.getConnection()).thenReturn(connection);
105 when(properties.getProperty("skipSetAutoCommitOnClose")).thenReturn("true");
106
107 transactionFactory.setProperties(properties);
108 Transaction result = transactionFactory.newTransaction(dataSource, TransactionIsolationLevel.READ_COMMITTED, true);
109
110 assertNotNull(result);
111 assertInstanceOf(JdbcTransaction.class, result);
112 assertEquals(connection, result.getConnection());
113 verify(connection).setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
114
115 assertEquals(dataSource, getValue(result.getClass().getDeclaredField("dataSource"), result));
116 assertEquals(TransactionIsolationLevel.READ_COMMITTED,
117 getValue(result.getClass().getDeclaredField("level"), result));
118 assertEquals(true, getValue(result.getClass().getDeclaredField("autoCommit"), result));
119 assertEquals(true, getValue(result.getClass().getDeclaredField("skipSetAutoCommitOnClose"), result));
120 }
121
122 }