-
Notifications
You must be signed in to change notification settings - Fork 13k
Add TransactionFactory, Transaction test cases #3277
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 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 f2a4a09
Merge branch 'master' into feature-transaction-test
mawen12 dcca250
replace wildcard imports with concrete imports
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
This file contains hidden or 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 |
---|---|---|
|
@@ -21,7 +21,6 @@ | |
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.apache.ibatis.domain.blog.Author; | ||
import org.apache.ibatis.domain.misc.RichType; | ||
import org.apache.ibatis.reflection.SystemMetaObject; | ||
import org.apache.ibatis.reflection.property.PropertyTokenizer; | ||
|
@@ -35,7 +34,7 @@ | |
* @author <a href="[email protected]">mawen12</a> | ||
* @see BeanWrapper | ||
*/ | ||
class BeanWrapperUnitTest extends ObjectWrapperBaseTest { | ||
class BeanWrapperUnitTest extends ObjectWrapperBase { | ||
|
||
private RichType richType; | ||
|
||
|
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
|
@@ -38,7 +38,7 @@ | |
* @author <a href="[email protected]">mawen12</a> | ||
* @see MapWrapper | ||
*/ | ||
class MapWrapperUnitTest extends ObjectWrapperBaseTest { | ||
class MapWrapperUnitTest extends ObjectWrapperBase { | ||
|
||
@Mock | ||
private Map<String, Object> map; | ||
|
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
|
@@ -47,7 +47,7 @@ | |
* @author <a href="[email protected]">mawen12</a> | ||
* @see <a href="https://mybatis.org/mybatis-3/dynamic-sql.html#choose-when-otherwise">choose</a> | ||
*/ | ||
class ChooseSqlNodeTest extends SqlNodeTest{ | ||
class ChooseSqlNodeTest extends SqlNodeBase { | ||
|
||
private static final String FIRST_TEXT = " AND title like #{title}"; | ||
private static final String SECOND_TEXT = " AND author_name like #{author.username}"; | ||
|
This file contains hidden or 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 |
---|---|---|
|
@@ -40,7 +40,7 @@ | |
* @author <a href="[email protected]">mawen12</a> | ||
* @see <a href="https://mybatis.org/mybatis-3/dynamic-sql.html#foreach">foreach</a> | ||
*/ | ||
class ForEachSqlNodeTest extends SqlNodeTest{ | ||
class ForEachSqlNodeTest extends SqlNodeBase { | ||
|
||
private SqlNode sqlNode; | ||
|
||
|
This file contains hidden or 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 |
---|---|---|
|
@@ -33,7 +33,7 @@ | |
* @author <a href="[email protected]">mawen12</a> | ||
* @see <a href="https://mybatis.org/mybatis-3/dynamic-sql.html#if">if</a> | ||
*/ | ||
class IfSqlNodeTest extends SqlNodeTest { | ||
class IfSqlNodeTest extends SqlNodeBase { | ||
|
||
private static final String CONDITION = "title != null"; | ||
private static final String TEXT = "AND title like #{title}"; | ||
|
This file contains hidden or 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 |
---|---|---|
|
@@ -25,7 +25,7 @@ | |
/** | ||
* @author <a href="[email protected]">mawen12</a> | ||
*/ | ||
class MixedSqlNodeTest extends SqlNodeTest{ | ||
class MixedSqlNodeTest extends SqlNodeBase { | ||
|
||
private static final String FIRST_TEXT = "abc"; | ||
private static final String SECOND_TEXT = "bcd"; | ||
|
This file contains hidden or 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 |
---|---|---|
|
@@ -41,7 +41,7 @@ | |
* @author <a href="[email protected]">mawen12</a> | ||
* @see <a href="https://mybatis.org/mybatis-3/dynamic-sql.html#trim-where-set">trim-where-set</a> | ||
*/ | ||
class SetSqlNodeTest extends SqlNodeTest { | ||
class SetSqlNodeTest extends SqlNodeBase { | ||
|
||
private static final String FIRST_TEXT = " username = #{username},"; | ||
private static final String SECOND_TEXT = " password = #{password}"; | ||
|
This file contains hidden or 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 |
---|---|---|
|
@@ -22,9 +22,10 @@ | |
|
||
/** | ||
* @author <a href="[email protected]">mawen12</a> | ||
* @see SqlNode | ||
*/ | ||
@ExtendWith(MockitoExtension.class) | ||
abstract class SqlNodeTest { | ||
abstract class SqlNodeBase { | ||
|
||
@Mock | ||
protected Configuration configuration; | ||
|
This file contains hidden or 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 |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
/** | ||
* @author <a href="[email protected]">mawen12</a> | ||
*/ | ||
class StaticTextSqlNodeTest extends SqlNodeTest { | ||
class StaticTextSqlNodeTest extends SqlNodeBase { | ||
|
||
private static final String TEXT = "select 1 from dual"; | ||
|
||
|
This file contains hidden or 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 |
---|---|---|
|
@@ -25,7 +25,7 @@ | |
/** | ||
* @author <a href="[email protected]">mawen12</a> | ||
*/ | ||
class TextSqlNodeTest extends SqlNodeTest { | ||
class TextSqlNodeTest extends SqlNodeBase { | ||
|
||
private static final String TEXT = "select 1 from dual"; | ||
private static final String DYNAMIC_TEXT = "select * from user where id = ${id}"; | ||
|
This file contains hidden or 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 |
---|---|---|
|
@@ -41,7 +41,7 @@ | |
* @author <a href="[email protected]">mawen12</a> | ||
* @see <a href="https://mybatis.org/mybatis-3/dynamic-sql.html#trim-where-set">trim-where-set</a> | ||
*/ | ||
class TrimSqlNodeTest extends SqlNodeTest { | ||
class TrimSqlNodeTest extends SqlNodeBase { | ||
|
||
private static final String FIRST_TEXT = " AND id = #{id}"; | ||
private static final String SECOND_TEXT = " AND name = #{name}"; | ||
|
This file contains hidden or 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 |
---|---|---|
|
@@ -33,7 +33,7 @@ | |
* @author <a href="[email protected]">mawen12</a> | ||
* @see <a href="https://mybatis.org/mybatis-3/dynamic-sql.html#bind">bind</a> | ||
*/ | ||
class VarDeclSqlNodeTest extends SqlNodeTest { | ||
class VarDeclSqlNodeTest extends SqlNodeBase { | ||
|
||
private SqlNode sqlNode; | ||
|
||
|
This file contains hidden or 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 |
---|---|---|
|
@@ -41,7 +41,7 @@ | |
* @author <a href="[email protected]">mawen12</a> | ||
* @see <a href="https://mybatis.org/mybatis-3/dynamic-sql.html#trim-where-set">trim-where-set</a> | ||
*/ | ||
class WhereSqlNodeTest extends SqlNodeTest { | ||
class WhereSqlNodeTest extends SqlNodeBase { | ||
|
||
private static final String FIRST_TEXT = " AND id = #{id}"; | ||
private static final String SECOND_TEXT = " AND name = #{name}"; | ||
|
39 changes: 39 additions & 0 deletions
39
src/test/java/org/apache/ibatis/transaction/TransactionBase.java
This file contains hidden or 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 TransactionBase { | ||
|
||
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; | ||
} |
42 changes: 42 additions & 0 deletions
42
src/test/java/org/apache/ibatis/transaction/TransactionFactoryBase.java
This file contains hidden or 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 TransactionFactoryBase { | ||
|
||
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); | ||
} | ||
|
||
} |
114 changes: 114 additions & 0 deletions
114
src/test/java/org/apache/ibatis/transaction/jdbc/JdbcTransactionFactoryUnitTest.java
This file contains hidden or 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.TransactionFactoryBase; | ||
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 TransactionFactoryBase { | ||
|
||
@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)); | ||
} | ||
|
||
} |
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.
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.
I'm ok with how this is for now. Yes assertj makes it a lot more readable. Possibly we can run open rewrite against this for tests to clean them up. Will check into that separately.