camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [3/3] git commit: CAMEL-6687: Bean using OGNL should accept null as valid response from OGNL invocation.
Date Sun, 01 Sep 2013 09:14:49 GMT
CAMEL-6687: Bean using OGNL should accept null as valid response from OGNL invocation.

Conflicts:
	camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java


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

Branch: refs/heads/camel-2.11.x
Commit: 95d7e579072c533295ede865b407e4b44d3968ba
Parents: d83f6ee
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Sun Sep 1 11:08:28 2013 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Sun Sep 1 11:14:33 2013 +0200

----------------------------------------------------------------------
 .../apache/camel/component/bean/MethodInfo.java | 55 ++++++-------
 .../BeanOgnlBodyMethodReturnNullValueTest.java  | 82 ++++++++++++++++++++
 2 files changed, 110 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/95d7e579/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 feee6de..68d4cc1 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
@@ -508,42 +508,43 @@ public class MethodInfo {
                     try {
                         expression = exchange.getContext().resolveLanguage("simple").createExpression(exp);
                         parameterValue = expression.evaluate(exchange, Object.class);
+                        // use "null" to indicate the expression returned a null value which
is a valid response we need to honor
+                        if (parameterValue == null) {
+                            parameterValue = "null";
+                        }
                     } catch (Exception e) {
                         throw new ExpressionEvaluationException(expression, "Cannot create/evaluate
simple expression: " + exp
                                 + " to be bound to parameter at index: " + index + " on method:
" + getMethod(), exchange, e);
                     }
 
-                    if (parameterValue != null) {
+                    // special for explicit null parameter values (as end users can explicit
indicate they want null as parameter)
+                    // see method javadoc for details
+                    if ("null".equals(parameterValue)) {
+                        return Void.TYPE;
+                    }
 
-                        // special for explicit null parameter values (as end users can explicit
indicate they want null as parameter)
-                        // see method javadoc for details
-                        if ("null".equals(parameterValue)) {
-                            return Void.TYPE;
-                        }
+                    // the parameter value was not already valid, but since the simple language
have evaluated the expression
+                    // which may change the parameterValue, so we have to check it again
to see if its now valid
+                    exp = exchange.getContext().getTypeConverter().convertTo(String.class,
parameterValue);
+                    // String values from the simple language is always valid
+                    if (!valid) {
+                        // re validate if the parameter was not valid the first time (String
values should be accepted)
+                        valid = parameterValue instanceof String || BeanHelper.isValidParameterValue(exp);
+                    }
 
-                        // the parameter value was not already valid, but since the simple
language have evaluated the expression
-                        // which may change the parameterValue, so we have to check it again
to see if its now valid
-                        exp = exchange.getContext().getTypeConverter().convertTo(String.class,
parameterValue);
-                        // String values from the simple language is always valid
-                        if (!valid) {
-                            // re validate if the parameter was not valid the first time
(String values should be accepted)
-                            valid = parameterValue instanceof String || BeanHelper.isValidParameterValue(exp);
+                    if (valid) {
+                        // we need to unquote String parameters, as the enclosing quotes
is there to denote a parameter value
+                        if (parameterValue instanceof String) {
+                            parameterValue = StringHelper.removeLeadingAndEndingQuotes((String)
parameterValue);
                         }
-
-                        if (valid) {
-                            // we need to unquote String parameters, as the enclosing quotes
is there to denote a parameter value
-                            if (parameterValue instanceof String) {
-                                parameterValue = StringHelper.removeLeadingAndEndingQuotes((String)
parameterValue);
-                            }
-                            try {
-                                // its a valid parameter value, so convert it to the expected
type of the parameter
-                                answer = exchange.getContext().getTypeConverter().mandatoryConvertTo(parameterType,
parameterValue);
-                                if (LOG.isTraceEnabled()) {
-                                    LOG.trace("Parameter #{} evaluated as: {} type: ", new
Object[]{index, answer, ObjectHelper.type(answer)});
-                                }
-                            } catch (NoTypeConversionAvailableException e) {
-                                throw ObjectHelper.wrapCamelExecutionException(exchange,
e);
+                        try {
+                            // its a valid parameter value, so convert it to the expected
type of the parameter
+                            answer = exchange.getContext().getTypeConverter().mandatoryConvertTo(parameterType,
parameterValue);
+                            if (LOG.isTraceEnabled()) {
+                                LOG.trace("Parameter #{} evaluated as: {} type: ", new Object[]{index,
answer, ObjectHelper.type(answer)});
                             }
+                        } catch (NoTypeConversionAvailableException e) {
+                            throw ObjectHelper.wrapCamelExecutionException(exchange, e);
                         }
                     }
                 }

http://git-wip-us.apache.org/repos/asf/camel/blob/95d7e579/camel-core/src/test/java/org/apache/camel/component/bean/BeanOgnlBodyMethodReturnNullValueTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/bean/BeanOgnlBodyMethodReturnNullValueTest.java
b/camel-core/src/test/java/org/apache/camel/component/bean/BeanOgnlBodyMethodReturnNullValueTest.java
new file mode 100644
index 0000000..b477479
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/component/bean/BeanOgnlBodyMethodReturnNullValueTest.java
@@ -0,0 +1,82 @@
+/**
+ * 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.builder.RouteBuilder;
+
+/**
+ *
+ */
+public class BeanOgnlBodyMethodReturnNullValueTest extends ContextTestSupport {
+
+    public void testBothValue() {
+        ExamplePojo fooBar = new ExamplePojo();
+        fooBar.setFoo("foo1");
+        fooBar.setBar("bar2");
+
+        String result = template.requestBody("direct:start", fooBar, String.class);
+        assertEquals("foo: foo1; bar: bar2", result);
+    }
+
+    public void testNullValue() {
+        ExamplePojo fooBar = new ExamplePojo();
+        fooBar.setFoo(null);
+        fooBar.setBar("test");
+
+        String result = template.requestBody("direct:start", fooBar, String.class);
+        assertEquals("foo: null; bar: test", result);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    .bean(new ExampleBean(), "doWithFooBar(${body.foo}, ${body.bar})");
+            }
+        };
+    }
+
+    public static class ExampleBean {
+        public String doWithFooBar(String foo, String bar) {
+            return String.format("foo: %s; bar: %s", foo, bar);
+        }
+    }
+
+    public static class ExamplePojo {
+        private String foo;
+        private String bar;
+
+        public String getFoo() {
+            return foo;
+        }
+
+        public void setFoo(String foo) {
+            this.foo = foo;
+        }
+
+        public String getBar() {
+            return bar;
+        }
+
+        public void setBar(String bar) {
+            this.bar = bar;
+        }
+    }
+}


Mime
View raw message