View Javadoc
1   /*
2    *    Copyright 2009-2022 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.mockito.Mockito.verify;
20  import static org.mockito.Mockito.verifyNoMoreInteractions;
21  
22  import java.sql.Connection;
23  import java.util.Properties;
24  
25  import org.apache.ibatis.BaseDataTest;
26  import org.apache.ibatis.transaction.Transaction;
27  import org.apache.ibatis.transaction.TransactionFactory;
28  import org.junit.jupiter.api.Test;
29  import org.junit.jupiter.api.extension.ExtendWith;
30  import org.mockito.Mock;
31  import org.mockito.junit.jupiter.MockitoExtension;
32  
33  @ExtendWith(MockitoExtension.class)
34  class ManagedTransactionFactoryTest extends BaseDataTest {
35  
36    @Mock
37    private Connection conn;
38  
39    @Test
40    void shouldEnsureThatCallsToManagedTransactionAPIDoNotForwardToManagedConnections() throws Exception {
41      TransactionFactory tf = new ManagedTransactionFactory();
42      tf.setProperties(new Properties());
43      Transaction tx = tf.newTransaction(conn);
44      assertEquals(conn, tx.getConnection());
45      tx.commit();
46      tx.rollback();
47      tx.close();
48      verify(conn).close();
49    }
50  
51    @Test
52    void shouldEnsureThatCallsToManagedTransactionAPIDoNotForwardToManagedConnectionsAndDoesNotCloseConnection()
53        throws Exception {
54      TransactionFactory tf = new ManagedTransactionFactory();
55      Properties props = new Properties();
56      props.setProperty("closeConnection", "false");
57      tf.setProperties(props);
58      Transaction tx = tf.newTransaction(conn);
59      assertEquals(conn, tx.getConnection());
60      tx.commit();
61      tx.rollback();
62      tx.close();
63      verifyNoMoreInteractions(conn);
64    }
65  
66  }