1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.transaction.managed;
17
18 import static org.junit.jupiter.api.Assertions.assertEquals;
19 import static org.junit.jupiter.api.Assertions.assertFalse;
20 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
21 import static org.junit.jupiter.api.Assertions.assertNotNull;
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 ManagedTransactionFactoryUnitTest 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 ManagedTransactionFactory();
60 }
61
62 @Test
63 @Override
64 public void shouldSetProperties() throws Exception {
65 when(properties.getProperty("closeConnection")).thenReturn("false");
66
67 transactionFactory.setProperties(properties);
68
69 assertFalse(
70 (Boolean) getValue(transactionFactory.getClass().getDeclaredField("closeConnection"), 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(ManagedTransaction.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 when(properties.getProperty("closeConnection")).thenReturn("false");
88
89 transactionFactory.setProperties(properties);
90 Transaction result = transactionFactory.newTransaction(dataSource, TransactionIsolationLevel.READ_COMMITTED, true);
91
92 assertNotNull(result);
93 assertInstanceOf(ManagedTransaction.class, result);
94 assertEquals(connection, result.getConnection());
95 verify(connection).setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
96
97 assertEquals(dataSource, getValue(result.getClass().getDeclaredField("dataSource"), result));
98 assertEquals(TransactionIsolationLevel.READ_COMMITTED,
99 getValue(result.getClass().getDeclaredField("level"), result));
100 assertEquals(false, getValue(result.getClass().getDeclaredField("closeConnection"), result));
101 }
102 }