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/16 15:24:41 UTC

svn commit: r785204 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/component/bean/MethodInfo.java test/java/org/apache/camel/component/bean/BeanWithHeadersAndBodyInject2Test.java

Author: davsclaus
Date: Tue Jun 16 13:24:40 2009
New Revision: 785204

URL: http://svn.apache.org/viewvc?rev=785204&view=rev
Log:
CAMEL-1719: Fixed bean binding to thrown NoTypeConverterException in case type convertions not possible instead of null.

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

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java?rev=785204&r1=785203&r2=785204&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java Tue Jun 16 13:24:40 2009
@@ -28,6 +28,7 @@
 import org.apache.camel.Exchange;
 import org.apache.camel.ExchangePattern;
 import org.apache.camel.Expression;
+import org.apache.camel.NoTypeConversionAvailableException;
 import org.apache.camel.Pattern;
 import org.apache.camel.processor.RecipientList;
 import org.apache.camel.util.ObjectHelper;
@@ -202,9 +203,22 @@
                     } else {
                         Expression expression = expressions[i];
                         if (expression != null) {
-                            value = expression.evaluate(exchange, parameters.get(i).getType());
-                            if (LOG.isTraceEnabled()) {
-                                LOG.trace("Parameter #" + i + " evaluated as: " + value + " type: " + ObjectHelper.type(value));
+                            // use object first to avoid type convertions so we know if there is a value or not
+                            Object result = expression.evaluate(exchange, Object.class);
+                            if (result != null) {
+                                // we got a value now try to convert it to the expected type
+                                value = exchange.getContext().getTypeConverter().convertTo(parameters.get(i).getType(), result);
+                                if (LOG.isTraceEnabled()) {
+                                    LOG.trace("Parameter #" + i + " evaluated as: " + value + " type: " + ObjectHelper.type(value));
+                                }
+                                if (value == null) {
+                                    Exception e = new NoTypeConversionAvailableException(result, parameters.get(i).getType()); 
+                                    throw ObjectHelper.wrapRuntimeCamelException(e);
+                                }
+                            } else {
+                                if (LOG.isTraceEnabled()) {
+                                    LOG.trace("Parameter #" + i + " evaluated as null");
+                                }
                             }
                         }
                     }

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithHeadersAndBodyInject2Test.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithHeadersAndBodyInject2Test.java?rev=785204&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithHeadersAndBodyInject2Test.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithHeadersAndBodyInject2Test.java Tue Jun 16 13:24:40 2009
@@ -0,0 +1,132 @@
+/**
+ * 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 java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import javax.naming.Context;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Header;
+import org.apache.camel.Message;
+import org.apache.camel.NoTypeConversionAvailableException;
+import org.apache.camel.Processor;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.util.jndi.JndiContext;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * @version $Revision$
+ */
+public class BeanWithHeadersAndBodyInject2Test extends ContextTestSupport {
+    private static final transient Log LOG = LogFactory.getLog(BeanWithHeadersAndBodyInject2Test.class);
+    private MyBean myBean = new MyBean();
+    private Map users = new HashMap();
+
+    public void testCannotBindToParameter() throws Exception {
+        // Create hashmap for testing purpose
+        users.put("charles", new User("Charles", "43"));
+        users.put("claus", new User("Claus", "33"));
+
+        Exchange out = template.send("direct:in", new Processor() {
+            public void process(Exchange exchange) throws Exception {
+                exchange.setProperty("p1", "abc");
+                exchange.setProperty("p2", 123);
+
+                Message in = exchange.getIn();
+                in.setHeader("users", users); // add users hashmap
+                in.setBody("TheBody");
+            }
+        });
+
+        assertTrue("Should fail", out.isFailed());
+        assertIsInstanceOf(RuntimeCamelException.class, out.getException());
+        assertIsInstanceOf(NoTypeConversionAvailableException.class, out.getException().getCause());
+    }
+
+    public void testBindToParameter() throws Exception {
+        final List list = new ArrayList();
+        list.add("Charles");
+        list.add("Claus");
+
+        Exchange out = template.send("direct:in", new Processor() {
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setBody("TheBody");
+                exchange.getIn().setHeader("users", list);
+            }
+        });
+
+        assertFalse("Should not fail", out.isFailed());
+        assertSame(list, myBean.users);
+        assertEquals("TheBody", myBean.body);
+    }
+
+    public void testBindToParameterIsNullValue() throws Exception {
+        Exchange out = template.send("direct:in", new Processor() {
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setBody("TheBody");
+                exchange.getIn().setHeader("users", null);
+            }
+        });
+
+        assertFalse("Should not fail", out.isFailed());
+        assertEquals("TheBody", myBean.body);
+    }
+
+    @Override
+    protected Context createJndiContext() throws Exception {
+        JndiContext answer = new JndiContext();
+        answer.bind("myBean", myBean);
+        return answer;
+    }
+
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:in").to("bean:myBean?method=myMethod");
+            }
+        };
+    }
+
+    public static class MyBean {
+        public Object body;
+        public List users;
+
+        public void myMethod(@Header(value = "users") List users, Object body) {
+            LOG.info("myMethod() method called on " + this);
+            LOG.info(" users " + users);
+            this.body = body;
+            this.users = users;
+        }
+    }
+
+    public static class User {
+        public String name;
+        public String age;
+
+        public User(String name, String age) {
+            this.name = name;
+            this.age = age;
+        }
+
+    }
+}

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

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