From e07a8ace15a1e015210b7ed51f210233a7ad9f26 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Thu, 24 Jun 2010 12:46:50 +0000 Subject: [PATCH] INT-1194, MOdified the ErrorMessage to allow for headers to be set as well as Message builder which builds Message as ErrorMessage if payload is of type Throwable --- .../integration/message/ErrorMessage.java | 7 ++ .../integration/message/MessageBuilder.java | 9 ++- ...wayInvokingMessageHandlerTests-context.xml | 37 +++++++++- .../GatewayInvokingMessageHandlerTests.java | 74 ++++++++++++++++++- 4 files changed, 120 insertions(+), 7 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/message/ErrorMessage.java b/spring-integration-core/src/main/java/org/springframework/integration/message/ErrorMessage.java index efd73833b3..ca8e84f350 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/message/ErrorMessage.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/message/ErrorMessage.java @@ -16,10 +16,13 @@ package org.springframework.integration.message; +import java.util.Map; + /** * A message implementation that accepts a {@link Throwable} payload. * * @author Mark Fisher + * @author Oleg Zhurakousky */ public class ErrorMessage extends GenericMessage { @@ -28,5 +31,9 @@ public class ErrorMessage extends GenericMessage { public ErrorMessage(Throwable payload) { super(payload); } + + public ErrorMessage(Throwable payload, Map headers) { + super(payload, headers); + } } diff --git a/spring-integration-core/src/main/java/org/springframework/integration/message/MessageBuilder.java b/spring-integration-core/src/main/java/org/springframework/integration/message/MessageBuilder.java index b9253dd0d6..608f108036 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/message/MessageBuilder.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/message/MessageBuilder.java @@ -32,6 +32,7 @@ import org.springframework.util.StringUtils; /** * @author Arjen Poutsma * @author Mark Fisher + * @author Oleg Zhurakousky */ public final class MessageBuilder { @@ -196,11 +197,17 @@ public final class MessageBuilder { return this.setHeader(MessageHeaders.PRIORITY, priority); } + @SuppressWarnings("unchecked") public Message build() { if (!this.modified && this.originalMessage != null) { return this.originalMessage; } - return new GenericMessage(this.payload, this.headers); + if (payload instanceof Throwable){ + Throwable t = (Throwable) payload; + return (Message) new ErrorMessage(t, this.headers); + } else { + return new GenericMessage(this.payload, this.headers); + } } private void verifyType(String headerName, Object headerValue) { diff --git a/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayInvokingMessageHandlerTests-context.xml b/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayInvokingMessageHandlerTests-context.xml index 8c04ebe14d..b5586e9502 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayInvokingMessageHandlerTests-context.xml +++ b/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayInvokingMessageHandlerTests-context.xml @@ -9,6 +9,37 @@ default-request-channel="inputA" default-reply-channel="inputB" service-interface="org.springframework.integration.gateway.GatewayInvokingMessageHandlerTests$SimpleGateway"/> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -18,7 +49,7 @@ - + @@ -31,7 +62,7 @@ - + @@ -40,7 +71,7 @@ - + diff --git a/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayInvokingMessageHandlerTests.java b/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayInvokingMessageHandlerTests.java index 05f7413a84..0e330255a0 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayInvokingMessageHandlerTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayInvokingMessageHandlerTests.java @@ -20,16 +20,20 @@ import junit.framework.Assert; import org.junit.Test; import org.junit.runner.RunWith; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.integration.channel.SubscribableChannel; import org.springframework.integration.core.Message; +import org.springframework.integration.message.ErrorMessage; +import org.springframework.integration.message.MessageBuilder; import org.springframework.integration.message.MessageHandler; +import org.springframework.integration.message.MessageHandlingException; import org.springframework.integration.message.StringMessage; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import com.sun.xml.internal.messaging.saaj.packaging.mime.MessagingException; + /** * @author Oleg Zhurakousky * @since 2.0 @@ -46,6 +50,10 @@ public class GatewayInvokingMessageHandlerTests { @Qualifier("simpleGateway") SimpleGateway gateway; + @Autowired + @Qualifier("gatewayWithError") + SimpleGateway gatewayWithError; + @Autowired @Qualifier("inputB") SubscribableChannel output; @@ -74,14 +82,74 @@ public class GatewayInvokingMessageHandlerTests { String result = gateway.sendRecieve("hello"); Assert.assertEquals("echo:echo:echo:hello", result); } + + @Test + public void validateGatewayWithErrorMessageReturned() { + try { + gatewayWithError.sendRecieve("echoWithErrorMessageChannel"); + Assert.fail(); + } catch (Exception e) { + Assert.assertEquals("echoWithErrorMessageChannel", e.getMessage()); + } + + try { + gatewayWithError.sendRecieve("echoWithRuntimeExceptionChannel"); + Assert.fail(); + } catch (Exception e) { + Assert.assertEquals("echoWithRuntimeExceptionChannel", e.getMessage()); + } + + try { + gatewayWithError.sendRecieve("echoWithMessagingExceptionChannel"); + Assert.fail(); + } catch (MessageHandlingException e) { + Assert.assertEquals("echoWithMessagingExceptionChannel", e.getFailedMessage().getPayload()); + } + + try { + gatewayWithError.sendRecieve("echoWithCheckedExceptionChannel"); + Assert.fail(); + } catch (Exception e) { + Assert.assertEquals("echoWithCheckedExceptionChannel", e.getCause().getMessage()); + } + + //String result = gatewayWithError.sendRecieve("echoWithErrorMessageChannel"); + //System.out.println("Result: " + result); + //Assert.assertEquals("echo:echo:echo:hello", result); + } + public static interface SimpleGateway { public String sendRecieve(String str); } public static class SimpleService { - public String echo(String str) { - return "echo:" + str; + public String echo(String value) { + return "echo:" + value; + } + public Message echoWithErrorMessage(String value) { + return MessageBuilder.withPayload(new RuntimeException(value)).build(); + } + public RuntimeException echoWithRuntimeException(String value) { + return new RuntimeException(value); + } + public MessageHandlingException echoWithMessagingException(String value) { + return new MessageHandlingException(new StringMessage(value)); + } + public SampleCheckedException echoWithCheckedException(String value) { + return new SampleCheckedException(value); + } + public String echoWithRuntimeExceptionThrown(String value) { + throw new RuntimeException(value); + } + public String echoWithMessagingExceptionThrown(String value) { + throw new MessageHandlingException(new StringMessage(value)); + } + } + + public static class SampleCheckedException extends Exception { + public SampleCheckedException(String message){ + super(message); } }