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.gitcommit cf86cdde44e4c4d558c816d607ce11f8a3022b2f
Author: Claus Ibsen <
[hidden email]>
AuthorDate: Wed Nov 13 05:53:20 2019 +0100
CAMEL-14126: Added unit test and made camel-stax accept more input data. Thanks to tomahi for sample project.
---
.../component/stax/StAXJAXBIteratorExpression.java | 8 +--
.../component/stax/IssueWithWrongEncodingTest.java | 72 ++++++++++++++++++++++
.../apache/camel/component/stax/model/Product.java | 36 +++++++++++
.../src/test/resources/products_with_non_utf8.xml | 9 +++
.../test/resources/products_with_valid_utf8.xml | 27 ++++++++
5 files changed, 147 insertions(+), 5 deletions(-)
diff --git a/components/camel-stax/src/main/java/org/apache/camel/component/stax/StAXJAXBIteratorExpression.java b/components/camel-stax/src/main/java/org/apache/camel/component/stax/StAXJAXBIteratorExpression.java
index 38d7b70..62f1fb4 100644
--- a/components/camel-stax/src/main/java/org/apache/camel/component/stax/StAXJAXBIteratorExpression.java
+++ b/components/camel-stax/src/main/java/org/apache/camel/component/stax/StAXJAXBIteratorExpression.java
@@ -115,13 +115,11 @@ public class StAXJAXBIteratorExpression<T> extends ExpressionAdapter {
@SuppressWarnings("unchecked")
public Object evaluate(Exchange exchange) {
try {
- XMLEventReader reader;
- if (isNamespaceAware) {
- reader = exchange.getIn().getMandatoryBody(XMLEventReader.class);
- } else {
+ XMLEventReader reader = exchange.getContext().getTypeConverter().tryConvertTo(XMLEventReader.class, exchange, exchange.getIn().getBody());
+ if (reader == null) {
InputStream inputStream = exchange.getIn().getMandatoryBody(InputStream.class);
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
- xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false);
+ xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, isNamespaceAware);
reader = xmlInputFactory.createXMLEventReader(inputStream);
}
diff --git a/components/camel-stax/src/test/java/org/apache/camel/component/stax/IssueWithWrongEncodingTest.java b/components/camel-stax/src/test/java/org/apache/camel/component/stax/IssueWithWrongEncodingTest.java
new file mode 100644
index 0000000..b4f2337
--- /dev/null
+++ b/components/camel-stax/src/test/java/org/apache/camel/component/stax/IssueWithWrongEncodingTest.java
@@ -0,0 +1,72 @@
+/*
+ * 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.stax;
+
+import java.io.File;
+import java.nio.charset.StandardCharsets;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.stax.model.Product;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+import static org.apache.camel.component.stax.StAXBuilder.stax;
+
+public class IssueWithWrongEncodingTest extends CamelTestSupport {
+
+ @Override
+ public void setUp() throws Exception {
+ deleteDirectory("target/encoding");
+ super.setUp();
+ }
+
+ @Test
+ public void testOkEncoding() throws Exception {
+ getMockEndpoint("mock:result").expectedMessageCount(1);
+
+ File file = new File("src/test/resources/products_with_valid_utf8.xml");
+ template.sendBodyAndHeader("file:target/encoding", file, Exchange.FILE_NAME, "products_with_valid_utf8.xml");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Test
+ public void testInvalidEncoding() throws Exception {
+ getMockEndpoint("mock:result").expectedMessageCount(0);
+ getMockEndpoint("mock:result").expectedFileExists("target/encoding/error/products_with_non_utf8.xml");
+
+ File file = new File("src/test/resources/products_with_non_utf8.xml");
+ template.sendBodyAndHeader("file:target/encoding", file, Exchange.FILE_NAME, "products_with_non_utf8.xml");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RoutesBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("file:target/encoding?moveFailed=error")
+ .split(stax(Product.class))
+ .to("mock:result");
+ }
+ };
+ }
+}
diff --git a/components/camel-stax/src/test/java/org/apache/camel/component/stax/model/Product.java b/components/camel-stax/src/test/java/org/apache/camel/component/stax/model/Product.java
new file mode 100644
index 0000000..f5ddc1e
--- /dev/null
+++ b/components/camel-stax/src/test/java/org/apache/camel/component/stax/model/Product.java
@@ -0,0 +1,36 @@
+/*
+ * 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.stax.model;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlType;
+
+@XmlType(name = "product")
+@XmlAccessorType(XmlAccessType.FIELD)
+public class Product {
+
+ private String name;
+
+ public String getName() {
+ return this.name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+}
diff --git a/components/camel-stax/src/test/resources/products_with_non_utf8.xml b/components/camel-stax/src/test/resources/products_with_non_utf8.xml
new file mode 100755
index 0000000..d8ae660
--- /dev/null
+++ b/components/camel-stax/src/test/resources/products_with_non_utf8.xml
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<products>
+ <product>
+ <name>first product ��</name>
+ </product>
+ <product>
+ <name>second product</name>
+ </product>
+</products>
\ No newline at end of file
diff --git a/components/camel-stax/src/test/resources/products_with_valid_utf8.xml b/components/camel-stax/src/test/resources/products_with_valid_utf8.xml
new file mode 100755
index 0000000..465908b
--- /dev/null
+++ b/components/camel-stax/src/test/resources/products_with_valid_utf8.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ 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.
+
+-->
+<products>
+ <product>
+ <name>first product</name>
+ </product>
+ <product>
+ <name>second product</name>
+ </product>
+</products>
\ No newline at end of file