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 2018/01/22 09:05:23 UTC

[camel] branch master updated (3701f2d -> 52b382b)

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git.


    from 3701f2d  CAMEL-12172 - Camel-AWS Kinesis: Add the ability to specify credentials and region at component level
     new 03b63be  CAMEL-12154: camel-saxon should set parameters on saxon using their type, eg string, boolean, integer etc. Thanks to Niels Bertram for the unit test.
     new 52b382b  CAMEL-12154: camel-saxon should set parameters on saxon using their type, eg string, boolean, integer etc. Thanks to Niels Bertram for the unit test.

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../camel/component/xquery/XQueryBuilder.java      | 33 +++++++-
 .../camel/builder/saxon/ParameterDynamicTest.java  | 96 ++++++++++++++++++++++
 2 files changed, 125 insertions(+), 4 deletions(-)
 create mode 100644 components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/ParameterDynamicTest.java

-- 
To stop receiving notification emails like this one, please contact
davsclaus@apache.org.

[camel] 02/02: CAMEL-12154: camel-saxon should set parameters on saxon using their type, eg string, boolean, integer etc. Thanks to Niels Bertram for the unit test.

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 52b382b864323363fd0a5831e7f7ed8d8221c244
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Jan 22 09:57:04 2018 +0100

    CAMEL-12154: camel-saxon should set parameters on saxon using their type, eg string, boolean, integer etc. Thanks to Niels Bertram for the unit test.
---
 .../org/apache/camel/component/xquery/XQueryBuilder.java   | 14 +++++---------
 .../apache/camel/builder/saxon/ParameterDynamicTest.java   |  6 +++---
 2 files changed, 8 insertions(+), 12 deletions(-)

diff --git a/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQueryBuilder.java b/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQueryBuilder.java
index 28aacc9..6932c93 100644
--- a/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQueryBuilder.java
+++ b/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQueryBuilder.java
@@ -22,7 +22,6 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.Reader;
 import java.io.StringWriter;
-import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.net.URL;
 import java.util.Collection;
@@ -42,14 +41,6 @@ import javax.xml.transform.stax.StAXSource;
 import javax.xml.transform.stream.StreamResult;
 import javax.xml.transform.stream.StreamSource;
 
-import net.sf.saxon.value.BigDecimalValue;
-import net.sf.saxon.value.BooleanValue;
-import net.sf.saxon.value.DoubleValue;
-import net.sf.saxon.value.FloatValue;
-import net.sf.saxon.value.Int64Value;
-import net.sf.saxon.value.IntegerValue;
-import net.sf.saxon.value.NumericValue;
-import net.sf.saxon.value.StringValue;
 import org.w3c.dom.Node;
 
 import net.sf.saxon.Configuration;
@@ -62,7 +53,12 @@ import net.sf.saxon.query.DynamicQueryContext;
 import net.sf.saxon.query.StaticQueryContext;
 import net.sf.saxon.query.XQueryExpression;
 import net.sf.saxon.trans.XPathException;
+import net.sf.saxon.value.BooleanValue;
+import net.sf.saxon.value.DoubleValue;
+import net.sf.saxon.value.FloatValue;
+import net.sf.saxon.value.Int64Value;
 import net.sf.saxon.value.ObjectValue;
+import net.sf.saxon.value.StringValue;
 import net.sf.saxon.value.Whitespace;
 import org.apache.camel.BytesSource;
 import org.apache.camel.Exchange;
diff --git a/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/ParameterDynamicTest.java b/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/ParameterDynamicTest.java
index c1c62f1..50afdda 100644
--- a/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/ParameterDynamicTest.java
+++ b/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/ParameterDynamicTest.java
@@ -5,9 +5,9 @@
  * 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
- * <p>
- * http://www.apache.org/licenses/LICENSE-2.0
- * <p>
+ *
+ *      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.

-- 
To stop receiving notification emails like this one, please contact
davsclaus@apache.org.

[camel] 01/02: CAMEL-12154: camel-saxon should set parameters on saxon using their type, eg string, boolean, integer etc. Thanks to Niels Bertram for the unit test.

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 03b63be0e6a1c393d6af682da0c734cb35c913a8
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Jan 22 09:54:56 2018 +0100

    CAMEL-12154: camel-saxon should set parameters on saxon using their type, eg string, boolean, integer etc. Thanks to Niels Bertram for the unit test.
---
 .../camel/component/xquery/XQueryBuilder.java      | 37 ++++++++-
 .../camel/builder/saxon/ParameterDynamicTest.java  | 96 ++++++++++++++++++++++
 2 files changed, 129 insertions(+), 4 deletions(-)

diff --git a/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQueryBuilder.java b/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQueryBuilder.java
index 98be79b..28aacc9 100644
--- a/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQueryBuilder.java
+++ b/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQueryBuilder.java
@@ -22,6 +22,8 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.Reader;
 import java.io.StringWriter;
+import java.math.BigDecimal;
+import java.math.BigInteger;
 import java.net.URL;
 import java.util.Collection;
 import java.util.Collections;
@@ -40,6 +42,14 @@ import javax.xml.transform.stax.StAXSource;
 import javax.xml.transform.stream.StreamResult;
 import javax.xml.transform.stream.StreamSource;
 
+import net.sf.saxon.value.BigDecimalValue;
+import net.sf.saxon.value.BooleanValue;
+import net.sf.saxon.value.DoubleValue;
+import net.sf.saxon.value.FloatValue;
+import net.sf.saxon.value.Int64Value;
+import net.sf.saxon.value.IntegerValue;
+import net.sf.saxon.value.NumericValue;
+import net.sf.saxon.value.StringValue;
 import org.w3c.dom.Node;
 
 import net.sf.saxon.Configuration;
