added advanced-testing-examples
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
<?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:int-jms="http://www.springframework.org/schema/integration/jms"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd
|
||||
http://www.springframework.org/schema/integration/jms http://www.springframework.org/schema/integration/jms/spring-integration-jms-2.0.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>
|
||||
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,188 @@
|
||||
/*
|
||||
* Copyright 2002-2011 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.samples.advance.testing.jms;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.jms.JMSException;
|
||||
import javax.jms.TextMessage;
|
||||
|
||||
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.Message;
|
||||
import org.springframework.integration.MessageChannel;
|
||||
import org.springframework.integration.MessagingException;
|
||||
import org.springframework.integration.core.MessageHandler;
|
||||
import org.springframework.integration.core.SubscribableChannel;
|
||||
|
||||
import org.springframework.jms.core.JmsTemplate;
|
||||
import org.springframework.jms.support.converter.SimpleMessageConverter;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration
|
||||
public class JmsMockTests {
|
||||
|
||||
@Autowired
|
||||
JmsTemplate mockJmsTemplate;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("inputChannel")
|
||||
MessageChannel inputChannel;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("outputChannel")
|
||||
SubscribableChannel outputChannel;
|
||||
|
||||
@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
|
||||
*/
|
||||
@Test
|
||||
public void testReceiveMessage() throws JMSException, InterruptedException, IOException {
|
||||
String msg = "hello";
|
||||
|
||||
boolean sent = verifyJmsMessageReceivedOnOutputChannel(msg, outputChannel,new CountDownHandler() {
|
||||
|
||||
@Override
|
||||
protected void verifyMessage(Message<?> message) {
|
||||
assertEquals("hello",message.getPayload());
|
||||
}
|
||||
}
|
||||
);
|
||||
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
|
||||
*/
|
||||
@Test
|
||||
public void testReceiveInvalidMessage() throws JMSException, IOException, InterruptedException {
|
||||
String msg = "whoops";
|
||||
boolean sent = verifyJmsMessageReceivedOnOutputChannel(msg, invalidMessageChannel,new CountDownHandler() {
|
||||
|
||||
@Override
|
||||
protected void verifyMessage(Message<?> message) {
|
||||
assertEquals("invalid payload",message.getPayload());
|
||||
}
|
||||
|
||||
}
|
||||
);
|
||||
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)
|
||||
* @param expectedOutputChannel The expected output channel
|
||||
* @param handler An instance of CountDownHandler to handle (verify) the output message
|
||||
* @return true if the message was received on the expected channel
|
||||
* @throws JMSException
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
protected boolean verifyJmsMessageReceivedOnOutputChannel(Object obj, SubscribableChannel expectedOutputChannel, CountDownHandler handler) throws JMSException, InterruptedException{
|
||||
return verifyJmsMessageOnOutputChannel(obj, expectedOutputChannel, handler, 5000);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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
|
||||
* not long enough
|
||||
* @return true if the message was received on the expected channel
|
||||
* @throws JMSException
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
protected boolean verifyJmsMessageOnOutputChannel(Object obj, SubscribableChannel expectedOutputChannel, CountDownHandler handler,int timeoutMillisec) throws JMSException,
|
||||
InterruptedException {
|
||||
|
||||
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());
|
||||
|
||||
String text = (String) obj;
|
||||
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
handler.setLatch(latch);
|
||||
|
||||
doReturn(text).when(message).getText();
|
||||
|
||||
expectedOutputChannel.subscribe(handler);
|
||||
|
||||
return latch.await(timeoutMillisec, TimeUnit.MILLISECONDS);
|
||||
|
||||
|
||||
}
|
||||
/*
|
||||
* A MessageHandler that uses a CountDownLatch to syncronize 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
|
||||
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
|
||||
|
||||
<!-- Appenders -->
|
||||
<appender name="console" class="org.apache.log4j.ConsoleAppender">
|
||||
<param name="Target" value="System.out" />
|
||||
<layout class="org.apache.log4j.PatternLayout">
|
||||
<param name="ConversionPattern" value="[%t] %-5p: %c - %m%n" />
|
||||
</layout>
|
||||
</appender>
|
||||
|
||||
<!-- Loggers -->
|
||||
<logger name="org.springframework.integration">
|
||||
<level value="info" />
|
||||
</logger>
|
||||
|
||||
<!-- Root Logger -->
|
||||
<root>
|
||||
<priority value="info" />
|
||||
<appender-ref ref="console" />
|
||||
</root>
|
||||
|
||||
</log4j:configuration>
|
||||
Reference in New Issue
Block a user