Update Javadoc, add tests and polish spring-messaging
This commit is contained in:
@@ -0,0 +1,257 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.messaging.core;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
import org.springframework.messaging.support.channel.ExecutorSubscribableChannel;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link AbstractDestinationResolvingMessagingTemplate}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class DestinationResolvingMessagingTemplateTests {
|
||||
|
||||
private TestDestinationResolvingMessagingTemplate template;
|
||||
|
||||
private ExecutorSubscribableChannel myChannel;
|
||||
|
||||
private Map<String, Object> headers;
|
||||
|
||||
private TestMessagePostProcessor postProcessor;
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
|
||||
TestMessageChannelDestinationResolver resolver = new TestMessageChannelDestinationResolver();
|
||||
|
||||
this.myChannel = new ExecutorSubscribableChannel();
|
||||
resolver.registerMessageChannel("myChannel", this.myChannel);
|
||||
|
||||
this.template = new TestDestinationResolvingMessagingTemplate();
|
||||
this.template.setDestinationResolver(resolver);
|
||||
|
||||
this.headers = Collections.<String, Object>singletonMap("key", "value");
|
||||
|
||||
this.postProcessor = new TestMessagePostProcessor();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void send() {
|
||||
Message<?> message = new GenericMessage<Object>("payload");
|
||||
this.template.send("myChannel", message);
|
||||
|
||||
assertSame(this.myChannel, this.template.messageChannel);
|
||||
assertSame(message, this.template.message);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void sendNoDestinationResolver() {
|
||||
TestDestinationResolvingMessagingTemplate template = new TestDestinationResolvingMessagingTemplate();
|
||||
template.send("myChannel", new GenericMessage<Object>("payload"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertAndSendPayload() {
|
||||
this.template.convertAndSend("myChannel", "payload");
|
||||
|
||||
assertSame(this.myChannel, this.template.messageChannel);
|
||||
assertNotNull(this.template.message);
|
||||
assertSame("payload", this.template.message.getPayload());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertAndSendPayloadAndHeaders() {
|
||||
this.template.convertAndSend("myChannel", "payload", this.headers);
|
||||
|
||||
assertSame(this.myChannel, this.template.messageChannel);
|
||||
assertNotNull(this.template.message);
|
||||
assertEquals("value", this.template.message.getHeaders().get("key"));
|
||||
assertEquals("payload", this.template.message.getPayload());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertAndSendPayloadWithPostProcessor() {
|
||||
this.template.convertAndSend("myChannel", "payload", this.postProcessor);
|
||||
|
||||
assertSame(this.myChannel, this.template.messageChannel);
|
||||
assertNotNull(this.template.message);
|
||||
assertEquals("payload", this.template.message.getPayload());
|
||||
|
||||
assertNotNull(this.postProcessor.getMessage());
|
||||
assertSame(this.postProcessor.getMessage(), this.template.message);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertAndSendPayloadAndHeadersWithPostProcessor() {
|
||||
this.template.convertAndSend("myChannel", "payload", this.headers, this.postProcessor);
|
||||
|
||||
assertSame(this.myChannel, this.template.messageChannel);
|
||||
assertNotNull(this.template.message);
|
||||
assertEquals("value", this.template.message.getHeaders().get("key"));
|
||||
assertEquals("payload", this.template.message.getPayload());
|
||||
|
||||
assertNotNull(this.postProcessor.getMessage());
|
||||
assertSame(this.postProcessor.getMessage(), this.template.message);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void receive() {
|
||||
Message<?> expected = new GenericMessage<Object>("payload");
|
||||
this.template.setReceiveMessage(expected);
|
||||
Message<?> actual = this.template.receive("myChannel");
|
||||
|
||||
assertSame(expected, actual);
|
||||
assertSame(this.myChannel, this.template.messageChannel);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void receiveAndConvert() {
|
||||
Message<?> expected = new GenericMessage<Object>("payload");
|
||||
this.template.setReceiveMessage(expected);
|
||||
String payload = this.template.receiveAndConvert("myChannel", String.class);
|
||||
|
||||
assertEquals("payload", payload);
|
||||
assertSame(this.myChannel, this.template.messageChannel);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sendAndReceive() {
|
||||
Message<?> requestMessage = new GenericMessage<Object>("request");
|
||||
Message<?> responseMessage = new GenericMessage<Object>("response");
|
||||
this.template.setReceiveMessage(responseMessage);
|
||||
Message<?> actual = this.template.sendAndReceive("myChannel", requestMessage);
|
||||
|
||||
assertEquals(requestMessage, this.template.message);
|
||||
assertSame(responseMessage, actual);
|
||||
assertSame(this.myChannel, this.template.messageChannel);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertSendAndReceive() {
|
||||
Message<?> responseMessage = new GenericMessage<Object>("response");
|
||||
this.template.setReceiveMessage(responseMessage);
|
||||
String actual = this.template.convertSendAndReceive("myChannel", "request", String.class);
|
||||
|
||||
assertEquals("request", this.template.message.getPayload());
|
||||
assertSame("response", actual);
|
||||
assertSame(this.myChannel, this.template.messageChannel);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertSendAndReceiveWithHeaders() {
|
||||
Message<?> responseMessage = new GenericMessage<Object>("response");
|
||||
this.template.setReceiveMessage(responseMessage);
|
||||
String actual = this.template.convertSendAndReceive("myChannel", "request", this.headers, String.class);
|
||||
|
||||
assertEquals("value", this.template.message.getHeaders().get("key"));
|
||||
assertEquals("request", this.template.message.getPayload());
|
||||
assertSame("response", actual);
|
||||
assertSame(this.myChannel, this.template.messageChannel);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertSendAndReceiveWithPostProcessor() {
|
||||
Message<?> responseMessage = new GenericMessage<Object>("response");
|
||||
this.template.setReceiveMessage(responseMessage);
|
||||
String actual = this.template.convertSendAndReceive("myChannel", "request", String.class, this.postProcessor);
|
||||
|
||||
assertEquals("request", this.template.message.getPayload());
|
||||
assertSame("request", this.postProcessor.getMessage().getPayload());
|
||||
assertSame("response", actual);
|
||||
assertSame(this.myChannel, this.template.messageChannel);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertSendAndReceiveWithHeadersAndPostProcessor() {
|
||||
Message<?> responseMessage = new GenericMessage<Object>("response");
|
||||
this.template.setReceiveMessage(responseMessage);
|
||||
String actual = this.template.convertSendAndReceive("myChannel", "request", this.headers,
|
||||
String.class, this.postProcessor);
|
||||
|
||||
assertEquals("value", this.template.message.getHeaders().get("key"));
|
||||
assertEquals("request", this.template.message.getPayload());
|
||||
assertSame("request", this.postProcessor.getMessage().getPayload());
|
||||
assertSame("response", actual);
|
||||
assertSame(this.myChannel, this.template.messageChannel);
|
||||
}
|
||||
|
||||
|
||||
private static class TestDestinationResolvingMessagingTemplate
|
||||
extends AbstractDestinationResolvingMessagingTemplate<MessageChannel> {
|
||||
|
||||
private MessageChannel messageChannel;
|
||||
|
||||
private Message<?> message;
|
||||
|
||||
private Message<?> receiveMessage;
|
||||
|
||||
|
||||
private void setReceiveMessage(Message<?> receiveMessage) {
|
||||
this.receiveMessage = receiveMessage;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doSend(MessageChannel channel, Message<?> message) {
|
||||
this.messageChannel = channel;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Message<?> doReceive(MessageChannel channel) {
|
||||
this.messageChannel = channel;
|
||||
return this.receiveMessage;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Message<?> doSendAndReceive(MessageChannel channel, Message<?> requestMessage) {
|
||||
this.message = requestMessage;
|
||||
this.messageChannel = channel;
|
||||
return this.receiveMessage;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class TestMessageChannelDestinationResolver implements DestinationResolver<MessageChannel> {
|
||||
|
||||
private final Map<String, MessageChannel> channels = new HashMap<>();
|
||||
|
||||
|
||||
public void registerMessageChannel(String name, MessageChannel channel) {
|
||||
this.channels.put(name, channel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MessageChannel resolveDestination(String name) throws DestinationResolutionException {
|
||||
return this.channels.get(name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.messaging.core;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.messaging.*;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
import org.springframework.messaging.support.channel.ExecutorSubscribableChannel;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static junit.framework.Assert.assertNull;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link GenericMessagingTemplate}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class GenericMessagingTemplateTests {
|
||||
|
||||
private GenericMessagingTemplate template;
|
||||
|
||||
private ThreadPoolTaskExecutor executor;
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
|
||||
this.template = new GenericMessagingTemplate();
|
||||
|
||||
this.executor = new ThreadPoolTaskExecutor();
|
||||
this.executor.afterPropertiesSet();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void sendAndReceive() {
|
||||
|
||||
SubscribableChannel channel = new ExecutorSubscribableChannel(this.executor);
|
||||
channel.subscribe(new MessageHandler() {
|
||||
@Override
|
||||
public void handleMessage(Message<?> message) throws MessagingException {
|
||||
MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel();
|
||||
replyChannel.send(new GenericMessage<String>("response"));
|
||||
}
|
||||
});
|
||||
|
||||
String actual = this.template.convertSendAndReceive(channel, "request", String.class);
|
||||
|
||||
assertEquals("response", actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sendAndReceiveTimeout() throws InterruptedException {
|
||||
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
|
||||
this.template.setReceiveTimeout(1);
|
||||
this.template.setThrowExceptionOnLateReply(true);
|
||||
|
||||
SubscribableChannel channel = new ExecutorSubscribableChannel(this.executor);
|
||||
channel.subscribe(new MessageHandler() {
|
||||
@Override
|
||||
public void handleMessage(Message<?> message) throws MessagingException {
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel();
|
||||
replyChannel.send(new GenericMessage<String>("response"));
|
||||
fail("Expected exception");
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
fail("Unexpected exception " + e.getMessage());
|
||||
}
|
||||
catch (MessageDeliveryException ex) {
|
||||
assertEquals("Reply message received but the receiving thread has already received a reply",
|
||||
ex.getMessage());
|
||||
}
|
||||
finally {
|
||||
latch.countDown();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
assertNull(this.template.convertSendAndReceive(channel, "request", String.class));
|
||||
|
||||
assertTrue(latch.await(1000, TimeUnit.MILLISECONDS));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.messaging.core;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertSame;
|
||||
|
||||
/**
|
||||
* Unit tests for receiving operations in {@link AbstractMessagingTemplate}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
*
|
||||
* @see MessageRequestReplyTemplateTests
|
||||
*/
|
||||
public class MessageReceivingTemplateTests {
|
||||
|
||||
private TestMessagingTemplate template;
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.template = new TestMessagingTemplate();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void receive() {
|
||||
Message<?> expected = new GenericMessage<Object>("payload");
|
||||
this.template.setDefaultDestination("home");
|
||||
this.template.setReceiveMessage(expected);
|
||||
Message<?> actual = this.template.receive();
|
||||
|
||||
assertEquals("home", this.template.destination);
|
||||
assertSame(expected, actual);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void receiveMissingDefaultDestination() {
|
||||
this.template.receive();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void receiveFromDestination() {
|
||||
Message<?> expected = new GenericMessage<Object>("payload");
|
||||
this.template.setReceiveMessage(expected);
|
||||
Message<?> actual = this.template.receive("somewhere");
|
||||
|
||||
assertEquals("somewhere", this.template.destination);
|
||||
assertSame(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void receiveAndConvert() {
|
||||
Message<?> expected = new GenericMessage<Object>("payload");
|
||||
this.template.setDefaultDestination("home");
|
||||
this.template.setReceiveMessage(expected);
|
||||
String payload = this.template.receiveAndConvert(String.class);
|
||||
|
||||
assertEquals("home", this.template.destination);
|
||||
assertSame("payload", payload);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void receiveAndConvertFromDestination() {
|
||||
Message<?> expected = new GenericMessage<Object>("payload");
|
||||
this.template.setReceiveMessage(expected);
|
||||
String payload = this.template.receiveAndConvert("somewhere", String.class);
|
||||
|
||||
assertEquals("somewhere", this.template.destination);
|
||||
assertSame("payload", payload);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static class TestMessagingTemplate extends AbstractMessagingTemplate<String> {
|
||||
|
||||
private String destination;
|
||||
|
||||
private Message<?> receiveMessage;
|
||||
|
||||
|
||||
private void setReceiveMessage(Message<?> receiveMessage) {
|
||||
this.receiveMessage = receiveMessage;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doSend(String destination, Message<?> message) {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Message<?> doReceive(String destination) {
|
||||
this.destination = destination;
|
||||
return this.receiveMessage;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Message<?> doSendAndReceive(String destination, Message<?> requestMessage) {
|
||||
this.destination = destination;
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.messaging.core;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertSame;
|
||||
|
||||
/**
|
||||
* Unit tests for request and reply operations in {@link AbstractMessagingTemplate}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
*
|
||||
* @see MessageReceivingTemplateTests
|
||||
*/
|
||||
public class MessageRequestReplyTemplateTests {
|
||||
|
||||
private TestMessagingTemplate template;
|
||||
|
||||
private TestMessagePostProcessor postProcessor;
|
||||
|
||||
private Map<String, Object> headers;
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.template = new TestMessagingTemplate();
|
||||
this.postProcessor = new TestMessagePostProcessor();
|
||||
this.headers = Collections.<String, Object>singletonMap("key", "value");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void sendAndReceive() {
|
||||
Message<?> requestMessage = new GenericMessage<Object>("request");
|
||||
Message<?> responseMessage = new GenericMessage<Object>("response");
|
||||
this.template.setDefaultDestination("home");
|
||||
this.template.setReceiveMessage(responseMessage);
|
||||
Message<?> actual = this.template.sendAndReceive(requestMessage);
|
||||
|
||||
assertEquals("home", this.template.destination);
|
||||
assertSame(requestMessage, this.template.requestMessage);
|
||||
assertSame(responseMessage, actual);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void sendAndReceiveMissingDestination() {
|
||||
this.template.sendAndReceive(new GenericMessage<Object>("request"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sendAndReceiveToDestination() {
|
||||
Message<?> requestMessage = new GenericMessage<Object>("request");
|
||||
Message<?> responseMessage = new GenericMessage<Object>("response");
|
||||
this.template.setReceiveMessage(responseMessage);
|
||||
Message<?> actual = this.template.sendAndReceive("somewhere", requestMessage);
|
||||
|
||||
assertEquals("somewhere", this.template.destination);
|
||||
assertSame(requestMessage, this.template.requestMessage);
|
||||
assertSame(responseMessage, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertAndSend() {
|
||||
Message<?> responseMessage = new GenericMessage<Object>("response");
|
||||
this.template.setDefaultDestination("home");
|
||||
this.template.setReceiveMessage(responseMessage);
|
||||
String response = this.template.convertSendAndReceive("request", String.class);
|
||||
|
||||
assertEquals("home", this.template.destination);
|
||||
assertSame("request", this.template.requestMessage.getPayload());
|
||||
assertSame("response", response);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertAndSendToDestination() {
|
||||
Message<?> responseMessage = new GenericMessage<Object>("response");
|
||||
this.template.setReceiveMessage(responseMessage);
|
||||
String response = this.template.convertSendAndReceive("somewhere", "request", String.class);
|
||||
|
||||
assertEquals("somewhere", this.template.destination);
|
||||
assertSame("request", this.template.requestMessage.getPayload());
|
||||
assertSame("response", response);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertAndSendToDestinationWithHeaders() {
|
||||
Message<?> responseMessage = new GenericMessage<Object>("response");
|
||||
this.template.setReceiveMessage(responseMessage);
|
||||
String response = this.template.convertSendAndReceive("somewhere", "request", this.headers, String.class);
|
||||
|
||||
assertEquals("somewhere", this.template.destination);
|
||||
assertEquals("value", this.template.requestMessage.getHeaders().get("key"));
|
||||
assertSame("request", this.template.requestMessage.getPayload());
|
||||
assertSame("response", response);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertAndSendWithPostProcessor() {
|
||||
Message<?> responseMessage = new GenericMessage<Object>("response");
|
||||
this.template.setDefaultDestination("home");
|
||||
this.template.setReceiveMessage(responseMessage);
|
||||
String response = this.template.convertSendAndReceive("request", String.class, this.postProcessor);
|
||||
|
||||
assertEquals("home", this.template.destination);
|
||||
assertSame("request", this.template.requestMessage.getPayload());
|
||||
assertSame("response", response);
|
||||
assertSame(this.postProcessor.getMessage(), this.template.requestMessage);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertAndSendToDestinationWithPostProcessor() {
|
||||
Message<?> responseMessage = new GenericMessage<Object>("response");
|
||||
this.template.setReceiveMessage(responseMessage);
|
||||
String response = this.template.convertSendAndReceive("somewhere", "request", String.class, this.postProcessor);
|
||||
|
||||
assertEquals("somewhere", this.template.destination);
|
||||
assertSame("request", this.template.requestMessage.getPayload());
|
||||
assertSame("response", response);
|
||||
assertSame(this.postProcessor.getMessage(), this.template.requestMessage);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertAndSendToDestinationWithHeadersAndPostProcessor() {
|
||||
Message<?> responseMessage = new GenericMessage<Object>("response");
|
||||
this.template.setReceiveMessage(responseMessage);
|
||||
String response = this.template.convertSendAndReceive("somewhere", "request", this.headers,
|
||||
String.class, this.postProcessor);
|
||||
|
||||
assertEquals("somewhere", this.template.destination);
|
||||
assertEquals("value", this.template.requestMessage.getHeaders().get("key"));
|
||||
assertSame("request", this.template.requestMessage.getPayload());
|
||||
assertSame("response", response);
|
||||
assertSame(this.postProcessor.getMessage(), this.template.requestMessage);
|
||||
}
|
||||
|
||||
|
||||
private static class TestMessagingTemplate extends AbstractMessagingTemplate<String> {
|
||||
|
||||
private String destination;
|
||||
|
||||
private Message<?> requestMessage;
|
||||
|
||||
private Message<?> receiveMessage;
|
||||
|
||||
|
||||
private void setReceiveMessage(Message<?> receiveMessage) {
|
||||
this.receiveMessage = receiveMessage;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doSend(String destination, Message<?> message) {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Message<?> doReceive(String destination) {
|
||||
this.destination = destination;
|
||||
return this.receiveMessage;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Message<?> doSendAndReceive(String destination, Message<?> requestMessage) {
|
||||
this.destination = destination;
|
||||
this.requestMessage = requestMessage;
|
||||
return this.receiveMessage;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.messaging.core;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import static junit.framework.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertSame;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link AbstractMessageSendingTemplate}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class MessageSendingTemplateTests {
|
||||
|
||||
private TestMessageSendingTemplate template;
|
||||
|
||||
private TestMessagePostProcessor postProcessor;
|
||||
|
||||
private Map<String, Object> headers;
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.template = new TestMessageSendingTemplate();
|
||||
this.postProcessor = new TestMessagePostProcessor();
|
||||
this.headers = Collections.<String, Object>singletonMap("key", "value");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void send() {
|
||||
Message<?> message = new GenericMessage<Object>("payload");
|
||||
this.template.setDefaultDestination("home");
|
||||
this.template.send(message);
|
||||
|
||||
assertEquals("home", this.template.destination);
|
||||
assertSame(message, this.template.message);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sendToDestination() {
|
||||
Message<?> message = new GenericMessage<Object>("payload");
|
||||
this.template.send("somewhere", message);
|
||||
|
||||
assertEquals("somewhere", this.template.destination);
|
||||
assertSame(message, this.template.message);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void sendMissingDestination() {
|
||||
Message<?> message = new GenericMessage<Object>("payload");
|
||||
this.template.send(message);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertAndSend() {
|
||||
this.template.convertAndSend("somewhere", "payload", headers, this.postProcessor);
|
||||
|
||||
assertEquals("somewhere", this.template.destination);
|
||||
assertNotNull(this.template.message);
|
||||
assertEquals("value", this.template.message.getHeaders().get("key"));
|
||||
assertEquals("payload", this.template.message.getPayload());
|
||||
|
||||
assertNotNull(this.postProcessor.getMessage());
|
||||
assertSame(this.template.message, this.postProcessor.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertAndSendPayload() {
|
||||
this.template.setDefaultDestination("home");
|
||||
this.template.convertAndSend("payload");
|
||||
|
||||
assertEquals("home", this.template.destination);
|
||||
assertNotNull(this.template.message);
|
||||
assertEquals("expected 'id' and 'timestamp' headers only", 2, this.template.message.getHeaders().size());
|
||||
assertEquals("payload", this.template.message.getPayload());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertAndSendPayloadToDestination() {
|
||||
this.template.convertAndSend("somewhere", "payload");
|
||||
|
||||
assertEquals("somewhere", this.template.destination);
|
||||
assertNotNull(this.template.message);
|
||||
assertEquals("expected 'id' and 'timestamp' headers only", 2, this.template.message.getHeaders().size());
|
||||
assertEquals("payload", this.template.message.getPayload());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertAndSendPayloadAndHeadersToDestination() {
|
||||
this.template.convertAndSend("somewhere", "payload", headers);
|
||||
|
||||
assertEquals("somewhere", this.template.destination);
|
||||
assertNotNull(this.template.message);
|
||||
assertEquals("value", this.template.message.getHeaders().get("key"));
|
||||
assertEquals("payload", this.template.message.getPayload());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void convertAndSendPayloadWithPostProcessor() {
|
||||
this.template.setDefaultDestination("home");
|
||||
this.template.convertAndSend((Object) "payload", this.postProcessor);
|
||||
|
||||
assertEquals("home", this.template.destination);
|
||||
assertNotNull(this.template.message);
|
||||
assertEquals("expected 'id' and 'timestamp' headers only", 2, this.template.message.getHeaders().size());
|
||||
assertEquals("payload", this.template.message.getPayload());
|
||||
|
||||
assertNotNull(this.postProcessor.getMessage());
|
||||
assertSame(this.template.message, this.postProcessor.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertAndSendPayloadWithPostProcessorToDestination() {
|
||||
this.template.convertAndSend("somewhere", "payload", this.postProcessor);
|
||||
|
||||
assertEquals("somewhere", this.template.destination);
|
||||
assertNotNull(this.template.message);
|
||||
assertEquals("expected 'id' and 'timestamp' headers only", 2, this.template.message.getHeaders().size());
|
||||
assertEquals("payload", this.template.message.getPayload());
|
||||
|
||||
assertNotNull(this.postProcessor.getMessage());
|
||||
assertSame(this.template.message, this.postProcessor.getMessage());
|
||||
}
|
||||
|
||||
|
||||
private static class TestMessageSendingTemplate extends AbstractMessageSendingTemplate<String> {
|
||||
|
||||
private String destination;
|
||||
|
||||
private Message<?> message;
|
||||
|
||||
@Override
|
||||
protected void doSend(String destination, Message<?> message) {
|
||||
this.destination = destination;
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class TestMessagePostProcessor implements MessagePostProcessor {
|
||||
|
||||
private Message<?> message;
|
||||
|
||||
|
||||
Message<?> getMessage() {
|
||||
return this.message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Message<?> postProcessMessage(Message<?> message) {
|
||||
this.message = message;
|
||||
return message;
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,7 @@ import org.springframework.core.GenericTypeResolver;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHandlingException;
|
||||
import org.springframework.messaging.handler.annotation.Header;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ import org.springframework.core.GenericTypeResolver;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHandlingException;
|
||||
import org.springframework.messaging.handler.annotation.PathVariable;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ public class MessageHeaderAccessorTests {
|
||||
Map<String, Object> original = new HashMap<>();
|
||||
original.put("foo", "bar");
|
||||
original.put("bar", "baz");
|
||||
GenericMessage<String> message = new GenericMessage<>("p", original);
|
||||
GenericMessage<String> message = new GenericMessage<>("payload", original);
|
||||
|
||||
MessageHeaderAccessor headers = new MessageHeaderAccessor(message);
|
||||
Map<String, Object> actual = headers.toMap();
|
||||
@@ -61,7 +61,7 @@ public class MessageHeaderAccessorTests {
|
||||
Map<String, Object> original = new HashMap<>();
|
||||
original.put("foo", "bar");
|
||||
original.put("bar", "baz");
|
||||
GenericMessage<String> message = new GenericMessage<>("p", original);
|
||||
GenericMessage<String> message = new GenericMessage<>("payload", original);
|
||||
|
||||
MessageHeaderAccessor headers = new MessageHeaderAccessor(message);
|
||||
headers.setHeader("foo", "BAR");
|
||||
|
||||
Reference in New Issue
Block a user