This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push: new 63b2a66 CAMEL-17466: Added unit test. Thanks to Felix for the test case. 63b2a66 is described below commit 63b2a66be519d118b79be9170c9e89ea8701b30b Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Wed Jan 12 10:04:52 2022 +0100 CAMEL-17466: Added unit test. Thanks to Felix for the test case. --- core/camel-xml-jaxp/pom.xml | 6 + .../XMLTokenizeExpressionIteratorWModeTest.java | 150 +++++++++++++++++++++ .../src/test/resources/log4j2.properties | 32 +++++ 3 files changed, 188 insertions(+) diff --git a/core/camel-xml-jaxp/pom.xml b/core/camel-xml-jaxp/pom.xml index 95d418b..fda0401 100644 --- a/core/camel-xml-jaxp/pom.xml +++ b/core/camel-xml-jaxp/pom.xml @@ -64,6 +64,12 @@ <artifactId>junit-jupiter</artifactId> <scope>test</scope> </dependency> + <!-- logging --> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-slf4j-impl</artifactId> + <scope>test</scope> + </dependency> </dependencies> </project> diff --git a/core/camel-xml-jaxp/src/test/java/org/apache/camel/language/xtokenizer/XMLTokenizeExpressionIteratorWModeTest.java b/core/camel-xml-jaxp/src/test/java/org/apache/camel/language/xtokenizer/XMLTokenizeExpressionIteratorWModeTest.java new file mode 100644 index 0000000..2722e69 --- /dev/null +++ b/core/camel-xml-jaxp/src/test/java/org/apache/camel/language/xtokenizer/XMLTokenizeExpressionIteratorWModeTest.java @@ -0,0 +1,150 @@ +/* + * 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.language.xtokenizer; + +import java.io.ByteArrayInputStream; +import java.io.Closeable; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class XMLTokenizeExpressionIteratorWModeTest { + + private static final String XML_BEFORE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + + "<root>\n" + + " <Level1>\n" + + " <Level2preceding>Included</Level2preceding>\n" + + " <Level2following>Not Included</Level2following>\n" + + " <Level2>\n" + + " <data>Hello, World!</data>\n" + + " <data>Hello, Camel!</data>\n" + + " <data>Hello, Apache Foundation!</data>\n" + + " </Level2>\n" + + " </Level1>\n" + + "</root>"; + + private static final String XML_AFTER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + + "<root>\n" + + " <Level1>\n" + + " <Level2preceding>Included</Level2preceding>\n" + + " <Level2>\n" + + " <data>Hello, World!</data>\n" + + " <data>Hello, Camel!</data>\n" + + " <data>Hello, Apache Foundation!</data>\n" + + " </Level2>\n" + + " <Level2following>Not Included</Level2following>\n" + + " </Level1>\n" + + "</root>"; + + private static final String RS1_BEFORE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + + "<root>\n" + + " <Level1>\n" + + " <Level2preceding>Included</Level2preceding>\n" + + " <Level2following>Not Included</Level2following>\n" + + " <Level2>\n" + + " <data>Hello, World!</data></Level2></Level1></root>"; + + private static final String RS2_BEFORE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + + "<root>\n" + + " <Level1>\n" + + " <Level2preceding>Included</Level2preceding>\n" + + " <Level2following>Not Included</Level2following>\n" + + " <Level2>\n" + + " <data>Hello, Camel!</data></Level2></Level1></root>"; + + private static final String RS3_BEFORE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + + "<root>\n" + + " <Level1>\n" + + " <Level2preceding>Included</Level2preceding>\n" + + " <Level2following>Not Included</Level2following>\n" + + " <Level2>\n" + + " <data>Hello, Apache Foundation!</data></Level2></Level1></root>"; + + private static final String[] RS_BEFORE = { RS1_BEFORE, RS2_BEFORE, RS3_BEFORE }; + + private static final String RS1_AFTER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + + "<root>\n" + + " <Level1>\n" + + " <Level2preceding>Included</Level2preceding>\n" + + " <Level2>\n" + + " <data>Hello, World!</data><Level2following>Not Included</Level2following></Level2></Level1></root>"; + + private static final String RS2_AFTER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + + "<root>\n" + + " <Level1>\n" + + " <Level2preceding>Included</Level2preceding>\n" + + " <Level2>\n" + + " <data>Hello, Camel!</data><Level2following>Not Included</Level2following></Level2></Level1></root>"; + + private static final String RS3_AFTER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + + "<root>\n" + + " <Level1>\n" + + " <Level2preceding>Included</Level2preceding>\n" + + " <Level2>\n" + + " <data>Hello, Apache Foundation!</data><Level2following>Not Included</Level2following></Level2></Level1></root>"; + + private static final String[] RS_AFTER = { RS1_AFTER, RS2_AFTER, RS3_AFTER }; + + private Map<String, String> nsmap; + + @BeforeEach + public void setUp() { + nsmap = new HashMap<>(); + nsmap.put("ns1", ""); + } + + @Test + public void testWModeBefore() throws Exception { + InputStream is = new ByteArrayInputStream(XML_BEFORE.getBytes(StandardCharsets.UTF_8)); + invokeAndVerify("/root/Level1/Level2/data", 'w', is, RS_BEFORE); + } + + @Test + @Disabled("TODO: https://issues.apache.org/jira/browse/CAMEL-17466") + public void testWModeAfter() throws Exception { + InputStream is = new ByteArrayInputStream(XML_AFTER.getBytes(StandardCharsets.UTF_8)); + invokeAndVerify("/root/Level1/Level2/data", 'w', is, RS_AFTER); + } + + private void invokeAndVerify(String path, char mode, InputStream in, String[] expected) throws Exception { + XMLTokenExpressionIterator xtei = new XMLTokenExpressionIterator(path, mode); + xtei.setNamespaces(nsmap); + + Iterator<?> it = xtei.createIterator(in); + List<String> results = new ArrayList<>(); + while (it.hasNext()) { + results.add((String) it.next()); + } + ((Closeable) it).close(); + + assertEquals(expected.length, results.size(), "token count"); + for (int i = 0; i < expected.length; i++) { + assertEquals(expected[i], results.get(i), "mismatch [" + i + "]"); + } + } + +} diff --git a/core/camel-xml-jaxp/src/test/resources/log4j2.properties b/core/camel-xml-jaxp/src/test/resources/log4j2.properties new file mode 100644 index 0000000..0ebe4ca --- /dev/null +++ b/core/camel-xml-jaxp/src/test/resources/log4j2.properties @@ -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. +## --------------------------------------------------------------------------- +appender.console.type = Console +appender.console.name = console +appender.console.layout.type = PatternLayout +appender.console.layout.pattern = %d [%-15.15t] %-5p %-30.30c{1} - %m%n + +appender.file.type = File +appender.file.name = file +appender.file.fileName = target/camel-xml-jaxp-test.log +appender.file.append = true +appender.file.layout.type = PatternLayout +appender.file.layout.pattern = %d [%-15.15t] %-5p %-30.30c{1} - %m%n + +rootLogger.level = INFO + +rootLogger.appenderRef.file.ref = file +#rootLogger.appenderRef.console.ref = console