-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add TransactionFactory, Transaction test cases #3277
Open
mawen12
wants to merge
5
commits into
mybatis:master
Choose a base branch
from
mawen12:feature-transaction-test
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e607571
add TransactionFactory, Transaction test cases
mawen12 90008ef
rename base test class
mawen12 6754444
remove test suffix from base test class
mawen12 201d6a5
rename SqlNode, ObjectWrapper test class name
mawen12 067b46a
refactor JdbcTransaction and ManagedTransaction test case
mawen12 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/test/java/org/apache/ibatis/transaction/TransactionFactoryTest.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,42 @@ | ||
/* | ||
* 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; | ||
|
||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.lang.reflect.Field; | ||
import java.sql.SQLException; | ||
|
||
/** | ||
* @author <a href="[email protected]">mawen12</a> | ||
* @see TransactionFactory | ||
*/ | ||
@ExtendWith(MockitoExtension.class) | ||
public abstract class TransactionFactoryTest { | ||
|
||
public abstract void shouldSetProperties() throws Exception; | ||
|
||
public abstract void shouldNewTransactionWithConnection() throws SQLException; | ||
|
||
public abstract void shouldNewTransactionWithDataSource() throws Exception; | ||
|
||
public static Object getValue(Field field, Object object) throws Exception { | ||
field.setAccessible(true); | ||
return field.get(object); | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
src/test/java/org/apache/ibatis/transaction/TransactionTest.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,39 @@ | ||
/* | ||
* 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; | ||
|
||
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 Transaction | ||
*/ | ||
@ExtendWith(MockitoExtension.class) | ||
public abstract class TransactionTest { | ||
|
||
public abstract void shouldGetConnection() throws SQLException; | ||
|
||
public abstract void shouldCommit() throws SQLException; | ||
|
||
public abstract void shouldRollback() throws SQLException; | ||
|
||
public abstract void shouldClose() throws SQLException; | ||
|
||
public abstract void shouldGetTimeout() throws SQLException; | ||
} |
114 changes: 114 additions & 0 deletions
114
src/test/java/org/apache/ibatis/transaction/jdbc/JdbcTransactionFactoryUnitTest.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,114 @@ | ||
/* | ||
* 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.apache.ibatis.session.TransactionIsolationLevel; | ||
import org.apache.ibatis.transaction.Transaction; | ||
import org.apache.ibatis.transaction.TransactionFactory; | ||
import org.apache.ibatis.transaction.TransactionFactoryTest; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import java.util.Properties; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
/** | ||
* @author <a href="[email protected]">mawen12</a> | ||
* @see JdbcTransactionFactory | ||
*/ | ||
class JdbcTransactionFactoryUnitTest extends TransactionFactoryTest { | ||
|
||
@Mock | ||
private Properties properties; | ||
|
||
@Mock | ||
private Connection connection; | ||
|
||
@Mock | ||
private DataSource dataSource; | ||
|
||
private TransactionFactory transactionFactory; | ||
|
||
@BeforeEach | ||
void setup() { | ||
this.transactionFactory = new JdbcTransactionFactory(); | ||
} | ||
|
||
@Test | ||
@Override | ||
public void shouldSetProperties() throws Exception { | ||
when(properties.getProperty("skipSetAutoCommitOnClose")).thenReturn("true"); | ||
|
||
transactionFactory.setProperties(properties); | ||
|
||
assertTrue((Boolean) getValue(transactionFactory.getClass().getDeclaredField("skipSetAutoCommitOnClose"), transactionFactory)); | ||
} | ||
|
||
@Test | ||
@Override | ||
public void shouldNewTransactionWithConnection() throws SQLException { | ||
Transaction result = transactionFactory.newTransaction(connection); | ||
|
||
assertNotNull(result); | ||
assertInstanceOf(JdbcTransaction.class, result); | ||
assertEquals(connection, result.getConnection()); | ||
} | ||
|
||
@Test | ||
@Override | ||
public void shouldNewTransactionWithDataSource() throws Exception { | ||
when(dataSource.getConnection()).thenReturn(connection); | ||
|
||
Transaction result = transactionFactory.newTransaction(dataSource, TransactionIsolationLevel.READ_COMMITTED, false); | ||
|
||
assertNotNull(result); | ||
assertInstanceOf(JdbcTransaction.class, result); | ||
assertEquals(connection, result.getConnection()); | ||
verify(connection).setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); | ||
|
||
assertEquals(dataSource, getValue(result.getClass().getDeclaredField("dataSource"), result)); | ||
assertEquals(TransactionIsolationLevel.READ_COMMITTED, getValue(result.getClass().getDeclaredField("level"), result)); | ||
assertEquals(false, getValue(result.getClass().getDeclaredField("autoCommit"), result)); | ||
assertEquals(false, getValue(result.getClass().getDeclaredField("skipSetAutoCommitOnClose"), result)); | ||
} | ||
|
||
@Test | ||
void shouldNewTransactionWithDataSourceAndCustomProperties() throws Exception { | ||
when(dataSource.getConnection()).thenReturn(connection); | ||
when(properties.getProperty("skipSetAutoCommitOnClose")).thenReturn("true"); | ||
|
||
transactionFactory.setProperties(properties); | ||
Transaction result = transactionFactory.newTransaction(dataSource, TransactionIsolationLevel.READ_COMMITTED, true); | ||
|
||
assertNotNull(result); | ||
assertInstanceOf(JdbcTransaction.class, result); | ||
assertEquals(connection, result.getConnection()); | ||
verify(connection).setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); | ||
|
||
assertEquals(dataSource, getValue(result.getClass().getDeclaredField("dataSource"), result)); | ||
assertEquals(TransactionIsolationLevel.READ_COMMITTED, getValue(result.getClass().getDeclaredField("level"), result)); | ||
assertEquals(true, getValue(result.getClass().getDeclaredField("autoCommit"), result)); | ||
assertEquals(true, getValue(result.getClass().getDeclaredField("skipSetAutoCommitOnClose"), result)); | ||
} | ||
|
||
} |
120 changes: 120 additions & 0 deletions
120
src/test/java/org/apache/ibatis/transaction/jdbc/JdbcTransactionWithConnectionTest.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,120 @@ | ||
/* | ||
* 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.apache.ibatis.transaction.Transaction; | ||
import org.apache.ibatis.transaction.TransactionTest; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
|
||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.mockito.Mockito.*; | ||
|
||
/** | ||
* @author <a href="[email protected]">mawen12</a> | ||
* @see JdbcTransaction | ||
*/ | ||
class JdbcTransactionWithConnectionTest extends TransactionTest { | ||
|
||
@Mock | ||
private Connection connection; | ||
|
||
private Transaction transaction; | ||
|
||
@BeforeEach | ||
void setup() { | ||
this.transaction = new JdbcTransaction(connection); | ||
} | ||
|
||
@Test | ||
@Override | ||
public void shouldGetConnection() throws SQLException { | ||
Connection result = transaction.getConnection(); | ||
|
||
assertEquals(connection, result); | ||
} | ||
|
||
@Test | ||
@Override | ||
public void shouldCommit() throws SQLException { | ||
when(connection.getAutoCommit()).thenReturn(false); | ||
|
||
transaction.commit(); | ||
|
||
verify(connection).commit(); | ||
} | ||
|
||
@Test | ||
void shouldAutoCommit() throws SQLException { | ||
when(connection.getAutoCommit()).thenReturn(true); | ||
|
||
transaction.commit(); | ||
|
||
verify(connection, never()).commit(); | ||
} | ||
|
||
@Test | ||
@Override | ||
public void shouldRollback() throws SQLException { | ||
when(connection.getAutoCommit()).thenReturn(false); | ||
|
||
transaction.rollback(); | ||
|
||
verify(connection).rollback(); | ||
} | ||
|
||
@Test | ||
void shouldAutoRollback() throws SQLException { | ||
when(connection.getAutoCommit()).thenReturn(true); | ||
|
||
transaction.rollback(); | ||
|
||
verify(connection, never()).rollback(); | ||
} | ||
|
||
@Test | ||
@Override | ||
public void shouldClose() throws SQLException { | ||
when(connection.getAutoCommit()).thenReturn(false); | ||
|
||
transaction.close(); | ||
|
||
verify(connection).close(); | ||
verify(connection).setAutoCommit(true); | ||
} | ||
|
||
@Test | ||
void shouldCloseWithAutoCommit() throws SQLException { | ||
when(connection.getAutoCommit()).thenReturn(true); | ||
|
||
transaction.close(); | ||
|
||
verify(connection).close(); | ||
verify(connection, never()).setAutoCommit(true); | ||
} | ||
|
||
@Test | ||
@Override | ||
public void shouldGetTimeout() throws SQLException { | ||
assertNull(transaction.getTimeout()); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Imo, using constructs like
assertTrue
, orassertEquals(true, xxx)
does not make it that clear what went wrong initially in test output.It's more a personal thing, but I prefer using assertj, as the failure messages are more descriptive. Not sure what others think.