Skip to content

Commit e607571

Browse files
committed
add TransactionFactory, Transaction test cases
1 parent 2a5fad6 commit e607571

8 files changed

+808
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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;
17+
18+
import org.junit.jupiter.api.extension.ExtendWith;
19+
import org.mockito.junit.jupiter.MockitoExtension;
20+
21+
import java.lang.reflect.Field;
22+
import java.sql.SQLException;
23+
24+
/**
25+
* @author <a href="[email protected]">mawen12</a>
26+
* @see TransactionFactory
27+
*/
28+
@ExtendWith(MockitoExtension.class)
29+
public abstract class TransactionFactoryTest {
30+
31+
public abstract void shouldSetProperties() throws Exception;
32+
33+
public abstract void shouldNewTransactionWithConnection() throws SQLException;
34+
35+
public abstract void shouldNewTransactionWithDataSource() throws Exception;
36+
37+
public static Object getValue(Field field, Object object) throws Exception {
38+
field.setAccessible(true);
39+
return field.get(object);
40+
}
41+
42+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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;
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 Transaction
26+
*/
27+
@ExtendWith(MockitoExtension.class)
28+
public abstract class TransactionTest {
29+
30+
public abstract void shouldGetConnection() throws SQLException;
31+
32+
public abstract void shouldCommit() throws SQLException;
33+
34+
public abstract void shouldRollback() throws SQLException;
35+
36+
public abstract void shouldClose() throws SQLException;
37+
38+
public abstract void shouldGetTimeout() throws SQLException;
39+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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.apache.ibatis.session.TransactionIsolationLevel;
19+
import org.apache.ibatis.transaction.Transaction;
20+
import org.apache.ibatis.transaction.TransactionFactory;
21+
import org.apache.ibatis.transaction.TransactionFactoryTest;
22+
import org.junit.jupiter.api.BeforeEach;
23+
import org.junit.jupiter.api.Test;
24+
import org.mockito.Mock;
25+
26+
import javax.sql.DataSource;
27+
import java.sql.Connection;
28+
import java.sql.SQLException;
29+
import java.util.Properties;
30+
31+
import static org.junit.jupiter.api.Assertions.*;
32+
import static org.mockito.Mockito.verify;
33+
import static org.mockito.Mockito.when;
34+
35+
/**
36+
* @author <a href="[email protected]">mawen12</a>
37+
* @see JdbcTransactionFactory
38+
*/
39+
class JdbcTransactionFactoryUnitTest extends TransactionFactoryTest {
40+
41+
@Mock
42+
private Properties properties;
43+
44+
@Mock
45+
private Connection connection;
46+
47+
@Mock
48+
private DataSource dataSource;
49+
50+
private TransactionFactory transactionFactory;
51+
52+
@BeforeEach
53+
void setup() {
54+
this.transactionFactory = new JdbcTransactionFactory();
55+
}
56+
57+
@Test
58+
@Override
59+
public void shouldSetProperties() throws Exception {
60+
when(properties.getProperty("skipSetAutoCommitOnClose")).thenReturn("true");
61+
62+
transactionFactory.setProperties(properties);
63+
64+
assertTrue((Boolean) getValue(transactionFactory.getClass().getDeclaredField("skipSetAutoCommitOnClose"), transactionFactory));
65+
}
66+
67+
@Test
68+
@Override
69+
public void shouldNewTransactionWithConnection() throws SQLException {
70+
Transaction result = transactionFactory.newTransaction(connection);
71+
72+
assertNotNull(result);
73+
assertInstanceOf(JdbcTransaction.class, result);
74+
assertEquals(connection, result.getConnection());
75+
}
76+
77+
@Test
78+
@Override
79+
public void shouldNewTransactionWithDataSource() throws Exception {
80+
when(dataSource.getConnection()).thenReturn(connection);
81+
82+
Transaction result = transactionFactory.newTransaction(dataSource, TransactionIsolationLevel.READ_COMMITTED, false);
83+
84+
assertNotNull(result);
85+
assertInstanceOf(JdbcTransaction.class, result);
86+
assertEquals(connection, result.getConnection());
87+
verify(connection).setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
88+
89+
assertEquals(dataSource, getValue(result.getClass().getDeclaredField("dataSource"), result));
90+
assertEquals(TransactionIsolationLevel.READ_COMMITTED, getValue(result.getClass().getDeclaredField("level"), result));
91+
assertEquals(false, getValue(result.getClass().getDeclaredField("autoCommit"), result));
92+
assertEquals(false, getValue(result.getClass().getDeclaredField("skipSetAutoCommitOnClose"), result));
93+
}
94+
95+
@Test
96+
void shouldNewTransactionWithDataSourceAndCustomProperties() throws Exception {
97+
when(dataSource.getConnection()).thenReturn(connection);
98+
when(properties.getProperty("skipSetAutoCommitOnClose")).thenReturn("true");
99+
100+
transactionFactory.setProperties(properties);
101+
Transaction result = transactionFactory.newTransaction(dataSource, TransactionIsolationLevel.READ_COMMITTED, true);
102+
103+
assertNotNull(result);
104+
assertInstanceOf(JdbcTransaction.class, result);
105+
assertEquals(connection, result.getConnection());
106+
verify(connection).setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
107+
108+
assertEquals(dataSource, getValue(result.getClass().getDeclaredField("dataSource"), result));
109+
assertEquals(TransactionIsolationLevel.READ_COMMITTED, getValue(result.getClass().getDeclaredField("level"), result));
110+
assertEquals(true, getValue(result.getClass().getDeclaredField("autoCommit"), result));
111+
assertEquals(true, getValue(result.getClass().getDeclaredField("skipSetAutoCommitOnClose"), result));
112+
}
113+
114+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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.apache.ibatis.transaction.Transaction;
19+
import org.apache.ibatis.transaction.TransactionTest;
20+
import org.junit.jupiter.api.BeforeEach;
21+
import org.junit.jupiter.api.Test;
22+
import org.mockito.Mock;
23+
24+
import java.sql.Connection;
25+
import java.sql.SQLException;
26+
27+
import static org.junit.jupiter.api.Assertions.assertEquals;
28+
import static org.junit.jupiter.api.Assertions.assertNull;
29+
import static org.mockito.Mockito.*;
30+
31+
/**
32+
* @author <a href="[email protected]">mawen12</a>
33+
* @see JdbcTransaction
34+
*/
35+
class JdbcTransactionWithConnectionTest extends TransactionTest {
36+
37+
@Mock
38+
private Connection connection;
39+
40+
private Transaction transaction;
41+
42+
@BeforeEach
43+
void setup() {
44+
this.transaction = new JdbcTransaction(connection);
45+
}
46+
47+
@Test
48+
@Override
49+
public void shouldGetConnection() throws SQLException {
50+
Connection result = transaction.getConnection();
51+
52+
assertEquals(connection, result);
53+
}
54+
55+
@Test
56+
@Override
57+
public void shouldCommit() throws SQLException {
58+
when(connection.getAutoCommit()).thenReturn(false);
59+
60+
transaction.commit();
61+
62+
verify(connection).commit();
63+
}
64+
65+
@Test
66+
void shouldAutoCommit() throws SQLException {
67+
when(connection.getAutoCommit()).thenReturn(true);
68+
69+
transaction.commit();
70+
71+
verify(connection, never()).commit();
72+
}
73+
74+
@Test
75+
@Override
76+
public void shouldRollback() throws SQLException {
77+
when(connection.getAutoCommit()).thenReturn(false);
78+
79+
transaction.rollback();
80+
81+
verify(connection).rollback();
82+
}
83+
84+
@Test
85+
void shouldAutoRollback() throws SQLException {
86+
when(connection.getAutoCommit()).thenReturn(true);
87+
88+
transaction.rollback();
89+
90+
verify(connection, never()).rollback();
91+
}
92+
93+
@Test
94+
@Override
95+
public void shouldClose() throws SQLException {
96+
when(connection.getAutoCommit()).thenReturn(false);
97+
98+
transaction.close();
99+
100+
verify(connection).close();
101+
verify(connection).setAutoCommit(true);
102+
}
103+
104+
@Test
105+
void shouldCloseWithAutoCommit() throws SQLException {
106+
when(connection.getAutoCommit()).thenReturn(true);
107+
108+
transaction.close();
109+
110+
verify(connection).close();
111+
verify(connection, never()).setAutoCommit(true);
112+
}
113+
114+
@Test
115+
@Override
116+
public void shouldGetTimeout() throws SQLException {
117+
assertNull(transaction.getTimeout());
118+
}
119+
120+
}

0 commit comments

Comments
 (0)