From 52f26be2d2bf43b8bb35b88cc3984bc2548ddab6 Mon Sep 17 00:00:00 2001 From: Niels Bertram Date: Sun, 14 Jul 2013 03:49:31 +1000 Subject: [PATCH] CAMEL-6548: ensure the saxon configuration set using XQueryBuilder.setConfiguration() is honoured during component initialization --- .../camel/component/xquery/XQueryBuilder.java | 4 +- .../xquery/XQueryWithExtensionTest.java | 123 ++++++++++++++++++ .../xquery/transformWithExtension.xquery | 6 + .../xquery/xqueryWithExtensionTest.xml | 35 +++++ 4 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 components/camel-saxon/src/test/java/org/apache/camel/component/xquery/XQueryWithExtensionTest.java create mode 100644 components/camel-saxon/src/test/resources/org/apache/camel/component/xquery/transformWithExtension.xquery create mode 100644 components/camel-saxon/src/test/resources/org/apache/camel/component/xquery/xqueryWithExtensionTest.xml 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 1b6e640d84722..2b17674c5b57f 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 @@ -636,7 +636,9 @@ protected synchronized void initialize(Exchange exchange) throws XPathException, // must use synchronized for concurrency issues and only let it initialize once if (!initialized.get()) { LOG.debug("Initializing XQueryBuilder {}", this); - configuration = new Configuration(); + if (configuration == null) { + configuration = new Configuration(); + } configuration.setHostLanguage(Configuration.XQUERY); configuration.setStripsWhiteSpace(isStripsAllWhiteSpace() ? Whitespace.ALL : Whitespace.IGNORABLE); diff --git a/components/camel-saxon/src/test/java/org/apache/camel/component/xquery/XQueryWithExtensionTest.java b/components/camel-saxon/src/test/java/org/apache/camel/component/xquery/XQueryWithExtensionTest.java new file mode 100644 index 0000000000000..6fe76b59e5e62 --- /dev/null +++ b/components/camel-saxon/src/test/java/org/apache/camel/component/xquery/XQueryWithExtensionTest.java @@ -0,0 +1,123 @@ +/** + * 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.xquery; + +import net.sf.saxon.Configuration; +import net.sf.saxon.expr.XPathContext; +import net.sf.saxon.lib.ExtensionFunctionCall; +import net.sf.saxon.lib.ExtensionFunctionDefinition; +import net.sf.saxon.om.Item; +import net.sf.saxon.om.SequenceIterator; +import net.sf.saxon.om.StructuredQName; +import net.sf.saxon.trans.XPathException; +import net.sf.saxon.tree.iter.SingletonIterator; +import net.sf.saxon.value.SequenceType; +import net.sf.saxon.value.StringValue; + +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.impl.ProcessorEndpoint; +import org.apache.camel.test.spring.CamelSpringTestSupport; +import org.junit.Test; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * @version + */ +public class XQueryWithExtensionTest extends CamelSpringTestSupport { + + @Test + public void testWithExtension() throws Exception { + + // have to get to the XQueryBuilder somehow to register our custom saxon + // extension function + ProcessorEndpoint pep = (ProcessorEndpoint) this.context + .getEndpoint("xquery://org/apache/camel/component/xquery/transformWithExtension.xquery"); + + XQueryBuilder xqb = (XQueryBuilder) pep.getProcessor(); + + // add a custom configuration to the XQueryBuilder with an externally + // registered xpath extension + Configuration conf = new Configuration(); + conf.registerExtensionFunction(new SimpleExtention()); + xqb.setConfiguration(conf); + + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedBodiesReceived(""); + + template.sendBody("direct:start", "test"); + + assertMockEndpointsSatisfied(); + } + + protected ClassPathXmlApplicationContext createApplicationContext() { + return new ClassPathXmlApplicationContext( + "org/apache/camel/component/xquery/xqueryWithExtensionTest.xml"); + } + + /** + * This is a very simple example of a saxon extension function. We will use + * this for testing purposes. + * + * Example: efx:simple('some text') will be rendered to + * arg1[some text] and returned in the XQuery response. + * + */ + public static class SimpleExtention extends ExtensionFunctionDefinition { + + private static final long serialVersionUID = 1L; + + @Override + public SequenceType[] getArgumentTypes() { + return new SequenceType[] { SequenceType.SINGLE_STRING }; + } + + @Override + public SequenceType getResultType(SequenceType[] suppliedArgumentTypes) { + return SequenceType.SINGLE_STRING; + } + + @Override + public StructuredQName getFunctionQName() { + return new StructuredQName("efx", "http://test/saxon/ext", "simple"); + } + + @Override + public ExtensionFunctionCall makeCallExpression() { + return new ExtensionFunctionCall() { + + private static final long serialVersionUID = 1L; + + @SuppressWarnings("rawtypes") + @Override + public SequenceIterator call( + SequenceIterator[] args, + XPathContext context) throws XPathException { + + // get value of first arg passed to the function + Item arg1 = args[0].next(); + String arg1Val = arg1.getStringValue(); + + // return a altered version of the first arg + StringValue sv = new StringValue("arg1[" + arg1Val + "]"); + return SingletonIterator.makeIterator(sv); + } + + }; + } + } + +} \ No newline at end of file diff --git a/components/camel-saxon/src/test/resources/org/apache/camel/component/xquery/transformWithExtension.xquery b/components/camel-saxon/src/test/resources/org/apache/camel/component/xquery/transformWithExtension.xquery new file mode 100644 index 0000000000000..dff3ece1185ce --- /dev/null +++ b/components/camel-saxon/src/test/resources/org/apache/camel/component/xquery/transformWithExtension.xquery @@ -0,0 +1,6 @@ +xquery version "1.0" encoding "UTF-8"; + +(: the prefix declaration for our custom extension :) +declare namespace efx = "http://test/saxon/ext"; + + \ No newline at end of file diff --git a/components/camel-saxon/src/test/resources/org/apache/camel/component/xquery/xqueryWithExtensionTest.xml b/components/camel-saxon/src/test/resources/org/apache/camel/component/xquery/xqueryWithExtensionTest.xml new file mode 100644 index 0000000000000..34ce888b5832d --- /dev/null +++ b/components/camel-saxon/src/test/resources/org/apache/camel/component/xquery/xqueryWithExtensionTest.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + +