Reuse JmsTemplate's MessageConverter in JmsMessagingTemplate
This commit makes sure that any custom MessageConverter set in a
JmsTemplate used by a JmsMessagingTemplate is reused as the payload
converter of the message.
Issue: SPR-15965
(cherry picked from commit b275a06)
This commit is contained in:
parent
5e904ddcaa
commit
35af7ff854
|
|
@ -50,6 +50,8 @@ public class JmsMessagingTemplate extends AbstractMessagingTemplate<Destination>
|
|||
|
||||
private MessageConverter jmsMessageConverter = new MessagingMessageConverter();
|
||||
|
||||
private boolean converterSet;
|
||||
|
||||
private String defaultDestinationName;
|
||||
|
||||
|
||||
|
|
@ -126,6 +128,7 @@ public class JmsMessagingTemplate extends AbstractMessagingTemplate<Destination>
|
|||
public void setJmsMessageConverter(MessageConverter jmsMessageConverter) {
|
||||
Assert.notNull(jmsMessageConverter, "MessageConverter must not be null");
|
||||
this.jmsMessageConverter = jmsMessageConverter;
|
||||
this.converterSet = true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -155,7 +158,11 @@ public class JmsMessagingTemplate extends AbstractMessagingTemplate<Destination>
|
|||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
Assert.notNull(getJmsTemplate(), "Property 'connectionFactory' or 'jmsTemplate' is required");
|
||||
Assert.notNull(this.jmsTemplate, "Property 'connectionFactory' or 'jmsTemplate' is required");
|
||||
if (!this.converterSet && this.jmsTemplate.getMessageConverter() != null) {
|
||||
((MessagingMessageConverter) this.jmsMessageConverter)
|
||||
.setPayloadConverter(this.jmsTemplate.getMessageConverter());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -54,6 +54,15 @@ public class MessagingMessageConverter implements MessageConverter, Initializing
|
|||
this(new SimpleMessageConverter(), new SimpleJmsHeaderMapper());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an instance with the specific payload converter.
|
||||
* @param payloadConverter the payload converter to use
|
||||
* @since 4.3.12
|
||||
*/
|
||||
public MessagingMessageConverter(MessageConverter payloadConverter) {
|
||||
this(payloadConverter, new SimpleJmsHeaderMapper());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an instance with the specified payload converter and
|
||||
* header mapper.
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2017 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.
|
||||
|
|
@ -36,13 +36,14 @@ import org.mockito.BDDMockito;
|
|||
import org.mockito.Captor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.jms.InvalidDestinationException;
|
||||
import org.springframework.jms.MessageNotReadableException;
|
||||
import org.springframework.jms.StubTextMessage;
|
||||
import org.springframework.jms.support.converter.MessageConverter;
|
||||
import org.springframework.jms.support.converter.MessagingMessageConverter;
|
||||
import org.springframework.jms.support.converter.SimpleMessageConverter;
|
||||
import org.springframework.jms.support.destination.DestinationResolutionException;
|
||||
import org.springframework.messaging.Message;
|
||||
|
|
@ -54,6 +55,8 @@ import static org.junit.Assert.*;
|
|||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* Tests for {@link JmsMessagingTemplate}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class JmsMessagingTemplateTests {
|
||||
|
|
@ -73,12 +76,53 @@ public class JmsMessagingTemplateTests {
|
|||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
messagingTemplate = new JmsMessagingTemplate(jmsTemplate);
|
||||
this.messagingTemplate = new JmsMessagingTemplate(this.jmsTemplate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validateJmsTemplate() {
|
||||
assertSame(this.jmsTemplate, messagingTemplate.getJmsTemplate());
|
||||
assertSame(this.jmsTemplate, this.messagingTemplate.getJmsTemplate());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void payloadConverterIsConsistentConstructor() {
|
||||
MessageConverter messageConverter = mock(MessageConverter.class);
|
||||
given(this.jmsTemplate.getMessageConverter()).willReturn(messageConverter);
|
||||
JmsMessagingTemplate messagingTemplate = new JmsMessagingTemplate(this.jmsTemplate);
|
||||
messagingTemplate.afterPropertiesSet();
|
||||
assertPayloadConverter(messagingTemplate, messageConverter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void payloadConverterIsConsistentSetter() {
|
||||
MessageConverter messageConverter = mock(MessageConverter.class);
|
||||
given(this.jmsTemplate.getMessageConverter()).willReturn(messageConverter);
|
||||
JmsMessagingTemplate messagingTemplate = new JmsMessagingTemplate();
|
||||
messagingTemplate.setJmsTemplate(this.jmsTemplate);
|
||||
messagingTemplate.afterPropertiesSet();
|
||||
assertPayloadConverter(messagingTemplate, messageConverter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customConverterAlwaysTakesPrecedence() {
|
||||
MessageConverter messageConverter = mock(MessageConverter.class);
|
||||
given(this.jmsTemplate.getMessageConverter()).willReturn(messageConverter);
|
||||
MessageConverter customMessageConverter = mock(MessageConverter.class);
|
||||
JmsMessagingTemplate messagingTemplate = new JmsMessagingTemplate();
|
||||
messagingTemplate.setJmsMessageConverter(
|
||||
new MessagingMessageConverter(customMessageConverter));
|
||||
messagingTemplate.setJmsTemplate(this.jmsTemplate);
|
||||
messagingTemplate.afterPropertiesSet();
|
||||
assertPayloadConverter(messagingTemplate, customMessageConverter);
|
||||
}
|
||||
|
||||
private void assertPayloadConverter(JmsMessagingTemplate messagingTemplate,
|
||||
MessageConverter messageConverter) {
|
||||
MessageConverter jmsMessageConverter = messagingTemplate.getJmsMessageConverter();
|
||||
assertNotNull(jmsMessageConverter);
|
||||
assertEquals(MessagingMessageConverter.class, jmsMessageConverter.getClass());
|
||||
assertSame(messageConverter, new DirectFieldAccessor(jmsMessageConverter)
|
||||
.getPropertyValue("payloadConverter"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -86,110 +130,110 @@ public class JmsMessagingTemplateTests {
|
|||
Destination destination = new Destination() {};
|
||||
Message<String> message = createTextMessage();
|
||||
|
||||
messagingTemplate.send(destination, message);
|
||||
verify(jmsTemplate).send(eq(destination), messageCreator.capture());
|
||||
assertTextMessage(messageCreator.getValue());
|
||||
this.messagingTemplate.send(destination, message);
|
||||
verify(this.jmsTemplate).send(eq(destination), this.messageCreator.capture());
|
||||
assertTextMessage(this.messageCreator.getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sendName() {
|
||||
Message<String> message = createTextMessage();
|
||||
|
||||
messagingTemplate.send("myQueue", message);
|
||||
verify(jmsTemplate).send(eq("myQueue"), messageCreator.capture());
|
||||
assertTextMessage(messageCreator.getValue());
|
||||
this.messagingTemplate.send("myQueue", message);
|
||||
verify(this.jmsTemplate).send(eq("myQueue"), this.messageCreator.capture());
|
||||
assertTextMessage(this.messageCreator.getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sendDefaultDestination() {
|
||||
Destination destination = new Destination() {};
|
||||
messagingTemplate.setDefaultDestination(destination);
|
||||
this.messagingTemplate.setDefaultDestination(destination);
|
||||
Message<String> message = createTextMessage();
|
||||
|
||||
messagingTemplate.send(message);
|
||||
verify(jmsTemplate).send(eq(destination), messageCreator.capture());
|
||||
assertTextMessage(messageCreator.getValue());
|
||||
this.messagingTemplate.send(message);
|
||||
verify(this.jmsTemplate).send(eq(destination), this.messageCreator.capture());
|
||||
assertTextMessage(this.messageCreator.getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sendDefaultDestinationName() {
|
||||
messagingTemplate.setDefaultDestinationName("myQueue");
|
||||
this.messagingTemplate.setDefaultDestinationName("myQueue");
|
||||
Message<String> message = createTextMessage();
|
||||
|
||||
messagingTemplate.send(message);
|
||||
verify(jmsTemplate).send(eq("myQueue"), messageCreator.capture());
|
||||
assertTextMessage(messageCreator.getValue());
|
||||
this.messagingTemplate.send(message);
|
||||
verify(this.jmsTemplate).send(eq("myQueue"), this.messageCreator.capture());
|
||||
assertTextMessage(this.messageCreator.getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sendNoDefaultSet() {
|
||||
Message<String> message = createTextMessage();
|
||||
|
||||
thrown.expect(IllegalStateException.class);
|
||||
messagingTemplate.send(message);
|
||||
this.thrown.expect(IllegalStateException.class);
|
||||
this.messagingTemplate.send(message);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sendPropertyInjection() {
|
||||
JmsMessagingTemplate t = new JmsMessagingTemplate();
|
||||
t.setJmsTemplate(jmsTemplate);
|
||||
t.setJmsTemplate(this.jmsTemplate);
|
||||
t.setDefaultDestinationName("myQueue");
|
||||
t.afterPropertiesSet();
|
||||
Message<String> message = createTextMessage();
|
||||
|
||||
t.send(message);
|
||||
verify(jmsTemplate).send(eq("myQueue"), messageCreator.capture());
|
||||
assertTextMessage(messageCreator.getValue());
|
||||
verify(this.jmsTemplate).send(eq("myQueue"), this.messageCreator.capture());
|
||||
assertTextMessage(this.messageCreator.getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertAndSendPayload() throws JMSException {
|
||||
Destination destination = new Destination() {};
|
||||
|
||||
messagingTemplate.convertAndSend(destination, "my Payload");
|
||||
verify(jmsTemplate).send(eq(destination), messageCreator.capture());
|
||||
TextMessage textMessage = createTextMessage(messageCreator.getValue());
|
||||
this.messagingTemplate.convertAndSend(destination, "my Payload");
|
||||
verify(this.jmsTemplate).send(eq(destination), this.messageCreator.capture());
|
||||
TextMessage textMessage = createTextMessage(this.messageCreator.getValue());
|
||||
assertEquals("my Payload", textMessage.getText());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertAndSendPayloadName() throws JMSException {
|
||||
messagingTemplate.convertAndSend("myQueue", "my Payload");
|
||||
verify(jmsTemplate).send(eq("myQueue"), messageCreator.capture());
|
||||
TextMessage textMessage = createTextMessage(messageCreator.getValue());
|
||||
this.messagingTemplate.convertAndSend("myQueue", "my Payload");
|
||||
verify(this.jmsTemplate).send(eq("myQueue"), this.messageCreator.capture());
|
||||
TextMessage textMessage = createTextMessage(this.messageCreator.getValue());
|
||||
assertEquals("my Payload", textMessage.getText());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertAndSendDefaultDestination() throws JMSException {
|
||||
Destination destination = new Destination() {};
|
||||
messagingTemplate.setDefaultDestination(destination);
|
||||
this.messagingTemplate.setDefaultDestination(destination);
|
||||
|
||||
messagingTemplate.convertAndSend("my Payload");
|
||||
verify(jmsTemplate).send(eq(destination), messageCreator.capture());
|
||||
TextMessage textMessage = createTextMessage(messageCreator.getValue());
|
||||
this.messagingTemplate.convertAndSend("my Payload");
|
||||
verify(this.jmsTemplate).send(eq(destination), this.messageCreator.capture());
|
||||
TextMessage textMessage = createTextMessage(this.messageCreator.getValue());
|
||||
assertEquals("my Payload", textMessage.getText());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertAndSendDefaultDestinationName() throws JMSException {
|
||||
messagingTemplate.setDefaultDestinationName("myQueue");
|
||||
this.messagingTemplate.setDefaultDestinationName("myQueue");
|
||||
|
||||
messagingTemplate.convertAndSend("my Payload");
|
||||
verify(jmsTemplate).send(eq("myQueue"), messageCreator.capture());
|
||||
TextMessage textMessage = createTextMessage(messageCreator.getValue());
|
||||
this.messagingTemplate.convertAndSend("my Payload");
|
||||
verify(this.jmsTemplate).send(eq("myQueue"), this.messageCreator.capture());
|
||||
TextMessage textMessage = createTextMessage(this.messageCreator.getValue());
|
||||
assertEquals("my Payload", textMessage.getText());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertAndSendNoDefaultSet() throws JMSException {
|
||||
thrown.expect(IllegalStateException.class);
|
||||
messagingTemplate.convertAndSend("my Payload");
|
||||
this.thrown.expect(IllegalStateException.class);
|
||||
this.messagingTemplate.convertAndSend("my Payload");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertAndSendCustomJmsMessageConverter() throws JMSException {
|
||||
messagingTemplate.setJmsMessageConverter(new SimpleMessageConverter() {
|
||||
this.messagingTemplate.setJmsMessageConverter(new SimpleMessageConverter() {
|
||||
@Override
|
||||
public javax.jms.Message toMessage(Object object, Session session)
|
||||
throws JMSException, org.springframework.jms.support.converter.MessageConversionException {
|
||||
|
|
@ -197,155 +241,155 @@ public class JmsMessagingTemplateTests {
|
|||
}
|
||||
});
|
||||
|
||||
messagingTemplate.convertAndSend("myQueue", "msg to convert");
|
||||
verify(jmsTemplate).send(eq("myQueue"), messageCreator.capture());
|
||||
this.messagingTemplate.convertAndSend("myQueue", "msg to convert");
|
||||
verify(this.jmsTemplate).send(eq("myQueue"), this.messageCreator.capture());
|
||||
|
||||
thrown.expect(org.springframework.messaging.converter.MessageConversionException.class);
|
||||
thrown.expectMessage(new StringContains("Test exception"));
|
||||
messageCreator.getValue().createMessage(mock(Session.class));
|
||||
this.thrown.expect(org.springframework.messaging.converter.MessageConversionException.class);
|
||||
this.thrown.expectMessage(new StringContains("Test exception"));
|
||||
this.messageCreator.getValue().createMessage(mock(Session.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertAndSendPayloadAndHeaders() throws JMSException {
|
||||
Destination destination = new Destination() {};
|
||||
Map<String, Object> headers = new HashMap<String, Object>();
|
||||
Map<String, Object> headers = new HashMap<>();
|
||||
headers.put("foo", "bar");
|
||||
|
||||
messagingTemplate.convertAndSend(destination, "Hello", headers);
|
||||
verify(jmsTemplate).send(eq(destination), messageCreator.capture());
|
||||
assertTextMessage(messageCreator.getValue()); // see createTextMessage
|
||||
this.messagingTemplate.convertAndSend(destination, "Hello", headers);
|
||||
verify(this.jmsTemplate).send(eq(destination), this.messageCreator.capture());
|
||||
assertTextMessage(this.messageCreator.getValue()); // see createTextMessage
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertAndSendPayloadAndHeadersName() throws JMSException {
|
||||
Map<String, Object> headers = new HashMap<String, Object>();
|
||||
Map<String, Object> headers = new HashMap<>();
|
||||
headers.put("foo", "bar");
|
||||
|
||||
messagingTemplate.convertAndSend("myQueue", "Hello", headers);
|
||||
verify(jmsTemplate).send(eq("myQueue"), messageCreator.capture());
|
||||
assertTextMessage(messageCreator.getValue()); // see createTextMessage
|
||||
this.messagingTemplate.convertAndSend("myQueue", "Hello", headers);
|
||||
verify(this.jmsTemplate).send(eq("myQueue"), this.messageCreator.capture());
|
||||
assertTextMessage(this.messageCreator.getValue()); // see createTextMessage
|
||||
}
|
||||
|
||||
@Test
|
||||
public void receive() {
|
||||
Destination destination = new Destination() {};
|
||||
javax.jms.Message jmsMessage = createJmsTextMessage();
|
||||
given(jmsTemplate.receive(destination)).willReturn(jmsMessage);
|
||||
given(this.jmsTemplate.receive(destination)).willReturn(jmsMessage);
|
||||
|
||||
Message<?> message = messagingTemplate.receive(destination);
|
||||
verify(jmsTemplate).receive(destination);
|
||||
Message<?> message = this.messagingTemplate.receive(destination);
|
||||
verify(this.jmsTemplate).receive(destination);
|
||||
assertTextMessage(message);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void receiveName() {
|
||||
javax.jms.Message jmsMessage = createJmsTextMessage();
|
||||
given(jmsTemplate.receive("myQueue")).willReturn(jmsMessage);
|
||||
given(this.jmsTemplate.receive("myQueue")).willReturn(jmsMessage);
|
||||
|
||||
Message<?> message = messagingTemplate.receive("myQueue");
|
||||
verify(jmsTemplate).receive("myQueue");
|
||||
Message<?> message = this.messagingTemplate.receive("myQueue");
|
||||
verify(this.jmsTemplate).receive("myQueue");
|
||||
assertTextMessage(message);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void receiveDefaultDestination() {
|
||||
Destination destination = new Destination() {};
|
||||
messagingTemplate.setDefaultDestination(destination);
|
||||
this.messagingTemplate.setDefaultDestination(destination);
|
||||
javax.jms.Message jmsMessage = createJmsTextMessage();
|
||||
given(jmsTemplate.receive(destination)).willReturn(jmsMessage);
|
||||
given(this.jmsTemplate.receive(destination)).willReturn(jmsMessage);
|
||||
|
||||
Message<?> message = messagingTemplate.receive();
|
||||
verify(jmsTemplate).receive(destination);
|
||||
Message<?> message = this.messagingTemplate.receive();
|
||||
verify(this.jmsTemplate).receive(destination);
|
||||
assertTextMessage(message);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void receiveDefaultDestinationName() {
|
||||
messagingTemplate.setDefaultDestinationName("myQueue");
|
||||
this.messagingTemplate.setDefaultDestinationName("myQueue");
|
||||
javax.jms.Message jmsMessage = createJmsTextMessage();
|
||||
given(jmsTemplate.receive("myQueue")).willReturn(jmsMessage);
|
||||
given(this.jmsTemplate.receive("myQueue")).willReturn(jmsMessage);
|
||||
|
||||
Message<?> message = messagingTemplate.receive();
|
||||
verify(jmsTemplate).receive("myQueue");
|
||||
Message<?> message = this.messagingTemplate.receive();
|
||||
verify(this.jmsTemplate).receive("myQueue");
|
||||
assertTextMessage(message);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void receiveNoDefaultSet() {
|
||||
thrown.expect(IllegalStateException.class);
|
||||
messagingTemplate.receive();
|
||||
this.thrown.expect(IllegalStateException.class);
|
||||
this.messagingTemplate.receive();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void receiveAndConvert() {
|
||||
Destination destination = new Destination() {};
|
||||
javax.jms.Message jmsMessage = createJmsTextMessage("my Payload");
|
||||
given(jmsTemplate.receive(destination)).willReturn(jmsMessage);
|
||||
given(this.jmsTemplate.receive(destination)).willReturn(jmsMessage);
|
||||
|
||||
String payload = messagingTemplate.receiveAndConvert(destination, String.class);
|
||||
String payload = this.messagingTemplate.receiveAndConvert(destination, String.class);
|
||||
assertEquals("my Payload", payload);
|
||||
verify(jmsTemplate).receive(destination);
|
||||
verify(this.jmsTemplate).receive(destination);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void receiveAndConvertName() {
|
||||
javax.jms.Message jmsMessage = createJmsTextMessage("my Payload");
|
||||
given(jmsTemplate.receive("myQueue")).willReturn(jmsMessage);
|
||||
given(this.jmsTemplate.receive("myQueue")).willReturn(jmsMessage);
|
||||
|
||||
String payload = messagingTemplate.receiveAndConvert("myQueue", String.class);
|
||||
String payload = this.messagingTemplate.receiveAndConvert("myQueue", String.class);
|
||||
assertEquals("my Payload", payload);
|
||||
verify(jmsTemplate).receive("myQueue");
|
||||
verify(this.jmsTemplate).receive("myQueue");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void receiveAndConvertDefaultDestination() {
|
||||
Destination destination = new Destination() {};
|
||||
messagingTemplate.setDefaultDestination(destination);
|
||||
this.messagingTemplate.setDefaultDestination(destination);
|
||||
javax.jms.Message jmsMessage = createJmsTextMessage("my Payload");
|
||||
given(jmsTemplate.receive(destination)).willReturn(jmsMessage);
|
||||
given(this.jmsTemplate.receive(destination)).willReturn(jmsMessage);
|
||||
|
||||
String payload = messagingTemplate.receiveAndConvert(String.class);
|
||||
String payload = this.messagingTemplate.receiveAndConvert(String.class);
|
||||
assertEquals("my Payload", payload);
|
||||
verify(jmsTemplate).receive(destination);
|
||||
verify(this.jmsTemplate).receive(destination);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void receiveAndConvertDefaultDestinationName() {
|
||||
messagingTemplate.setDefaultDestinationName("myQueue");
|
||||
this.messagingTemplate.setDefaultDestinationName("myQueue");
|
||||
javax.jms.Message jmsMessage = createJmsTextMessage("my Payload");
|
||||
given(jmsTemplate.receive("myQueue")).willReturn(jmsMessage);
|
||||
given(this.jmsTemplate.receive("myQueue")).willReturn(jmsMessage);
|
||||
|
||||
String payload = messagingTemplate.receiveAndConvert(String.class);
|
||||
String payload = this.messagingTemplate.receiveAndConvert(String.class);
|
||||
assertEquals("my Payload", payload);
|
||||
verify(jmsTemplate).receive("myQueue");
|
||||
verify(this.jmsTemplate).receive("myQueue");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void receiveAndConvertWithConversion() {
|
||||
javax.jms.Message jmsMessage = createJmsTextMessage("123");
|
||||
given(jmsTemplate.receive("myQueue")).willReturn(jmsMessage);
|
||||
given(this.jmsTemplate.receive("myQueue")).willReturn(jmsMessage);
|
||||
|
||||
messagingTemplate.setMessageConverter(new GenericMessageConverter());
|
||||
this.messagingTemplate.setMessageConverter(new GenericMessageConverter());
|
||||
|
||||
Integer payload = messagingTemplate.receiveAndConvert("myQueue", Integer.class);
|
||||
Integer payload = this.messagingTemplate.receiveAndConvert("myQueue", Integer.class);
|
||||
assertEquals(Integer.valueOf(123), payload);
|
||||
verify(jmsTemplate).receive("myQueue");
|
||||
verify(this.jmsTemplate).receive("myQueue");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void receiveAndConvertNoConverter() {
|
||||
javax.jms.Message jmsMessage = createJmsTextMessage("Hello");
|
||||
given(jmsTemplate.receive("myQueue")).willReturn(jmsMessage);
|
||||
given(this.jmsTemplate.receive("myQueue")).willReturn(jmsMessage);
|
||||
|
||||
thrown.expect(org.springframework.messaging.converter.MessageConversionException.class);
|
||||
messagingTemplate.receiveAndConvert("myQueue", Writer.class);
|
||||
this.thrown.expect(org.springframework.messaging.converter.MessageConversionException.class);
|
||||
this.messagingTemplate.receiveAndConvert("myQueue", Writer.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void receiveAndConvertNoInput() {
|
||||
given(jmsTemplate.receive("myQueue")).willReturn(null);
|
||||
given(this.jmsTemplate.receive("myQueue")).willReturn(null);
|
||||
|
||||
assertNull(messagingTemplate.receiveAndConvert("myQueue", String.class));
|
||||
assertNull(this.messagingTemplate.receiveAndConvert("myQueue", String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -353,10 +397,10 @@ public class JmsMessagingTemplateTests {
|
|||
Destination destination = new Destination() {};
|
||||
Message<String> request = createTextMessage();
|
||||
javax.jms.Message replyJmsMessage = createJmsTextMessage();
|
||||
given(jmsTemplate.sendAndReceive(eq(destination), anyObject())).willReturn(replyJmsMessage);
|
||||
given(this.jmsTemplate.sendAndReceive(eq(destination), any())).willReturn(replyJmsMessage);
|
||||
|
||||
Message<?> actual = messagingTemplate.sendAndReceive(destination, request);
|
||||
verify(jmsTemplate, times(1)).sendAndReceive(eq(destination), anyObject());
|
||||
Message<?> actual = this.messagingTemplate.sendAndReceive(destination, request);
|
||||
verify(this.jmsTemplate, times(1)).sendAndReceive(eq(destination), any());
|
||||
assertTextMessage(actual);
|
||||
}
|
||||
|
||||
|
|
@ -364,35 +408,35 @@ public class JmsMessagingTemplateTests {
|
|||
public void sendAndReceiveName() {
|
||||
Message<String> request = createTextMessage();
|
||||
javax.jms.Message replyJmsMessage = createJmsTextMessage();
|
||||
given(jmsTemplate.sendAndReceive(eq("myQueue"), anyObject())).willReturn(replyJmsMessage);
|
||||
given(this.jmsTemplate.sendAndReceive(eq("myQueue"), any())).willReturn(replyJmsMessage);
|
||||
|
||||
Message<?> actual = messagingTemplate.sendAndReceive("myQueue", request);
|
||||
verify(jmsTemplate, times(1)).sendAndReceive(eq("myQueue"), anyObject());
|
||||
Message<?> actual = this.messagingTemplate.sendAndReceive("myQueue", request);
|
||||
verify(this.jmsTemplate, times(1)).sendAndReceive(eq("myQueue"), any());
|
||||
assertTextMessage(actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sendAndReceiveDefaultDestination() {
|
||||
Destination destination = new Destination() {};
|
||||
messagingTemplate.setDefaultDestination(destination);
|
||||
this.messagingTemplate.setDefaultDestination(destination);
|
||||
Message<String> request = createTextMessage();
|
||||
javax.jms.Message replyJmsMessage = createJmsTextMessage();
|
||||
given(jmsTemplate.sendAndReceive(eq(destination), anyObject())).willReturn(replyJmsMessage);
|
||||
given(this.jmsTemplate.sendAndReceive(eq(destination), any())).willReturn(replyJmsMessage);
|
||||
|
||||
Message<?> actual = messagingTemplate.sendAndReceive(request);
|
||||
verify(jmsTemplate, times(1)).sendAndReceive(eq(destination), anyObject());
|
||||
Message<?> actual = this.messagingTemplate.sendAndReceive(request);
|
||||
verify(this.jmsTemplate, times(1)).sendAndReceive(eq(destination), any());
|
||||
assertTextMessage(actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sendAndReceiveDefaultDestinationName() {
|
||||
messagingTemplate.setDefaultDestinationName("myQueue");
|
||||
this.messagingTemplate.setDefaultDestinationName("myQueue");
|
||||
Message<String> request = createTextMessage();
|
||||
javax.jms.Message replyJmsMessage = createJmsTextMessage();
|
||||
given(jmsTemplate.sendAndReceive(eq("myQueue"), anyObject())).willReturn(replyJmsMessage);
|
||||
given(this.jmsTemplate.sendAndReceive(eq("myQueue"), any())).willReturn(replyJmsMessage);
|
||||
|
||||
Message<?> actual = messagingTemplate.sendAndReceive(request);
|
||||
verify(jmsTemplate, times(1)).sendAndReceive(eq("myQueue"), anyObject());
|
||||
Message<?> actual = this.messagingTemplate.sendAndReceive(request);
|
||||
verify(this.jmsTemplate, times(1)).sendAndReceive(eq("myQueue"), any());
|
||||
assertTextMessage(actual);
|
||||
}
|
||||
|
||||
|
|
@ -400,58 +444,58 @@ public class JmsMessagingTemplateTests {
|
|||
public void sendAndReceiveNoDefaultSet() {
|
||||
Message<String> message = createTextMessage();
|
||||
|
||||
thrown.expect(IllegalStateException.class);
|
||||
messagingTemplate.sendAndReceive(message);
|
||||
this.thrown.expect(IllegalStateException.class);
|
||||
this.messagingTemplate.sendAndReceive(message);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertSendAndReceivePayload() throws JMSException {
|
||||
Destination destination = new Destination() {};
|
||||
javax.jms.Message replyJmsMessage = createJmsTextMessage("My reply");
|
||||
given(jmsTemplate.sendAndReceive(eq(destination), anyObject())).willReturn(replyJmsMessage);
|
||||
given(this.jmsTemplate.sendAndReceive(eq(destination), any())).willReturn(replyJmsMessage);
|
||||
|
||||
String reply = messagingTemplate.convertSendAndReceive(destination, "my Payload", String.class);
|
||||
verify(jmsTemplate, times(1)).sendAndReceive(eq(destination), anyObject());
|
||||
String reply = this.messagingTemplate.convertSendAndReceive(destination, "my Payload", String.class);
|
||||
verify(this.jmsTemplate, times(1)).sendAndReceive(eq(destination), any());
|
||||
assertEquals("My reply", reply);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertSendAndReceivePayloadName() throws JMSException {
|
||||
javax.jms.Message replyJmsMessage = createJmsTextMessage("My reply");
|
||||
given(jmsTemplate.sendAndReceive(eq("myQueue"), anyObject())).willReturn(replyJmsMessage);
|
||||
given(this.jmsTemplate.sendAndReceive(eq("myQueue"), any())).willReturn(replyJmsMessage);
|
||||
|
||||
String reply = messagingTemplate.convertSendAndReceive("myQueue", "my Payload", String.class);
|
||||
verify(jmsTemplate, times(1)).sendAndReceive(eq("myQueue"), anyObject());
|
||||
String reply = this.messagingTemplate.convertSendAndReceive("myQueue", "my Payload", String.class);
|
||||
verify(this.jmsTemplate, times(1)).sendAndReceive(eq("myQueue"), any());
|
||||
assertEquals("My reply", reply);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertSendAndReceiveDefaultDestination() throws JMSException {
|
||||
Destination destination = new Destination() {};
|
||||
messagingTemplate.setDefaultDestination(destination);
|
||||
this.messagingTemplate.setDefaultDestination(destination);
|
||||
javax.jms.Message replyJmsMessage = createJmsTextMessage("My reply");
|
||||
given(jmsTemplate.sendAndReceive(eq(destination), anyObject())).willReturn(replyJmsMessage);
|
||||
given(this.jmsTemplate.sendAndReceive(eq(destination), any())).willReturn(replyJmsMessage);
|
||||
|
||||
String reply = messagingTemplate.convertSendAndReceive("my Payload", String.class);
|
||||
verify(jmsTemplate, times(1)).sendAndReceive(eq(destination), anyObject());
|
||||
String reply = this.messagingTemplate.convertSendAndReceive("my Payload", String.class);
|
||||
verify(this.jmsTemplate, times(1)).sendAndReceive(eq(destination), any());
|
||||
assertEquals("My reply", reply);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertSendAndReceiveDefaultDestinationName() throws JMSException {
|
||||
messagingTemplate.setDefaultDestinationName("myQueue");
|
||||
this.messagingTemplate.setDefaultDestinationName("myQueue");
|
||||
javax.jms.Message replyJmsMessage = createJmsTextMessage("My reply");
|
||||
given(jmsTemplate.sendAndReceive(eq("myQueue"), anyObject())).willReturn(replyJmsMessage);
|
||||
given(this.jmsTemplate.sendAndReceive(eq("myQueue"), any())).willReturn(replyJmsMessage);
|
||||
|
||||
String reply = messagingTemplate.convertSendAndReceive("my Payload", String.class);
|
||||
verify(jmsTemplate, times(1)).sendAndReceive(eq("myQueue"), anyObject());
|
||||
String reply = this.messagingTemplate.convertSendAndReceive("my Payload", String.class);
|
||||
verify(this.jmsTemplate, times(1)).sendAndReceive(eq("myQueue"), any());
|
||||
assertEquals("My reply", reply);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertSendAndReceiveNoDefaultSet() throws JMSException {
|
||||
thrown.expect(IllegalStateException.class);
|
||||
messagingTemplate.convertSendAndReceive("my Payload", String.class);
|
||||
this.thrown.expect(IllegalStateException.class);
|
||||
this.messagingTemplate.convertSendAndReceive("my Payload", String.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -459,12 +503,12 @@ public class JmsMessagingTemplateTests {
|
|||
Message<String> message = createTextMessage();
|
||||
MessageConverter messageConverter = mock(MessageConverter.class);
|
||||
willThrow(org.springframework.jms.support.converter.MessageConversionException.class)
|
||||
.given(messageConverter).toMessage(eq(message), anyObject());
|
||||
messagingTemplate.setJmsMessageConverter(messageConverter);
|
||||
invokeMessageCreator("myQueue");
|
||||
.given(messageConverter).toMessage(eq(message), any());
|
||||
this.messagingTemplate.setJmsMessageConverter(messageConverter);
|
||||
invokeMessageCreator();
|
||||
|
||||
thrown.expect(org.springframework.messaging.converter.MessageConversionException.class);
|
||||
messagingTemplate.send("myQueue", message);
|
||||
this.thrown.expect(org.springframework.messaging.converter.MessageConversionException.class);
|
||||
this.messagingTemplate.send("myQueue", message);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -473,89 +517,86 @@ public class JmsMessagingTemplateTests {
|
|||
MessageConverter messageConverter = mock(MessageConverter.class);
|
||||
willThrow(org.springframework.jms.support.converter.MessageConversionException.class)
|
||||
.given(messageConverter).fromMessage(message);
|
||||
messagingTemplate.setJmsMessageConverter(messageConverter);
|
||||
given(jmsTemplate.receive("myQueue")).willReturn(message);
|
||||
this.messagingTemplate.setJmsMessageConverter(messageConverter);
|
||||
given(this.jmsTemplate.receive("myQueue")).willReturn(message);
|
||||
|
||||
thrown.expect(org.springframework.messaging.converter.MessageConversionException.class);
|
||||
messagingTemplate.receive("myQueue");
|
||||
this.thrown.expect(org.springframework.messaging.converter.MessageConversionException.class);
|
||||
this.messagingTemplate.receive("myQueue");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertMessageNotReadableException() throws JMSException {
|
||||
willThrow(MessageNotReadableException.class).given(jmsTemplate).receive("myQueue");
|
||||
willThrow(MessageNotReadableException.class).given(this.jmsTemplate).receive("myQueue");
|
||||
|
||||
thrown.expect(MessagingException.class);
|
||||
messagingTemplate.receive("myQueue");
|
||||
this.thrown.expect(MessagingException.class);
|
||||
this.messagingTemplate.receive("myQueue");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertDestinationResolutionExceptionOnSend() {
|
||||
Destination destination = new Destination() {};
|
||||
willThrow(DestinationResolutionException.class).given(jmsTemplate).send(eq(destination), anyObject());
|
||||
willThrow(DestinationResolutionException.class).given(this.jmsTemplate).send(eq(destination), any());
|
||||
|
||||
thrown.expect(org.springframework.messaging.core.DestinationResolutionException.class);
|
||||
messagingTemplate.send(destination, createTextMessage());
|
||||
this.thrown.expect(org.springframework.messaging.core.DestinationResolutionException.class);
|
||||
this.messagingTemplate.send(destination, createTextMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertDestinationResolutionExceptionOnReceive() {
|
||||
Destination destination = new Destination() {};
|
||||
willThrow(DestinationResolutionException.class).given(jmsTemplate).receive(destination);
|
||||
willThrow(DestinationResolutionException.class).given(this.jmsTemplate).receive(destination);
|
||||
|
||||
thrown.expect(org.springframework.messaging.core.DestinationResolutionException.class);
|
||||
messagingTemplate.receive(destination);
|
||||
this.thrown.expect(org.springframework.messaging.core.DestinationResolutionException.class);
|
||||
this.messagingTemplate.receive(destination);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertMessageFormatException() throws JMSException {
|
||||
Message<String> message = createTextMessage();
|
||||
MessageConverter messageConverter = mock(MessageConverter.class);
|
||||
willThrow(MessageFormatException.class).given(messageConverter).toMessage(eq(message), anyObject());
|
||||
messagingTemplate.setJmsMessageConverter(messageConverter);
|
||||
invokeMessageCreator("myQueue");
|
||||
willThrow(MessageFormatException.class).given(messageConverter).toMessage(eq(message), any());
|
||||
this.messagingTemplate.setJmsMessageConverter(messageConverter);
|
||||
invokeMessageCreator();
|
||||
|
||||
thrown.expect(org.springframework.messaging.converter.MessageConversionException.class);
|
||||
messagingTemplate.send("myQueue", message);
|
||||
this.thrown.expect(org.springframework.messaging.converter.MessageConversionException.class);
|
||||
this.messagingTemplate.send("myQueue", message);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertMessageNotWritableException() throws JMSException {
|
||||
Message<String> message = createTextMessage();
|
||||
MessageConverter messageConverter = mock(MessageConverter.class);
|
||||
willThrow(MessageNotWriteableException.class).given(messageConverter).toMessage(eq(message), anyObject());
|
||||
messagingTemplate.setJmsMessageConverter(messageConverter);
|
||||
invokeMessageCreator("myQueue");
|
||||
willThrow(MessageNotWriteableException.class).given(messageConverter).toMessage(eq(message), any());
|
||||
this.messagingTemplate.setJmsMessageConverter(messageConverter);
|
||||
invokeMessageCreator();
|
||||
|
||||
thrown.expect(org.springframework.messaging.converter.MessageConversionException.class);
|
||||
messagingTemplate.send("myQueue", message);
|
||||
this.thrown.expect(org.springframework.messaging.converter.MessageConversionException.class);
|
||||
this.messagingTemplate.send("myQueue", message);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertInvalidDestinationExceptionOnSendAndReceiveWithName() {
|
||||
willThrow(InvalidDestinationException.class).given(jmsTemplate).sendAndReceive(eq("unknownQueue"), anyObject());
|
||||
willThrow(InvalidDestinationException.class).given(this.jmsTemplate).sendAndReceive(eq("unknownQueue"), any());
|
||||
|
||||
thrown.expect(org.springframework.messaging.core.DestinationResolutionException.class);
|
||||
messagingTemplate.sendAndReceive("unknownQueue", createTextMessage());
|
||||
this.thrown.expect(org.springframework.messaging.core.DestinationResolutionException.class);
|
||||
this.messagingTemplate.sendAndReceive("unknownQueue", createTextMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertInvalidDestinationExceptionOnSendAndReceive() {
|
||||
Destination destination = new Destination() {};
|
||||
willThrow(InvalidDestinationException.class).given(jmsTemplate).sendAndReceive(eq(destination), anyObject());
|
||||
willThrow(InvalidDestinationException.class).given(this.jmsTemplate).sendAndReceive(eq(destination), any());
|
||||
|
||||
thrown.expect(org.springframework.messaging.core.DestinationResolutionException.class);
|
||||
messagingTemplate.sendAndReceive(destination, createTextMessage());
|
||||
this.thrown.expect(org.springframework.messaging.core.DestinationResolutionException.class);
|
||||
this.messagingTemplate.sendAndReceive(destination, createTextMessage());
|
||||
}
|
||||
|
||||
private void invokeMessageCreator(String destinationName) {
|
||||
willAnswer(new Answer<Object>() {
|
||||
@Override
|
||||
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||
private void invokeMessageCreator() {
|
||||
willAnswer(invocation -> {
|
||||
MessageCreator messageCreator = (MessageCreator) invocation.getArguments()[1];
|
||||
messageCreator.createMessage(null);
|
||||
return null;
|
||||
}
|
||||
}).given(jmsTemplate).send(eq("myQueue"), anyObject());
|
||||
}).given(this.jmsTemplate).send(eq("myQueue"), any());
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -604,15 +645,11 @@ public class JmsMessagingTemplateTests {
|
|||
|
||||
protected TextMessage createTextMessage(MessageCreator creator) throws JMSException {
|
||||
Session mock = mock(Session.class);
|
||||
given(mock.createTextMessage(BDDMockito.<String> any())).willAnswer(
|
||||
new Answer<TextMessage>() {
|
||||
@Override
|
||||
public TextMessage answer(InvocationOnMock invocation) throws Throwable {
|
||||
return new StubTextMessage((String) invocation.getArguments()[0]);
|
||||
}
|
||||
});
|
||||
given(mock.createTextMessage(BDDMockito.any())).willAnswer(
|
||||
(Answer<TextMessage>) invocation ->
|
||||
new StubTextMessage((String) invocation.getArguments()[0]));
|
||||
javax.jms.Message message = creator.createMessage(mock);
|
||||
verify(mock).createTextMessage(BDDMockito.<String> any());
|
||||
verify(mock).createTextMessage(BDDMockito.any());
|
||||
return TextMessage.class.cast(message);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2017 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.
|
||||
|
|
@ -78,7 +78,7 @@ public abstract class AbstractMessageSendingTemplate<D> implements MessageSendin
|
|||
* @param messageConverter the message converter to use
|
||||
*/
|
||||
public void setMessageConverter(MessageConverter messageConverter) {
|
||||
Assert.notNull(messageConverter, "'messageConverter' must not be null");
|
||||
Assert.notNull(messageConverter, "MessageConverter must not be null");
|
||||
this.converter = messageConverter;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue