View Javadoc
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 static org.junit.jupiter.api.Assertions.assertEquals;
19  import static org.junit.jupiter.api.Assertions.assertInstanceOf;
20  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
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   * @author <a href="1181963012mw@gmail.com">mawen12</a>
41   *
42   * @see JdbcTransactionFactory
43   */
44  class JdbcTransactionFactoryUnitTest 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 JdbcTransactionFactory();
60    }
61  
62    @Test
63    @Override
64    public void shouldSetProperties() throws Exception {
65      when(properties.getProperty("skipSetAutoCommitOnClose")).thenReturn("true");
66  
67      transactionFactory.setProperties(properties);
68  
69      assertTrue((Boolean) getValue(transactionFactory.getClass().getDeclaredField("skipSetAutoCommitOnClose"),
70          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(JdbcTransaction.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  
88      Transaction result = transactionFactory.newTransaction(dataSource, TransactionIsolationLevel.READ_COMMITTED, false);
89  
90      assertNotNull(result);
91      assertInstanceOf(JdbcTransaction.class, result);
92      assertEquals(connection, result.getConnection());
93      verify(connection).setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
94  
95      assertEquals(dataSource, getValue(result.getClass().getDeclaredField("dataSource"), result));
96      assertEquals(TransactionIsolationLevel.READ_COMMITTED,
97          getValue(result.getClass().getDeclaredField("level"), result));
98      assertEquals(false, getValue(result.getClass().getDeclaredField("autoCommit"), result));
99      assertEquals(false, getValue(result.getClass().getDeclaredField("skipSetAutoCommitOnClose"), result));
100   }
101 
102   @Test
103   void shouldNewTransactionWithDataSourceAndCustomProperties() throws Exception {
104     when(dataSource.getConnection()).thenReturn(connection);
105     when(properties.getProperty("skipSetAutoCommitOnClose")).thenReturn("true");
106 
107     transactionFactory.setProperties(properties);
108     Transaction result = transactionFactory.newTransaction(dataSource, TransactionIsolationLevel.READ_COMMITTED, true);
109 
110     assertNotNull(result);
111     assertInstanceOf(JdbcTransaction.class, result);
112     assertEquals(connection, result.getConnection());
113     verify(connection).setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
114 
115     assertEquals(dataSource, getValue(result.getClass().getDeclaredField("dataSource"), result));
116     assertEquals(TransactionIsolationLevel.READ_COMMITTED,
117         getValue(result.getClass().getDeclaredField("level"), result));
118     assertEquals(true, getValue(result.getClass().getDeclaredField("autoCommit"), result));
119     assertEquals(true, getValue(result.getClass().getDeclaredField("skipSetAutoCommitOnClose"), result));
120   }
121 
122 }