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 2011/10/26 13:15:29 UTC

svn commit: r1189136 - in /camel/trunk/components/camel-script/src: main/java/org/apache/camel/builder/script/ test/java/org/apache/camel/builder/script/ test/resources/org/apache/camel/builder/script/

Author: davsclaus
Date: Wed Oct 26 11:15:28 2011
New Revision: 1189136

URL: http://svn.apache.org/viewvc?rev=1189136&view=rev
Log:
CAMEL-4586: Added properties function to make it easier to use the Camel properties component from camel-script. eg to use property placeholders in scripts.

Added:
    camel/trunk/components/camel-script/src/main/java/org/apache/camel/builder/script/PropertiesFunction.java
    camel/trunk/components/camel-script/src/main/java/org/apache/camel/builder/script/ScriptPropertiesFunction.java
    camel/trunk/components/camel-script/src/test/java/org/apache/camel/builder/script/GroovyPropertiesFunctionTest.java
    camel/trunk/components/camel-script/src/test/java/org/apache/camel/builder/script/GroovyPropertyComponentTest.java   (contents, props changed)
      - copied, changed from r1189041, camel/trunk/components/camel-script/src/test/java/org/apache/camel/builder/script/JavaScriptExpressionTest.java
    camel/trunk/components/camel-script/src/test/java/org/apache/camel/builder/script/GroovySetHeaderPropertyComponentTest.java
    camel/trunk/components/camel-script/src/test/java/org/apache/camel/builder/script/JavaScriptPropertiesFunctionTest.java
    camel/trunk/components/camel-script/src/test/resources/org/apache/camel/builder/script/myproperties.properties
Modified:
    camel/trunk/components/camel-script/src/main/java/org/apache/camel/builder/script/ScriptBuilder.java

Added: camel/trunk/components/camel-script/src/main/java/org/apache/camel/builder/script/PropertiesFunction.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-script/src/main/java/org/apache/camel/builder/script/PropertiesFunction.java?rev=1189136&view=auto
==============================================================================
--- camel/trunk/components/camel-script/src/main/java/org/apache/camel/builder/script/PropertiesFunction.java (added)
+++ camel/trunk/components/camel-script/src/main/java/org/apache/camel/builder/script/PropertiesFunction.java Wed Oct 26 11:15:28 2011
@@ -0,0 +1,32 @@
+/**
+ * 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.builder.script;
+
+/**
+ * Function in scripting languages to leverage the Camel {@link org.apache.camel.component.properties.PropertiesComponent}
+ * to make it easier to resolve property placeholders.
+ */
+public interface PropertiesFunction {
+
+    /**
+     * Resolve the given key using the property placeholders
+     *
+     * @param key the key
+     * @return the resolved value
+     */
+    String resolve(String key) throws Exception;
+}

Modified: camel/trunk/components/camel-script/src/main/java/org/apache/camel/builder/script/ScriptBuilder.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-script/src/main/java/org/apache/camel/builder/script/ScriptBuilder.java?rev=1189136&r1=1189135&r2=1189136&view=diff
==============================================================================
--- camel/trunk/components/camel-script/src/main/java/org/apache/camel/builder/script/ScriptBuilder.java (original)
+++ camel/trunk/components/camel-script/src/main/java/org/apache/camel/builder/script/ScriptBuilder.java Wed Oct 26 11:15:28 2011
@@ -570,6 +570,8 @@ public class ScriptBuilder implements Ex
         if (exchange.hasOut()) {
             context.setAttribute("response", exchange.getOut(), scope);
         }
+        // to make using properties component easier
+        context.setAttribute("properties", new ScriptPropertiesFunction(exchange.getContext()), scope);
     }
 
     @SuppressWarnings("unchecked")

