camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1151048 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/ main/java/org/apache/camel/component/bean/ test/java/org/apache/camel/component/bean/
Date Tue, 26 Jul 2011 10:00:13 GMT
Author: davsclaus
Date: Tue Jul 26 10:00:12 2011
New Revision: 1151048

URL: http://svn.apache.org/viewvc?rev=1151048&view=rev
Log:
CAMEL-3961: Added support for explicit null parameter in method name option. Added better
error message if simple language cannot parse the option name syntax.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanParameterInvalidValueTest.java
      - copied, changed from r1150997, camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanParameterValueTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/ExpressionEvaluationException.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanHelper.java
    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/ExpressionEvaluationException.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/ExpressionEvaluationException.java?rev=1151048&r1=1151047&r2=1151048&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/ExpressionEvaluationException.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/ExpressionEvaluationException.java
Tue Jul 26 10:00:12 2011
@@ -34,6 +34,12 @@ public class ExpressionEvaluationExcepti
         this.exchange = exchange;
     }
 
+    public ExpressionEvaluationException(Expression expression, String message, Exchange
exchange, Throwable cause) {
+        super(message, cause);
+        this.expression = expression;
+        this.exchange = exchange;
+    }
+
     public Expression getExpression() {
         return expression;
     }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanHelper.java?rev=1151048&r1=1151047&r2=1151048&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanHelper.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanHelper.java Tue
Jul 26 10:00:12 2011
@@ -60,6 +60,11 @@ public final class BeanHelper {
             return true;
         }
 
+        // null is valid (to force a null value)
+        if (value.equals("null")) {
+            return true;
+        }
+
         // simple language tokens is valid
         if (StringHelper.hasStartToken(value, "simple")) {
             return true;

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=1151048&r1=1151047&r2=1151048&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
Jul 26 10:00:12 2011
@@ -33,6 +33,7 @@ import org.apache.camel.CamelContext;
 import org.apache.camel.Exchange;
 import org.apache.camel.ExchangePattern;
 import org.apache.camel.Expression;
+import org.apache.camel.ExpressionEvaluationException;
 import org.apache.camel.NoTypeConversionAvailableException;
 import org.apache.camel.Pattern;
 import org.apache.camel.Processor;
@@ -389,7 +390,9 @@ public class MethodInfo {
                     }
 
                     // remember the value to use
-                    answer[i] = value;
+                    if (value != Void.TYPE) {
+                        answer[i] = value;
+                    }
                 }
 
                 return (T) answer;
