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.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  import java.util.function.BooleanSupplier;
27  
28  import javax.sql.DataSource;
29  
30  import org.apache.ibatis.session.TransactionIsolationLevel;
31  import org.apache.ibatis.transaction.Transaction;
32  import org.junit.jupiter.api.Test;
33  import org.mockito.Mock;
34  
35  /**
36   * @author <a href="1181963012mw@gmail.com">mawen12</a>
37   *
38   * @see JdbcTransaction
39   */
40  class JdbcTransactionWithDataSourceTest extends JdbcTransactionBase {
41  
42    @Mock
43    private DataSource dataSource;
44  
45    @Mock
46    private Connection connection;
47  
48    @Mock
49    private BooleanSupplier desiredAutoCommit;
50  
51    @Mock
52    private BooleanSupplier skipSetAutoCommitClose;
53  
54    private Transaction transaction;
55  
56    @Test
57    @Override
58    void shouldGetConnection() throws SQLException {
59      when(dataSource.getConnection()).thenReturn(connection);
60      when(desiredAutoCommit.getAsBoolean()).thenReturn(true);
61      when(connection.getAutoCommit()).thenReturn(false);
62  
63      buildTransaction();
64      Connection result = transaction.getConnection();
65  
66      assertEquals(connection, result);
67      verify(dataSource).getConnection();
68      verify(connection).setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
69      verify(connection).setAutoCommit(true);
70    }
71  
72    @Test
73    void shouldGetConnectionWithNotAutoCommit() throws SQLException {
74      when(dataSource.getConnection()).thenReturn(connection);
75      when(desiredAutoCommit.getAsBoolean()).thenReturn(false);
76      when(connection.getAutoCommit()).thenReturn(true);
77  
78      buildTransaction();
79      Connection result = transaction.getConnection();
80  
81      assertEquals(connection, result);
82      verify(dataSource).getConnection();
83      verify(connection).setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
84      verify(connection).setAutoCommit(false);
85    }
86  
87    @Test
88    @Override
89    void shouldCommitWhenConnectionIsNotAutoCommit() throws SQLException {
90      when(dataSource.getConnection()).thenReturn(connection);
91      when(connection.getAutoCommit()).thenReturn(false);
92  
93      buildTransaction();
94      transaction.getConnection();
95      transaction.commit();
96  
97      verify(connection).commit();
98    }
99  
100   @Test
101   @Override
102   void shouldAutoCommitWhenConnectionIsAutoCommit() throws SQLException {
103     when(dataSource.getConnection()).thenReturn(connection);
104     when(connection.getAutoCommit()).thenReturn(true);
105 
106     buildTransaction();
107     transaction.getConnection();
108     transaction.commit();
109 
110     verify(connection, never()).commit();
111   }
112 
113   @Test
114   @Override
115   void shouldRollbackWhenConnectionIsNotAutoCommit() throws SQLException {
116     when(dataSource.getConnection()).thenReturn(connection);
117     when(connection.getAutoCommit()).thenReturn(false);
118 
119     buildTransaction();
120     transaction.getConnection();
121     transaction.rollback();
122 
123     verify(connection).rollback();
124   }
125 
126   @Test
127   @Override
128   void shouldAutoRollbackWhenConnectionIsAutoCommit() throws SQLException {
129     when(dataSource.getConnection()).thenReturn(connection);
130     when(connection.getAutoCommit()).thenReturn(true);
131 
132     buildTransaction();
133     transaction.getConnection();
134     transaction.commit();
135 
136     verify(connection, never()).rollback();
137   }
138 
139   @Test
140   @Override
141   void shouldCloseAndSetAutoCommitWhenConnectionIsNotAutoCommit() throws SQLException {
142     when(dataSource.getConnection()).thenReturn(connection);
143     when(desiredAutoCommit.getAsBoolean()).thenReturn(false);
144     when(skipSetAutoCommitClose.getAsBoolean()).thenReturn(false);
145 
146     buildTransaction();
147     transaction.getConnection();
148     transaction.close();
149 
150     verify(connection).close();
151     verify(connection).setAutoCommit(true);
152   }
153 
154   @Test
155   @Override
156   void shouldCloseAndNotSetAutoCommitWhenConnectionIsAutoCommit() throws SQLException {
157     when(dataSource.getConnection()).thenReturn(connection);
158     when(desiredAutoCommit.getAsBoolean()).thenReturn(false);
159     when(skipSetAutoCommitClose.getAsBoolean()).thenReturn(false);
160     when(connection.getAutoCommit()).thenReturn(true);
161 
162     buildTransaction();
163     transaction.getConnection();
164     transaction.close();
165 
166     verify(connection).close();
167     verify(connection, never()).setAutoCommit(true);
168   }
169 
170   @Test
171   @Override
172   void shouldReturnNullWhenGetTimeout() throws SQLException {
173     buildTransaction();
174 
175     assertNull(transaction.getTimeout());
176   }
177 
178   private void buildTransaction() {
179     this.transaction = new JdbcTransaction(dataSource, TransactionIsolationLevel.REPEATABLE_READ,
180         desiredAutoCommit.getAsBoolean(), skipSetAutoCommitClose.getAsBoolean());
181   }
182 }