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 2010/07/09 09:24:19 UTC

svn commit: r962430 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/language/bean/ main/java/org/apache/camel/model/ main/java/org/apache/camel/model/language/ test/java/org/apache/camel/component/bean/ test/java/org/apache/camel/view/

Author: davsclaus
Date: Fri Jul  9 07:24:18 2010
New Revision: 962430

URL: http://svn.apache.org/viewvc?rev=962430&view=rev
Log:
CAMEL-2920: Eager validation for beanRef and method ref that the bean ref exists in registry.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanRefNotFoundTest.java   (with props)
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MethodCallBeanRefNotFoundTest.java   (with props)
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/language/bean/BeanExpression.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/BeanDefinition.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/MethodCallExpression.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/view/DotViewTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/language/bean/BeanExpression.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/language/bean/BeanExpression.java?rev=962430&r1=962429&r2=962430&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/language/bean/BeanExpression.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/language/bean/BeanExpression.java Fri Jul  9 07:24:18 2010
@@ -190,7 +190,7 @@ public class BeanExpression implements E
                 // keep up with how far are we doing
                 ognlPath += methodName;
 
-                // get rid of leading ?. or . as we only needed that to determine if elvis was enabled or not
+                // get rid of leading ?. or . as we only needed that to determine if null safe was enabled or not
                 methodName = OgnlHelper.removeLeadingOperators(methodName);
 
                 // are we doing an index lookup (eg in Map/List/array etc)?
@@ -264,7 +264,7 @@ public class BeanExpression implements E
                         return list.get(num);
                     }
                     if (!nullSafe) {
-                        // not elvis then its mandatory so thrown out of bounds exception
+                        // not null safe then its mandatory so thrown out of bounds exception
                         throw new IndexOutOfBoundsException("Index: " + num + ", Size: " + list.size()
                                 + " out of bounds with List from bean: " + bean + "using OGNL path [" + ognlPath + "]");
                     }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/BeanDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/BeanDefinition.java?rev=962430&r1=962429&r2=962430&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/BeanDefinition.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/BeanDefinition.java Fri Jul  9 07:24:18 2010
@@ -22,6 +22,7 @@ import javax.xml.bind.annotation.XmlAttr
 import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.bind.annotation.XmlTransient;
 
+import org.apache.camel.NoSuchBeanException;
 import org.apache.camel.Processor;
 import org.apache.camel.component.bean.BeanProcessor;
 import org.apache.camel.component.bean.RegistryBean;