Added: camel/trunk/components/camel-script/src/main/java/org/apache/camel/builder/script/ScriptPropertiesFunction.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-script/src/main/java/org/apache/camel/builder/script/ScriptPropertiesFunction.java?rev=1189136&view=auto
==============================================================================
--- camel/trunk/components/camel-script/src/main/java/org/apache/camel/builder/script/ScriptPropertiesFunction.java (added)
+++ camel/trunk/components/camel-script/src/main/java/org/apache/camel/builder/script/ScriptPropertiesFunction.java Wed Oct 26 11:15:28 2011
@@ -0,0 +1,47 @@
+/**
+ * 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.builder.script;
+
+import org.apache.camel.CamelContext;
+
+public class ScriptPropertiesFunction implements PropertiesFunction {
+
+    private final CamelContext context;
+
+    public ScriptPropertiesFunction(CamelContext context) {
+        this.context = context;
+    }
+
+    @Override
+    public String resolve(String key) throws Exception {
+        if (key == null) {
+            return null;
+        }
+
+        String token = context.getPropertyPrefixToken();
+        if (token == null) {
+            return key;
+        }
+
+        if (!key.contains(token)) {
+            // enclose key with tokens so placeholder can lookup and resolve it
+            key = context.getPropertyPrefixToken() + key + context.getPropertySuffixToken();
+        }
+        return context.resolvePropertyPlaceholders(key);
+    }
+
+}

Added: camel/trunk/components/camel-script/src/test/java/org/apache/camel/builder/script/GroovyPropertiesFunctionTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-script/src/test/java/org/apache/camel/builder/script/GroovyPropertiesFunctionTest.java?rev=1189136&view=auto
==============================================================================
--- camel/trunk/components/camel-script/src/test/java/org/apache/camel/builder/script/GroovyPropertiesFunctionTest.java (added)
+++ camel/trunk/components/camel-script/src/test/java/org/apache/camel/builder/script/GroovyPropertiesFunctionTest.java Wed Oct 26 11:15:28 2011
@@ -0,0 +1,60 @@
+/**
+ * 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.builder.script;
+
+import org.apache.camel.ScriptTestHelper;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.component.properties.PropertiesComponent;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class GroovyPropertiesFunctionTest extends CamelTestSupport {
+
+    @Test
+    public void testSendMatchingMessage() throws Exception {
+        if (!ScriptTestHelper.canRunTestOnThisPlatform()) {
+            return;
+        }
+
+        log.info("Can run this test");
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Hello World");
+        mock.expectedHeaderReceived("myHeader", "Kong");
+
+        template.sendBodyAndHeader("direct:start", "Hello World", "foo", "bar");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
+                pc.setLocation("org/apache/camel/builder/script/myproperties.properties");
+
+                from("direct:start")
+                    .setHeader("myHeader").groovy("properties.resolve(request.headers.get('foo'))")
+                    .to("mock:result");
+            }
+        };
+    }
+}

Copied: camel/trunk/components/camel-script/src/test/java/org/apache/camel/builder/script/GroovyPropertyComponentTest.java (from r1189041, camel/trunk/components/camel-script/src/test/java/org/apache/camel/builder/script/JavaScriptExpressionTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-script/src/test/java/org/apache/camel/builder/script/GroovyPropertyComponentTest.java?p2=camel/trunk/components/camel-script/src/test/java/org/apache/camel/builder/script/GroovyPropertyComponentTest.java&p1=camel/trunk/components/camel-script/src/test/java/org/apache/camel/builder/script/JavaScriptExpressionTest.java&r1=1189041&r2=1189136&rev=1189136&view=diff
==============================================================================
--- camel/trunk/components/camel-script/src/test/java/org/apache/camel/builder/script/JavaScriptExpressionTest.java (original)
+++ camel/trunk/components/camel-script/src/test/java/org/apache/camel/builder/script/GroovyPropertyComponentTest.java Wed Oct 26 11:15:28 2011
@@ -16,19 +16,17 @@
  */
 package org.apache.camel.builder.script;
 
-import java.util.HashMap;
-import java.util.Map;
-
 import org.apache.camel.ScriptTestHelper;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.component.properties.PropertiesComponent;
 import org.apache.camel.test.junit4.CamelTestSupport;
 import org.junit.Test;
 
 /**
- * Tests a routing expression using JavaScript
+ *
  */
