INTSAMPLES-102 - Advanced-testing-examples - Fix failing test

Reference: https://jira.springsource.org/browse/INTSAMPLES-102
This commit is contained in:
Gunnar Hillert
2012-12-11 23:48:41 -05:00
parent 76bce333da
commit caef9515ee
4 changed files with 121 additions and 113 deletions

View File

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

View File

@@ -1,33 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration"
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
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">
<!--
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
easily testable.
-->
<!-- 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 easily testable. -->
<!-- Throw an exception -->
<int:transformer input-channel="inputChannel" output-channel="outputChannel">
<int-groovy:script><![CDATA[
if (!payload.equals('hello')) {
throw new RuntimeException('invalid payload')
}
payload
]]>
</int-groovy:script>
</int:transformer>
<int:channel id="outputChannel"/>
<!-- Do some error handling -->
<int:transformer input-channel="errorChannel" output-channel="invalidMessageChannel" expression="payload.cause.cause.message"/>
<int:channel id="invalidMessageChannel"/>
<int:transformer input-channel="inputChannel"
output-channel="outputChannel">
<int-groovy:script><![CDATA[
if (!payload.equals('hello')) {
throw new RuntimeException('invalid payload')
}
payload
]]>
</int-groovy:script>
</int:transformer>
<int:channel id="outputChannel" />
<!-- Do some error handling -->
<int:transformer input-channel="errorChannel"
output-channel="invalidMessageChannel" expression="payload.cause.cause.message" />
<int:channel id="invalidMessageChannel" />
</beans>

View File

@@ -6,26 +6,21 @@
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/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
This is a test context that injects a Mock JMS template into a JMS
inbound channel adapter. The inbound-channel-adapter provides or overrides the 'real'
adapter for testing purposes.
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">
<int:poller fixed-rate="500" max-messages-per-poll="1" error-channel="errorChannel"/>
</int-jms:inbound-channel-adapter>
<bean id="jmsTemplate" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="org.springframework.jms.core.JmsTemplate"/>
</bean>
<!-- This is a test context that injects a Mock JMS template into a JMS
inbound channel adapter. The inbound-channel-adapter provides or overrides
the 'real' adapter for testing purposes. 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">
<int:poller fixed-rate="500" max-messages-per-poll="1" error-channel="errorChannel" />
</int-jms:inbound-channel-adapter>
<bean id="jmsTemplate" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="org.springframework.jms.core.JmsTemplate" />
</bean>
</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
* 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.
@@ -25,6 +25,7 @@ import java.util.concurrent.TimeUnit;
import javax.jms.JMSException;
import javax.jms.TextMessage;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@@ -44,6 +45,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ContextConfiguration
public class JmsMockTests {
private static final Logger LOGGER = Logger.getLogger(JmsMockTests.class);
@Autowired
JmsTemplate mockJmsTemplate;
@@ -58,12 +61,11 @@ public class JmsMockTests {
@Autowired
@Qualifier("invalidMessageChannel")
SubscribableChannel invalidMessageChannel;
/**
* 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
*
*
* @throws JMSException
* @throws InterruptedException
* @throws IOException
@@ -71,7 +73,7 @@ public class JmsMockTests {
@Test
public void testReceiveMessage() throws JMSException, InterruptedException, IOException {
String msg = "hello";
boolean sent = verifyJmsMessageReceivedOnOutputChannel(msg, outputChannel,new CountDownHandler() {
@Override
@@ -82,11 +84,11 @@ public class JmsMockTests {
);
assertTrue("message not sent to expected output channel", sent);
}
/**
* 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
*
*
* @throws JMSException
* @throws IOException
* @throws InterruptedException
@@ -100,12 +102,12 @@ public class JmsMockTests {
protected void verifyMessage(Message<?> message) {
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
* @param obj The message provided to the poller (currently must be a String)
@@ -116,16 +118,16 @@ public class JmsMockTests {
* @throws 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 expectedOutputChannel The expected output channel
* @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
* @return true if the message was received on the expected channel
* @throws JMSException
@@ -137,12 +139,12 @@ public class JmsMockTests {
if (!(obj instanceof String)) {
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
* is also a mock.
*/
TextMessage message = mock(TextMessage.class);
doReturn(new SimpleMessageConverter()).when(mockJmsTemplate).getMessageConverter();
doReturn(message).when(mockJmsTemplate).receiveSelected(anyString());
@@ -151,38 +153,43 @@ public class JmsMockTests {
CountDownLatch latch = new CountDownLatch(1);
handler.setLatch(latch);
doReturn(text).when(message).getText();
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 {
CountDownLatch latch;
public final void setLatch(CountDownLatch latch){
this.latch = latch;
}
protected abstract void verifyMessage(Message<?> message);
/*
* (non-Javadoc)
*
*
* @see
* org.springframework.integration.core.MessageHandler#handleMessage
* (org.springframework.integration.Message)
*/
public void handleMessage(Message<?> message) throws MessagingException {
verifyMessage(message);
latch.countDown();
latch.countDown();
}
}
}