INT-1257, changed convinience method to MessageHistory.containsComponent(string), added/modified tests in XML, WS, JMS to validate MessageHistory

This commit is contained in:
Oleg Zhurakousky
2010-09-21 17:21:50 -04:00
parent f8b501e4f9
commit 0d387756b3
14 changed files with 142 additions and 27 deletions

View File

@@ -88,7 +88,7 @@ public class MessageHistory implements List<Properties> {
return this.components.contains(o);
}
public boolean containsEntryWithName(String name) {
public boolean containsComponent(String name) {
for (Properties properties : components) {
if (properties.getProperty("name").equals(name)){
return true;

View File

@@ -16,6 +16,7 @@
package org.springframework.integration.jms.config;
import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@@ -27,6 +28,7 @@ import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.Message;
import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.history.MessageHistory;
/**
* @author Mark Fisher
@@ -41,6 +43,8 @@ public class JmsInboundChannelAdapterParserTests {
"jmsInboundWithJmsTemplate.xml", this.getClass());
PollableChannel output = (PollableChannel) context.getBean("output");
Message<?> message = output.receive(timeoutOnReceive);
MessageHistory history = MessageHistory.read(message);
assertTrue(history.containsComponent("inboundAdapter"));
assertNotNull("message should not be null", message);
assertEquals("polling-test", message.getPayload());
}

View File

@@ -30,6 +30,8 @@ import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.Message;
import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.history.HistoryWritingMessagePostProcessor;
import org.springframework.integration.history.MessageHistory;
import org.springframework.integration.jms.JmsMessageDrivenEndpoint;
import org.springframework.jms.connection.JmsTransactionManager;
import org.springframework.jms.listener.AbstractMessageListenerContainer;
@@ -49,6 +51,8 @@ public class JmsInboundGatewayParserTests {
assertEquals(JmsMessageDrivenEndpoint.class, gateway.getClass());
context.start();
Message<?> message = channel.receive(3000);
MessageHistory history = MessageHistory.read(message);
assertTrue(history.containsComponent("jmsGateway"));
assertNotNull("message should not be null", message);
assertEquals("message-driven-test", message.getPayload());
context.stop();

View File

@@ -16,6 +16,7 @@
package org.springframework.integration.jms.config;
import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@@ -25,6 +26,7 @@ import org.springframework.beans.DirectFieldAccessor;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.Message;
import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.history.MessageHistory;
import org.springframework.integration.jms.JmsMessageDrivenEndpoint;
import org.springframework.jms.support.destination.JmsDestinationAccessor;
@@ -41,6 +43,8 @@ public class JmsMessageDrivenChannelAdapterParserTests {
"jmsInboundWithMessageSelector.xml", this.getClass());
PollableChannel output = (PollableChannel) context.getBean("output2");
Message<?> message = output.receive(timeoutOnReceive);
MessageHistory history = MessageHistory.read(message);
assertTrue(history.containsComponent("messageDrivenAdapter"));
assertNotNull("message should not be null", message);
assertEquals("test [with selector: TestProperty = 'foo']", message.getPayload());
}

View File

@@ -17,12 +17,23 @@ package org.springframework.integration.jms.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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.MessagingTemplate;
import org.springframework.integration.core.SubscribableChannel;
import org.springframework.integration.endpoint.EventDrivenConsumer;
import org.springframework.integration.endpoint.PollingConsumer;
import org.springframework.integration.history.MessageHistory;
import org.springframework.integration.jms.JmsOutboundGateway;
import org.springframework.integration.jms.StubMessageConverter;
import org.springframework.jms.support.converter.MessageConverter;
@@ -57,16 +68,26 @@ public class JmsOutboundGatewayParserTests {
}
@Test
public void gatewayMaintainsReplyChannel() {
public void gatewayMaintainsReplyChannelAndInboundHistory() {
ActiveMqTestUtils.prepare();
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"gatewayMaintainsReplyChannel.xml", this.getClass());
SampleGateway gateway = context.getBean("gateway", SampleGateway.class);
SubscribableChannel jmsInput = context.getBean("jmsInput", SubscribableChannel.class);
MessageHandler handler = new MessageHandler() {
public void handleMessage(Message<?> message) throws MessagingException {
MessageHistory history = MessageHistory.read(message);
assertTrue(history.containsComponent("inboundGateway"));
new MessagingTemplate((MessageChannel) message.getHeaders().getReplyChannel()).send(message);
}
};
handler = spy(handler);
jmsInput.subscribe(handler);
String result = gateway.echo("hello");
assertEquals("HELLO", result);
verify(handler, times(1)).handleMessage(Mockito.any(Message.class));
assertEquals("hello", result);
}
public static interface SampleGateway{
public String echo(String value);
}

View File

@@ -6,6 +6,8 @@
http://www.springframework.org/schema/integration/jms http://www.springframework.org/schema/integration/jms/spring-integration-jms-2.0.xsd"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-jms="http://www.springframework.org/schema/integration/jms">
<int:message-history/>
<int:gateway id="gateway"
service-interface="org.springframework.integration.jms.config.JmsOutboundGatewayParserTests$SampleGateway"
@@ -13,14 +15,17 @@
<int:method name="echo">
<int:header name="foo" value="bar"/>
</int:method>
<int:method name="echoAsMaessage">
<int:header name="foo" value="bar"/>
</int:method>
</int:gateway>
<int:chain input-channel="requests">
<int-jms:outbound-gateway request-destination="requestQueueA"
reply-destination="replyQueueB"
connection-factory="connectionFactory"
receive-timeout="10000"
reply-timeout="20000"/>
receive-timeout="100000"
reply-timeout="200000"/>
</int:chain>
@@ -44,11 +49,11 @@
<property name="cacheProducers" value="false"/>
</bean>
<int-jms:inbound-gateway request-channel="jmsInput" request-destination="requestQueueA"/>
<int-jms:inbound-gateway id="inboundGateway" request-channel="jmsInput" request-destination="requestQueueA"/>
<int:channel id="jmsInput"/>
<int:service-activator input-channel="jmsInput">
<bean class="org.springframework.integration.jms.config.JmsOutboundGatewayParserTests$SampleService"/>
</int:service-activator>
<!-- <int:service-activator input-channel="jmsInput">-->
<!-- <bean class="org.springframework.integration.jms.config.JmsOutboundGatewayParserTests$SampleService"/>-->
<!-- </int:service-activator>-->
</beans>

View File

@@ -10,6 +10,8 @@
http://www.springframework.org/schema/integration/jms
http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd">
<si:message-history/>
<si:channel id="requestChannel">
<si:queue/>
</si:channel>

View File

@@ -10,7 +10,9 @@
http://www.springframework.org/schema/integration/jms
http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd">
<jms:inbound-channel-adapter jms-template="jmsTemplate" channel="output"/>
<integration:message-history/>
<jms:inbound-channel-adapter id="inboundAdapter" jms-template="jmsTemplate" channel="output"/>
<integration:channel id="output">
<integration:queue capacity="1"/>

View File

@@ -10,6 +10,8 @@
http://www.springframework.org/schema/integration/jms
http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd">
<si:message-history/>
<si:channel id="output1">
<si:queue capacity="1"/>
</si:channel>

View File

@@ -27,6 +27,7 @@ import org.springframework.expression.ExpressionException;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.MessagingException;
import org.springframework.integration.gateway.MessagingGatewaySupport;
import org.springframework.integration.history.TrackableComponent;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
import org.springframework.scheduling.TaskScheduler;
@@ -37,13 +38,12 @@ import org.springframework.ws.server.endpoint.AbstractMarshallingPayloadEndpoint
* @since 1.0.2
*/
public class MarshallingWebServiceInboundGateway extends AbstractMarshallingPayloadEndpoint
implements BeanNameAware, BeanFactoryAware, InitializingBean, SmartLifecycle {
implements BeanNameAware, BeanFactoryAware, InitializingBean, SmartLifecycle, TrackableComponent {
private final ReentrantLock lifecycleLock = new ReentrantLock();
private final GatewayDelegate gatewayDelegate = new GatewayDelegate();
/**
* Creates a new <code>MarshallingWebServiceInboundGateway</code>.
* The {@link Marshaller} and {@link Unmarshaller} must be injected using properties.
@@ -196,4 +196,15 @@ public class MarshallingWebServiceInboundGateway extends AbstractMarshallingPayl
}
}
public String getComponentName() {
return this.gatewayDelegate.getComponentName();
}
public String getComponentType() {
return this.gatewayDelegate.getComponentType();
}
public void setShouldTrack(boolean shouldTrack) {
this.gatewayDelegate.setShouldTrack(shouldTrack);
}
}

View File

@@ -16,9 +16,14 @@
package org.springframework.integration.ws.config;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.transform.Source;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.WebServiceMessageFactory;
@@ -28,7 +33,10 @@ import org.springframework.ws.WebServiceMessageFactory;
public class StubMessageFactory implements WebServiceMessageFactory {
public WebServiceMessage createWebServiceMessage() {
return null;
WebServiceMessage message = mock(WebServiceMessage.class);
Source source = mock(Source.class);
when(message.getPayloadSource()).thenReturn(source);
return message;
}
public WebServiceMessage createWebServiceMessage(InputStream inputStream) throws IOException {

View File

@@ -11,15 +11,25 @@
http://www.springframework.org/schema/integration/ws/spring-integration-ws.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<si:message-history/>
<ws:inbound-gateway id="simple" request-channel="requests" />
<ws:inbound-gateway id="simple" request-channel="requestsVerySimple" />
<ws:inbound-gateway id="extractsPayload" request-channel="requests" extract-payload="false"/>
<si:channel id="requestsVerySimple"></si:channel>
<ws:inbound-gateway id="marshalling" request-channel="requests"
<ws:inbound-gateway id="extractsPayload" request-channel="requestsSimple" extract-payload="false"/>
<ws:inbound-gateway id="marshalling" request-channel="requestsMarshalling"
marshaller="marshaller" unmarshaller="marshaller" />
<si:channel id="requests" />
<si:channel id="requestsMarshalling">
<si:queue/>
</si:channel>
<si:channel id="requestsSimple">
<si:queue/>
</si:channel>
<bean id="marshaller" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="org.springframework.oxm.AbstractMarshaller" />

View File

@@ -15,25 +15,39 @@
*/
package org.springframework.integration.ws.config;
import static junit.framework.Assert.assertTrue;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static junit.framework.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import javax.xml.transform.Source;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.beans.DirectFieldAccessor;
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.core.PollableChannel;
import org.springframework.integration.history.MessageHistory;
import org.springframework.integration.ws.MarshallingWebServiceInboundGateway;
import org.springframework.integration.ws.SimpleWebServiceInboundGateway;
import org.springframework.oxm.AbstractMarshaller;
import org.springframework.oxm.Unmarshaller;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.ws.context.DefaultMessageContext;
import org.springframework.ws.context.MessageContext;
/**
*
* @author Iwein Fuld
* @author Oleg Zhurakousky
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@@ -41,8 +55,16 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
public class WebServiceInboundGatewayParserTests {
@Autowired
@Qualifier("requests")
MessageChannel requestChannel;
@Qualifier("requestsMarshalling")
PollableChannel requestsMarshalling;
@Autowired
@Qualifier("requestsSimple")
PollableChannel requestsSimple;
@Autowired
@Qualifier("requestsVerySimple")
MessageChannel requestsVerySimple;
@Test
public void configOk() throws Exception {
@@ -59,7 +81,7 @@ public class WebServiceInboundGatewayParserTests {
DirectFieldAccessor accessor = new DirectFieldAccessor(simpleGateway);
assertThat(
(MessageChannel) accessor.getPropertyValue("requestChannel"),
is(requestChannel));
is(requestsVerySimple));
}
//extractPayload = false
@@ -78,6 +100,7 @@ public class WebServiceInboundGatewayParserTests {
//marshalling
@Autowired
MarshallingWebServiceInboundGateway marshallingGateway;
@Autowired
AbstractMarshaller marshaller;
@@ -90,4 +113,23 @@ public class WebServiceInboundGatewayParserTests {
is(marshaller));
assertTrue("messaging gateway is not running", marshallingGateway.isRunning());
}
@Test
public void testMessageHistoryWithMarshallingGateway() throws Exception {
MessageContext context = new DefaultMessageContext(new StubMessageFactory());
Unmarshaller unmarshaller = mock(Unmarshaller.class);
when(unmarshaller.unmarshal((Source)Mockito.any())).thenReturn("hello");
marshallingGateway.setUnmarshaller(unmarshaller);
marshallingGateway.invoke(context);
Message<?> message = requestsMarshalling.receive(100);
MessageHistory history = MessageHistory.read(message);
assertTrue(history.containsComponent("marshalling"));
}
@Test
public void testMessageHistoryWithSimpleGateway() throws Exception {
MessageContext context = new DefaultMessageContext(new StubMessageFactory());
payloadExtractingGateway.invoke(context);
Message<?> message = requestsSimple.receive(100);
MessageHistory history = MessageHistory.read(message);
assertTrue(history.containsComponent("extractsPayload"));
}
}

View File

@@ -56,7 +56,7 @@ public class XsltTransformerTests {
input.send(message);
Message<?> resultMessage = output.receive();
MessageHistory history = MessageHistory.read(resultMessage);
assertTrue(history.containsEntryWithName("paramHeadersWithStartWildCharacter"));
assertTrue(history.containsComponent("paramHeadersWithStartWildCharacter"));
assertEquals("Wrong payload type",String.class, resultMessage.getPayload().getClass());
assertTrue(((String) resultMessage.getPayload()).contains("testParamValue"));
assertFalse(((String) resultMessage.getPayload()).contains("FOO"));
@@ -71,7 +71,7 @@ public class XsltTransformerTests {
input.send(message);
Message<?> resultMessage = output.receive();
MessageHistory history = MessageHistory.read(resultMessage);
assertTrue(history.containsEntryWithName("paramHeadersWithEndWildCharacter"));
assertTrue(history.containsComponent("paramHeadersWithEndWildCharacter"));
assertEquals("Wrong payload type",String.class, resultMessage.getPayload().getClass());
assertTrue(((String) resultMessage.getPayload()).contains("testParamValue"));
assertTrue(((String) resultMessage.getPayload()).contains("FOO"));
@@ -86,7 +86,7 @@ public class XsltTransformerTests {
input.send(message);
Message<?> resultMessage = output.receive();
MessageHistory history = MessageHistory.read(resultMessage);
assertTrue(history.containsEntryWithName("paramHeadersWithIndividualParameters"));
assertTrue(history.containsComponent("paramHeadersWithIndividualParameters"));
assertEquals("Wrong payload type",String.class, resultMessage.getPayload().getClass());
assertTrue(((String) resultMessage.getPayload()).contains("testParamValue"));
assertTrue(((String) resultMessage.getPayload()).contains("FOO"));
@@ -103,7 +103,7 @@ public class XsltTransformerTests {
input.send(message);
Message<?> resultMessage = output.receive();
MessageHistory history = MessageHistory.read(resultMessage);
assertTrue(history.containsEntryWithName("paramHeadersComboChannel"));
assertTrue(history.containsComponent("paramHeadersComboChannel"));
assertEquals("Wrong payload type",String.class, resultMessage.getPayload().getClass());
assertTrue(((String) resultMessage.getPayload()).contains("testParamValue"));
assertTrue(((String) resultMessage.getPayload()).contains("FOO"));