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    *       http://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.Assert.*;
19  import static org.mockito.Mockito.*;
20  
21  import java.util.Properties;
22  
23  import javax.sql.DataSource;
24  
25  import org.apache.ibatis.session.TransactionIsolationLevel;
26  import org.apache.ibatis.transaction.Transaction;
27  import org.junit.jupiter.api.Test;
28  
29  class JdbcTransactionFactoryTest {
30  
31    @Test
32    void testNullProperties() throws Exception {
33      TestConnection connection = new TestConnection(false);
34      JdbcTransactionFactory factory = new JdbcTransactionFactory();
35      factory.setProperties(null);
36      Transaction transaction = factory.newTransaction(connection);
37      transaction.getConnection();
38      transaction.close();
39      assertTrue(connection.getAutoCommit());
40    }
41  
42    @Test
43    void testSkipSetAutoCommitOnClose() throws Exception {
44      TestConnection connection = new TestConnection(false);
45      DataSource ds = mock(DataSource.class);
46      when(ds.getConnection()).thenReturn(connection);
47  
48      JdbcTransactionFactory factory = new JdbcTransactionFactory();
49      Properties properties = new Properties();
50      properties.setProperty("skipSetAutoCommitOnClose", "true");
51      factory.setProperties(properties);
52      Transaction transaction = factory.newTransaction(ds, TransactionIsolationLevel.NONE, false);
53      transaction.getConnection();
54      transaction.close();
55      assertFalse(connection.getAutoCommit());
56    }
57  
58  }