Skip to content

Commit 067b46a

Browse files
committed
refactor JdbcTransaction and ManagedTransaction test case
1 parent 201d6a5 commit 067b46a

File tree

6 files changed

+108
-66
lines changed

6 files changed

+108
-66
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.junit.jupiter.api.extension.ExtendWith;
19+
import org.mockito.junit.jupiter.MockitoExtension;
20+
21+
import java.sql.SQLException;
22+
23+
/**
24+
* @author <a href="[email protected]">mawen12</a>
25+
* @see JdbcTransaction
26+
*/
27+
@ExtendWith(MockitoExtension.class)
28+
abstract class JdbcTransactionBase {
29+
30+
abstract void shouldGetConnection() throws SQLException;
31+
32+
abstract void shouldCommitWhenConnectionIsNotAutoCommit() throws SQLException;
33+
34+
abstract void shouldAutoCommitWhenConnectionIsAutoCommit() throws SQLException;
35+
36+
abstract void shouldRollbackWhenConnectionIsNotAutoCommit() throws SQLException;
37+
38+
abstract void shouldAutoRollbackWhenConnectionIsAutoCommit() throws SQLException;
39+
40+
abstract void shouldCloseAndSetAutoCommitWhenConnectionIsNotAutoCommit() throws SQLException;
41+
42+
abstract void shouldCloseAndNotSetAutoCommitWhenConnectionIsAutoCommit() throws SQLException;
43+
44+
abstract void shouldReturnNullWhenGetTimeout() throws SQLException;
45+
}

src/test/java/org/apache/ibatis/transaction/jdbc/JdbcTransactionWithConnectionTest.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package org.apache.ibatis.transaction.jdbc;
1717

