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.assertNull;
20 import static org.mockito.Mockito.never;
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.when;
23
24 import java.sql.Connection;
25 import java.sql.SQLException;
26
27 import javax.sql.DataSource;
28
29 import org.apache.ibatis.session.TransactionIsolationLevel;
30 import org.apache.ibatis.transaction.Transaction;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33 import org.mockito.Mock;
34
35
36
37
38
39
40 class ManagedTransactionWithDataSourceTest extends ManagedTransactionBase {
41
42 @Mock
43 private DataSource dataSource;
44
45 @Mock
46 private Connection connection;
47
48 private Transaction transaction;
49
50 @BeforeEach
51 void setup() {
52 this.transaction = new ManagedTransaction(dataSource, TransactionIsolationLevel.READ_COMMITTED, true);
53 }
54
55 @Override
56 @Test
57 void shouldGetConnection() throws SQLException {
58 when(dataSource.getConnection()).thenReturn(connection);
59
60 Connection result = transaction.getConnection();
61
62 assertEquals(connection, result);
63 verify(connection).setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
64 }
65
66 @Test
67 @Override
68 void shouldNotCommitWhetherConnectionIsAutoCommit() throws SQLException {
69 when(dataSource.getConnection()).thenReturn(connection);
70
71 transaction.getConnection();
72 transaction.commit();
73
74 verify(connection, never()).commit();
75 verify(connection, never()).getAutoCommit();
76 }
77
78 @Test
79 @Override
80 void shouldNotRollbackWhetherConnectionIsAutoCommit() throws SQLException {
81 when(dataSource.getConnection()).thenReturn(connection);
82
83 transaction.getConnection();
84 transaction.rollback();
85
86 verify(connection, never()).rollback();
87 verify(connection, never()).getAutoCommit();
88 }
89
90 @Test
91 @Override
92 void shouldCloseWhenSetCloseConnectionIsTrue() throws SQLException {
93 when(dataSource.getConnection()).thenReturn(connection);
94
95 transaction.getConnection();
96 transaction.close();
97
98 verify(connection).close();
99 }
100
101 @Test
102 @Override
103 void shouldNotCloseWhenSetCloseConnectionIsFalse() throws SQLException {
104 transaction = new ManagedTransaction(dataSource, TransactionIsolationLevel.READ_COMMITTED, false);
105 when(dataSource.getConnection()).thenReturn(connection);
106
107 transaction.getConnection();
108 transaction.close();
109
110 verify(connection, never()).close();
111 }
112
113 @Test
114 @Override
115 void shouldReturnNullWhenGetTimeout() throws SQLException {
116 assertNull(transaction.getTimeout());
117 }
118 }