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/04/24 19:15:14 UTC

svn commit: r1471543 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/component/bean/ main/java/org/apache/camel/util/ test/java/org/apache/camel/component/bean/ test/java/org/apache/camel/language/simple/ test/java/org/apache/camel/util/

Author: davsclaus
Date: Wed Apr 24 17:15:14 2013
New Revision: 1471543

URL: http://svn.apache.org/r1471543
Log:
CAMEL-6307: Fixed bean component OGNL calling methods with empty spaces around simple language functions.

Added:
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/MyCurrency.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MyCurrencyBean.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MyCurrencyBeanTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/StringHelper.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/StringQuoteHelper.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/util/StingQuoteHelperTest.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=1471543&r1=1471542&r2=1471543&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 Wed Apr 24 17:15:14 2013
@@ -426,7 +426,7 @@ public class MethodInfo {
                 if (methodParameters != null) {
                     // split the parameters safely separated by comma, but beware that we can have
                     // quoted parameters which contains comma as well, so do a safe quote split
-                    String[] parameters = StringQuoteHelper.splitSafeQuote(methodParameters, ',', false);
+                    String[] parameters = StringQuoteHelper.splitSafeQuote(methodParameters, ',', true);
                     it = ObjectHelper.createIterator(parameters, ",", true);
                 }
 

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/MyCurrency.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/MyCurrency.java?rev=1471543&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/MyCurrency.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/MyCurrency.java Wed Apr 24 17:15:14 2013
@@ -0,0 +1,30 @@
+/**
+ * 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;
+
+public final class MyCurrency {
+
+    private String symbol;
+
+    public MyCurrency(String symbol) {
+        this.symbol = symbol;
+    }
+
+    public String getSymbol() {
+        return symbol;
+    }
+}

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/StringHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/StringHelper.java?rev=1471543&r1=1471542&r2=1471543&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/StringHelper.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/StringHelper.java Wed Apr 24 17:15:14 2013
@@ -81,12 +81,15 @@ public final class StringHelper {
             return s;
         }
 
-        if (s.startsWith("'") && s.endsWith("'")) {
-            return s.substring(1, s.length() - 1);
+        String copy = s.trim();
+        if (copy.startsWith("'") && copy.endsWith("'")) {
+            return copy.substring(1, copy.length() - 1);
         }
-        if (s.startsWith("\"") && s.endsWith("\"")) {
-            return s.substring(1, s.length() - 1);
+        if (copy.startsWith("\"") && copy.endsWith("\"")) {
+            return copy.substring(1, copy.length() - 1);
         }
+
+        // no quotes, so return as-is
         return s;
     }
     

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/StringQuoteHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/StringQuoteHelper.java?rev=1471543&r1=1471542&r2=1471543&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/StringQuoteHelper.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/StringQuoteHelper.java Wed Apr 24 17:15:14 2013
@@ -82,6 +82,15 @@ public final class StringQuoteHelper {
                     // its an empty quote so add empty text
                     answer.add("");
                 }
+                // special logic needed if this quote is the end
+                if (i == input.length() - 1) {
+                    if (singleQuoted && sb.length() > 0) {
+                        String text = sb.toString();
+                        // do not trim a quoted string
+                        answer.add(text);
+                        sb.setLength(0);
+                    }
+                }
                 singleQuoted = !singleQuoted;
                 continue;
             } else if (ch == '"') {
@@ -89,6 +98,15 @@ public final class StringQuoteHelper {
                     // its an empty quote so add empty text
                     answer.add("");
                 }
+                // special logic needed if this quote is the end
+                if (i == input.length() - 1) {
+                    if (doubleQuoted && sb.length() > 0) {
+                        String text = sb.toString();
+                        // do not trim a quoted string
+                        answer.add(text);
+                        sb.setLength(0);
+                    }
+                }
                 doubleQuoted = !doubleQuoted;
                 continue;
             } else if (ch == ' ') {

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MyCurrencyBean.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MyCurrencyBean.java?rev=1471543&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MyCurrencyBean.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MyCurrencyBean.java Wed Apr 24 17:15:14 2013
@@ -0,0 +1,28 @@
+/**
+ * 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;
+
+public class MyCurrencyBean {
+
+    public String display(MyCurrency currency) {
+        return "Currency is " + currency.getSymbol();
+    }
+
+    public String displayPrice(MyCurrency currency, int amount) {
+        return "Price is " + currency.getSymbol() + amount;
+    }
+}

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MyCurrencyBeanTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MyCurrencyBeanTest.java?rev=1471543&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MyCurrencyBeanTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MyCurrencyBeanTest.java Wed Apr 24 17:15:14 2013
@@ -0,0 +1,63 @@
+/**
+ * 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;
+import org.apache.camel.impl.JndiRegistry;
+
+public class MyCurrencyBeanTest extends ContextTestSupport {
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+        jndi.bind("myCurrencyBean", new MyCurrencyBean());
+        return jndi;
+    }
+
+    public void testDisplay() throws Exception {
+        getMockEndpoint("mock:result").expectedBodiesReceived("Currency is $");
+
+        template.sendBody("direct:start", new MyCurrency("$"));
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testDisplayPrice() throws Exception {
+        getMockEndpoint("mock:result").expectedBodiesReceived("Price is $123");
+
+        template.sendBodyAndHeader("direct:price", new MyCurrency("$"), "price", 123);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    .to("bean:myCurrencyBean?method=display( ${body} )")
+                    .to("mock:result");
+
+                from("direct:price")
+                    .to("bean:myCurrencyBean?method=displayPrice( ${body}, ${header.price} )")
+                    .to("mock:result");
+            }
+        };
+    }
+}

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java?rev=1471543&r1=1471542&r2=1471543&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java Wed Apr 24 17:15:14 2013
@@ -1134,6 +1134,21 @@ public class SimpleTest extends Language
         assertExpression("${body.replaceFirst('http:',' ')}", " camel.apache.org");
     }
 
+    public void testBodyOgnlSpaces() throws Exception {
+        exchange.getIn().setBody("Hello World");
+
+        // no quotes, which is discouraged to use
+        assertExpression("${body.compareTo(Hello World)}", 0);
+
+        assertExpression("${body.compareTo('Hello World')}", 0);
+        assertExpression("${body.compareTo(${body})}", 0);
+        assertExpression("${body.compareTo('foo')}", "Hello World".compareTo("foo"));
+
+        assertExpression("${body.compareTo( 'Hello World' )}", 0);
+        assertExpression("${body.compareTo( ${body} )}", 0);
+        assertExpression("${body.compareTo( 'foo' )}", "Hello World".compareTo("foo"));
+    }
+
     public void testClassSimpleName() throws Exception {
         Animal tiger = new Animal("Tony the Tiger", 13);
         exchange.getIn().setBody(tiger);

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/util/StingQuoteHelperTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/util/StingQuoteHelperTest.java?rev=1471543&r1=1471542&r2=1471543&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/util/StingQuoteHelperTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/util/StingQuoteHelperTest.java Wed Apr 24 17:15:14 2013
@@ -117,4 +117,18 @@ public class StingQuoteHelperTest extend
         assertEquals("true", out[2]);
     }
 
+    public void testLastIsQuote() throws Exception {
+        String[] out = StringQuoteHelper.splitSafeQuote(" ${body}, 5, 'Hello World'", ',', true);
+        assertEquals(3, out.length);
+        assertEquals("${body}", out[0]);
+        assertEquals("5", out[1]);
+        assertEquals("Hello World", out[2]);
+
+        out = StringQuoteHelper.splitSafeQuote(" ${body}, 5, \"Hello World\"", ',', true);
+        assertEquals(3, out.length);
+        assertEquals("${body}", out[0]);
+        assertEquals("5", out[1]);
+        assertEquals("Hello World", out[2]);
+    }
+
 }