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  
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   * @author <a href="1181963012mw@gmail.com">mawen12</a>
34   *
35   * @see JdbcTransaction
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 }