Skip to content

Commit

Permalink
refactor JdbcTransaction and ManagedTransaction test case
Browse files Browse the repository at this point in the history
  • Loading branch information
mawen12 committed Oct 21, 2024
1 parent 201d6a5 commit 067b46a
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.transaction.jdbc;

import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import java.sql.SQLException;

/**
* @author <a href="[email protected]">mawen12</a>
* @see JdbcTransaction
*/
@ExtendWith(MockitoExtension.class)
abstract class JdbcTransactionBase {

abstract void shouldGetConnection() throws SQLException;

abstract void shouldCommitWhenConnectionIsNotAutoCommit() throws SQLException;

abstract void shouldAutoCommitWhenConnectionIsAutoCommit() throws SQLException;

abstract void shouldRollbackWhenConnectionIsNotAutoCommit() throws SQLException;

abstract void shouldAutoRollbackWhenConnectionIsAutoCommit() throws SQLException;

abstract void shouldCloseAndSetAutoCommitWhenConnectionIsNotAutoCommit() throws SQLException;

abstract void shouldCloseAndNotSetAutoCommitWhenConnectionIsAutoCommit() throws SQLException;

abstract void shouldReturnNullWhenGetTimeout() throws SQLException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package org.apache.ibatis.transaction.jdbc;

import org.apache.ibatis.transaction.Transaction;
import org.apache.ibatis.transaction.TransactionBase;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
Expand All @@ -32,7 +31,7 @@
* @author <a href="[email protected]">mawen12</a>
* @see JdbcTransaction
*/
class JdbcTransactionWithConnectionTest extends TransactionBase {
class JdbcTransactionWithConnectionTest extends JdbcTransactionBase {

@Mock
private Connection connection;
Expand All @@ -46,74 +45,83 @@ void setup() {

@Test
@Override
public void shouldGetConnection() throws SQLException {
void shouldGetConnection() throws SQLException {
Connection result = transaction.getConnection();

assertEquals(connection, result);
}

@Test
@Override
public void shouldCommit() throws SQLException {
void shouldCommitWhenConnectionIsNotAutoCommit() throws SQLException {
when(connection.getAutoCommit()).thenReturn(false);

transaction.commit();

verify(connection).commit();
verify(connection).getAutoCommit();
}

@Test
void shouldAutoCommit() throws SQLException {
@Override
void shouldAutoCommitWhenConnectionIsAutoCommit() throws SQLException {
when(connection.getAutoCommit()).thenReturn(true);

transaction.commit();

verify(connection, never()).commit();
verify(connection).getAutoCommit();
}

@Test
@Override
public void shouldRollback() throws SQLException {
void shouldRollbackWhenConnectionIsNotAutoCommit() throws SQLException {
when(connection.getAutoCommit()).thenReturn(false);

transaction.rollback();

verify(connection).rollback();
verify(connection).getAutoCommit();
}

@Test
void shouldAutoRollback() throws SQLException {
@Override
void shouldAutoRollbackWhenConnectionIsAutoCommit() throws SQLException {
when(connection.getAutoCommit()).thenReturn(true);

transaction.rollback();

verify(connection, never()).rollback();
verify(connection).getAutoCommit();
}

@Test
@Override
public void shouldClose() throws SQLException {
void shouldCloseAndSetAutoCommitWhenConnectionIsNotAutoCommit() throws SQLException {
when(connection.getAutoCommit()).thenReturn(false);

transaction.close();

verify(connection).close();
verify(connection).setAutoCommit(true);
verify(connection).getAutoCommit();
}

@Test
void shouldCloseWithAutoCommit() throws SQLException {
@Override
void shouldCloseAndNotSetAutoCommitWhenConnectionIsAutoCommit() throws SQLException {
when(connection.getAutoCommit()).thenReturn(true);

transaction.close();

verify(connection).close();
verify(connection, never()).setAutoCommit(true);
verify(connection).getAutoCommit();
}

@Test
@Override
public void shouldGetTimeout() throws SQLException {
void shouldReturnNullWhenGetTimeout() throws SQLException {
assertNull(transaction.getTimeout());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import org.apache.ibatis.session.TransactionIsolationLevel;
import org.apache.ibatis.transaction.Transaction;
import org.apache.ibatis.transaction.TransactionBase;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;

Expand All @@ -34,7 +33,7 @@
* @author <a href="[email protected]">mawen12</a>
* @see JdbcTransaction
*/
class JdbcTransactionWithDataSourceTest extends TransactionBase {
class JdbcTransactionWithDataSourceTest extends JdbcTransactionBase {

@Mock
private DataSource dataSource;
Expand All @@ -52,7 +51,7 @@ class JdbcTransactionWithDataSourceTest extends TransactionBase {

@Test
@Override
public void shouldGetConnection() throws SQLException {
void shouldGetConnection() throws SQLException {
when(dataSource.getConnection()).thenReturn(connection);
when(desiredAutoCommit.getAsBoolean()).thenReturn(true);
when(connection.getAutoCommit()).thenReturn(false);
Expand Down Expand Up @@ -83,7 +82,7 @@ void shouldGetConnectionWithNotAutoCommit() throws SQLException {

@Test
@Override
public void shouldCommit() throws SQLException {
void shouldCommitWhenConnectionIsNotAutoCommit() throws SQLException {
when(dataSource.getConnection()).thenReturn(connection);
when(connection.getAutoCommit()).thenReturn(false);

Expand All @@ -95,7 +94,8 @@ public void shouldCommit() throws SQLException {
}

@Test
void shouldAutoCommit() throws SQLException {
@Override
void shouldAutoCommitWhenConnectionIsAutoCommit() throws SQLException {
when(dataSource.getConnection()).thenReturn(connection);
when(connection.getAutoCommit()).thenReturn(true);

Expand All @@ -108,7 +108,7 @@ void shouldAutoCommit() throws SQLException {

@Test
@Override
public void shouldRollback() throws SQLException {
void shouldRollbackWhenConnectionIsNotAutoCommit() throws SQLException {
when(dataSource.getConnection()).thenReturn(connection);
when(connection.getAutoCommit()).thenReturn(false);

Expand All @@ -120,7 +120,8 @@ public void shouldRollback() throws SQLException {
}

@Test
void shouldAutoRollback() throws SQLException {
@Override
void shouldAutoRollbackWhenConnectionIsAutoCommit() throws SQLException {
when(dataSource.getConnection()).thenReturn(connection);
when(connection.getAutoCommit()).thenReturn(true);

Expand All @@ -133,25 +134,10 @@ void shouldAutoRollback() throws SQLException {

@Test
@Override
public void shouldClose() throws SQLException {
when(dataSource.getConnection()).thenReturn(connection);
when(desiredAutoCommit.getAsBoolean()).thenReturn(false);
when(skipSetAutoCommitClose.getAsBoolean()).thenReturn(false);

buildTransaction();
transaction.getConnection();
transaction.close();

verify(connection).close();
verify(connection).setAutoCommit(true);
}

@Test
void shouldNotSetAutoCommitWhenConnectionIsAutoCommit() throws SQLException {
void shouldCloseAndSetAutoCommitWhenConnectionIsNotAutoCommit() throws SQLException {
when(dataSource.getConnection()).thenReturn(connection);
when(desiredAutoCommit.getAsBoolean()).thenReturn(false);
when(skipSetAutoCommitClose.getAsBoolean()).thenReturn(false);
when(connection.getAutoCommit()).thenReturn(false);

buildTransaction();
transaction.getConnection();
Expand All @@ -162,7 +148,8 @@ void shouldNotSetAutoCommitWhenConnectionIsAutoCommit() throws SQLException {
}

@Test
void shouldNotSetAutoCommitWhenSkipSetAutoCommit() throws SQLException {
@Override
void shouldCloseAndNotSetAutoCommitWhenConnectionIsAutoCommit() throws SQLException {
when(dataSource.getConnection()).thenReturn(connection);
when(desiredAutoCommit.getAsBoolean()).thenReturn(false);
when(skipSetAutoCommitClose.getAsBoolean()).thenReturn(false);
Expand All @@ -178,7 +165,7 @@ void shouldNotSetAutoCommitWhenSkipSetAutoCommit() throws SQLException {

@Test
@Override
public void shouldGetTimeout() throws SQLException {
void shouldReturnNullWhenGetTimeout() throws SQLException {
buildTransaction();

assertNull(transaction.getTimeout());
Expand All @@ -187,5 +174,4 @@ public void shouldGetTimeout() throws SQLException {
private void buildTransaction() {
this.transaction = new JdbcTransaction(dataSource, TransactionIsolationLevel.REPEATABLE_READ, desiredAutoCommit.getAsBoolean(), skipSetAutoCommitClose.getAsBoolean());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.transaction;
package org.apache.ibatis.transaction.managed;

import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
Expand All @@ -22,18 +22,20 @@

/**
* @author <a href="[email protected]">mawen12</a>
* @see Transaction
* @see ManagedTransaction
*/
@ExtendWith(MockitoExtension.class)
public abstract class TransactionBase {
abstract class ManagedTransactionBase {

public abstract void shouldGetConnection() throws SQLException;
abstract void shouldGetConnection() throws SQLException;

public abstract void shouldCommit() throws SQLException;
abstract void shouldNotCommitWhetherConnectionIsAutoCommit() throws SQLException;

public abstract void shouldRollback() throws SQLException;
abstract void shouldNotRollbackWhetherConnectionIsAutoCommit() throws SQLException;

public abstract void shouldClose() throws SQLException;
abstract void shouldCloseWhenSetCloseConnectionIsTrue() throws SQLException;

public abstract void shouldGetTimeout() throws SQLException;
}
abstract void shouldNotCloseWhenSetCloseConnectionIsFalse() throws SQLException;

abstract void shouldReturnNullWhenGetTimeout() throws SQLException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package org.apache.ibatis.transaction.managed;

import org.apache.ibatis.transaction.Transaction;
import org.apache.ibatis.transaction.TransactionBase;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
Expand All @@ -26,14 +25,13 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.*;

/**
* @author <a href="[email protected]">mawen12</a>
* @see ManagedTransaction
*/
class ManagedTransactionWithConnectionTest extends TransactionBase {
class ManagedTransactionWithConnectionTest extends ManagedTransactionBase {

@Mock
private Connection connection;
Expand All @@ -46,39 +44,41 @@ void setup() {
}

@Test
@Override
public void shouldGetConnection() throws SQLException {
void shouldGetConnection() throws SQLException {
Connection result = transaction.getConnection();

assertEquals(connection, result);
}

@Test
@Override
public void shouldCommit() throws SQLException {
void shouldNotCommitWhetherConnectionIsAutoCommit() throws SQLException {
transaction.commit();

verify(connection, never()).commit();
verify(connection, never()).getAutoCommit();
}

@Test
@Override
public void shouldRollback() throws SQLException {
void shouldNotRollbackWhetherConnectionIsAutoCommit() throws SQLException {
transaction.commit();

verify(connection, never()).rollback();
verify(connection, never()).getAutoCommit();
}

@Test
@Override
public void shouldClose() throws SQLException {
void shouldCloseWhenSetCloseConnectionIsTrue() throws SQLException {
transaction.close();

verify(connection).close();
}

@Test
void shouldNotCloseWhenSetNonCloseConnection() throws SQLException {
@Override
void shouldNotCloseWhenSetCloseConnectionIsFalse() throws SQLException {
this.transaction = new ManagedTransaction(connection, false);

transaction.close();
Expand All @@ -88,7 +88,7 @@ void shouldNotCloseWhenSetNonCloseConnection() throws SQLException {

@Test
@Override
public void shouldGetTimeout() throws SQLException {
void shouldReturnNullWhenGetTimeout() throws SQLException {
assertNull(transaction.getTimeout());
}

Expand Down
Loading

0 comments on commit 067b46a

Please sign in to comment.