Added tests for transaction configuration within the @Poller annotation when applied to a MessageConsumer (e.g. @ServiceActivator).

This commit is contained in:
Mark Fisher
2008-10-09 20:00:59 +00:00
parent fc6dc7488a
commit 3070a63e39
13 changed files with 174 additions and 15 deletions

View File

@@ -26,10 +26,10 @@ import org.springframework.integration.annotation.ServiceActivator;
* @author Mark Fisher
*/
@MessageEndpoint
public class PollerAnnotationServiceActivator {
public class PollerAnnotationAdviceChainTestBean {
@ServiceActivator(inputChannel="input", outputChannel="output")
@Poller(interval=5000, timeUnit=TimeUnit.SECONDS, maxMessagesPerPoll=1,
@Poller(interval=5, timeUnit=TimeUnit.SECONDS, maxMessagesPerPoll=1,
adviceChain="beforeAdvice ,aroundAdvice, afterAdvice ") // spacing intentional
public String testMethod(String input) {
return input.toUpperCase();

View File

@@ -20,11 +20,7 @@
<queue capacity="1"/>
</channel>
<channel id="inboundChannelAdapterOutput">
<queue capacity="5"/>
</channel>
<beans:bean id="service" class="org.springframework.integration.config.PollerAnnotationServiceActivator"/>
<beans:bean id="service" class="org.springframework.integration.config.PollerAnnotationAdviceChainTestBean"/>
<beans:bean id="latch" class="java.util.concurrent.CountDownLatch">
<beans:constructor-arg value="4"/>

View File

@@ -0,0 +1,27 @@
<?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"
xmlns:stream="http://www.springframework.org/schema/integration/stream"
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-1.0.xsd
http://www.springframework.org/schema/integration/stream
http://www.springframework.org/schema/integration/stream/spring-integration-stream-1.0.xsd">
<message-bus enable-annotations="true"/>
<channel id="input">
<queue capacity="1"/>
</channel>
<channel id="output">
<queue capacity="1"/>
</channel>
<beans:bean id="service" class="org.springframework.integration.config.PollerAnnotationTransactionalTestBean"/>
<beans:bean id="testTransactionManager" class="org.springframework.integration.util.TestTransactionManager"/>
</beans:beans>

View File

@@ -0,0 +1,87 @@
/*
* Copyright 2002-2008 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
*
* http://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.springframework.integration.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.PollableChannel;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.StringMessage;
import org.springframework.integration.util.TestTransactionManager;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.TransactionDefinition;
/**
* @author Mark Fisher
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class PollerAnnotationConsumerTransactionalTests {
@Autowired @Qualifier("input")
private MessageChannel input;
@Autowired @Qualifier("output")
private PollableChannel output;
@Autowired
private TestTransactionManager transactionManager;
@Before
public void resetTransactionManager() {
transactionManager.reset();
}
@Test
public void commit() throws InterruptedException {
input.send(new StringMessage("test"));
transactionManager.waitForCompletion(1000);
Message<?> reply = output.receive(1000);
assertEquals("TEST", reply.getPayload());
assertEquals(1, transactionManager.getCommitCount());
assertEquals(0, transactionManager.getRollbackCount());
}
@Test
public void rollback() throws InterruptedException {
input.send(new StringMessage("bad"));
transactionManager.waitForCompletion(1000);
assertNull(output.receive(0));
assertEquals(0, transactionManager.getCommitCount());
assertEquals(1, transactionManager.getRollbackCount());
}
@Test
public void verifyPropagationSetting() throws InterruptedException {
input.send(new StringMessage("test"));
transactionManager.waitForCompletion(1000);
assertEquals(TransactionDefinition.PROPAGATION_REQUIRES_NEW,
transactionManager.getLastDefinition().getPropagationBehavior());
}
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2002-2008 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
*
* http://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.springframework.integration.config;
import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.Poller;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
/**
* @author Mark Fisher
*/
@MessageEndpoint
public class PollerAnnotationTransactionalTestBean {
@ServiceActivator(inputChannel="input", outputChannel="output")
@Poller(interval=100, maxMessagesPerPoll=1,
transactionManager="testTransactionManager",
transactionAttributes=@Transactional(propagation=Propagation.REQUIRES_NEW))
public String testMethod(String input) {
if ("bad".equals(input)) {
throw new IllegalArgumentException("bad input");
}
return input.toUpperCase();
}
}

View File

@@ -27,6 +27,7 @@ import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.PollableChannel;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.StringMessage;
import org.springframework.integration.util.TestTransactionManager;
import org.springframework.transaction.IllegalTransactionStateException;
import org.springframework.transaction.annotation.Propagation;

View File

@@ -33,6 +33,6 @@
<beans:bean id="testBean" class="org.springframework.integration.dispatcher.TestBean"/>
<beans:bean id="txManager" class="org.springframework.integration.dispatcher.TestTransactionManager"/>
<beans:bean id="txManager" class="org.springframework.integration.util.TestTransactionManager"/>
</beans:beans>

View File

@@ -29,6 +29,6 @@
<beans:bean id="testBean" class="org.springframework.integration.dispatcher.TestBean"/>
<beans:bean id="txManager" class="org.springframework.integration.dispatcher.TestTransactionManager"/>
<beans:bean id="txManager" class="org.springframework.integration.util.TestTransactionManager"/>
</beans:beans>

View File

@@ -29,6 +29,6 @@
<beans:bean id="testBean" class="org.springframework.integration.dispatcher.TestBean"/>
<beans:bean id="txManager" class="org.springframework.integration.dispatcher.TestTransactionManager"/>
<beans:bean id="txManager" class="org.springframework.integration.util.TestTransactionManager"/>
</beans:beans>

View File

@@ -29,6 +29,6 @@
<beans:bean id="testBean" class="org.springframework.integration.dispatcher.TestBean"/>
<beans:bean id="txManager" class="org.springframework.integration.dispatcher.TestTransactionManager"/>
<beans:bean id="txManager" class="org.springframework.integration.util.TestTransactionManager"/>
</beans:beans>

View File

@@ -29,6 +29,6 @@
<beans:bean id="testBean" class="org.springframework.integration.dispatcher.TestBean"/>
<beans:bean id="txManager" class="org.springframework.integration.dispatcher.TestTransactionManager"/>
<beans:bean id="txManager" class="org.springframework.integration.util.TestTransactionManager"/>
</beans:beans>

View File

@@ -41,6 +41,6 @@
<beans:bean id="testBean" class="org.springframework.integration.dispatcher.TestBean"/>
<beans:bean id="txManager" class="org.springframework.integration.dispatcher.TestTransactionManager"/>
<beans:bean id="txManager" class="org.springframework.integration.util.TestTransactionManager"/>
</beans:beans>

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.integration.dispatcher;
package org.springframework.integration.util;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@@ -36,11 +36,17 @@ public class TestTransactionManager extends AbstractPlatformTransactionManager {
private final AtomicInteger rollbackCounter = new AtomicInteger();
private final CountDownLatch latch = new CountDownLatch(1);
private volatile CountDownLatch latch = new CountDownLatch(1);
private volatile TransactionDefinition lastDefinition;
public void reset() {
this.latch = new CountDownLatch(1);
this.commitCounter.set(0);
this.rollbackCounter.set(0);
}
public int getCommitCount() {
return this.commitCounter.get();
}