@@ -623,19 +633,19 @@ public abstract class XQueryBuilder implements Expression, Predicate, NamespaceA
         addParameters(dynamicQueryContext, exchange.getIn().getHeaders(), "in.headers.");
         dynamicQueryContext.setParameter(
             StructuredQName.fromClarkName("in.body"),
-            new ObjectValue(exchange.getIn().getBody())
+            getAsParameter(exchange.getIn().getBody())
         );
 
         addParameters(dynamicQueryContext, getParameters());
 
         dynamicQueryContext.setParameter(
             StructuredQName.fromClarkName("exchange"),
-            new ObjectValue(exchange)
+            getAsParameter(exchange)
         );
         if (exchange.hasOut() && exchange.getPattern().isOutCapable()) {
             dynamicQueryContext.setParameter(
                 StructuredQName.fromClarkName("out.body"),
-                new ObjectValue(exchange.getOut().getBody())
+                getAsParameter(exchange.getOut().getBody())
             );
 
             addParameters(dynamicQueryContext, exchange.getOut().getHeaders(), "out.headers.");
@@ -653,12 +663,31 @@ public abstract class XQueryBuilder implements Expression, Predicate, NamespaceA
             if (entry.getValue() != null) {
                 dynamicQueryContext.setParameter(
                         StructuredQName.fromClarkName(parameterPrefix + entry.getKey()),
-                        new ObjectValue(entry.getValue())
+                        getAsParameter(entry.getValue())
                 );
             }
         }
     }
 
+    @SuppressWarnings("unchecked")
+    protected Item getAsParameter(Object value) {
+        if (value instanceof String) {
+            return new StringValue((CharSequence) value);
+        } else if (value instanceof Boolean) {
+            return BooleanValue.get((Boolean) value);
+        } else if (value instanceof Long) {
+            return Int64Value.makeIntegerValue((Long) value);
+        } else if (value instanceof BigInteger) {
+            return Int64Value.makeIntegerValue((BigInteger) value);
+        } else if (value instanceof Double) {
+            return DoubleValue.makeDoubleValue((double) value);
+        } else if (value instanceof Float) {
+            return FloatValue.makeFloatValue((float) value);
+        } else {
+            return new ObjectValue(value);
+        }
+    }
+
     protected boolean matches(Exchange exchange, List<?> results) {
         return ObjectHelper.matches(results);
     }
diff --git a/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/ParameterDynamicTest.java b/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/ParameterDynamicTest.java
new file mode 100644
index 0000000..c1c62f1
--- /dev/null
+++ b/components/camel-saxon/src/test/java/org/apache/camel/builder/saxon/ParameterDynamicTest.java
@@ -0,0 +1,96 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.saxon;
+
+import net.sf.saxon.Configuration;
+import net.sf.saxon.om.Item;
+import net.sf.saxon.om.StructuredQName;
+import net.sf.saxon.query.DynamicQueryContext;
+import net.sf.saxon.query.XQueryExpression;
+import net.sf.saxon.value.BooleanValue;
+import net.sf.saxon.value.ObjectValue;
+import org.apache.camel.Exchange;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.impl.DefaultExchange;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.apache.camel.component.xquery.XQueryBuilder.xquery;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+public class ParameterDynamicTest {
+
+    private static final String TEST_QUERY = new StringBuilder()
+        .append("xquery version \"3.0\" encoding \"UTF-8\";\n")
+        .append("declare variable $extParam as xs:boolean external := false();\n")
+        .append("if($extParam) then(true()) else (false())")
+        .toString();
+
+    private Configuration conf = new Configuration();
+    private XQueryExpression query;
+    private DynamicQueryContext context;
+
+    @Before
+    public void setup() throws Exception {
+        conf.setCompileWithTracing(true);
+        query = conf.newStaticQueryContext().compileQuery(TEST_QUERY);
+        context = new DynamicQueryContext(conf);
+    }
+
+    /**
+     * This is what Camel XQueryBuilder executes, which leads to a parameter binding type error.
+     */
+    @Test
+    public void testObjectParameter() throws Exception {
+        context.setParameter(StructuredQName.fromClarkName("extParam"), new ObjectValue<>(true));
+        try {
+            Item result = query.iterator(context).next();
+            fail("Should have thrown an exception");
+            assertTrue(result instanceof BooleanValue);
+            assertEquals(true, ((BooleanValue) result).getBooleanValue());
+        } catch (Exception e) {
+            // expected
+        }
+    }
+
+    /**
+     * This is what Camel XQueryBuilder should execute to allow Saxon to bind the parameter type properly.
+     */
+    @Test
+    public void testBooleanParameter() throws Exception {
+        context.setParameter(StructuredQName.fromClarkName("extParam"), BooleanValue.TRUE);
+        Item result = query.iterator(context).next();
+        assertTrue(result instanceof BooleanValue);
+        assertEquals(true, ((BooleanValue) result).getBooleanValue());
+    }
+
+    @Test
+    public void testXQueryBuilder() throws Exception {
+        Exchange exchange = new DefaultExchange(new DefaultCamelContext());
+        exchange.getIn().setBody("<foo><bar>abc_def_ghi</bar></foo>");
+        exchange.setProperty("extParam", true);
+
+        Object result = xquery(TEST_QUERY).asString().evaluate(exchange, boolean.class);
+        assertEquals(true, result);
+
+        exchange.setProperty("extParam", false);
+        result = xquery(TEST_QUERY).asString().evaluate(exchange, boolean.class);
+        assertEquals(false, result);
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
davsclaus@apache.org.