[jboss-svn-commits] JBL Code SVN: r38189 - in labs/jbossesb/trunk/product/rosetta: src/org/jboss/soa/esb/listeners/message/errors and 2 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Sep 21 07:31:19 EDT 2012


Author: tadayosi
Date: 2012-09-21 07:31:19 -0400 (Fri, 21 Sep 2012)
New Revision: 38189

Added:
   labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/listeners/message/errors/
   labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/listeners/message/errors/FactoryUnitTest.java
Modified:
   labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/listeners/message/ActionProcessingPipeline.java
   labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/listeners/message/errors/Factory.java
Log:
JBESB-3796
Add another Factory#createErrorMessage(...) method, which creates an error message from the Call information.
Change the innermost ActionProcessingPipeline#processPipeline(Message) method to use this Factory#createErrorMessage(...) method for error message creation.

Modified: labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/listeners/message/ActionProcessingPipeline.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/listeners/message/ActionProcessingPipeline.java	2012-09-20 18:29:17 UTC (rev 38188)
+++ labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/listeners/message/ActionProcessingPipeline.java	2012-09-21 11:31:19 UTC (rev 38189)
@@ -687,14 +687,14 @@
 
 							if (fault.getFaultMessage() == null)
 							{
-								faultTo(callDetails, Factory.createErrorMessage(Factory.PROCESSING_ERROR, message, ex));
+								faultTo(callDetails, Factory.createErrorMessage(Factory.PROCESSING_ERROR, message.getType(), callDetails, ex, message));
 							}
 							else
 								faultTo(callDetails, fault.getFaultMessage());
 						}
 						else if (!throwRuntime)
 						{
-							faultTo(callDetails, Factory.createErrorMessage(Factory.UNEXPECTED_ERROR, message, ex));
+							faultTo(callDetails, Factory.createErrorMessage(Factory.UNEXPECTED_ERROR, message.getType(), callDetails, ex, message));
 						}
 
 						final long totalProcTime = System.nanoTime() - start;
