Added tests for transaction propagation.
This commit is contained in:
@@ -38,6 +38,8 @@ public class TestTransactionManager extends AbstractPlatformTransactionManager {
|
||||
|
||||
private final CountDownLatch latch = new CountDownLatch(1);
|
||||
|
||||
private volatile TransactionDefinition lastDefinition;
|
||||
|
||||
|
||||
public int getCommitCount() {
|
||||
return this.commitCounter.get();
|
||||
@@ -47,12 +49,17 @@ public class TestTransactionManager extends AbstractPlatformTransactionManager {
|
||||
return this.rollbackCounter.get();
|
||||
}
|
||||
|
||||
public TransactionDefinition getLastDefinition() {
|
||||
return this.lastDefinition;
|
||||
}
|
||||
|
||||
public void waitForCompletion(long timeout) throws InterruptedException {
|
||||
this.latch.await(timeout, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doBegin(Object transaction, TransactionDefinition definition) throws TransactionException {
|
||||
this.lastDefinition = definition;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -24,8 +24,14 @@ import org.junit.Test;
|
||||
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.integration.channel.MessageChannel;
|
||||
import org.springframework.integration.endpoint.MessageEndpoint;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
import org.springframework.transaction.IllegalTransactionStateException;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.support.TransactionCallback;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
@@ -66,4 +72,94 @@ public class TransactionInterceptorTests {
|
||||
assertEquals(1, txManager.getRollbackCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPropagationRequired() {
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"transactionInterceptorPropagationTests.xml", this.getClass());
|
||||
TestTransactionManager txManager = (TestTransactionManager) context.getBean("txManager");
|
||||
final MessageEndpoint endpoint = (MessageEndpoint) context.getBean("required");
|
||||
assertEquals(0, txManager.getCommitCount());
|
||||
endpoint.invoke(new StringMessage("test"));
|
||||
assertEquals(1, txManager.getCommitCount());
|
||||
TestTransactionManager outerTxManager = new TestTransactionManager();
|
||||
TransactionTemplate txTemplate = new TransactionTemplate(outerTxManager);
|
||||
txTemplate.execute(new TransactionCallback() {
|
||||
public Object doInTransaction(TransactionStatus status) {
|
||||
return endpoint.invoke(new StringMessage("test"));
|
||||
}
|
||||
});
|
||||
assertEquals(1, outerTxManager.getCommitCount());
|
||||
assertEquals(2, txManager.getCommitCount());
|
||||
assertEquals(Propagation.REQUIRED.value(), txManager.getLastDefinition().getPropagationBehavior());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPropagationRequiresNew() {
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"transactionInterceptorPropagationTests.xml", this.getClass());
|
||||
TestTransactionManager txManager = (TestTransactionManager) context.getBean("txManager");
|
||||
final MessageEndpoint endpoint = (MessageEndpoint) context.getBean("requiresNew");
|
||||
assertEquals(0, txManager.getCommitCount());
|
||||
endpoint.invoke(new StringMessage("test"));
|
||||
assertEquals(1, txManager.getCommitCount());
|
||||
TestTransactionManager outerTxManager = new TestTransactionManager();
|
||||
TransactionTemplate txTemplate = new TransactionTemplate(outerTxManager);
|
||||
txTemplate.execute(new TransactionCallback() {
|
||||
public Object doInTransaction(TransactionStatus status) {
|
||||
return endpoint.invoke(new StringMessage("test"));
|
||||
}
|
||||
});
|
||||
assertEquals(1, outerTxManager.getCommitCount());
|
||||
assertEquals(2, txManager.getCommitCount());
|
||||
assertEquals(Propagation.REQUIRES_NEW.value(), txManager.getLastDefinition().getPropagationBehavior());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPropagationSupports() {
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"transactionInterceptorPropagationTests.xml", this.getClass());
|
||||
TestTransactionManager txManager = (TestTransactionManager) context.getBean("txManager");
|
||||
final MessageEndpoint endpoint = (MessageEndpoint) context.getBean("supports");
|
||||
assertEquals(0, txManager.getCommitCount());
|
||||
endpoint.invoke(new StringMessage("test"));
|
||||
assertEquals(0, txManager.getCommitCount());
|
||||
TestTransactionManager outerTxManager = new TestTransactionManager();
|
||||
TransactionTemplate txTemplate = new TransactionTemplate(outerTxManager);
|
||||
txTemplate.execute(new TransactionCallback() {
|
||||
public Object doInTransaction(TransactionStatus status) {
|
||||
return endpoint.invoke(new StringMessage("test"));
|
||||
}
|
||||
});
|
||||
assertEquals(0, txManager.getCommitCount());
|
||||
assertEquals(1, outerTxManager.getCommitCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPropagationNotSupported() {
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"transactionInterceptorPropagationTests.xml", this.getClass());
|
||||
TestTransactionManager txManager = (TestTransactionManager) context.getBean("txManager");
|
||||
final MessageEndpoint endpoint = (MessageEndpoint) context.getBean("notSupported");
|
||||
assertEquals(0, txManager.getCommitCount());
|
||||
endpoint.invoke(new StringMessage("test"));
|
||||
assertEquals(0, txManager.getCommitCount());
|
||||
TestTransactionManager outerTxManager = new TestTransactionManager();
|
||||
TransactionTemplate txTemplate = new TransactionTemplate(outerTxManager);
|
||||
txTemplate.execute(new TransactionCallback() {
|
||||
public Object doInTransaction(TransactionStatus status) {
|
||||
return endpoint.invoke(new StringMessage("test"));
|
||||
}
|
||||
});
|
||||
assertEquals(0, txManager.getCommitCount());
|
||||
assertEquals(1, outerTxManager.getCommitCount());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalTransactionStateException.class)
|
||||
public void testPropagationMandatoryCalledWithoutTransaction() {
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"transactionInterceptorPropagationTests.xml", this.getClass());
|
||||
final MessageEndpoint endpoint = (MessageEndpoint) context.getBean("mandatory");
|
||||
endpoint.invoke(new StringMessage("test"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans:beans xmlns="http://www.springframework.org/schema/integration"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
|
||||
http://www.springframework.org/schema/integration
|
||||
http://www.springframework.org/schema/integration/spring-integration-core-1.0.xsd">
|
||||
|
||||
<message-bus/>
|
||||
|
||||
<channel id="input"/>
|
||||
<channel id="output"/>
|
||||
|
||||
<handler-endpoint id="required"
|
||||
input-channel="input"
|
||||
handler="testBean"
|
||||
method="good"
|
||||
output-channel="output">
|
||||
<schedule period="10000"/>
|
||||
<interceptors>
|
||||
<transaction-interceptor transaction-manager="txManager" propagation="REQUIRED"/>
|
||||
</interceptors>
|
||||
</handler-endpoint>
|
||||
|
||||
<handler-endpoint id="requiresNew"
|
||||
input-channel="input"
|
||||
handler="testBean"
|
||||
method="good"
|
||||
output-channel="output">
|
||||
<schedule period="10000"/>
|
||||
<interceptors>
|
||||
<transaction-interceptor transaction-manager="txManager" propagation="REQUIRES_NEW"/>
|
||||
</interceptors>
|
||||
</handler-endpoint>
|
||||
|
||||
<handler-endpoint id="supports"
|
||||
input-channel="input"
|
||||
handler="testBean"
|
||||
method="good"
|
||||
output-channel="output">
|
||||
<schedule period="10000"/>
|
||||
<interceptors>
|
||||
<transaction-interceptor transaction-manager="txManager" propagation="SUPPORTS"/>
|
||||
</interceptors>
|
||||
</handler-endpoint>
|
||||
|
||||
<handler-endpoint id="notSupported"
|
||||
input-channel="input"
|
||||
handler="testBean"
|
||||
method="good"
|
||||
output-channel="output">
|
||||
<schedule period="10000"/>
|
||||
<interceptors>
|
||||
<transaction-interceptor transaction-manager="txManager" propagation="NOT_SUPPORTED"/>
|
||||
</interceptors>
|
||||
</handler-endpoint>
|
||||
|
||||
<handler-endpoint id="mandatory"
|
||||
input-channel="input"
|
||||
handler="testBean"
|
||||
method="good"
|
||||
output-channel="output">
|
||||
<schedule period="10000"/>
|
||||
<interceptors>
|
||||
<transaction-interceptor transaction-manager="txManager" propagation="MANDATORY"/>
|
||||
</interceptors>
|
||||
</handler-endpoint>
|
||||
|
||||
<beans:bean id="testBean" class="org.springframework.integration.endpoint.interceptor.TestBean"/>
|
||||
|
||||
<beans:bean id="txManager" class="org.springframework.integration.endpoint.interceptor.TestTransactionManager"/>
|
||||
|
||||
</beans:beans>
|
||||
Reference in New Issue
Block a user