-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor JdbcTransaction and ManagedTransaction test case
- Loading branch information
Showing
6 changed files
with
108 additions
and
66 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
src/test/java/org/apache/ibatis/transaction/jdbc/JdbcTransactionBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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()); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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; | ||
|
@@ -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); | ||
|
@@ -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); | ||
|
||
|
@@ -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); | ||
|
||
|
@@ -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); | ||
|
||
|
@@ -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); | ||
|
||
|
@@ -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(); | ||
|
@@ -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); | ||
|
@@ -178,7 +165,7 @@ void shouldNotSetAutoCommitWhenSkipSetAutoCommit() throws SQLException { | |
|
||
@Test | ||
@Override | ||
public void shouldGetTimeout() throws SQLException { | ||
void shouldReturnNullWhenGetTimeout() throws SQLException { | ||
buildTransaction(); | ||
|
||
assertNull(transaction.getTimeout()); | ||
|
@@ -187,5 +174,4 @@ public void shouldGetTimeout() throws SQLException { | |
private void buildTransaction() { | ||
this.transaction = new JdbcTransaction(dataSource, TransactionIsolationLevel.REPEATABLE_READ, desiredAutoCommit.getAsBoolean(), skipSetAutoCommitClose.getAsBoolean()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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(); | ||
|
@@ -88,7 +88,7 @@ void shouldNotCloseWhenSetNonCloseConnection() throws SQLException { | |
|
||
@Test | ||
@Override | ||
public void shouldGetTimeout() throws SQLException { | ||
void shouldReturnNullWhenGetTimeout() throws SQLException { | ||
assertNull(transaction.getTimeout()); | ||
} | ||
|
||
|
Oops, something went wrong.