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/06/20 17:58:00 UTC

git commit: CAMEL-6455: Improved bean component to support specifying empty parameter in the method name. And thrown a MethodNotFoundException if not found.

Updated Branches:
  refs/heads/master efbb85961 -> a52864dfc


CAMEL-6455: Improved bean component to support specifying empty parameter in the method name. And thrown a MethodNotFoundException if not found.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a52864df
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a52864df
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a52864df

Branch: refs/heads/master
Commit: a52864dfc931ee898cd53850c830049c820bf5a3
Parents: efbb859
Author: Claus Ibsen <da...@apache.org>
Authored: Thu Jun 20 16:41:54 2013 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Thu Jun 20 17:09:49 2013 +0200

----------------------------------------------------------------------
 .../apache/camel/component/bean/BeanInfo.java   | 14 ++++++
 .../apache/camel/component/bean/MethodInfo.java |  4 ++
 .../component/bean/MethodNotFoundException.java |  6 +++
 ...eterAndNoMethodWithNoParameterIssueTest.java | 49 +++++++++++++++++---
 4 files changed, 66 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/a52864df/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java b/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
index a511943..dc9eb04 100644
--- a/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
+++ b/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
@@ -206,6 +206,7 @@ public class BeanInfo {
             if (methodName.contains("(")) {
                 name = ObjectHelper.before(methodName, "(");
             }
+            boolean emptyParameters = methodName.endsWith("()");
 
             // special for getClass, as we want the user to be able to invoke this method
             // for example to log the class type or the likes
@@ -221,6 +222,11 @@ public class BeanInfo {
                 if (methods != null && methods.size() == 1) {
                     // only one method then choose it
                     methodInfo = methods.get(0);
+
+                    // validate that if we want an explict no-arg method, then that's what we get
+                    if (emptyParameters && methodInfo.hasParameters()) {
+                        throw new MethodNotFoundException(exchange, pojo, methodName, "(with no parameters)");
+                    }
                 } else if (methods != null) {
                     // there are more methods with that name so we cannot decide which to use
 
@@ -228,6 +234,14 @@ public class BeanInfo {
                     // must use the method name which may have qualifiers
                     methodInfo = chooseMethod(pojo, exchange, methodName);
 
+                    // validate that if we want an explicit no-arg method, then that's what we get
+                    if (emptyParameters) {
+                        if (methodInfo == null || methodInfo.hasParameters()) {
+                            // we could not find a no-arg method with that name
+                            throw new MethodNotFoundException(exchange, pojo, methodName, "(with no parameters)");
+                        }
+                    }
+
                     if (methodInfo == null || !name.equals(methodInfo.getMethod().getName())) {
                         throw new AmbiguousMethodCallException(exchange, methods);
                     }

http://git-wip-us.apache.org/repos/asf/camel/blob/a52864df/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java b/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java
index 721a2c5..5080e24 100644
--- a/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java
+++ b/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java
@@ -378,6 +378,10 @@ public class MethodInfo {
         return hasHandlerAnnotation;
     }
 
+    public boolean hasParameters() {
+        return !parameters.isEmpty();
+    }
+
     public boolean isReturnTypeVoid() {
         return method.getReturnType().getName().equals("void");
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/a52864df/camel-core/src/main/java/org/apache/camel/component/bean/MethodNotFoundException.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/bean/MethodNotFoundException.java b/camel-core/src/main/java/org/apache/camel/component/bean/MethodNotFoundException.java
index 71e9760..e31c729 100644
--- a/camel-core/src/main/java/org/apache/camel/component/bean/MethodNotFoundException.java
+++ b/camel-core/src/main/java/org/apache/camel/component/bean/MethodNotFoundException.java
@@ -35,6 +35,12 @@ public class MethodNotFoundException extends CamelExchangeException {
         this.bean = pojo;
     }
 
+    public MethodNotFoundException(Exchange exchange, Object pojo, String methodName, String postfix) {
+        super("Method with name: " + methodName + " " + postfix + " not found on bean: " + pojo + " of type: " + ObjectHelper.className(pojo), exchange);
+        this.methodName = methodName;
+        this.bean = pojo;
+    }
+
     public MethodNotFoundException(Exchange exchange, Class<?> type, String methodName, boolean isStaticMethod) {
         super((isStaticMethod ? "Static method" : "Method") + " with name: " + methodName + " not found on class: " + ObjectHelper.name(type), exchange);
         this.methodName = methodName;

http://git-wip-us.apache.org/repos/asf/camel/blob/a52864df/camel-core/src/test/java/org/apache/camel/component/bean/BeanMethodWithEmptyParameterAndNoMethodWithNoParameterIssueTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/bean/BeanMethodWithEmptyParameterAndNoMethodWithNoParameterIssueTest.java b/camel-core/src/test/java/org/apache/camel/component/bean/BeanMethodWithEmptyParameterAndNoMethodWithNoParameterIssueTest.java
index 3058eb8..af45526 100644
--- a/camel-core/src/test/java/org/apache/camel/component/bean/BeanMethodWithEmptyParameterAndNoMethodWithNoParameterIssueTest.java
+++ b/camel-core/src/test/java/org/apache/camel/component/bean/BeanMethodWithEmptyParameterAndNoMethodWithNoParameterIssueTest.java
@@ -16,30 +16,49 @@
  */
 package org.apache.camel.component.bean;
 
+import org.apache.camel.CamelExecutionException;
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.impl.JndiRegistry;
 
 /**
- *
+ * CAMEL-6455
  */
 public class BeanMethodWithEmptyParameterAndNoMethodWithNoParameterIssueTest extends ContextTestSupport {
 
-    // TODO: CAMEL-6455
-
     @Override
     protected JndiRegistry createRegistry() throws Exception {
         JndiRegistry jndi = super.createRegistry();
         jndi.bind("myBean", new MyBean());
+        jndi.bind("myOtherBean", new MyOtherBean());
         return jndi;
     }
 
     public void testBean() throws Exception {
-        getMockEndpoint("mock:result").expectedBodiesReceived("Camel");
-        getMockEndpoint("mock:result").expectedHeaderReceived("foo", "bar");
+        getMockEndpoint("mock:result").expectedMessageCount(0);
+
+        try {
+            template.sendBody("direct:start", "Camel");
+            fail("Should have thrown exception");
+        } catch (CamelExecutionException e) {
+            MethodNotFoundException cause = assertIsInstanceOf(MethodNotFoundException.class, e.getCause());
+            assertEquals("doSomething()", cause.getMethodName());
+        }
+
+        assertMockEndpointsSatisfied();
+    }
 
-        template.sendBody("direct:start", "Camel");
+    public void testOtherBean() throws Exception {
+        getMockEndpoint("mock:result").expectedMessageCount(0);
+
+        try {
+            template.sendBody("direct:other", "Camel");
+            fail("Should have thrown exception");
+        } catch (CamelExecutionException e) {
+            MethodNotFoundException cause = assertIsInstanceOf(MethodNotFoundException.class, e.getCause());
+            assertEquals("doSomething()", cause.getMethodName());
+        }
 
         assertMockEndpointsSatisfied();
     }
@@ -50,7 +69,11 @@ public class BeanMethodWithEmptyParameterAndNoMethodWithNoParameterIssueTest ext
             @Override
             public void configure() throws Exception {
                 from("direct:start")
-                    .to("bean:myBean?method=doSomething(*)")
+                    .to("bean:myBean?method=doSomething()")
+                    .to("mock:result");
+
+                from("direct:other")
+                    .to("bean:myOtherBean?method=doSomething()")
                     .to("mock:result");
             }
         };
@@ -63,4 +86,16 @@ public class BeanMethodWithEmptyParameterAndNoMethodWithNoParameterIssueTest ext
         }
 
     }
+
+    public static final class MyOtherBean {
+
+        public static void doSomething(Exchange exchange) {
+            exchange.getIn().setHeader("foo", "bar");
+        }
+
+        public static void doSomething(Exchange exchange, String foo, String bar) {
+            exchange.getIn().setHeader(foo, bar);
+        }
+
+    }
 }