-public class JavaScriptExpressionTest extends CamelTestSupport {
+public class GroovyPropertyComponentTest extends CamelTestSupport {
 
     @Test
     public void testSendMatchingMessage() throws Exception {
@@ -36,113 +34,13 @@ public class JavaScriptExpressionTest ex
             return;
         }
 
+        log.info("Can run this test");
+
         MockEndpoint mock = getMockEndpoint("mock:result");
         mock.expectedMessageCount(1);
-
         getMockEndpoint("mock:unmatched").expectedMessageCount(0);
 
-        Map<String, Object> headers = new HashMap<String, Object>();
-        headers.put("foo", "bar");
-        sendBody("direct:start", "hello", headers);
-
-        assertEquals("Should get the message header here", mock.getExchanges().get(0).getIn().getHeader("foo"), "bar");
-        assertMockEndpointsSatisfied();
-    }
-
-    @Test
-    public void testSendNonMatchingMessage() throws Exception {
-        if (!ScriptTestHelper.canRunTestOnThisPlatform()) {
-            return;
-        }
-
-        getMockEndpoint("mock:result").expectedMessageCount(0);
-        getMockEndpoint("mock:unmatched").expectedMessageCount(1);
-
-        Map<String, Object> headers = new HashMap<String, Object>();
-        headers.put("foo", "foo");
-        sendBody("direct:start", "hello", headers);
-
-        assertMockEndpointsSatisfied();
-    }
-
-    @Test
-    // START SNIPPET: e1
-    public void testArgumentsExample() throws Exception {
-        if (!ScriptTestHelper.canRunTestOnThisPlatform()) {
-            return;
-        }
-
-        getMockEndpoint("mock:result").expectedMessageCount(0);
-        getMockEndpoint("mock:unmatched").expectedMessageCount(1);
-
-        // additional arguments to ScriptEngine
-        Map<String, Object> arguments = new HashMap<String, Object>();
-        arguments.put("foo", "bar");
-        arguments.put("baz", 7);
-
-        // those additional arguments is provided as a header on the Camel Message
-        template.sendBodyAndHeader("direct:start", "hello", ScriptBuilder.ARGUMENTS, arguments);
-
-        assertMockEndpointsSatisfied();
-    }
-    // END SNIPPET: e1
-
-    @Test
-    public void testArgumentsWithStringMap() throws Exception {
-        if (!ScriptTestHelper.canRunTestOnThisPlatform()) {
-            return;
-        }
-
-        getMockEndpoint("mock:result").expectedMessageCount(0);
-        getMockEndpoint("mock:unmatched").expectedMessageCount(1);
-
-        Map<String, Object> headers = new HashMap<String, Object>();
-        Map<String, Object> arguments = new HashMap<String, Object>();
-        arguments.put("foo", "bar");
-        arguments.put("baz", 7);
-        arguments.put("", "foo");
-        arguments.put(null, "bar");
-        headers.put(ScriptBuilder.ARGUMENTS, arguments);
-
-        sendBody("direct:start", "hello", headers);
-
-        assertMockEndpointsSatisfied();
-    }
-
-    @Test
-    public void testArgumentsWithIntegerMap() throws Exception {
-        if (!ScriptTestHelper.canRunTestOnThisPlatform()) {
-            return;
-        }
-
-        getMockEndpoint("mock:result").expectedMessageCount(0);
-        getMockEndpoint("mock:unmatched").expectedMessageCount(1);
-
-        Map<String, Object> headers = new HashMap<String, Object>();
-        Map<Integer, Object> arguments = new HashMap<Integer, Object>();
-        arguments.put(0, "bar");
-        arguments.put(1, 7);
-        headers.put(ScriptBuilder.ARGUMENTS, arguments);
-
-        sendBody("direct:start", "hello", headers);
-
-        assertMockEndpointsSatisfied();
-    }
-
-    @Test
-    public void testArgumentsWithNonMap() throws Exception {
-        if (!ScriptTestHelper.canRunTestOnThisPlatform()) {
-            return;
-        }
-
-        getMockEndpoint("mock:result").expectedMessageCount(0);
-        getMockEndpoint("mock:unmatched").expectedMessageCount(1);
-
-        Map<String, Object> headers = new HashMap<String, Object>();
-        String arguments = "foo";
-        headers.put(ScriptBuilder.ARGUMENTS, arguments);
-
-        sendBody("direct:start", "hello", headers);
+        template.sendBodyAndHeader("direct:start", "Hello World", "foo", "Camel");
 
         assertMockEndpointsSatisfied();
     }
@@ -150,9 +48,14 @@ public class JavaScriptExpressionTest ex
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             public void configure() throws Exception {
+                PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
+                pc.setLocation("org/apache/camel/builder/script/myproperties.properties");
+
                 from("direct:start").choice().
-                        when().javaScript("request.headers.get('foo') == 'bar'").to("log:info?showAll=true").to("mock:result")
-                        .otherwise().to("mock:unmatched");
+                        when().groovy("request.headers.get('foo') == context.resolvePropertyPlaceholders('{{foo}}')")
+                            .to("mock:result")
+                        .otherwise()
+                            .to("mock:unmatched");
             }
         };
     }

