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 2009/06/18 09:26:28 UTC

svn commit: r785941 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/component/bean/BeanInfo.java main/java/org/apache/camel/component/bean/MethodNotFoundException.java test/java/org/apache/camel/component/bean/BeanWithMethodHeaderTest.java

Author: davsclaus
Date: Thu Jun 18 07:26:27 2009
New Revision: 785941

URL: http://svn.apache.org/viewvc?rev=785941&view=rev
Log:
CAMEL-1728: bean component throws MethodNotFoundException if a method name was given to invoke but not found on pojo.

Added:
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/MethodNotFoundException.java   (with props)
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithMethodHeaderTest.java   (with props)
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java?rev=785941&r1=785940&r2=785941&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java Thu Jun 18 07:26:27 2009
@@ -121,7 +121,7 @@
         return answer;
     }
 
-    public MethodInvocation createInvocation(Method method, Object pojo, Exchange exchange) throws RuntimeCamelException {
+    public MethodInvocation createInvocation(Method method, Object pojo, Exchange exchange) {
         MethodInfo methodInfo = introspect(type, method);
         if (methodInfo != null) {
             return methodInfo.createMethodInvocation(pojo, exchange);
@@ -129,7 +129,7 @@
         return null;
     }
 
-    public MethodInvocation createInvocation(Object pojo, Exchange exchange) throws RuntimeCamelException, AmbiguousMethodCallException {
+    public MethodInvocation createInvocation(Object pojo, Exchange exchange) throws AmbiguousMethodCallException, MethodNotFoundException {
         MethodInfo methodInfo = null;
 
         String name = exchange.getIn().getHeader(Exchange.BEAN_METHOD_NAME, String.class);
@@ -139,6 +139,9 @@
                 if (methods != null && methods.size() == 1) {
                     methodInfo = methods.get(0);
                 }
+            } else {
+                // a specific method was given to invoke but not found
+                throw new MethodNotFoundException(exchange, pojo, name);
             }
         }
         if (methodInfo == null) {

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/MethodNotFoundException.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/MethodNotFoundException.java?rev=785941&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/MethodNotFoundException.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/MethodNotFoundException.java Thu Jun 18 07:26:27 2009
@@ -0,0 +1,43 @@
+/**
+ * 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.CamelExchangeException;
+import org.apache.camel.Exchange;
+
+/**
+ * @version $Revision$
+ */
+public class MethodNotFoundException extends CamelExchangeException {
+
+    private final String methodName;
+    private final Object bean;
+
+    public MethodNotFoundException(Exchange exchange, Object pojo, String methodName) {
+        super("Method with name: " + methodName + " not found on bean: " + pojo, exchange);
+        this.methodName = methodName;
+        this.bean = pojo;
+    }
+
+    public String getMethodName() {
+        return methodName;
+    }
+
+    public Object getBean() {
+        return bean;
+    }
+}

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

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

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithMethodHeaderTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithMethodHeaderTest.java?rev=785941&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithMethodHeaderTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithMethodHeaderTest.java Thu Jun 18 07:26:27 2009
@@ -0,0 +1,109 @@
+/**
+ * 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 javax.naming.Context;
+
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.util.jndi.JndiContext;
+
+/**
+ * @version $Revision$
+ */
+public class BeanWithMethodHeaderTest extends ContextTestSupport {
+
+    private MyBean bean;
+
+    public void testEcho() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("echo Hello World");
+
+        template.sendBody("direct:echo", "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testHi() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("hi Hello World");
+
+        template.sendBody("direct:hi", "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testFail() throws Exception {
+        try {
+            template.sendBody("direct:fail", "Hello World");
+            fail("Should throw an exception");
+        } catch (CamelExecutionException e) {
+            assertIsInstanceOf(AmbiguousMethodCallException.class, e.getCause());
+            AmbiguousMethodCallException ace = (AmbiguousMethodCallException) e.getCause();
+            assertEquals(2, ace.getMethods().size());
+        }
+    }
+
+    public void testMethodNotExists() throws Exception {
+        try {
+            template.sendBody("direct:typo", "Hello World");
+            fail("Should throw an exception");
+        } catch (CamelExecutionException e) {
+            assertIsInstanceOf(MethodNotFoundException.class, e.getCause());
+            MethodNotFoundException mnfe = (MethodNotFoundException) e.getCause();
+            assertEquals("ups", mnfe.getMethodName());
+            assertSame(bean, mnfe.getBean());
+        }
+    }
+
+    protected Context createJndiContext() throws Exception {
+        JndiContext answer = new JndiContext();
+        bean = new MyBean();
+        answer.bind("myBean", bean);
+        return answer;
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:echo").beanRef("myBean", "echo").to("mock:result");
+
+                from("direct:hi").beanRef("myBean", "hi").to("mock:result");
+
+                from("direct:fail").beanRef("myBean").to("mock:result");
+
+                from("direct:typo").beanRef("myBean", "ups").to("mock:result");
+            }
+        };
+    }
+
+    public static class MyBean {
+
+        public String hi(String s) {
+            return "hi " + s;
+        }
+
+        public String echo(String s) {
+            return "echo " + s;
+        }
+    }
+
+}

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

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