From 12a9df8a1c1a24afdaabd615de33ed265250fa4d Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 28 May 2014 16:48:48 +0200 Subject: [PATCH] Throw a proper exception if no convert is found Prior to this commit, no exception was raised if a message could not be converted to the requested payload because no suitable converter were found. This commit adds an explicit check if the converted payload is null. Issue: SPR-11817 --- .../jms/messaging/JmsMessagingTemplate.java | 3 +- .../messaging/JmsMessagingTemplateTests.java | 4 +-- .../AbstractMessageReceivingTemplate.java | 16 ++++++++- .../core/MessageReceivingTemplateTests.java | 36 +++++++++++++++++++ 4 files changed, 53 insertions(+), 6 deletions(-) diff --git a/spring-jms/src/main/java/org/springframework/jms/messaging/JmsMessagingTemplate.java b/spring-jms/src/main/java/org/springframework/jms/messaging/JmsMessagingTemplate.java index 4e139446c6..6a281ec096 100644 --- a/spring-jms/src/main/java/org/springframework/jms/messaging/JmsMessagingTemplate.java +++ b/spring-jms/src/main/java/org/springframework/jms/messaging/JmsMessagingTemplate.java @@ -194,11 +194,10 @@ public class JmsMessagingTemplate } @Override - @SuppressWarnings("unchecked") public T receiveAndConvert(String destinationName, Class targetClass) throws MessagingException { Message message = doReceive(destinationName); if (message != null) { - return (T) getMessageConverter().fromMessage(message, targetClass); + return doConvert(message, targetClass); } else { return null; diff --git a/spring-jms/src/test/java/org/springframework/jms/messaging/JmsMessagingTemplateTests.java b/spring-jms/src/test/java/org/springframework/jms/messaging/JmsMessagingTemplateTests.java index b3e1ed403c..9b3ede44fd 100644 --- a/spring-jms/src/test/java/org/springframework/jms/messaging/JmsMessagingTemplateTests.java +++ b/spring-jms/src/test/java/org/springframework/jms/messaging/JmsMessagingTemplateTests.java @@ -26,7 +26,6 @@ import javax.jms.Session; import javax.jms.TextMessage; import org.junit.Before; -import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -326,12 +325,11 @@ public class JmsMessagingTemplateTests { } @Test - @Ignore("SPR-11817") public void receiveAndConvertNoConverter() { javax.jms.Message jmsMessage = createJmsTextMessage("Hello"); given(jmsTemplate.receive("myQueue")).willReturn(jmsMessage); - thrown.expect(MessageConversionException.class); + thrown.expect(org.springframework.messaging.converter.MessageConversionException.class); messagingTemplate.receiveAndConvert("myQueue", Writer.class); } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/core/AbstractMessageReceivingTemplate.java b/spring-messaging/src/main/java/org/springframework/messaging/core/AbstractMessageReceivingTemplate.java index 55c4bc9849..2debce8b6c 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/core/AbstractMessageReceivingTemplate.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/core/AbstractMessageReceivingTemplate.java @@ -17,6 +17,8 @@ package org.springframework.messaging.core; import org.springframework.messaging.Message; +import org.springframework.messaging.converter.MessageConversionException; +import org.springframework.messaging.converter.MessageConverter; /** * An extension of {@link AbstractMessageSendingTemplate} that adds support for @@ -53,11 +55,23 @@ public abstract class AbstractMessageReceivingTemplate extends AbstractMessag public T receiveAndConvert(D destination, Class targetClass) { Message message = doReceive(destination); if (message != null) { - return (T) getMessageConverter().fromMessage(message, targetClass); + return doConvert(message, targetClass); } else { return null; } } + @SuppressWarnings("unchecked") + protected T doConvert(Message message, Class targetClass) { + MessageConverter messageConverter = getMessageConverter(); + T value = (T) messageConverter.fromMessage(message, targetClass); + if (value == null) { + throw new MessageConversionException("Unable to convert payload='" + + message.getPayload() + "' to type='" + targetClass + + "', converter=[" + messageConverter + "]"); + } + return value; + } + } diff --git a/spring-messaging/src/test/java/org/springframework/messaging/core/MessageReceivingTemplateTests.java b/spring-messaging/src/test/java/org/springframework/messaging/core/MessageReceivingTemplateTests.java index 688b9c80a4..29260dc482 100644 --- a/spring-messaging/src/test/java/org/springframework/messaging/core/MessageReceivingTemplateTests.java +++ b/spring-messaging/src/test/java/org/springframework/messaging/core/MessageReceivingTemplateTests.java @@ -17,13 +17,23 @@ package org.springframework.messaging.core; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; + +import org.springframework.core.convert.ConversionFailedException; +import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.messaging.Message; +import org.springframework.messaging.converter.GenericMessageConverter; +import org.springframework.messaging.converter.MessageConversionException; import org.springframework.messaging.support.GenericMessage; +import static org.hamcrest.CoreMatchers.isA; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertSame; +import java.io.Writer; + /** * Unit tests for receiving operations in {@link AbstractMessagingTemplate}. * @@ -33,6 +43,9 @@ import static org.junit.Assert.assertSame; */ public class MessageReceivingTemplateTests { + @Rule + public final ExpectedException thrown = ExpectedException.none(); + private TestMessagingTemplate template; @@ -88,6 +101,29 @@ public class MessageReceivingTemplateTests { assertSame("payload", payload); } + @Test + public void receiveAndConvertFailed() { + Message expected = new GenericMessage("not a number test"); + this.template.setReceiveMessage(expected); + this.template.setMessageConverter(new GenericMessageConverter(new DefaultConversionService())); + + thrown.expect(MessageConversionException.class); + thrown.expectCause(isA(ConversionFailedException.class)); + this.template.receiveAndConvert("somewhere", Integer.class); + } + + @Test + public void receiveAndConvertNoConverter() { + Message expected = new GenericMessage("payload"); + this.template.setDefaultDestination("home"); + this.template.setReceiveMessage(expected); + this.template.setMessageConverter(new GenericMessageConverter(new DefaultConversionService())); + + thrown.expect(MessageConversionException.class); + thrown.expectMessage("payload"); + this.template.receiveAndConvert(Writer.class); + } + private static class TestMessagingTemplate extends AbstractMessagingTemplate {