Author: davsclaus
Date: Thu Jul  1 07:52:48 2010
New Revision: 959549

URL: http://svn.apache.org/viewvc?rev=959549&view=rev
Log:
CAMEL-2846: bean DSL validate that instance is not a String as a String is for 
beanRef DSL instead.

Added:
    
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanValidateTypeTest.java
   (with props)
Modified:
    
camel/trunk/camel-core/src/main/java/org/apache/camel/model/BeanDefinition.java

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=959549&r1=959548&r2=959549&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 
Thu Jul  1 07:52:48 2010
@@ -152,6 +152,14 @@ public class BeanDefinition extends Outp
                 ObjectHelper.notNull(beanType, "bean, ref or beanType", this);
                 bean = 
CamelContextHelper.newInstance(routeContext.getCamelContext(), beanType);
             }
+            ObjectHelper.notNull(bean, "bean", this);
+
+            // validate the bean type is not from java so you by mistake think 
its a reference
+            // to a bean name but the String is being invoke instead
+            if (bean instanceof String) {
+                throw new IllegalArgumentException("The bean instance is a 
java.lang.String type: " + bean
+                    + ". We suppose you want to refer to a bean instance by 
its id instead. Please use beanRef.");
+            }
             answer = new BeanProcessor(bean, routeContext.getCamelContext());
         }
         if (method != null) {

Added: 
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanValidateTypeTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanValidateTypeTest.java?rev=959549&view=auto
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanValidateTypeTest.java
 (added)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanValidateTypeTest.java
 Thu Jul  1 07:52:48 2010
@@ -0,0 +1,66 @@
+/**
+ * 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 org.apache.camel.ContextTestSupport;
+import org.apache.camel.FailedToCreateRouteException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.JndiRegistry;
+
+/**
+ * @version $Revision$
+ */
+public class BeanValidateTypeTest extends ContextTestSupport {
+
+    public static class MyCoolBean {
+
+        public String hello(String s) {
+            return "Hello " + s;
+        }
+
+    }
+
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+        jndi.bind("cool", new MyCoolBean());
+        return jndi;
+    }
+
+    public void testBeanValidateType() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                // we should use beanRef instead to refer to cool
+                from("direct:start").bean("cool");
+            }
+        });
+        try {
+            context.start();
+            fail("Should have thrown exception");
+        } catch (FailedToCreateRouteException e) {
+            assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
+            assertTrue(e.getCause().getMessage().startsWith("The bean instance 
is a java.lang.String type: cool."));
+        }
+    }
+
+}

Propchange: 
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanValidateTypeTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/BeanValidateTypeTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date


Reply via email to