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/06/10 10:41:40 UTC

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

Author: davsclaus
Date: Fri Jun 10 08:41:39 2011
New Revision: 1134236

URL: http://svn.apache.org/viewvc?rev=1134236&view=rev
Log:
CAMEL-4022: Validate onException that it has been properly configured.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionMisconfiguredTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/OnExceptionDefinition.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionComplexWithNestedErrorHandlerRouteWithDefaultErrorHandlerTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/OnExceptionDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/OnExceptionDefinition.java?rev=1134236&r1=1134235&r2=1134236&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/OnExceptionDefinition.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/OnExceptionDefinition.java Fri Jun 10 08:41:39 2011
@@ -138,25 +138,14 @@ public class OnExceptionDefinition exten
     }
 
     public void addRoutes(RouteContext routeContext, Collection<Route> routes) throws Exception {
-        if (isInheritErrorHandler() != null && isInheritErrorHandler()) {
-            throw new IllegalArgumentException(this + " cannot have the inheritErrorHandler option set to true");
-        }
 
         setHandledFromExpressionType(routeContext);
         setContinuedFromExpressionType(routeContext);
         setRetryWhileFromExpressionType(routeContext);
+        setOnRedeliveryFromRedeliveryRef(routeContext);
 
-        // only one of handled or continued is allowed
-        if (getHandledPolicy() != null && getContinuedPolicy() != null) {
-            throw new IllegalArgumentException("Only one of handled or continued is allowed to be configured on: " + this);
-        }
-
-        // lookup onRedelivery if ref is provided
-        if (ObjectHelper.isNotEmpty(onRedeliveryRef)) {
-            // if ref is provided then use mandatory lookup to fail if not found
-            Processor onRedelivery = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), onRedeliveryRef, Processor.class);
-            setOnRedelivery(onRedelivery);
-        }
+        // must validate configuration before creating processor
+        validateConfiguration();
 
         // lets attach this on exception to the route error handler
         Processor child = routeContext.createProcessor(this);
@@ -175,6 +164,9 @@ public class OnExceptionDefinition exten
 
     @Override
     public CatchProcessor createProcessor(RouteContext routeContext) throws Exception {
+        // must validate configuration before creating processor
+        validateConfiguration();
+
         Processor childProcessor = this.createChildProcessor(routeContext, false);
 
         Predicate when = null;
@@ -187,12 +179,32 @@ public class OnExceptionDefinition exten
             handle = handled.createPredicate(routeContext);
         }
 
+        return new CatchProcessor(getExceptionClasses(), childProcessor, when, handle);
+    }
+
+    protected void validateConfiguration() {
+        if (isInheritErrorHandler() != null && isInheritErrorHandler()) {
+            throw new IllegalArgumentException(this + " cannot have the inheritErrorHandler option set to true");
+        }
+
         List<Class> exceptions = getExceptionClasses();
         if (exceptions.isEmpty()) {
             throw new IllegalArgumentException("At least one exception must be configured on " + this);
         }
 
-        return new CatchProcessor(exceptions, childProcessor, when, handle);
+        // only one of handled or continued is allowed
+        if (getHandledPolicy() != null && getContinuedPolicy() != null) {
+            throw new IllegalArgumentException("Only one of handled or continued is allowed to be configured on: " + this);
+        }
+
+        // validate that at least some option is set as you cannot just have onException(Exception.class);
+        if (outputs == null || getOutputs().isEmpty()) {
+            // no outputs so there should be some sort of configuration
+            if (handledPolicy == null && continuedPolicy == null && retryWhilePolicy == null
+                    && redeliveryPolicy == null && useOriginalMessagePolicy == null && onRedelivery == null) {
+                throw new IllegalArgumentException(this + " is not configured.");
+            }
+        }
     }
 
     // Fluent API
@@ -853,4 +865,14 @@ public class OnExceptionDefinition exten
             retryWhile(getRetryWhile().createPredicate(routeContext));
         }
     }
