Added tests for SimpleMessagingGateway.
This commit is contained in:
@@ -20,13 +20,13 @@
|
||||
</publications>
|
||||
|
||||
<dependencies>
|
||||
<dependency org="org.junit" name="com.springsource.org.junit" rev="4.4.0"/>
|
||||
<dependency org="org.junit" name="com.springsource.org.junit" rev="4.4.0" conf="test->runtime"/>
|
||||
<dependency org="org.easymock" name="com.springsource.org.easymock" rev="2.3.0" conf="test->runtime"/>
|
||||
<dependency org="org.springframework" name="org.springframework.transaction" rev="2.5.4.A" conf="test->runtime"/>
|
||||
<dependency org="org.springframework" name="org.springframework.test" rev="2.5.4.A" conf="compile->runtime"/>
|
||||
<dependency org="org.springframework" name="org.springframework.aop" rev="2.5.4.A" conf="compile->runtime"/>
|
||||
<dependency org="org.springframework.integration" name="org.springframework.integration" rev="latest.integration" conf="compile->compile"/>
|
||||
<dependency org="org.springframework.security" name="org.springframework.security" rev="2.0.0.A" conf="compile->runtime"/>
|
||||
<dependency org="org.easymock" name="com.springsource.org.easymock" rev="2.3.0" />
|
||||
</dependencies>
|
||||
|
||||
</ivy-module>
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.aopalliance/com.springsource.org.aopalliance/1.0.0/com.springsource.org.aopalliance-1.0.0.jar" sourcepath="/IVY_CACHE/org.aopalliance/com.springsource.org.aopalliance/1.0.0/com.springsource.org.aopalliance-sources-1.0.0.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.apache.commons/com.springsource.org.apache.commons.logging/1.1.1/com.springsource.org.apache.commons.logging-1.1.1.jar" sourcepath="/IVY_CACHE/org.apache.commons/com.springsource.org.apache.commons.logging/1.1.1/com.springsource.org.apache.commons.logging-sources-1.1.1.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.easymock/com.springsource.org.easymock/2.3.0/com.springsource.org.easymock-2.3.0.jar" sourcepath="/IVY_CACHE/org.easymock/com.springsource.org.easymock/2.3.0/com.springsource.org.easymock-sources-2.3.0.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.junit/com.springsource.org.junit/4.4.0/com.springsource.org.junit-4.4.0.jar" sourcepath="/IVY_CACHE/org.junit/com.springsource.org.junit/4.4.0/com.springsource.org.junit-sources-4.4.0.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.springframework/org.springframework.aop/2.5.4.A/org.springframework.aop-2.5.4.A.jar" sourcepath="/IVY_CACHE/org.springframework/org.springframework.aop/2.5.4.A/org.springframework.aop-sources-2.5.4.A.jar"/>
|
||||
<classpathentry kind="var" path="IVY_CACHE/org.springframework/org.springframework.beans/2.5.4.A/org.springframework.beans-2.5.4.A.jar" sourcepath="/IVY_CACHE/org.springframework/org.springframework.beans/2.5.4.A/org.springframework.beans-sources-2.5.4.A.jar"/>
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
|
||||
<dependencies>
|
||||
<dependency org="org.junit" name="com.springsource.org.junit" rev="4.4.0" conf="test->runtime"/>
|
||||
<dependency org="org.easymock" name="com.springsource.org.easymock" rev="2.3.0" conf="test->runtime"/>
|
||||
<dependency org="org.springframework" name="org.springframework.aop" rev="2.5.4.A" conf="compile->runtime"/>
|
||||
<dependency org="org.springframework" name="org.springframework.context" rev="2.5.4.A" conf="compile->runtime"/>
|
||||
<dependency org="org.springframework" name="org.springframework.transaction" rev="2.5.4.A" conf="compile->runtime"/>
|
||||
|
||||
@@ -29,6 +29,7 @@ import org.springframework.integration.endpoint.HandlerEndpoint;
|
||||
import org.springframework.integration.handler.ReplyHandler;
|
||||
import org.springframework.integration.handler.ReplyMessageCorrelator;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessageDeliveryException;
|
||||
import org.springframework.integration.message.MessagingException;
|
||||
import org.springframework.integration.message.selector.MessageSelector;
|
||||
import org.springframework.integration.scheduling.Subscription;
|
||||
@@ -131,22 +132,24 @@ public class RequestReplyTemplate implements MessageBusAware {
|
||||
|
||||
public boolean send(Message<?> message) {
|
||||
if (message == null) {
|
||||
throw new MessagingException("Message must not be null.");
|
||||
throw new MessageDeliveryException(message, "Message must not be null.");
|
||||
}
|
||||
if (this.requestChannel == null) {
|
||||
throw new MessagingException("No request channel has been configured. Cannot send message.");
|
||||
throw new MessageDeliveryException(message,
|
||||
"No request channel has been configured. Cannot send message.");
|
||||
}
|
||||
boolean sent = (this.requestTimeout >= 0) ?
|
||||
this.requestChannel.send(message, this.requestTimeout) : this.requestChannel.send(message);
|
||||
if (!sent) {
|
||||
throw new MessagingException("Failed to send request message.");
|
||||
throw new MessageDeliveryException(message, "Failed to send request message.");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public Message<?> receive() {
|
||||
if (this.replyChannel == null) {
|
||||
throw new MessagingException("No reply channel has been configured. Cannot perform receive only operation.");
|
||||
throw new MessagingException(
|
||||
"No reply channel has been configured. Cannot perform receive only operation.");
|
||||
}
|
||||
return this.receiveResponse(this.replyChannel);
|
||||
}
|
||||
@@ -170,7 +173,8 @@ public class RequestReplyTemplate implements MessageBusAware {
|
||||
*/
|
||||
public Message<?> request(Message<?> message) {
|
||||
if (this.requestChannel == null) {
|
||||
throw new MessagingException("No request channel available. Cannot send request message.");
|
||||
throw new MessageDeliveryException(message,
|
||||
"No request channel available. Cannot send request message.");
|
||||
}
|
||||
if (this.replyChannel != null) {
|
||||
return this.sendAndReceiveWithReplyMessageCorrelator(message);
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
* 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.gateway;
|
||||
|
||||
import static org.easymock.EasyMock.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.easymock.IAnswer;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.channel.MessageChannel;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessageDeliveryException;
|
||||
import org.springframework.integration.message.MessageMapper;
|
||||
|
||||
/**
|
||||
* @author Iwein Fuld
|
||||
*/
|
||||
public class SimpleMessagingGatewayTests {
|
||||
|
||||
private SimpleMessagingGateway simpleMessagingGateway;
|
||||
|
||||
private MessageChannel requestChannel;
|
||||
|
||||
private Object[] allmocks;
|
||||
|
||||
private Message messageMock;
|
||||
|
||||
private MessageChannel replyChannel;
|
||||
|
||||
private MessageMapper messageMapperMock;
|
||||
|
||||
|
||||
@Before
|
||||
public void initializeSample() {
|
||||
this.requestChannel = createMock(MessageChannel.class);
|
||||
this.replyChannel = createMock(MessageChannel.class);
|
||||
this.messageMock = createMock(Message.class);
|
||||
this.messageMapperMock = createMock(MessageMapper.class);
|
||||
|
||||
this.simpleMessagingGateway = new SimpleMessagingGateway(requestChannel);
|
||||
this.simpleMessagingGateway.setReplyChannel(replyChannel);
|
||||
this.simpleMessagingGateway.setMessageMapper(messageMapperMock);
|
||||
this.allmocks = new Object[] { requestChannel, replyChannel, messageMock, messageMapperMock };
|
||||
}
|
||||
|
||||
|
||||
/* send tests */
|
||||
|
||||
@Test
|
||||
public void sendMessage() {
|
||||
expect(requestChannel.send(messageMock)).andReturn(true);
|
||||
replay(allmocks);
|
||||
this.simpleMessagingGateway.send(messageMock);
|
||||
verify(allmocks);
|
||||
}
|
||||
|
||||
@Test(expected=MessageDeliveryException.class)
|
||||
public void sendMessage_failure() {
|
||||
expect(requestChannel.send(messageMock)).andReturn(false);
|
||||
replay(allmocks);
|
||||
this.simpleMessagingGateway.send(messageMock);
|
||||
verify(allmocks);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sendObject() {
|
||||
expect(requestChannel.send(isA(Message.class))).andAnswer(new IAnswer<Boolean>() {
|
||||
public Boolean answer() throws Throwable {
|
||||
assertEquals("test", ((Message) getCurrentArguments()[0]).getPayload());
|
||||
return true;
|
||||
}
|
||||
});
|
||||
replay(allmocks);
|
||||
this.simpleMessagingGateway.send("test");
|
||||
verify(allmocks);
|
||||
}
|
||||
|
||||
@Test(expected=MessageDeliveryException.class)
|
||||
public void sendObject_failure() {
|
||||
expect(requestChannel.send(isA(Message.class))).andAnswer(new IAnswer<Boolean>() {
|
||||
public Boolean answer() throws Throwable {
|
||||
assertEquals("test", ((Message) getCurrentArguments()[0]).getPayload());
|
||||
return false;
|
||||
}
|
||||
});
|
||||
replay(allmocks);
|
||||
this.simpleMessagingGateway.send("test");
|
||||
verify(allmocks);
|
||||
}
|
||||
|
||||
//TODO null cases for send
|
||||
|
||||
/* receive tests */
|
||||
|
||||
@Test
|
||||
public void receiveMessage() {
|
||||
expect(replyChannel.receive()).andReturn(messageMock);
|
||||
expect(messageMapperMock.mapMessage(messageMock)).andReturn(messageMock);
|
||||
replay(allmocks);
|
||||
assertEquals(this.simpleMessagingGateway.receive(), messageMock);
|
||||
verify(allmocks);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void receiveMessage_null() {
|
||||
expect(replyChannel.receive()).andReturn(null);
|
||||
replay(allmocks);
|
||||
assertEquals(this.simpleMessagingGateway.receive(), null);
|
||||
verify(allmocks);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user