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.managed;
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  
23  import java.sql.Connection;
24  import java.sql.SQLException;
25  
26  import org.apache.ibatis.transaction.Transaction;
27  import org.junit.jupiter.api.BeforeEach;
28  import org.junit.jupiter.api.Test;
29  import org.mockito.Mock;
30  
31  /**
32   * @author <a href="1181963012mw@gmail.com">mawen12</a>
33   *
34   * @see ManagedTransaction
35   */
36  class ManagedTransactionWithConnectionTest extends ManagedTransactionBase {
37  
38    @Mock
39    private Connection connection;
40  
41    private Transaction transaction;
42  
43    @BeforeEach
44    void setup() {
45      this.transaction = new ManagedTransaction(connection, true);
46    }
47  
48    @Override
49    @Test
50    void shouldGetConnection() throws SQLException {
51      Connection result = transaction.getConnection();
52  
53      assertEquals(connection, result);
54    }
55  
56    @Test
57    @Override
58    void shouldNotCommitWhetherConnectionIsAutoCommit() throws SQLException {
59      transaction.commit();
60  
61      verify(connection, never()).commit();
62      verify(connection, never()).getAutoCommit();
63    }
64  
65    @Test
66    @Override
67    void shouldNotRollbackWhetherConnectionIsAutoCommit() throws SQLException {
68      transaction.commit();
69  
70      verify(connection, never()).rollback();
71      verify(connection, never()).getAutoCommit();
72    }
73  
74    @Test
75    @Override
76    void shouldCloseWhenSetCloseConnectionIsTrue() throws SQLException {
77      transaction.close();
78  
79      verify(connection).close();
80    }
81  
82    @Test
83    @Override
84    void shouldNotCloseWhenSetCloseConnectionIsFalse() throws SQLException {
85      this.transaction = new ManagedTransaction(connection, false);
86  
87      transaction.close();
88  
89      verify(connection, never()).close();
90    }
91  
92    @Test
93    @Override
94    void shouldReturnNullWhenGetTimeout() throws SQLException {
95      assertNull(transaction.getTimeout());
96    }
97  
98  }