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 2013/02/28 08:04:19 UTC

svn commit: r1451119 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/model/ components/camel-spring/src/test/java/org/apache/camel/spring/processor/ components/camel-spring/src/test/resources/org/apache/camel/spring/processor/

Author: davsclaus
Date: Thu Feb 28 07:04:19 2013
New Revision: 1451119

URL: http://svn.apache.org/r1451119
Log:
CAMEL-6109: Camel now validates if try catch finally has been mis configured when used in routes.

Added:
    camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringTryCatchMisconfiguredTest.java
      - copied, changed from r1451097, camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringTryCatchMustHaveExceptionConfiguredTest.java
    camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringTryCatchMisconfiguredFinallyTest.xml
    camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringTryCatchMisconfiguredTest.xml
      - copied, changed from r1451097, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringTryCatchMustHaveExceptionConfiguredTest.xml
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/CatchDefinition.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/FinallyDefinition.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/TryDefinition.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/CatchDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/CatchDefinition.java?rev=1451119&r1=1451118&r2=1451119&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/CatchDefinition.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/CatchDefinition.java Thu Feb 28 07:04:19 2013
@@ -95,6 +95,11 @@ public class CatchDefinition extends Pro
             throw new IllegalArgumentException("At least one Exception must be configured to catch");
         }
 
+        // parent must be a try
+        if (!(getParent() instanceof TryDefinition)) {
+            throw new IllegalArgumentException("This doCatch should have a doTry as its parent on " + this);
+        }
+
         // do catch does not mandate a child processor
         Processor childProcessor = this.createChildProcessor(routeContext, false);
 

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/FinallyDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/FinallyDefinition.java?rev=1451119&r1=1451118&r2=1451119&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/FinallyDefinition.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/FinallyDefinition.java Thu Feb 28 07:04:19 2013
@@ -49,6 +49,11 @@ public class FinallyDefinition extends O
      
     @Override
     public Processor createProcessor(RouteContext routeContext) throws Exception {
+        // parent must be a try
+        if (!(getParent() instanceof TryDefinition)) {
+            throw new IllegalArgumentException("This doFinally should have a doTry as its parent on " + this);
+        }
+
         // do finally does mandate a child processor
         return this.createChildProcessor(routeContext, true);
     }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/TryDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/TryDefinition.java?rev=1451119&r1=1451118&r2=1451119&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/TryDefinition.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/TryDefinition.java Thu Feb 28 07:04:19 2013
@@ -20,7 +20,6 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Iterator;
 import java.util.List;
-
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlRootElement;
@@ -89,6 +88,11 @@ public class TryDefinition extends Outpu
             }
         }
 
+        // must have either a catch or finally
+        if (finallyClause == null && catchClauses == null) {
+            throw new IllegalArgumentException("doTry must have one or more catch or finally blocks on " + this);
+        }
+
         return new TryProcessor(tryProcessor, catchProcessors, finallyProcessor);
     }
 

Copied: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringTryCatchMisconfiguredTest.java (from r1451097, camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringTryCatchMustHaveExceptionConfiguredTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringTryCatchMisconfiguredTest.java?p2=camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringTryCatchMisconfiguredTest.java&p1=camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringTryCatchMustHaveExceptionConfiguredTest.java&r1=1451097&r2=1451119&rev=1451119&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringTryCatchMustHaveExceptionConfiguredTest.java (original)
+++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringTryCatchMisconfiguredTest.java Thu Feb 28 07:04:19 2013
@@ -22,23 +22,32 @@ import org.apache.camel.FailedToCreateRo
 
 import static org.apache.camel.spring.processor.SpringTestHelper.createSpringCamelContext;
 
-public class SpringTryCatchMustHaveExceptionConfiguredTest extends ContextTestSupport {
+public class SpringTryCatchMisconfiguredTest extends ContextTestSupport {
 
     protected CamelContext createCamelContext() throws Exception {
         try {
-            createSpringCamelContext(this, "org/apache/camel/spring/processor/SpringTryCatchMustHaveExceptionConfiguredTest.xml");
+            createSpringCamelContext(this, "org/apache/camel/spring/processor/SpringTryCatchMisconfiguredTest.xml");
             fail("Should have thrown exception");
         } catch (Exception e) {
             assertIsInstanceOf(FailedToCreateRouteException.class, e.getCause());
             assertIsInstanceOf(IllegalArgumentException.class, e.getCause().getCause());
-            assertEquals("At least one Exception must be configured to catch", e.getCause().getCause().getMessage());
+            assertEquals("This doCatch should have a doTry as its parent on DoCatch[ [class java.io.IOException] -> [To[mock:fail]]]", e.getCause().getCause().getMessage());
+        }
+
+        try {
+            createSpringCamelContext(this, "org/apache/camel/spring/processor/SpringTryCatchMisconfiguredFinallyTest.xml");
+            fail("Should have thrown exception");
+        } catch (Exception e) {
+            assertIsInstanceOf(FailedToCreateRouteException.class, e.getCause());
+            assertIsInstanceOf(IllegalArgumentException.class, e.getCause().getCause());
+            assertEquals("This doFinally should have a doTry as its parent on DoFinally[[To[mock:finally]]]", e.getCause().getCause().getMessage());
         }
 
         // return a working context instead, to let this test pass
         return createSpringCamelContext(this, "org/apache/camel/spring/processor/SpringTryProcessorHandledTest.xml");
     }
 
-    public void testTryCatchMustHaveExceptionConfigured() {
+    public void testTryCatchMisconfigured() {
         // noop
     }
 

Added: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringTryCatchMisconfiguredFinallyTest.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringTryCatchMisconfiguredFinallyTest.xml?rev=1451119&view=auto
==============================================================================
--- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringTryCatchMisconfiguredFinallyTest.xml (added)
+++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringTryCatchMisconfiguredFinallyTest.xml Thu Feb 28 07:04:19 2013
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    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.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="
+       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
+    ">
+
+    <camelContext xmlns="http://camel.apache.org/schema/spring">
+        <route>
+            <from uri="direct:start"/>
+            <doTry>
+                <to uri="mock:result"/>
+            </doTry>
+            <doFinally>
+                <to uri="mock:finally"/>
+            </doFinally>
+        </route>
+    </camelContext>
+
+</beans>

Copied: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringTryCatchMisconfiguredTest.xml (from r1451097, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringTryCatchMustHaveExceptionConfiguredTest.xml)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringTryCatchMisconfiguredTest.xml?p2=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringTryCatchMisconfiguredTest.xml&p1=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringTryCatchMustHaveExceptionConfiguredTest.xml&r1=1451097&r2=1451119&rev=1451119&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringTryCatchMustHaveExceptionConfiguredTest.xml (original)
+++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringTryCatchMisconfiguredTest.xml Thu Feb 28 07:04:19 2013
@@ -27,10 +27,11 @@
             <from uri="direct:start"/>
             <doTry>
                 <to uri="mock:result"/>
-                <doCatch>
-                    <to uri="mock:fail"/>
-                </doCatch>
             </doTry>
+            <doCatch>
+                <exception>java.io.IOException</exception>
+                <to uri="mock:fail"/>
+            </doCatch>
         </route>
     </camelContext>