You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2011/09/24 13:09:06 UTC

svn commit: r1175157 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/processor/ test/java/org/apache/camel/processor/

Author: davsclaus
Date: Sat Sep 24 11:09:06 2011
New Revision: 1175157

URL: http://svn.apache.org/viewvc?rev=1175157&view=rev
Log:
CAMEL-4484: Added unit tests for other similar EIPs

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DynamicRouterThrowExceptionFromExpressionTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RoutingSlipThrowExceptionFromExpressionTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DynamicRouter.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/RoutingSlip.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DynamicRouter.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DynamicRouter.java?rev=1175157&r1=1175156&r2=1175157&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DynamicRouter.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DynamicRouter.java Sat Sep 24 11:09:06 2011
@@ -43,7 +43,7 @@ public class DynamicRouter extends Routi
     }
 
     @Override
-    protected RoutingSlipIterator createRoutingSlipIterator(Exchange exchange) {
+    protected RoutingSlipIterator createRoutingSlipIterator(Exchange exchange) throws Exception {
         return new DynamicRoutingSlipIterator(expression);
     }
 

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/RoutingSlip.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/RoutingSlip.java?rev=1175157&r1=1175156&r2=1175157&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/RoutingSlip.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/RoutingSlip.java Sat Sep 24 11:09:06 2011
@@ -127,7 +127,9 @@ public class RoutingSlip extends Service
 
     public boolean process(Exchange exchange, AsyncCallback callback) {
         if (!isStarted()) {
-            throw new IllegalStateException("RoutingSlip has not been started: " + this);
+            exchange.setException(new IllegalStateException("RoutingSlip has not been started: " + this));
+            callback.done(true);
+            return true;
         }
 
         return doRoutingSlip(exchange, callback);
@@ -148,8 +150,13 @@ public class RoutingSlip extends Service
      * @param exchange the exchange
      * @return the iterator, should never be <tt>null</tt>
      */
-    protected RoutingSlipIterator createRoutingSlipIterator(final Exchange exchange) {
+    protected RoutingSlipIterator createRoutingSlipIterator(final Exchange exchange) throws Exception {
         Object slip = expression.evaluate(exchange, Object.class);
+        if (exchange.getException() != null) {
+            // force any exceptions occurred during evaluation to be thrown
+            throw exchange.getException();
+        }
+
         final Iterator<Object> delegate = ObjectHelper.createIterator(slip, uriDelimiter);
 
         return new RoutingSlipIterator() {
@@ -165,7 +172,14 @@ public class RoutingSlip extends Service
 
     private boolean doRoutingSlip(Exchange exchange, AsyncCallback callback) {
         Exchange current = exchange;
-        RoutingSlipIterator iter = createRoutingSlipIterator(exchange);
+        RoutingSlipIterator iter;
+        try {
+            iter = createRoutingSlipIterator(exchange);
+        } catch (Exception e) {
+            exchange.setException(e);
+            callback.done(true);
+            return true;
+        }
 
         // ensure the slip is empty when we start
         if (current.hasProperties()) {

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DynamicRouterThrowExceptionFromExpressionTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DynamicRouterThrowExceptionFromExpressionTest.java?rev=1175157&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DynamicRouterThrowExceptionFromExpressionTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DynamicRouterThrowExceptionFromExpressionTest.java Sat Sep 24 11:09:06 2011
@@ -0,0 +1,62 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.processor;
+
+import java.util.List;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.ExpressionEvaluationException;
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ *
+ */
+public class DynamicRouterThrowExceptionFromExpressionTest extends ContextTestSupport {
+
+    public void testDynamicRouterAndVerifyException() throws Exception {
+        getMockEndpoint("mock:error").expectedMessageCount(1);
+        getMockEndpoint("mock:result").expectedMessageCount(0);
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                onException(ExpressionEvaluationException.class)
+                    .handled(true)
+                    .to("mock://error");
+
+                from("direct://start")
+                    .to("log:foo")
+                    .dynamicRouter().method(DynamicRouterThrowExceptionFromExpressionTest.class, "routeTo")
+                        .to("mock://result")
+                    .end();
+            }
+        };
+    }
+
+    public List<String> routeTo(Exchange exchange) throws ExpressionEvaluationException {
+        throw new ExpressionEvaluationException(null, exchange, null);
+    }
+
+}

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RoutingSlipThrowExceptionFromExpressionTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RoutingSlipThrowExceptionFromExpressionTest.java?rev=1175157&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RoutingSlipThrowExceptionFromExpressionTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RoutingSlipThrowExceptionFromExpressionTest.java Sat Sep 24 11:09:06 2011
@@ -0,0 +1,62 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.processor;
+
+import java.util.List;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.ExpressionEvaluationException;
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ *
+ */
+public class RoutingSlipThrowExceptionFromExpressionTest extends ContextTestSupport {
+
+    public void testRoutingSlipAndVerifyException() throws Exception {
+        getMockEndpoint("mock:error").expectedMessageCount(1);
+        getMockEndpoint("mock:result").expectedMessageCount(0);
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                onException(ExpressionEvaluationException.class)
+                    .handled(true)
+                    .to("mock://error");
+
+                from("direct://start")
+                    .to("log:foo")
+                    .routingSlip().method(RoutingSlipThrowExceptionFromExpressionTest.class, "slipTo")
+                        .to("mock://result")
+                    .end();
+            }
+        };
+    }
+
+    public List<String> slipTo(Exchange exchange) throws ExpressionEvaluationException {
+        throw new ExpressionEvaluationException(null, exchange, null);
+    }
+
+}