Author: davsclaus
Date: Fri Jun 10 12:59:54 2011
New Revision: 1134300

URL: http://svn.apache.org/viewvc?rev=1134300&view=rev
Log:
CAMEL-4077: Sanitiy check route in XML DSL, that there is input and outputs in 
routes.

Added:
    
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringRouteNoFromTest.java
    
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringRouteNoOutputTest.java
    
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/SpringRouteNoFromTest.xml
      - copied, changed from r1134273, 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/CamelPropertiesTest.xml
    
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/SpringRouteNoOutputTest.xml
Modified:
    
camel/trunk/camel-core/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java
    
camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java?rev=1134300&r1=1134299&r2=1134300&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java
 Fri Jun 10 12:59:54 2011
@@ -23,6 +23,7 @@ import org.apache.camel.CamelContext;
 import org.apache.camel.builder.ErrorHandlerBuilder;
 import org.apache.camel.util.CamelContextHelper;
 import org.apache.camel.util.EndpointHelper;
+import org.apache.camel.util.ObjectHelper;
 
 /**
  * Helper for {@link RouteDefinition}
@@ -134,6 +135,32 @@ public final class RouteDefinitionHelper
         route.getOutputs().addAll(0, upper);
     }
 
+    /**
+     * Sanity check the route, that it has input(s) and outputs.
+     *
+     * @param route the route
+     * @throws IllegalArgumentException is thrown if the route is invalid
+     */
+    public static void sanityCheckRoute(RouteDefinition route) {
+        ObjectHelper.notNull(route, "route");
+
+        if (route.getInputs() == null || route.getInputs().isEmpty()) {
+            String msg = "Route has no inputs: " + route;
+            if (route.getId() != null) {
+                msg = "Route " + route.getId() + " has no inputs: " + route;
+            }
+            throw new IllegalArgumentException(msg);
+        }
+
+        if (route.getOutputs() == null || route.getOutputs().isEmpty()) {
+            String msg = "Route has no outputs: " + route;
+            if (route.getId() != null) {
+                msg = "Route " + route.getId() + " has no outputs: " + route;
+            }
+            throw new IllegalArgumentException(msg);
+        }
+    }
+
     private static void initParentAndErrorHandlerBuilder(CamelContext context, 
RouteDefinition route,
                                                          
List<ProcessorDefinition> abstracts, List<OnExceptionDefinition> onExceptions) {
 

Modified: 
camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java?rev=1134300&r1=1134299&r2=1134300&view=diff
==============================================================================
--- 
camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
 (original)
+++ 
camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
 Fri Jun 10 12:59:54 2011
@@ -313,6 +313,9 @@ public abstract class AbstractCamelConte
      */
     private void prepareRoutes() {
         for (RouteDefinition route : getRoutes()) {
+            // sanity check first as the route is created using XML
+            RouteDefinitionHelper.sanityCheckRoute(route);
+
             // leverage logic from route definition helper to prepare the route
             RouteDefinitionHelper.prepareRoute(getContext(), route, 
getOnExceptions(), getIntercepts(), getInterceptFroms(),
                     getInterceptSendToEndpoints(), getOnCompletions());

Added: 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringRouteNoFromTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringRouteNoFromTest.java?rev=1134300&view=auto
==============================================================================
--- 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringRouteNoFromTest.java
 (added)
+++ 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringRouteNoFromTest.java
 Fri Jun 10 12:59:54 2011
@@ -0,0 +1,47 @@
+/**
+ * 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.spring.config;
+
+import org.apache.camel.spring.SpringTestSupport;
+import org.springframework.context.support.AbstractXmlApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ *
+ */
+public class SpringRouteNoFromTest extends SpringTestSupport {
+
+    @Override
+    protected void setUp() throws Exception {
+        createApplicationContext();
+    }
+
+    public void testRouteNoFrom() {
+        // noop
+    }
+
+    @Override
+    protected AbstractXmlApplicationContext createApplicationContext() {
+        try {
+            return new 
ClassPathXmlApplicationContext("org/apache/camel/spring/config/SpringRouteNoFromTest.xml");
+        } catch (Exception e) {
+            IllegalArgumentException iae = (IllegalArgumentException) 
e.getCause().getCause();
+            assertEquals("Route myRoute has no inputs: Route[[] -> 
[To[mock:result]]]", iae.getMessage());
+            return null;
+        }
+    }
+}

Added: 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringRouteNoOutputTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringRouteNoOutputTest.java?rev=1134300&view=auto
==============================================================================
--- 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringRouteNoOutputTest.java
 (added)
+++ 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/SpringRouteNoOutputTest.java
 Fri Jun 10 12:59:54 2011
@@ -0,0 +1,47 @@
+/**
+ * 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.spring.config;
+
+import org.apache.camel.spring.SpringTestSupport;
+import org.springframework.context.support.AbstractXmlApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ *
+ */
+public class SpringRouteNoOutputTest extends SpringTestSupport {
+
+    @Override
+    protected void setUp() throws Exception {
+        createApplicationContext();
+    }
+
+    public void testRouteNoOutput() {
+        // noop
+    }
+
+    @Override
+    protected AbstractXmlApplicationContext createApplicationContext() {
+        try {
+            return new 
ClassPathXmlApplicationContext("org/apache/camel/spring/config/SpringRouteNoOutputTest.xml");
+        } catch (Exception e) {
+            IllegalArgumentException iae = (IllegalArgumentException) 
e.getCause().getCause();
+            assertEquals("Route myRoute has no outputs: 
Route[[From[direct:start]] -> []]", iae.getMessage());
+            return null;
+        }
+    }
+}

Copied: 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/SpringRouteNoFromTest.xml
 (from r1134273, 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/CamelPropertiesTest.xml)
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/SpringRouteNoFromTest.xml?p2=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/SpringRouteNoFromTest.xml&p1=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/CamelPropertiesTest.xml&r1=1134273&r2=1134300&rev=1134300&view=diff
==============================================================================
--- 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/CamelPropertiesTest.xml
 (original)
+++ 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/SpringRouteNoFromTest.xml
 Fri Jun 10 12:59:54 2011
@@ -22,15 +22,11 @@
        http://camel.apache.org/schema/spring 
http://camel.apache.org/schema/spring/camel-spring.xsd
     ">
 
-    <camelContext xmlns="http://camel.apache.org/schema/spring";>
-        <properties>
-            <property key="foo" value="123"/>
-            <property key="bar" value="cheese"/>
-        </properties>
-        <route>
-            <from uri="direct:start"/>
-            <to uri="mock:result"/>
-        </route>
-    </camelContext>
+  <camelContext xmlns="http://camel.apache.org/schema/spring";>
+    <!-- this route has no from -->
+    <route id="myRoute">
+      <to uri="mock:result"/>
+    </route>
+  </camelContext>
 
 </beans>

Added: 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/SpringRouteNoOutputTest.xml
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/SpringRouteNoOutputTest.xml?rev=1134300&view=auto
==============================================================================
--- 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/SpringRouteNoOutputTest.xml
 (added)
+++ 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/SpringRouteNoOutputTest.xml
 Fri Jun 10 12:59:54 2011
@@ -0,0 +1,32 @@
+<?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";>
+    <!-- this route has no outputs -->
+    <route id="myRoute">
+      <from uri="direct:start"/>
+    </route>
+  </camelContext>
+
+</beans>


Reply via email to