@@ -767,7 +767,7 @@
 			if (validationFailure != null)
 			{
 				final MessageValidationException mve = new MessageValidationException(validationFailure) ;
-				faultTo(callDetails, Factory.createErrorMessage(Factory.VALIDATION_FAILURE, message, mve));
+				faultTo(callDetails, Factory.createErrorMessage(Factory.VALIDATION_FAILURE, message.getType(), callDetails, mve, message));
 				long procTime = System.nanoTime() - start;
 				MessageCounterStatistics.getMessageCounterStatistics().update(new MessageStatusBean(procTime, message,
 					MessageStatusBean.MESSAGE_FAILED));

Modified: labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/listeners/message/errors/Factory.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/listeners/message/errors/Factory.java	2012-09-20 18:29:17 UTC (rev 38188)
+++ labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/listeners/message/errors/Factory.java	2012-09-21 11:31:19 UTC (rev 38189)
@@ -23,12 +23,16 @@
 import java.net.URI;
 
 import org.apache.log4j.Logger;
+import org.jboss.soa.esb.addressing.Call;
 import org.jboss.soa.esb.addressing.EPR;
 import org.jboss.soa.esb.couriers.FaultMessageException;
 import org.jboss.soa.esb.message.Fault;
 import org.jboss.soa.esb.message.Message;
 import org.jboss.soa.esb.message.format.MessageFactory;
 
+/**
+ * Factory for error messages.
+ */
 public class Factory
 {
 	public static final String ERROR_ATTRIBUTE = "org.jboss.soa.esb.listeners.message.errors";
@@ -50,17 +54,44 @@
 			throw new FaultMessageException(reason, code, msg, cause);
 	}
 	
+	/**
+	 * Creates an error message from the input message.
+	 * 
+	 * @param type The type of the error message.
+	 * @param input The input message.
+	 * @param problem The throwable content of this error message.
+	 * @return An error message, or the input message if the input message has no destination information.
+	 */
 	public static Message createErrorMessage (String type, Message input, Throwable problem)
 	{
 		if (input == null)
 			throw new IllegalArgumentException();
 		
-		Message errorMessage = MessageFactory.getInstance().getMessage(input.getType());
+		return createErrorMessage(type, input.getType(), input.getHeader().getCall(), problem, input);
+	}
+	
+	/**
+	 * Creates an error message from the Call information of the input message.
+	 * 
+	 * @param type The type of this error message.
+	 * @param messageType The type of the input message.
+	 * @param call The Call information of the input message 
+	 * @param problem The throwable content of this error message.
+	 * @param defaultError The default error message.
+	 * @return An error message, or the default error message if the Call has no destination information.
+	 */
+	// JBESB-3796
+	public static Message createErrorMessage (String type, URI messageType, Call call, Throwable problem, Message defaultError)
+	{
+		if (call == null)
+			throw new IllegalArgumentException();
 		
+		Message errorMessage = MessageFactory.getInstance().getMessage(messageType);
+		
 		if (errorMessage == null)
-			throw new IllegalArgumentException("Could not create error message from "+input.getType());
+			throw new IllegalArgumentException("Could not create error message from "+messageType);
 		
-		if (modifyMessage(input, errorMessage))
+		if (modifyMessage(call, errorMessage))
 		{
 			errorMessage.getBody().add(Fault.THROWABLE_CONTENT, problem);
 			
@@ -85,30 +116,29 @@
 			return errorMessage;
 		}
 		else
-			return input;
+			return defaultError;
 	}
 	
 	/**
-	 * Where should the error message go? Check the header of the original
+	 * Where should the error message go? Check the Call header of the original
 	 * input message.
 	 */
-	
-	private final static boolean modifyMessage (Message input, Message errorMessage)
+	private final static boolean modifyMessage (Call call, Message errorMessage)
 	{
-		EPR destination = input.getHeader().getCall().getFaultTo();
+		EPR destination = call.getFaultTo();
 		
-		if ((destination == null) && (input.getHeader().getCall().getReplyTo() != null))
-			destination = input.getHeader().getCall().getReplyTo();
+		if ((destination == null) && (call.getReplyTo() != null))
+			destination = call.getReplyTo();
 		
-		if ((destination == null) && (input.getHeader().getCall().getFrom() != null))
-			destination = input.getHeader().getCall().getFrom();
+		if ((destination == null) && (call.getFrom() != null))
+			destination = call.getFrom();
 		
 		if (destination != null)
 		{
 			errorMessage.getHeader().getCall().setTo(destination);
 			
-			if (input.getHeader().getCall().getMessageID() != null)
-				errorMessage.getHeader().getCall().setRelatesTo(input.getHeader().getCall().getMessageID());
+			if (call.getMessageID() != null)
+				errorMessage.getHeader().getCall().setRelatesTo(call.getMessageID());
 			
 			return true;
 		}

Added: labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/listeners/message/errors/FactoryUnitTest.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/listeners/message/errors/FactoryUnitTest.java	                        (rev 0)
+++ labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/listeners/message/errors/FactoryUnitTest.java	2012-09-21 11:31:19 UTC (rev 38189)
@@ -0,0 +1,119 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * (C) 2005-2008, JBoss Inc.
+ */
+package org.jboss.soa.esb.listeners.message.errors;
+
+import static org.junit.Assert.*;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import org.jboss.soa.esb.addressing.Call;
+import org.jboss.soa.esb.addressing.EPR;
+import org.jboss.soa.esb.couriers.FaultMessageException;
+import org.jboss.soa.esb.message.Fault;
+import org.jboss.soa.esb.message.Message;
+import org.jboss.soa.esb.message.format.MessageFactory;
+import org.junit.Test;
+
+/**
+ * Unit tests for {@link Factory}.
+ */
+public class FactoryUnitTest {
+
+	@Test(expected = FaultMessageException.class)
+	public void testCreateExceptionFromFault() throws FaultMessageException, URISyntaxException {
+		Message faultMessage = MessageFactory.getInstance().getMessage();
+		faultMessage.getFault().setCode(new URI("test.code"));
+		faultMessage.getFault().setReason("test.reason");
+		faultMessage.getFault().setCause(new Exception());
+		Factory.createExceptionFromFault(faultMessage);
+	}
+
+	@Test
+	public void testCreateErrorMessage_ok() throws URISyntaxException {
+		Message input = MessageFactory.getInstance().getMessage();
+		setHeader(input, "test.message.id", "test.from", "test.to", "test.reply.to", "test.fault.to");
+		Exception exception = new Exception("dummy exception");
+		Message errorMessage = Factory.createErrorMessage(Factory.PROCESSING_ERROR, input, exception);
+
+		assertNotNull(errorMessage);
+		assertNotSame(input, errorMessage);
+		assertEquals(input.getType(), errorMessage.getType());
+		assertCall(errorMessage.getHeader().getCall(), "test.fault.to", "test.message.id");
+		assertFault(errorMessage, exception, Factory.PROCESSING_ERROR);
+	}
+
+	@Test
+	public void testCreateErrorMessage_noDestination() {
+		Message noDestination = MessageFactory.getInstance().getMessage(); // message without destination
+		Message errorMessage = Factory.createErrorMessage(Factory.PROCESSING_ERROR, noDestination, new Exception());
+
+		assertSame(noDestination, errorMessage);
+	}
+
+	@Test
+	public void testCreateErrorMessage_fromCall_ok() throws URISyntaxException {
+		Message input = MessageFactory.getInstance().getMessage();
+		setHeader(input, "test.message.id", "test.from", "test.to", "test.reply.to", "test.fault.to");
+		Exception exception = new Exception("dummy exception");
+		Message defaultError = MessageFactory.getInstance().getMessage();
+		Message errorMessage = Factory.createErrorMessage(Factory.PROCESSING_ERROR, input.getType(), input.getHeader()
+				.getCall(), exception, defaultError);
+
+		assertNotNull(errorMessage);
+		assertNotSame(defaultError, errorMessage);
+		assertEquals(input.getType(), errorMessage.getType());
+		assertCall(errorMessage.getHeader().getCall(), "test.fault.to", "test.message.id");
+		assertFault(errorMessage, exception, Factory.PROCESSING_ERROR);
+	}
+
+	@Test
+	public void testCreateErrorMessage_fromCall_noDestination() {
+		Message noDestination = MessageFactory.getInstance().getMessage(); // message without destination
+		Message defaultError = MessageFactory.getInstance().getMessage();
+		Message errorMessage = Factory.createErrorMessage(Factory.PROCESSING_ERROR, noDestination.getType(),
+				noDestination.getHeader().getCall(), new Exception(), defaultError);
+
+		assertSame(defaultError, errorMessage);
+	}
+
+	private static void setHeader(Message input, String messageID, String from, String to, String replyTo,
+			String faultTo) throws URISyntaxException {
+		Call call = input.getHeader().getCall();
+		call.setMessageID(new URI(messageID));
+		call.setFrom(new EPR(new URI(from)));
+		call.setTo(new EPR(new URI(to)));
+		call.setReplyTo(new EPR(new URI(replyTo)));
+		call.setFaultTo(new EPR(new URI(faultTo)));
+	}
+
+	private static void assertCall(Call actualCall, String expectedTo, String expectedRelatesTo)
+			throws URISyntaxException {
+		assertEquals(new URI(expectedTo), actualCall.getTo().getURI());
+		assertEquals(new URI(expectedRelatesTo), actualCall.getRelatesTo());
+	}
+
+	private static void assertFault(Message actualMessage, Throwable expectedThrowable, String expectedFaultCode)
+			throws URISyntaxException {
+		assertEquals(expectedThrowable, actualMessage.getBody().get(Fault.THROWABLE_CONTENT));
+		assertEquals(new URI(expectedFaultCode), actualMessage.getFault().getCode());
+		assertEquals(expectedThrowable.toString(), actualMessage.getFault().getReason());
+	}
+}


Property changes on: labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/listeners/message/errors/FactoryUnitTest.java
___________________________________________________________________
Added: svn:mime-type
   + text/plain



More information about the jboss-svn-commits mailing list