1818
import org.apache.ibatis.transaction.Transaction;
19-
import org.apache.ibatis.transaction.TransactionBase;
2019
import org.junit.jupiter.api.BeforeEach;
2120
import org.junit.jupiter.api.Test;
2221
import org.mockito.Mock;
@@ -32,7 +31,7 @@
3231
* @author <a href="[email protected]">mawen12</a>
3332
* @see JdbcTransaction
3433
*/
35-
class JdbcTransactionWithConnectionTest extends TransactionBase {
34+
class JdbcTransactionWithConnectionTest extends JdbcTransactionBase {
3635

3736
@Mock
3837
private Connection connection;
@@ -46,74 +45,83 @@ void setup() {
4645

4746
@Test
4847
@Override
49-
public void shouldGetConnection() throws SQLException {
48+
void shouldGetConnection() throws SQLException {
5049
Connection result = transaction.getConnection();
5150

5251
assertEquals(connection, result);
5352
}
5453

5554
@Test
5655
@Override
57-
public void shouldCommit() throws SQLException {
56+
void shouldCommitWhenConnectionIsNotAutoCommit() throws SQLException {
5857
when(connection.getAutoCommit()).thenReturn(false);
5958

6059
transaction.commit();
6160

6261
verify(connection).commit();
62+
verify(connection).getAutoCommit();
6363
}
6464

6565
@Test
66-
void shouldAutoCommit() throws SQLException {
66+
@Override
67+
void shouldAutoCommitWhenConnectionIsAutoCommit() throws SQLException {
6768
when(connection.getAutoCommit()).thenReturn(true);
6869

6970
transaction.commit();
7071

7172
verify(connection, never()).commit();
73+
verify(connection).getAutoCommit();
7274
}
7375

7476
@Test
7577
@Override
76-
public void shouldRollback() throws SQLException {
78+
void shouldRollbackWhenConnectionIsNotAutoCommit() throws SQLException {
7779
when(connection.getAutoCommit()).thenReturn(false);
7880

7981
transaction.rollback();
8082

8183
verify(connection).rollback();
84+
verify(connection).getAutoCommit();
8285
}
8386

8487
@Test
85-
void shouldAutoRollback() throws SQLException {
88+
@Override
89+
void shouldAutoRollbackWhenConnectionIsAutoCommit() throws SQLException {
8690
when(connection.getAutoCommit()).thenReturn(true);
8791

8892
transaction.rollback();
8993

9094
verify(connection, never()).rollback();
95+
verify(connection).getAutoCommit();
9196
}
9297

9398
@Test
9499
@Override
95-
public void shouldClose() throws SQLException {
100+
void shouldCloseAndSetAutoCommitWhenConnectionIsNotAutoCommit() throws SQLException {
96101
when(connection.getAutoCommit()).thenReturn(false);
97102

98103
transaction.close();
99104

100105
verify(connection).close();
101106
verify(connection).setAutoCommit(true);
107+
verify(connection).getAutoCommit();
102108
}
103109

104110
@Test
105-
void shouldCloseWithAutoCommit() throws SQLException {
111+
@Override
112+
void shouldCloseAndNotSetAutoCommitWhenConnectionIsAutoCommit() throws SQLException {
106113
when(connection.getAutoCommit()).thenReturn(true);
107114

108115
transaction.close();
109116

110117
verify(connection).close();
111118
verify(connection, never()).setAutoCommit(true);
119+
verify(connection).getAutoCommit();
112120
}
113121

114122
@Test
115123
@Override
116-
public void shouldGetTimeout() throws SQLException {
124+
void shouldReturnNullWhenGetTimeout() throws SQLException {
117125
assertNull(transaction.getTimeout());
118126
}
119127

src/test/java/org/apache/ibatis/transaction/jdbc/JdbcTransactionWithDataSourceTest.java

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import org.apache.ibatis.session.TransactionIsolationLevel;
1919
import org.apache.ibatis.transaction.Transaction;
20-
import org.apache.ibatis.transaction.TransactionBase;
2120
import org.junit.jupiter.api.Test;
2221
import org.mockito.Mock;
2322

@@ -34,7 +33,7 @@
3433
* @author <a href="[email protected]">mawen12</a>
3534
* @see JdbcTransaction
3635
*/
37-
class JdbcTransactionWithDataSourceTest extends TransactionBase {
36+
class JdbcTransactionWithDataSourceTest extends JdbcTransactionBase {
3837

3938
@Mock
4039
private DataSource dataSource;
@@ -52,7 +51,7 @@ class JdbcTransactionWithDataSourceTest extends TransactionBase {
5251

5352
@Test
5453
@Override
55-
public void shouldGetConnection() throws SQLException {
54+
void shouldGetConnection() throws SQLException {
5655
when(dataSource.getConnection()).thenReturn(connection);
5756
when(desiredAutoCommit.getAsBoolean()).thenReturn(true);
5857
when(connection.getAutoCommit()).thenReturn(false);
@@ -83,7 +82,7 @@ void shouldGetConnectionWithNotAutoCommit() throws SQLException {
8382

8483
@Test
8584
@Override
86-
public void shouldCommit() throws SQLException {
85+
void shouldCommitWhenConnectionIsNotAutoCommit() throws SQLException {
8786
when(dataSource.getConnection()).thenReturn(connection);
8887
when(connection.getAutoCommit()).thenReturn(false);
8988

@@ -95,7 +94,8 @@ public void shouldCommit() throws SQLException {
9594
}
9695

9796
@Test
98-
void shouldAutoCommit() throws SQLException {
97+
@Override
98+
void shouldAutoCommitWhenConnectionIsAutoCommit() throws SQLException {
9999
when(dataSource.getConnection()).thenReturn(connection);
100100
when(connection.getAutoCommit()).thenReturn(true);
101101

@@ -108,7 +108,7 @@ void shouldAutoCommit() throws SQLException {
108108

109109
@Test
110110
@Override
111-
public void shouldRollback() throws SQLException {
111+
void shouldRollbackWhenConnectionIsNotAutoCommit() throws SQLException {
112112
when(dataSource.getConnection()).thenReturn(connection);
113113
when(connection.getAutoCommit()).thenReturn(false);
114114

@@ -120,7 +120,8 @@ public void shouldRollback() throws SQLException {
120120
}
121121

122122
@Test
123-
void shouldAutoRollback() throws SQLException {
123+
@Override
124+
void shouldAutoRollbackWhenConnectionIsAutoCommit() throws SQLException {
124125
when(dataSource.getConnection()).thenReturn(connection);
125126
when(connection.getAutoCommit()).thenReturn(true);
126127

@@ -133,25 +134,10 @@ void shouldAutoRollback() throws SQLException {
133134

134135
@Test
135136
@Override
136-
public void shouldClose() throws SQLException {
137-
when(dataSource.getConnection()).thenReturn(connection);
138-
when(desiredAutoCommit.getAsBoolean()).thenReturn(false);
139-
when(skipSetAutoCommitClose.getAsBoolean()).thenReturn(false);
140-
141-
buildTransaction();
142-
transaction.getConnection();
143-
transaction.close();
144-
145-
verify(connection).close();
146-
verify(connection).setAutoCommit(true);
147-
}
148-
149-
@Test
150-
void shouldNotSetAutoCommitWhenConnectionIsAutoCommit() throws SQLException {
137+
void shouldCloseAndSetAutoCommitWhenConnectionIsNotAutoCommit() throws SQLException {
151138
when(dataSource.getConnection()).thenReturn(connection);
152139
when(desiredAutoCommit.getAsBoolean()).thenReturn(false);
153140
when(skipSetAutoCommitClose.getAsBoolean()).thenReturn(false);
154-
when(connection.getAutoCommit()).thenReturn(false);
155141

156142
buildTransaction();
157143
transaction.getConnection();
@@ -162,7 +148,8 @@ void shouldNotSetAutoCommitWhenConnectionIsAutoCommit() throws SQLException {
162148
}
163149

164150
@Test
165-
void shouldNotSetAutoCommitWhenSkipSetAutoCommit() throws SQLException {
151+
@Override
152+
void shouldCloseAndNotSetAutoCommitWhenConnectionIsAutoCommit() throws SQLException {
166153
when(dataSource.getConnection()).thenReturn(connection);
167154
when(desiredAutoCommit.getAsBoolean()).thenReturn(false);
168155
when(skipSetAutoCommitClose.getAsBoolean()).thenReturn(false);
@@ -178,7 +165,7 @@ void shouldNotSetAutoCommitWhenSkipSetAutoCommit() throws SQLException {
178165

179166
@Test
180167
@Override
181-
public void shouldGetTimeout() throws SQLException {
168+
void shouldReturnNullWhenGetTimeout() throws SQLException {
182169
buildTransaction();
183170

184171
assertNull(transaction.getTimeout());
@@ -187,5 +174,4 @@ public void shouldGetTimeout() throws SQLException {
187174
private void buildTransaction() {
188175
this.transaction = new JdbcTransaction(dataSource, TransactionIsolationLevel.REPEATABLE_READ, desiredAutoCommit.getAsBoolean(), skipSetAutoCommitClose.getAsBoolean());
189176
}
190-
191177
}

src/test/java/org/apache/ibatis/transaction/TransactionBase.java renamed to src/test/java/org/apache/ibatis/transaction/managed/ManagedTransactionBase.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.apache.ibatis.transaction;
16+
package org.apache.ibatis.transaction.managed;
1717

1818
import org.junit.jupiter.api.extension.ExtendWith;
1919
import org.mockito.junit.jupiter.MockitoExtension;
@@ -22,18 +22,20 @@
2222

2323
/**
2424
* @author <a href="[email protected]">mawen12</a>
25-
* @see Transaction
25+
* @see ManagedTransaction
2626
*/
2727
@ExtendWith(MockitoExtension.class)
28-
public abstract class TransactionBase {
28+
abstract class ManagedTransactionBase {
2929

30-
public abstract void shouldGetConnection() throws SQLException;
30+
abstract void shouldGetConnection() throws SQLException;
3131

32-
public abstract void shouldCommit() throws SQLException;
32+
abstract void shouldNotCommitWhetherConnectionIsAutoCommit() throws SQLException;
3333

34-
public abstract void shouldRollback() throws SQLException;
34+
abstract void shouldNotRollbackWhetherConnectionIsAutoCommit() throws SQLException;
3535

36-
public abstract void shouldClose() throws SQLException;
36+
abstract void shouldCloseWhenSetCloseConnectionIsTrue() throws SQLException;
3737

38-
public abstract void shouldGetTimeout() throws SQLException;
39-
}
38+
abstract void shouldNotCloseWhenSetCloseConnectionIsFalse() throws SQLException;
39+
40+
abstract void shouldReturnNullWhenGetTimeout() throws SQLException;
41+
}

src/test/java/org/apache/ibatis/transaction/managed/ManagedTransactionWithConnectionTest.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package org.apache.ibatis.transaction.managed;
1717

1818
import org.apache.ibatis.transaction.Transaction;
19-
import org.apache.ibatis.transaction.TransactionBase;
2019
import org.junit.jupiter.api.BeforeEach;
2120
import org.junit.jupiter.api.Test;
2221
import org.mockito.Mock;
@@ -26,14 +25,13 @@
2625

2726
import static org.junit.jupiter.api.Assertions.assertEquals;
2827
import static org.junit.jupiter.api.Assertions.assertNull;
29-
import static org.mockito.Mockito.never;
30-
import static org.mockito.Mockito.verify;
28+
import static org.mockito.Mockito.*;
3129

3230
/**
3331
* @author <a href="[email protected]">mawen12</a>
3432
* @see ManagedTransaction
3533
*/
36-
class ManagedTransactionWithConnectionTest extends TransactionBase {
34+
class ManagedTransactionWithConnectionTest extends ManagedTransactionBase {
3735

3836
@Mock
3937
private Connection connection;
@@ -46,39 +44,41 @@ void setup() {
4644
}
4745

4846
@Test
49-
@Override
50-
public void shouldGetConnection() throws SQLException {
47+
void shouldGetConnection() throws SQLException {
5148
Connection result = transaction.getConnection();
5249

5350
assertEquals(connection, result);
5451
}
5552

5653
@Test
5754
@Override
58-
public void shouldCommit() throws SQLException {
55+
void shouldNotCommitWhetherConnectionIsAutoCommit() throws SQLException {
5956
transaction.commit();
6057

6158
verify(connection, never()).commit();
59+
verify(connection, never()).getAutoCommit();
6260
}
6361

6462
@Test
6563
@Override
66-
public void shouldRollback() throws SQLException {
64+
void shouldNotRollbackWhetherConnectionIsAutoCommit() throws SQLException {
6765
transaction.commit();
6866

6967
verify(connection, never()).rollback();
68+
verify(connection, never()).getAutoCommit();
7069
}
7170

7271
@Test
7372
@Override
74-
public void shouldClose() throws SQLException {
73+
void shouldCloseWhenSetCloseConnectionIsTrue() throws SQLException {
7574
transaction.close();
7675

7776
verify(connection).close();
7877
}
7978

8079
@Test
81-
void shouldNotCloseWhenSetNonCloseConnection() throws SQLException {
80+
@Override
81+
void shouldNotCloseWhenSetCloseConnectionIsFalse() throws SQLException {
8282
this.transaction = new ManagedTransaction(connection, false);
8383

8484
transaction.close();
@@ -88,7 +88,7 @@ void shouldNotCloseWhenSetNonCloseConnection() throws SQLException {
8888

8989
@Test
9090
@Override
91-
public void shouldGetTimeout() throws SQLException {
91+
void shouldReturnNullWhenGetTimeout() throws SQLException {
9292
assertNull(transaction.getTimeout());
9393
}
9494

0 commit comments

Comments
 (0)