@@ -148,6 +149,10 @@ public class BeanDefinition extends Outp
     public Processor createProcessor(RouteContext routeContext) {
         BeanProcessor answer;
         if (ObjectHelper.isNotEmpty(ref)) {
+            // if its a ref then check that the ref exists
+            if (routeContext.getCamelContext().getRegistry().lookup(ref) == null) {
+                throw new NoSuchBeanException(ref);
+            }
             answer = new BeanProcessor(new RegistryBean(routeContext.getCamelContext(), ref));
         } else {
             if (bean == null) {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/MethodCallExpression.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/MethodCallExpression.java?rev=962430&r1=962429&r2=962430&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/MethodCallExpression.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/MethodCallExpression.java Fri Jul  9 07:24:18 2010
@@ -24,6 +24,7 @@ import javax.xml.bind.annotation.XmlTran
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Expression;
+import org.apache.camel.NoSuchBeanException;
 import org.apache.camel.Predicate;
 import org.apache.camel.language.bean.BeanExpression;
 import org.apache.camel.util.ObjectHelper;
@@ -102,7 +103,12 @@ public class MethodCallExpression extend
         } else if (instance != null) {
             return new BeanExpression(instance, getMethod());
         } else {
-            return new BeanExpression(beanName(), getMethod());
+            String ref = beanName();
+            // if its a ref then check that the ref exists
+            if (camelContext.getRegistry().lookup(ref) == null) {
+                throw new NoSuchBeanException(ref);
+            }
+            return new BeanExpression(ref, getMethod());
         }
     }
 
@@ -113,7 +119,12 @@ public class MethodCallExpression extend
         } else if (instance != null) {
             return new BeanExpression(instance, getMethod());
         } else {
-            return new BeanExpression(beanName(), getMethod());
+            String ref = beanName();
+            // if its a ref then check that the ref exists
+            if (camelContext.getRegistry().lookup(ref) == null) {
+                throw new NoSuchBeanException(ref);
+            }
+            return new BeanExpression(ref, getMethod());
         }
     }
 

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanRefNotFoundTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanRefNotFoundTest.java?rev=962430&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanRefNotFoundTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanRefNotFoundTest.java Fri Jul  9 07:24:18 2010
@@ -0,0 +1,60 @@
+/**
+ * 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.component.bean;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.FailedToCreateRouteException;
+import org.apache.camel.NoSuchBeanException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.JndiRegistry;
+
+/**
+ * @version $Revision$
+ */
+public class BeanRefNotFoundTest extends ContextTestSupport {
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+        jndi.bind("foo", new MyFooBean());
+        return jndi;
+    }
+
+    public void testBeanRefNotFound() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:a").routeId("a").beanRef("foo").to("mock:a");
+
+                from("direct:b").routeId("b").beanRef("bar").to("mock:b");
+            }
+        });
+        try {
+            context.start();
+            fail("Should have thrown exception");
+        } catch (FailedToCreateRouteException e) {
+            assertEquals("b", e.getRouteId());
+            NoSuchBeanException cause = assertIsInstanceOf(NoSuchBeanException.class, e.getCause());
+            assertEquals("bar", cause.getName());
+        }
+    }
+
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+}

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanRefNotFoundTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanRefNotFoundTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MethodCallBeanRefNotFoundTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MethodCallBeanRefNotFoundTest.java?rev=962430&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MethodCallBeanRefNotFoundTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MethodCallBeanRefNotFoundTest.java Fri Jul  9 07:24:18 2010
@@ -0,0 +1,60 @@
+/**
+ * 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.component.bean;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.FailedToCreateRouteException;
+import org.apache.camel.NoSuchBeanException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.JndiRegistry;
+
+/**
+ * @version $Revision$
+ */
+public class MethodCallBeanRefNotFoundTest extends ContextTestSupport {
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+        jndi.bind("foo", new MyFooBean());
+        return jndi;
+    }
+
+    public void testMethodCallBeanRefNotFound() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:b").routeId("b").split().method("foo", "myMethod").to("mock:b");
+
+                from("direct:b").routeId("b").split().method("bar", "myMethod").to("mock:b");
+            }
+        });
+        try {
+            context.start();
+            fail("Should have thrown exception");
+        } catch (FailedToCreateRouteException e) {
+            assertEquals("b", e.getRouteId());
+            NoSuchBeanException cause = assertIsInstanceOf(NoSuchBeanException.class, e.getCause());
+            assertEquals("bar", cause.getName());
+        }
+    }
+
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+}
\ No newline at end of file

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MethodCallBeanRefNotFoundTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MethodCallBeanRefNotFoundTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/view/DotViewTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/view/DotViewTest.java?rev=962430&r1=962429&r2=962430&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/view/DotViewTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/view/DotViewTest.java Fri Jul  9 07:24:18 2010
@@ -19,6 +19,8 @@ package org.apache.camel.view;
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.builder.xml.XPathBuilder;
+import org.apache.camel.component.bean.MyFooBean;
+import org.apache.camel.impl.JndiRegistry;
 import org.apache.camel.processor.aggregate.UseLatestAggregationStrategy;
 
 
@@ -34,6 +36,13 @@ public class DotViewTest extends Context
     }
 
     @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+        jndi.bind("myBean", new MyFooBean());
+        return jndi;
+    }
+
+    @Override
     protected void setUp() throws Exception {
         super.setUp();