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.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 org.apache.ibatis.transaction.Transaction;
28 import org.junit.jupiter.api.BeforeEach;
29 import org.junit.jupiter.api.Test;
30 import org.mockito.Mock;
31
32
33
34
35
36
37 class JdbcTransactionWithConnectionTest extends JdbcTransactionBase {
38
39 @Mock
40 private Connection connection;
41
42 private Transaction transaction;
43
44 @BeforeEach
45 void setup() {
46 this.transaction = new JdbcTransaction(connection);
47 }
48
49 @Test
50 @Override
51 void shouldGetConnection() throws SQLException {
52 Connection result = transaction.getConnection();
53
54 assertEquals(connection, result);
55 }
56
57 @Test
58 @Override
59 void shouldCommitWhenConnectionIsNotAutoCommit() throws SQLException {
60 when(connection.getAutoCommit()).thenReturn(false);
61
62 transaction.commit();
63
64 verify(connection).commit();
65 verify(connection).getAutoCommit();
66 }
67
68 @Test
69 @Override
70 void shouldAutoCommitWhenConnectionIsAutoCommit() throws SQLException {
71 when(connection.getAutoCommit()).thenReturn(true);
72
73 transaction.commit();
74
75 verify(connection, never()).commit();
76 verify(connection).getAutoCommit();
77 }
78
79 @Test
80 @Override
81 void shouldRollbackWhenConnectionIsNotAutoCommit() throws SQLException {
82 when(connection.getAutoCommit()).thenReturn(false);
83
84 transaction.rollback();
85
86 verify(connection).rollback();
87 verify(connection).getAutoCommit();
88 }
89
90 @Test
91 @Override
92 void shouldAutoRollbackWhenConnectionIsAutoCommit() throws SQLException {
93 when(connection.getAutoCommit()).thenReturn(true);
94
95 transaction.rollback();
96
97 verify(connection, never()).rollback();
98 verify(connection).getAutoCommit();
99 }
100
101 @Test
102 @Override
103 void shouldCloseAndSetAutoCommitWhenConnectionIsNotAutoCommit() throws SQLException {
104 when(connection.getAutoCommit()).thenReturn(false);
105
106 transaction.close();
107
108 verify(connection).close();
109 verify(connection).setAutoCommit(true);
110 verify(connection).getAutoCommit();
111 }
112
113 @Test
114 @Override
115 void shouldCloseAndNotSetAutoCommitWhenConnectionIsAutoCommit() throws SQLException {
116 when(connection.getAutoCommit()).thenReturn(true);
117
118 transaction.close();
119
120 verify(connection).close();
121 verify(connection, never()).setAutoCommit(true);
122 verify(connection).getAutoCommit();
123 }
124
125 @Test
126 @Override
127 void shouldReturnNullWhenGetTimeout() throws SQLException {
128 assertNull(transaction.getTimeout());
129 }
130
131 }