+
+    private void setOnRedeliveryFromRedeliveryRef(RouteContext routeContext) {
+        // lookup onRedelivery if ref is provided
+        if (ObjectHelper.isNotEmpty(onRedeliveryRef)) {
+            // if ref is provided then use mandatory lookup to fail if not found
+            Processor onRedelivery = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), onRedeliveryRef, Processor.class);
+            setOnRedelivery(onRedelivery);
+        }
+    }
+
 }

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionComplexWithNestedErrorHandlerRouteWithDefaultErrorHandlerTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionComplexWithNestedErrorHandlerRouteWithDefaultErrorHandlerTest.java?rev=1134236&r1=1134235&r2=1134236&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionComplexWithNestedErrorHandlerRouteWithDefaultErrorHandlerTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionComplexWithNestedErrorHandlerRouteWithDefaultErrorHandlerTest.java Fri Jun 10 08:41:39 2011
@@ -48,7 +48,7 @@ public class OnExceptionComplexWithNeste
                 from("direct:start")
                     // route specific on exception for MyFunctionalException
                     // we MUST use .end() to indicate that this sub block is ended
-                    .onException(MyFunctionalException.class).end()
+                    .onException(MyFunctionalException.class).handled(false).end()
                     .to("bean:myServiceBean")
                     .to("mock:result");
 

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionMisconfiguredTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionMisconfiguredTest.java?rev=1134236&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionMisconfiguredTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionMisconfiguredTest.java Fri Jun 10 08:41:39 2011
@@ -0,0 +1,157 @@
+/**
+ * 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.onexception;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.FailedToCreateRouteException;
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ *
+ */
+public class OnExceptionMisconfiguredTest extends ContextTestSupport {
+
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+
+    public void testOnExceptionMisconfigured() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                onException(Exception.class);
+
+                from("direct:start").to("mock:result");
+            }
+        });
+        try {
+            context.start();
+            fail("Should have thrown exception");
+        } catch (FailedToCreateRouteException e) {
+            IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
+            assertEquals("OnException[[class java.lang.Exception] -> []] is not configured.", iae.getMessage());
+        }
+    }
+
+    public void testOnExceptionMisconfigured2() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                onException(Exception.class).end();
+
+                from("direct:start").to("mock:result");
+            }
+        });
+        try {
+            context.start();
+            fail("Should have thrown exception");
+        } catch (FailedToCreateRouteException e) {
+            IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
+            assertEquals("OnException[[class java.lang.Exception] -> []] is not configured.", iae.getMessage());
+        }
+    }
+
+    public void testOnExceptionMisconfigured3() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                onException();
+
+                from("direct:start").to("mock:result");
+            }
+        });
+        try {
+            context.start();
+            fail("Should have thrown exception");
+        } catch (FailedToCreateRouteException e) {
+            IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
+            assertEquals("OnException[[class java.lang.Exception] -> []] is not configured.", iae.getMessage());
+        }
+    }
+
+    public void testOnExceptionMisconfigured4() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                onException().end();
+
+                from("direct:start").to("mock:result");
+            }
+        });
+        try {
+            context.start();
+            fail("Should have thrown exception");
+        } catch (FailedToCreateRouteException e) {
+            IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
+            assertEquals("OnException[[class java.lang.Exception] -> []] is not configured.", iae.getMessage());
+        }
+    }
+
+    public void testOnExceptionNotMisconfigured() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                onException().handled(true);
+
+                from("direct:start").to("mock:result");
+            }
+        });
+        context.start();
+        // okay
+    }
+
+    public void testOnExceptionNotMisconfigured2() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                onException().continued(true);
+
+                from("direct:start").to("mock:result");
+            }
+        });
+        context.start();
+        // okay
+    }
+
+    public void testOnExceptionNotMisconfigured3() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                onException(Exception.class).handled(true);
+
+                from("direct:start").to("mock:result");
+            }
+        });
+        context.start();
+        // okay
+    }
+
+    public void testOnExceptionNotMisconfigured4() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                onException(Exception.class).continued(true);
+
+                from("direct:start").to("mock:result");
+            }
+        });
+        context.start();
+        // okay
+    }
+
+}