Merge pull request #82 from ghillert/INTSAMPLES-102

INTSAMPLES-102 - Advanced-testing-examples - Fix failing test
This commit is contained in:
Gunnar Hillert
2012-12-11 20:51:13 -08:00
4 changed files with 121 additions and 113 deletions

View File

@@ -7,12 +7,17 @@
<version>2.2.0.BUILD-SNAPSHOT</version> <version>2.2.0.BUILD-SNAPSHOT</version>
<name>Samples (Advanced) - Advanced Testing Examples</name> <name>Samples (Advanced) - Advanced Testing Examples</name>
<packaging>jar</packaging> <packaging>jar</packaging>
<prerequisites>
<maven>2.2.1</maven>
</prerequisites>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.integration.version>2.2.0.RELEASE</spring.integration.version> <spring.integration.version>2.2.0.RELEASE</spring.integration.version>
<spring.version>3.1.3.RELEASE</spring.version> <spring.version>3.1.3.RELEASE</spring.version>
<log4j.version>1.2.16</log4j.version> <log4j.version>1.2.17</log4j.version>
<junit.version>4.10</junit.version> <junit.version>4.11</junit.version>
</properties> </properties>
<dependencies> <dependencies>
<dependency> <dependency>
@@ -51,60 +56,62 @@
<version>${spring.version}</version> <version>${spring.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.geronimo.specs</groupId> <groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_1.1_spec</artifactId> <artifactId>geronimo-jms_1.1_spec</artifactId>
<version>1.1.1</version> <version>1.1.1</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.mockito</groupId> <groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId> <artifactId>mockito-all</artifactId>
<version>1.8.5</version> <version>1.9.5</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
<testResources> <testResources>
<testResource> <testResource>
<directory>src/test/java</directory> <directory>src/test/java</directory>
<includes> <includes>
<include>**/*</include> <include>**/*</include>
</includes> </includes>
<excludes> <excludes>
<exclude>**/*.java</exclude> <exclude>**/*.java</exclude>
</excludes> </excludes>
</testResource> </testResource>
<testResource> <testResource>
<directory>src/test/resources</directory> <directory>src/test/resources</directory>
<includes> <includes>
<include>**/*</include> <include>**/*</include>
</includes> </includes>
</testResource> </testResource>
</testResources> </testResources>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version> <version>3.0</version>
<configuration> <configuration>
<source>1.5</source> <source>1.6</source>
<target>1.5</target> <target>1.6</target>
<compilerArgument>-Xlint:all</compilerArgument> <compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings> <showWarnings>true</showWarnings>
<showDeprecation>false</showDeprecation> <showDeprecation>true</showDeprecation>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>
<artifactId>maven-surefire-plugin</artifactId> <groupId>org.apache.maven.plugins</groupId>
<configuration> <artifactId>maven-surefire-plugin</artifactId>
<includes> <version>2.12.4</version>
<include>**/*Tests.java</include> <configuration>
</includes> <includes>
</configuration> <include>**/*Tests.java</include>
</includes>
</configuration>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>

View File

@@ -1,33 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-groovy="http://www.springframework.org/schema/integration/groovy" xmlns:int-groovy="http://www.springframework.org/schema/integration/groovy"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/groovy http://www.springframework.org/schema/integration/groovy/spring-integration-groovy.xsd http://www.springframework.org/schema/integration/groovy http://www.springframework.org/schema/integration/groovy/spring-integration-groovy.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
This implements the core business logic of the integration flow. Not <!-- This implements the core business logic of the integration flow. Not
binding input and output channels to JMS (or any other protocol) within makes this flow more binding input and output channels to JMS (or any other protocol) within makes
easily testable. this flow more easily testable. -->
-->
<!-- Throw an exception --> <!-- Throw an exception -->
<int:transformer input-channel="inputChannel" output-channel="outputChannel"> <int:transformer input-channel="inputChannel"
<int-groovy:script><![CDATA[ output-channel="outputChannel">
if (!payload.equals('hello')) { <int-groovy:script><![CDATA[
throw new RuntimeException('invalid payload') if (!payload.equals('hello')) {
} throw new RuntimeException('invalid payload')
payload }
]]> payload
</int-groovy:script> ]]>
</int:transformer> </int-groovy:script>
<int:channel id="outputChannel"/> </int:transformer>
<int:channel id="outputChannel" />
<!-- Do some error handling -->
<int:transformer input-channel="errorChannel" output-channel="invalidMessageChannel" expression="payload.cause.cause.message"/> <!-- Do some error handling -->
<int:transformer input-channel="errorChannel"
<int:channel id="invalidMessageChannel"/> output-channel="invalidMessageChannel" expression="payload.cause.cause.message" />
<int:channel id="invalidMessageChannel" />
</beans> </beans>

View File

@@ -6,26 +6,21 @@
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/jms http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd http://www.springframework.org/schema/integration/jms http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- <!-- This is a test context that injects a Mock JMS template into a JMS
This is a test context that injects a Mock JMS template into a JMS inbound channel adapter. The inbound-channel-adapter provides or overrides
inbound channel adapter. The inbound-channel-adapter provides or overrides the 'real' the 'real' adapter for testing purposes. If the flow imported flow throws
adapter for testing purposes. an exception, an ErrorMessage containg the exception will be sent to the
error channel for additional handling. -->
If the flow imported flow throws an exception, an ErrorMessage containg the exception will be sent
to the error channel for additional handling. <import resource="classpath:integration-config.xml" />
-->
<int-jms:inbound-channel-adapter jms-template="jmsTemplate" channel="inputChannel">
<import resource="classpath:integration-config.xml"/> <int:poller fixed-rate="500" max-messages-per-poll="1" error-channel="errorChannel" />
</int-jms:inbound-channel-adapter>
<int-jms:inbound-channel-adapter jms-template="jmsTemplate"
channel="inputChannel"> <bean id="jmsTemplate" class="org.mockito.Mockito" factory-method="mock">
<int:poller fixed-rate="500" max-messages-per-poll="1" error-channel="errorChannel"/> <constructor-arg value="org.springframework.jms.core.JmsTemplate" />
</int-jms:inbound-channel-adapter> </bean>
<bean id="jmsTemplate" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="org.springframework.jms.core.JmsTemplate"/>
</bean>
</beans> </beans>

View File

@@ -1,11 +1,11 @@
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2012 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 * 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 * the License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * 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 * 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 * 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. * specific language governing permissions and limitations under the License.
@@ -25,6 +25,7 @@ import java.util.concurrent.TimeUnit;
import javax.jms.JMSException; import javax.jms.JMSException;
import javax.jms.TextMessage; import javax.jms.TextMessage;
import org.apache.log4j.Logger;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -44,6 +45,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ContextConfiguration @ContextConfiguration
public class JmsMockTests { public class JmsMockTests {
private static final Logger LOGGER = Logger.getLogger(JmsMockTests.class);
@Autowired @Autowired
JmsTemplate mockJmsTemplate; JmsTemplate mockJmsTemplate;
@@ -58,12 +61,11 @@ public class JmsMockTests {
@Autowired @Autowired
@Qualifier("invalidMessageChannel") @Qualifier("invalidMessageChannel")
SubscribableChannel invalidMessageChannel; SubscribableChannel invalidMessageChannel;
/** /**
* This test verifies that a message received on a polling JMS inbound channel adapter is * This test verifies that a message received on a polling JMS inbound channel adapter is
* routed to the designated channel and that the message payload is as expected * routed to the designated channel and that the message payload is as expected
* *
* @throws JMSException * @throws JMSException
* @throws InterruptedException * @throws InterruptedException
* @throws IOException * @throws IOException
@@ -71,7 +73,7 @@ public class JmsMockTests {
@Test @Test
public void testReceiveMessage() throws JMSException, InterruptedException, IOException { public void testReceiveMessage() throws JMSException, InterruptedException, IOException {
String msg = "hello"; String msg = "hello";
boolean sent = verifyJmsMessageReceivedOnOutputChannel(msg, outputChannel,new CountDownHandler() { boolean sent = verifyJmsMessageReceivedOnOutputChannel(msg, outputChannel,new CountDownHandler() {
@Override @Override
@@ -82,11 +84,11 @@ public class JmsMockTests {
); );
assertTrue("message not sent to expected output channel", sent); assertTrue("message not sent to expected output channel", sent);
} }
/** /**
* This test verifies that a message received on a polling JMS inbound channel adapter is * This test verifies that a message received on a polling JMS inbound channel adapter is
* routed to the errorChannel and that the message payload is the expected exception * routed to the errorChannel and that the message payload is the expected exception
* *
* @throws JMSException * @throws JMSException
* @throws IOException * @throws IOException
* @throws InterruptedException * @throws InterruptedException
@@ -100,12 +102,12 @@ public class JmsMockTests {
protected void verifyMessage(Message<?> message) { protected void verifyMessage(Message<?> message) {
assertEquals("invalid payload",message.getPayload()); assertEquals("invalid payload",message.getPayload());
} }
} }
); );
assertTrue("message not sent to expected output channel", sent); assertTrue("message not sent to expected output channel", sent);
} }
/** /**
* Provide a message via a mock JMS template and wait for the default timeout to receive the message on the expected channel * Provide a message via a mock JMS template and wait for the default timeout to receive the message on the expected channel
* @param obj The message provided to the poller (currently must be a String) * @param obj The message provided to the poller (currently must be a String)
@@ -116,16 +118,16 @@ public class JmsMockTests {
* @throws InterruptedException * @throws InterruptedException
*/ */
protected boolean verifyJmsMessageReceivedOnOutputChannel(Object obj, SubscribableChannel expectedOutputChannel, CountDownHandler handler) throws JMSException, InterruptedException{ protected boolean verifyJmsMessageReceivedOnOutputChannel(Object obj, SubscribableChannel expectedOutputChannel, CountDownHandler handler) throws JMSException, InterruptedException{
return verifyJmsMessageOnOutputChannel(obj, expectedOutputChannel, handler, 5000); return verifyJmsMessageOnOutputChannel(obj, expectedOutputChannel, handler, 7000);
} }
/** /**
* Provide a message via a mock JMS template and wait for the specified timeout to receive the message on the expected channel * Provide a message via a mock JMS template and wait for the specified timeout to receive the message on the expected channel
* @param obj The message provided to the poller (currently must be a String) * @param obj The message provided to the poller (currently must be a String)
* @param expectedOutputChannel The expected output channel * @param expectedOutputChannel The expected output channel
* @param handler An instance of CountDownHandler to handle (verify) the output message * @param handler An instance of CountDownHandler to handle (verify) the output message
* @param timeoutMillisec The timeout period. Note that this must allow at least enough time to process the entire flow. Only set if the default is * @param timeoutMillisec The timeout period. Note that this must allow at least enough time to process the entire flow. Only set if the default is
* not long enough * not long enough
* @return true if the message was received on the expected channel * @return true if the message was received on the expected channel
* @throws JMSException * @throws JMSException
@@ -137,12 +139,12 @@ public class JmsMockTests {
if (!(obj instanceof String)) { if (!(obj instanceof String)) {
throw new IllegalArgumentException("Only TextMessage is currently supported"); throw new IllegalArgumentException("Only TextMessage is currently supported");
} }
/* /*
* Use mocks to create a message returned to the JMS inbound adapter. It is assumed that the JmsTemplate * Use mocks to create a message returned to the JMS inbound adapter. It is assumed that the JmsTemplate
* is also a mock. * is also a mock.
*/ */
TextMessage message = mock(TextMessage.class); TextMessage message = mock(TextMessage.class);
doReturn(new SimpleMessageConverter()).when(mockJmsTemplate).getMessageConverter(); doReturn(new SimpleMessageConverter()).when(mockJmsTemplate).getMessageConverter();
doReturn(message).when(mockJmsTemplate).receiveSelected(anyString()); doReturn(message).when(mockJmsTemplate).receiveSelected(anyString());
@@ -151,38 +153,43 @@ public class JmsMockTests {
CountDownLatch latch = new CountDownLatch(1); CountDownLatch latch = new CountDownLatch(1);
handler.setLatch(latch); handler.setLatch(latch);
doReturn(text).when(message).getText(); doReturn(text).when(message).getText();
expectedOutputChannel.subscribe(handler); expectedOutputChannel.subscribe(handler);
return latch.await(timeoutMillisec, TimeUnit.MILLISECONDS); boolean latchCountedToZero = latch.await(timeoutMillisec, TimeUnit.MILLISECONDS);
if (!latchCountedToZero) {
LOGGER.warn(String.format("The specified waiting time of the latch (%s ms) elapsed.", timeoutMillisec));
}
return latchCountedToZero;
} }
/* /*
* A MessageHandler that uses a CountDownLatch to syncronize with the calling thread * A MessageHandler that uses a CountDownLatch to synchronize with the calling thread
*/ */
private abstract class CountDownHandler implements MessageHandler { private abstract class CountDownHandler implements MessageHandler {
CountDownLatch latch; CountDownLatch latch;
public final void setLatch(CountDownLatch latch){ public final void setLatch(CountDownLatch latch){
this.latch = latch; this.latch = latch;
} }
protected abstract void verifyMessage(Message<?> message); protected abstract void verifyMessage(Message<?> message);
/* /*
* (non-Javadoc) * (non-Javadoc)
* *
* @see * @see
* org.springframework.integration.core.MessageHandler#handleMessage * org.springframework.integration.core.MessageHandler#handleMessage
* (org.springframework.integration.Message) * (org.springframework.integration.Message)
*/ */
public void handleMessage(Message<?> message) throws MessagingException { public void handleMessage(Message<?> message) throws MessagingException {
verifyMessage(message); verifyMessage(message);
latch.countDown(); latch.countDown();
} }
} }
} }