-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DasTransactional.rollback and DasRunner (#24)
* Add DasTransactional.rollback and DasRunner * scope of junit dependency back to test * Add testNestedTransaction and testNestedTransactionException in DasRunnerTest
- Loading branch information
1 parent
86ba9e8
commit 7ac5831
Showing
10 changed files
with
245 additions
and
56 deletions.
There are no files selected for viewing
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
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
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
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
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
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
32 changes: 32 additions & 0 deletions
32
das-client/src/main/java/com/ppdai/das/core/test/DasRunner.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,32 @@ | ||
package com.ppdai.das.core.test; | ||
|
||
import com.ppdai.das.core.client.DalTransactionManager; | ||
import org.junit.runners.model.InitializationError; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
|
||
/** | ||
* Use this Runner to support declarative rollback in JUnit. | ||
* The DasRunner is-a SpringJUnit4ClassRunner. | ||
* | ||
* sample: | ||
* @Test | ||
* @DasTransactional(logicDbName="MySqlSimple", rollback = true) | ||
* public void test() throws SQLException { | ||
* dao.insert(list); | ||
* } | ||
* | ||
* @see {@link com.ppdai.das.client.annotation.DasTransactional}. | ||
*/ | ||
public class DasRunner extends SpringJUnit4ClassRunner { | ||
public DasRunner(Class<?> clazz) throws InitializationError { | ||
super(clazz); | ||
} | ||
|
||
@Override | ||
protected Object createTest() throws Exception { | ||
Class clz = this.getTestClass().getJavaClass(); | ||
Object testInstance = DalTransactionManager.create(clz); | ||
this.getTestContextManager().prepareTestInstance(testInstance); | ||
return testInstance; | ||
} | ||
} |
106 changes: 54 additions & 52 deletions
106
das-client/src/test/java/com/ppdai/das/client/AllDasClientTests.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 |
---|---|---|
@@ -1,52 +1,54 @@ | ||
package com.ppdai.das.client; | ||
|
||
import com.ppdai.das.client.transaction.normal.BaseDalTransactionalAnnotationTest; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Suite; | ||
import org.junit.runners.Suite.SuiteClasses; | ||
|
||
import com.ppdai.das.client.delegate.datasync.DataSyncConfigurationTest; | ||
import com.ppdai.das.client.delegate.datasync.SequenceGeneratorTest; | ||
import com.ppdai.das.client.sqlbuilder.AllSqlBuilderTests; | ||
import com.ppdai.das.client.sqlbuilder.SqlBuilderSerializeTest; | ||
import com.ppdai.das.configure.SlaveFreshnessScannerMysqlTest; | ||
import com.ppdai.das.core.datasource.RefreshableDataSourceTest; | ||
import com.ppdai.das.core.helper.ShardingManagerTest; | ||
import com.ppdai.das.strategy.AllStrategyTests; | ||
import com.ppdai.das.util.ConvertUtilsTest; | ||
|
||
@RunWith(Suite.class) | ||
@SuiteClasses({ | ||
AllSqlBuilderTests.class, | ||
AllStrategyTests.class, | ||
AllTableDaoTests.class, | ||
|
||
BatchCallBuilderTest.class, | ||
CallBuilderTest.class, | ||
|
||
DasClientTest.class, | ||
DasClientDBTest.class, | ||
DasClientTableTest.class, | ||
DasClientDbTableTest.class, | ||
DasClientDbTableZeroTest.class, | ||
DasClientDiagnoseTest.class, | ||
|
||
SqlBuilderDBShardTest.class, | ||
SqlBuilderTableShardTest.class, | ||
SqlBuilderDbTableShardTest.class, | ||
|
||
DistributedTransactionDbTest.class, | ||
DistributedTransactionTableTest.class, | ||
NestedTransactionTest.class, | ||
BaseDalTransactionalAnnotationTest.class, | ||
|
||
ConvertUtilsTest.class, | ||
DataSyncConfigurationTest.class, | ||
SequenceGeneratorTest.class, | ||
ShardingManagerTest.class, | ||
RefreshableDataSourceTest.class, | ||
SqlBuilderSerializeTest.class, | ||
SlaveFreshnessScannerMysqlTest.class, | ||
}) | ||
public class AllDasClientTests { | ||
} | ||
package com.ppdai.das.client; | ||
|
||
import com.ppdai.das.client.transaction.normal.BaseDalTransactionalAnnotationTest; | ||
import com.ppdai.das.core.test.DasRunnerTest; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Suite; | ||
import org.junit.runners.Suite.SuiteClasses; | ||
|
||
import com.ppdai.das.client.delegate.datasync.DataSyncConfigurationTest; | ||
import com.ppdai.das.client.delegate.datasync.SequenceGeneratorTest; | ||
import com.ppdai.das.client.sqlbuilder.AllSqlBuilderTests; | ||
import com.ppdai.das.client.sqlbuilder.SqlBuilderSerializeTest; | ||
import com.ppdai.das.configure.SlaveFreshnessScannerMysqlTest; | ||
import com.ppdai.das.core.datasource.RefreshableDataSourceTest; | ||
import com.ppdai.das.core.helper.ShardingManagerTest; | ||
import com.ppdai.das.strategy.AllStrategyTests; | ||
import com.ppdai.das.util.ConvertUtilsTest; | ||
|
||
@RunWith(Suite.class) | ||
@SuiteClasses({ | ||
AllSqlBuilderTests.class, | ||
AllStrategyTests.class, | ||
AllTableDaoTests.class, | ||
|
||
BatchCallBuilderTest.class, | ||
CallBuilderTest.class, | ||
|
||
DasClientTest.class, | ||
DasClientDBTest.class, | ||
DasClientTableTest.class, | ||
DasClientDbTableTest.class, | ||
DasClientDbTableZeroTest.class, | ||
DasClientDiagnoseTest.class, | ||
|
||
SqlBuilderDBShardTest.class, | ||
SqlBuilderTableShardTest.class, | ||
SqlBuilderDbTableShardTest.class, | ||
|
||
DasRunnerTest.class, | ||
DistributedTransactionDbTest.class, | ||
DistributedTransactionTableTest.class, | ||
NestedTransactionTest.class, | ||
BaseDalTransactionalAnnotationTest.class, | ||
|
||
ConvertUtilsTest.class, | ||
DataSyncConfigurationTest.class, | ||
SequenceGeneratorTest.class, | ||
ShardingManagerTest.class, | ||
RefreshableDataSourceTest.class, | ||
SqlBuilderSerializeTest.class, | ||
SlaveFreshnessScannerMysqlTest.class, | ||
}) | ||
public class AllDasClientTests { | ||
} |
31 changes: 31 additions & 0 deletions
31
das-client/src/test/java/com/ppdai/das/core/test/DaoBean.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,31 @@ | ||
package com.ppdai.das.core.test; | ||
|
||
import com.ppdai.das.client.DasClient; | ||
import com.ppdai.das.client.DasClientFactory; | ||
import com.ppdai.das.client.Person; | ||
import com.ppdai.das.client.annotation.DasTransactional; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.sql.SQLException; | ||
|
||
@Component | ||
public class DaoBean { | ||
private static final String DB_NAME = "MySqlSimple"; | ||
|
||
@DasTransactional(logicDbName = DB_NAME) | ||
public void commitMethod() throws SQLException { | ||
insert(); | ||
} | ||
|
||
@DasTransactional(logicDbName = DB_NAME, rollback = true) | ||
public void rollbackMethod() throws SQLException { | ||
insert(); | ||
} | ||
|
||
private void insert() throws SQLException { | ||
DasClient dasClient = DasClientFactory.getClient(DB_NAME); | ||
Person p = new Person(); | ||
p.setName("name"); | ||
dasClient.insert(p); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
das-client/src/test/java/com/ppdai/das/core/test/DasRunnerTest.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,76 @@ | ||
package com.ppdai.das.core.test; | ||
|
||
import com.ppdai.das.client.DasClient; | ||
import com.ppdai.das.client.DasClientFactory; | ||
import com.ppdai.das.client.Person; | ||
import com.ppdai.das.client.SqlBuilder; | ||
import com.ppdai.das.client.annotation.DasTransactional; | ||
import com.ppdai.das.client.transaction.DasTransactionalEnabler; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.ContextConfiguration; | ||
|
||
import java.sql.SQLException; | ||
|
||
@RunWith(DasRunner.class) | ||
@ContextConfiguration(classes = {DasTransactionalEnabler.class, DasRunnerTest.class, DaoBean.class}) | ||
@SpringBootTest | ||
public class DasRunnerTest { | ||
|
||
private static final String DB_NAME = "MySqlSimple"; | ||
private DasClient dasClient; | ||
private boolean isRollback = false; | ||
private long count = 0; | ||
|
||
@Autowired | ||
DaoBean daoBean; | ||
|
||
@Before | ||
public void before() throws SQLException { | ||
dasClient = DasClientFactory.getClient(DB_NAME); | ||
count = dasClient.queryObject(SqlBuilder.selectCount().from(Person.PERSON).intoObject()); | ||
} | ||
|
||
@After | ||
public void after() throws SQLException { | ||
long now = dasClient.queryObject(SqlBuilder.selectCount().from(Person.PERSON).intoObject()); | ||
Assert.assertEquals(now, isRollback ? count : count + 1); | ||
} | ||
|
||
@Test | ||
@DasTransactional(logicDbName = DB_NAME, rollback = true) | ||
public void testRollback() throws SQLException { | ||
isRollback = true; | ||
Person p = new Person(); | ||
p.setName("name"); | ||
dasClient.insert(p); | ||
} | ||
|
||
@Test | ||
@DasTransactional(logicDbName = DB_NAME) | ||
public void testCommit() throws SQLException { | ||
isRollback = false; | ||
Person p = new Person(); | ||
p.setName("name"); | ||
dasClient.insert(p); | ||
} | ||
|
||
@Test | ||
@DasTransactional(logicDbName = DB_NAME, rollback = true) | ||
public void testNestedTransaction() throws SQLException { | ||
isRollback = true; | ||
daoBean.commitMethod(); | ||
} | ||
|
||
@Test(expected = SQLException.class) | ||
@DasTransactional(logicDbName = DB_NAME, rollback = false) | ||
public void testNestedTransactionException() throws SQLException { | ||
isRollback = true; | ||
daoBean.rollbackMethod(); | ||
} | ||
} |