Propchange: camel/trunk/components/camel-script/src/test/java/org/apache/camel/builder/script/GroovyPropertyComponentTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-script/src/test/java/org/apache/camel/builder/script/GroovyPropertyComponentTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: camel/trunk/components/camel-script/src/test/java/org/apache/camel/builder/script/GroovySetHeaderPropertyComponentTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-script/src/test/java/org/apache/camel/builder/script/GroovySetHeaderPropertyComponentTest.java?rev=1189136&view=auto
==============================================================================
--- camel/trunk/components/camel-script/src/test/java/org/apache/camel/builder/script/GroovySetHeaderPropertyComponentTest.java (added)
+++ camel/trunk/components/camel-script/src/test/java/org/apache/camel/builder/script/GroovySetHeaderPropertyComponentTest.java Wed Oct 26 11:15:28 2011
@@ -0,0 +1,60 @@
+/**
+ * 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.builder.script;
+
+import org.apache.camel.ScriptTestHelper;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.component.properties.PropertiesComponent;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class GroovySetHeaderPropertyComponentTest extends CamelTestSupport {
+
+    @Test
+    public void testSendMatchingMessage() throws Exception {
+        if (!ScriptTestHelper.canRunTestOnThisPlatform()) {
+            return;
+        }
+
+        log.info("Can run this test");
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Hello World");
+        mock.expectedHeaderReceived("myHeader", "Kong");
+
+        template.sendBodyAndHeader("direct:start", "Hello World", "foo", "bar");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
+                pc.setLocation("org/apache/camel/builder/script/myproperties.properties");
+
+                from("direct:start")
+                    .setHeader("myHeader").groovy("context.resolvePropertyPlaceholders('{{' + request.headers.get('foo') + '}}')")
+                    .to("mock:result");
+            }
+        };
+    }
+}

Added: camel/trunk/components/camel-script/src/test/java/org/apache/camel/builder/script/JavaScriptPropertiesFunctionTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-script/src/test/java/org/apache/camel/builder/script/JavaScriptPropertiesFunctionTest.java?rev=1189136&view=auto
==============================================================================
--- camel/trunk/components/camel-script/src/test/java/org/apache/camel/builder/script/JavaScriptPropertiesFunctionTest.java (added)
+++ camel/trunk/components/camel-script/src/test/java/org/apache/camel/builder/script/JavaScriptPropertiesFunctionTest.java Wed Oct 26 11:15:28 2011
@@ -0,0 +1,58 @@
+/**
+ * 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.builder.script;
+
+import org.apache.camel.ScriptTestHelper;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.component.properties.PropertiesComponent;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class JavaScriptPropertiesFunctionTest extends CamelTestSupport {
+
+    @Test
+    public void testSendMatchingMessage() throws Exception {
+        if (!ScriptTestHelper.canRunTestOnThisPlatform()) {
+            return;
+        }
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Hello World");
+        mock.expectedHeaderReceived("myHeader", "Kong");
+
+        template.sendBodyAndHeader("direct:start", "Hello World", "foo", "bar");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
+                pc.setLocation("org/apache/camel/builder/script/myproperties.properties");
+
+                from("direct:start")
+                    .setHeader("myHeader").javaScript("properties.resolve(request.headers.get('foo'))")
+                    .to("mock:result");
+            }
+        };
+    }
+}

Added: camel/trunk/components/camel-script/src/test/resources/org/apache/camel/builder/script/myproperties.properties
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-script/src/test/resources/org/apache/camel/builder/script/myproperties.properties?rev=1189136&view=auto
==============================================================================
--- camel/trunk/components/camel-script/src/test/resources/org/apache/camel/builder/script/myproperties.properties (added)
+++ camel/trunk/components/camel-script/src/test/resources/org/apache/camel/builder/script/myproperties.properties Wed Oct 26 11:15:28 2011
@@ -0,0 +1,19 @@
+## ---------------------------------------------------------------------------
+## 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.
+## ---------------------------------------------------------------------------
+
+foo=Camel
+bar=Kong
\ No newline at end of file