@@ -397,6 +400,13 @@ public class MethodInfo {
 
             /**
              * Evaluate using parameter values where the values can be provided in the method
name syntax.
+             * <p/>
+             * This methods returns accordingly:
+             * <ul>
+             *     <li><tt>null</tt> - if not a parameter value</li>
+             *     <li><tt>Void.TYPE</tt> - if an explicit null, forcing
Camel to pass in <tt>null</tt> for that given parameter</li>
+             *     <li>a non <tt>null</tt> value - if the parameter was
a parameter value, and to be used</li>
+             * </ul>
              *
              * @since 2.9
              */
@@ -422,8 +432,23 @@ public class MethodInfo {
                     }
 
                     // use simple language to evaluate the expression, as it may use the
simple language to refer to message body, headers etc.
-                    parameterValue = exchange.getContext().resolveLanguage("simple").createExpression(exp).evaluate(exchange,
Object.class);
+                    Expression expression = null;
+                    try {
+                        expression = exchange.getContext().resolveLanguage("simple").createExpression(exp);
+                        parameterValue = expression.evaluate(exchange, Object.class);
+                    } 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;
+                        }
+
                         // 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);

Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanParameterInvalidValueTest.java
(from r1150997, camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanParameterValueTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanParameterInvalidValueTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanParameterInvalidValueTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanParameterValueTest.java&r1=1150997&r2=1151048&rev=1151048&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanParameterValueTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanParameterInvalidValueTest.java
Tue Jul 26 10:00:12 2011
@@ -18,67 +18,69 @@ package org.apache.camel.component.bean;
 
 import java.util.Map;
 
+import org.apache.camel.CamelExecutionException;
 import org.apache.camel.ContextTestSupport;
+import org.apache.camel.ExpressionEvaluationException;
+import org.apache.camel.NoTypeConversionAvailableException;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.impl.JndiRegistry;
 
 /**
  *
  */
-public class BeanParameterValueTest extends ContextTestSupport {
+public class BeanParameterInvalidValueTest extends ContextTestSupport {
 
-    public void testBeanParameterValueBoolean() throws Exception {
-        getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
+    public void testBeanParameterInvalidValueA() throws Exception {
+        getMockEndpoint("mock:result").expectedMessageCount(0);
 
-        template.sendBody("direct:start", "World");
-
-        assertMockEndpointsSatisfied();
-    }
-
-    public void testBeanParameterValueBoolean2() throws Exception {
-        getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
-
-        template.sendBody("direct:start2", "World");
-
-        assertMockEndpointsSatisfied();
-    }
-
-    public void testBeanParameterValueBoolean3() throws Exception {
-        getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
-
-        template.sendBody("direct:start3", "World");
-
-        assertMockEndpointsSatisfied();
-    }
-
-    public void testBeanParameterValueBoolean4() throws Exception {
-        getMockEndpoint("mock:result").expectedBodiesReceived("Hello Camel");
-
-        template.sendBody("direct:start4", "World");
+        try {
+            template.sendBody("direct:a", "World");
+            fail("Should have thrown exception");
+        } catch (CamelExecutionException e) {
+            NoTypeConversionAvailableException cause = assertIsInstanceOf(NoTypeConversionAvailableException.class,
e.getCause());
+            assertEquals(String.class, cause.getFromType());
+            assertEquals(int.class, cause.getToType());
+            assertEquals("A", cause.getValue());
+        }
 
         assertMockEndpointsSatisfied();
     }
 
-    public void testBeanParameterValueInteger() throws Exception {
-        getMockEndpoint("mock:result").expectedBodiesReceived("WorldWorldWorld");
+    public void testBeanParameterInvalidValueB() throws Exception {
+        getMockEndpoint("mock:result").expectedMessageCount(0);
 
-        template.sendBody("direct:echo", "World");
+        try {
+            template.sendBody("direct:b", "World");
+            fail("Should have thrown exception");
+        } catch (CamelExecutionException e) {
+            NoTypeConversionAvailableException cause = assertIsInstanceOf(NoTypeConversionAvailableException.class,
e.getCause());
+            assertEquals(String.class, cause.getFromType());
+            assertEquals(int.class, cause.getToType());
+            assertEquals("true", cause.getValue());
+        }
 
         assertMockEndpointsSatisfied();
     }
 
-    public void testBeanParameterValueHeaderInteger() throws Exception {
-        getMockEndpoint("mock:result").expectedBodiesReceived("WorldWorld");
+    public void testBeanParameterNullC() throws Exception {
+        // should be an empty string
+        getMockEndpoint("mock:result").expectedBodiesReceived("");
 
-        template.sendBodyAndHeader("direct:echo2", "World", "times", 2);
+        template.sendBody("direct:c", "World");
 
         assertMockEndpointsSatisfied();
     }
 
-    public void testBeanParameterValueMap() throws Exception {
-        getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
+    public void testBeanParameterInvalidValueD() throws Exception {
+        getMockEndpoint("mock:result").expectedMessageCount(0);
 
-        template.sendBodyAndHeader("direct:heads", "World", "hello", "Hello");
+        try {
+            template.sendBody("direct:d", "World");
+            fail("Should have thrown exception");
+        } catch (CamelExecutionException e) {
+            ExpressionEvaluationException cause = assertIsInstanceOf(ExpressionEvaluationException.class,
e.getCause());
+            assertEquals("Illegal syntax: xxx", cause.getCause().getMessage());
+        }
 
         assertMockEndpointsSatisfied();
     }
@@ -95,32 +97,20 @@ public class BeanParameterValueTest exte
         return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                from("direct:start")
-                    .to("bean:foo?method=bar(*,true)")
+                from("direct:a")
+                    .to("bean:foo?method=echo(*, 'A')")
                     .to("mock:result");
 
-                from("direct:start2")
-                    .to("bean:foo?method=bar(body,true)")
+                from("direct:b")
+                    .to("bean:foo?method=echo(*, true)")
                     .to("mock:result");
 
-                from("direct:start3")
-                    .to("bean:foo?method=bar(${body}, true)")
+                from("direct:c")
+                    .to("bean:foo?method=echo(null, 2)")
                     .to("mock:result");
 
-                from("direct:start4")
-                    .to("bean:foo?method=bar('Camel', true)")
-                    .to("mock:result");
-
-                from("direct:echo")
-                    .to("bean:foo?method=echo(*, 3)")
-                    .to("mock:result");
-
-                from("direct:echo2")
-                    .to("bean:foo?method=echo(*, ${in.header.times})")
-                    .to("mock:result");
-
-                from("direct:heads")
-                    .to("bean:foo?method=heads(${body}, ${headers})")
+                from("direct:d")
+                    .to("bean:foo?method=echo(${xxx}, 2)")
                     .to("mock:result");
             }
         };
@@ -128,15 +118,12 @@ public class BeanParameterValueTest exte
 
     public static class MyBean {
 
-        public String bar(String body, boolean hello) {
-            if (hello) {
-                return "Hello " + body;
-            } else {
-                return body;
+        public String echo(String body, int times) {
+            if (body == null) {
+                // use an empty string for no body
+                return "";
             }
-        }
 
-        public String echo(String body, int times) {
             if (times > 0) {
                 StringBuilder sb = new StringBuilder();
                 for (int i = 0; i < times; i++) {



Mime
View raw message