Initial test for Jackson-based JMS reply
Issue: SPR-13237
This commit is contained in:
@@ -33,7 +33,9 @@ import org.junit.Test;
|
||||
import org.springframework.beans.factory.support.StaticListableBeanFactory;
|
||||
import org.springframework.jms.StubTextMessage;
|
||||
import org.springframework.jms.support.JmsHeaders;
|
||||
import org.springframework.jms.support.converter.MappingJackson2MessageConverter;
|
||||
import org.springframework.jms.support.converter.MessageConverter;
|
||||
import org.springframework.jms.support.converter.MessageType;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.converter.MessageConversionException;
|
||||
import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
|
||||
@@ -137,9 +139,7 @@ public class MessagingMessageListenerAdapterTests {
|
||||
MessageConverter messageConverter = mock(MessageConverter.class);
|
||||
given(messageConverter.toMessage("Response", session)).willReturn(new StubTextMessage("Response"));
|
||||
|
||||
Message<String> result = MessageBuilder.withPayload("Response")
|
||||
.build();
|
||||
|
||||
Message<String> result = MessageBuilder.withPayload("Response").build();
|
||||
MessagingMessageListenerAdapter listener = getSimpleInstance("echo", Message.class);
|
||||
listener.setMessageConverter(messageConverter);
|
||||
javax.jms.Message replyMessage = listener.buildMessage(session, result);
|
||||
@@ -152,12 +152,10 @@ public class MessagingMessageListenerAdapterTests {
|
||||
@Test
|
||||
public void replyPayloadToQueue() throws JMSException {
|
||||
Message<String> request = MessageBuilder.withPayload("Response").build();
|
||||
|
||||
Session session = mock(Session.class);
|
||||
Queue replyDestination = mock(Queue.class);
|
||||
given(session.createQueue("queueOut")).willReturn(replyDestination);
|
||||
|
||||
|
||||
MessageProducer messageProducer = mock(MessageProducer.class);
|
||||
TextMessage responseMessage = mock(TextMessage.class);
|
||||
given(session.createTextMessage("Response")).willReturn(responseMessage);
|
||||
@@ -166,7 +164,6 @@ public class MessagingMessageListenerAdapterTests {
|
||||
MessagingMessageListenerAdapter listener = getPayloadInstance(request, "replyPayloadToQueue", Message.class);
|
||||
listener.onMessage(mock(javax.jms.Message.class), session);
|
||||
|
||||
|
||||
verify(session).createQueue("queueOut");
|
||||
verify(session).createTextMessage("Response");
|
||||
verify(messageProducer).send(responseMessage);
|
||||
@@ -176,12 +173,10 @@ public class MessagingMessageListenerAdapterTests {
|
||||
@Test
|
||||
public void replyPayloadToTopic() throws JMSException {
|
||||
Message<String> request = MessageBuilder.withPayload("Response").build();
|
||||
|
||||
Session session = mock(Session.class);
|
||||
Topic replyDestination = mock(Topic.class);
|
||||
given(session.createTopic("topicOut")).willReturn(replyDestination);
|
||||
|
||||
|
||||
MessageProducer messageProducer = mock(MessageProducer.class);
|
||||
TextMessage responseMessage = mock(TextMessage.class);
|
||||
given(session.createTextMessage("Response")).willReturn(responseMessage);
|
||||
@@ -190,7 +185,6 @@ public class MessagingMessageListenerAdapterTests {
|
||||
MessagingMessageListenerAdapter listener = getPayloadInstance(request, "replyPayloadToTopic", Message.class);
|
||||
listener.onMessage(mock(javax.jms.Message.class), session);
|
||||
|
||||
|
||||
verify(session).createTopic("topicOut");
|
||||
verify(session).createTextMessage("Response");
|
||||
verify(messageProducer).send(responseMessage);
|
||||
@@ -240,6 +234,38 @@ public class MessagingMessageListenerAdapterTests {
|
||||
verify(messageProducer).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void replyJackson() throws JMSException {
|
||||
TextMessage reply = testReplyWithJackson("replyJackson",
|
||||
"{\"counter\":42,\"name\":\"Response\",\"description\":\"lengthy description\"}");
|
||||
verify(reply).setObjectProperty("foo", "bar");
|
||||
}
|
||||
|
||||
private TextMessage testReplyWithJackson(String methodName, String replyContent) throws JMSException {
|
||||
Queue replyDestination = mock(Queue.class);
|
||||
Message<String> request = MessageBuilder.withPayload("Response").build();
|
||||
|
||||
Session session = mock(Session.class);
|
||||
MessageProducer messageProducer = mock(MessageProducer.class);
|
||||
TextMessage responseMessage = mock(TextMessage.class);
|
||||
given(session.createTextMessage(replyContent)).willReturn(responseMessage);
|
||||
given(session.createProducer(replyDestination)).willReturn(messageProducer);
|
||||
|
||||
MessagingMessageListenerAdapter listener = getPayloadInstance(request, methodName, Message.class);
|
||||
MappingJackson2MessageConverter messageConverter = new MappingJackson2MessageConverter();
|
||||
messageConverter.setTargetType(MessageType.TEXT);
|
||||
listener.setMessageConverter(messageConverter);
|
||||
listener.setDefaultResponseDestination(replyDestination);
|
||||
listener.onMessage(mock(javax.jms.Message.class), session);
|
||||
|
||||
verify(session, times(0)).createQueue(anyString());
|
||||
verify(session).createTextMessage(replyContent);
|
||||
verify(messageProducer).send(responseMessage);
|
||||
verify(messageProducer).close();
|
||||
return responseMessage;
|
||||
}
|
||||
|
||||
|
||||
protected MessagingMessageListenerAdapter getSimpleInstance(String methodName, Class... parameterTypes) {
|
||||
Method m = ReflectionUtils.findMethod(SampleBean.class, methodName, parameterTypes);
|
||||
return createInstance(m);
|
||||
@@ -253,14 +279,15 @@ public class MessagingMessageListenerAdapterTests {
|
||||
|
||||
protected MessagingMessageListenerAdapter getPayloadInstance(final Object payload,
|
||||
String methodName, Class... parameterTypes) {
|
||||
Method m = ReflectionUtils.findMethod(SampleBean.class, methodName, parameterTypes);
|
||||
|
||||
Method method = ReflectionUtils.findMethod(SampleBean.class, methodName, parameterTypes);
|
||||
MessagingMessageListenerAdapter adapter = new MessagingMessageListenerAdapter() {
|
||||
@Override
|
||||
protected Object extractMessage(javax.jms.Message message) {
|
||||
return payload;
|
||||
}
|
||||
};
|
||||
adapter.setHandlerMethod(factory.createInvocableHandlerMethod(sample, m));
|
||||
adapter.setHandlerMethod(factory.createInvocableHandlerMethod(sample, method));
|
||||
return adapter;
|
||||
}
|
||||
|
||||
@@ -302,6 +329,15 @@ public class MessagingMessageListenerAdapterTests {
|
||||
return new JmsResponse<>(input.getPayload(), null);
|
||||
}
|
||||
|
||||
public Message<SampleResponse> replyJackson(Message<String> input) {
|
||||
return MessageBuilder.withPayload(createSampleResponse(input.getPayload()))
|
||||
.setHeader("foo", "bar").build();
|
||||
}
|
||||
|
||||
private SampleResponse createSampleResponse(String name) {
|
||||
return new SampleResponse(name, "lengthy description");
|
||||
}
|
||||
|
||||
public void fail(String input) {
|
||||
throw new IllegalArgumentException("Expected test exception");
|
||||
}
|
||||
@@ -311,4 +347,43 @@ public class MessagingMessageListenerAdapterTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class SampleResponse {
|
||||
|
||||
private int counter = 42;
|
||||
|
||||
private String name;
|
||||
|
||||
private String description;
|
||||
|
||||
public SampleResponse(String name, String description) {
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public int getCounter() {
|
||||
return counter;
|
||||
}
|
||||
|
||||
public void setCounter(int counter) {
|
||||
this.counter = counter;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user