You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ja...@apache.org on 2020/02/28 07:38:13 UTC

[camel-quarkus] 02/03: Fix #498 Improve the XSLT test coverage

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

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

commit 03723a8a7722a41f4277166da162db4ce2b8442a
Author: Peter Palaga <pp...@redhat.com>
AuthorDate: Thu Feb 27 18:20:42 2020 +0100

    Fix #498 Improve the XSLT test coverage
---
 .../xslt/deployment/XsltNativeImageProcessor.java  | 29 ++++++++++++-----
 .../component/xslt/deployment/XsltProcessor.java   |  3 +-
 integration-tests/xml/pom.xml                      |  4 ---
 .../quarkus/component/xml/it/XmlResource.java      | 25 ++++++++++++++
 .../quarkus/component/xml/it/XmlRouteBuilder.java  | 12 +++++++
 .../xml/src/main/resources/application.properties  |  2 +-
 .../xml/src/main/resources/xslt/html-to-text.xsl   | 32 ++++++++++++++++++
 .../xml/src/main/resources/xslt/html-transform.xsl | 38 ++++++++++++++++++++++
 .../camel/quarkus/component/xml/it/XmlTest.java    | 38 ++++++++++++++++++++++
 9 files changed, 168 insertions(+), 15 deletions(-)

diff --git a/extensions/xslt/deployment/src/main/java/org/apache/camel/quarkus/component/xslt/deployment/XsltNativeImageProcessor.java b/extensions/xslt/deployment/src/main/java/org/apache/camel/quarkus/component/xslt/deployment/XsltNativeImageProcessor.java
index 8c78c3b..fb32115 100644
--- a/extensions/xslt/deployment/src/main/java/org/apache/camel/quarkus/component/xslt/deployment/XsltNativeImageProcessor.java
+++ b/extensions/xslt/deployment/src/main/java/org/apache/camel/quarkus/component/xslt/deployment/XsltNativeImageProcessor.java
@@ -17,13 +17,14 @@
 package org.apache.camel.quarkus.component.xslt.deployment;
 
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.List;
 import java.util.Objects;
 import java.util.stream.Collectors;
 
+import io.quarkus.deployment.annotations.BuildProducer;
 import io.quarkus.deployment.annotations.BuildStep;
 import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem;
+import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBundleBuildItem;
 import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
 import org.apache.camel.quarkus.component.xslt.CamelXsltConfig;
 import org.apache.camel.support.ResourceHelper;
@@ -45,25 +46,35 @@ class XsltNativeImageProcessor {
     }
 
     @BuildStep
-    List<NativeImageResourceBuildItem> xsltResources(CamelXsltConfig config) {
+    void xsltResources(
+            CamelXsltConfig config,
+            BuildProducer<NativeImageResourceBuildItem> nativeResources,
+            BuildProducer<NativeImageResourceBundleBuildItem> nativeResourceBundles) {
         if (!config.sources.isPresent()) {
-            return Collections.emptyList();
+            return;
         }
 
-        List<NativeImageResourceBuildItem> items = new ArrayList<>(config.sources.get().size());
-
-        for (String source : config.sources.get()) {
+        final List<String> sources = config.sources.get();
+        List<String> paths = new ArrayList<>(sources.size() + 5);
+        for (String source : sources) {
             String scheme = ResourceHelper.getScheme(source);
 
             if (Objects.isNull(scheme) || Objects.equals(scheme, CLASSPATH_SCHEME)) {
                 if (Objects.equals(scheme, CLASSPATH_SCHEME)) {
                     source = source.substring(CLASSPATH_SCHEME.length() + 1);
                 }
-
-                items.add(new NativeImageResourceBuildItem(source));
+                paths.add(source);
             }
         }
+        paths.add("org/apache/xml/serializer/Encodings.properties");
+        paths.add("org/apache/xml/serializer/output_html.properties");
+        paths.add("org/apache/xml/serializer/output_text.properties");
+        paths.add("org/apache/xml/serializer/output_unknown.properties");
+        paths.add("org/apache/xml/serializer/output_xml.properties");
+        nativeResources.produce(new NativeImageResourceBuildItem(paths));
 
-        return items;
+        nativeResourceBundles.produce(new NativeImageResourceBundleBuildItem("org.apache.xml.serializer.HTMLEntities"));
+        nativeResourceBundles.produce(new NativeImageResourceBundleBuildItem("org.apache.xml.serializer.XMLEntities"));
     }
+
 }
diff --git a/extensions/xslt/deployment/src/main/java/org/apache/camel/quarkus/component/xslt/deployment/XsltProcessor.java b/extensions/xslt/deployment/src/main/java/org/apache/camel/quarkus/component/xslt/deployment/XsltProcessor.java
index c5f2165..e0cf8cd 100644
--- a/extensions/xslt/deployment/src/main/java/org/apache/camel/quarkus/component/xslt/deployment/XsltProcessor.java
+++ b/extensions/xslt/deployment/src/main/java/org/apache/camel/quarkus/component/xslt/deployment/XsltProcessor.java
@@ -108,7 +108,7 @@ class XsltProcessor {
                     tf.setErrorListener(new CamelXsltErrorListener());
                     tf.newTemplates(resolvedUri.source);
                 } catch (TransformerException e) {
-                    throw new RuntimeException(e);
+                    throw new RuntimeException("Could not compile XSLT " + uri, e);
                 }
             }
 
@@ -135,4 +135,5 @@ class XsltProcessor {
                     .forEach(File::delete);
         }
     }
+
 }
diff --git a/integration-tests/xml/pom.xml b/integration-tests/xml/pom.xml
index 6636c29..0fb48f7 100644
--- a/integration-tests/xml/pom.xml
+++ b/integration-tests/xml/pom.xml
@@ -56,10 +56,6 @@
             <groupId>io.quarkus</groupId>
             <artifactId>quarkus-resteasy</artifactId>
         </dependency>
-        <dependency>
-            <groupId>xerces</groupId>
-            <artifactId>xercesImpl</artifactId>
-        </dependency>
 
         <!-- test dependencies -->
         <dependency>
diff --git a/integration-tests/xml/src/main/java/org/apache/camel/quarkus/component/xml/it/XmlResource.java b/integration-tests/xml/src/main/java/org/apache/camel/quarkus/component/xml/it/XmlResource.java
index 72511ae..586cd07 100644
--- a/integration-tests/xml/src/main/java/org/apache/camel/quarkus/component/xml/it/XmlResource.java
+++ b/integration-tests/xml/src/main/java/org/apache/camel/quarkus/component/xml/it/XmlResource.java
@@ -54,4 +54,29 @@ public class XmlResource {
     public String classpath(String body) throws Exception {
         return producerTemplate.requestBody("xslt:xslt/classpath-transform.xsl", body, String.class);
     }
+
+    @Path("/html-transform")
+    @POST
+    @Consumes(MediaType.TEXT_HTML)
+    @Produces(MediaType.TEXT_PLAIN)
+    public String htmlTransform(String html) {
+        LOG.debugf("Parsing HTML %s", html);
+        return producerTemplate.requestBody(
+                XmlRouteBuilder.DIRECT_HTML_TRANSFORM,
+                html,
+                String.class);
+    }
+
+    @Path("/html-to-text")
+    @POST
+    @Consumes(MediaType.TEXT_HTML)
+    @Produces(MediaType.TEXT_PLAIN)
+    public String htmlToText(String html) {
+        LOG.debugf("Parsing HTML %s", html);
+        return producerTemplate.requestBody(
+                XmlRouteBuilder.DIRECT_HTML_TO_TEXT,
+                html,
+                String.class);
+    }
+
 }
diff --git a/integration-tests/xml/src/main/java/org/apache/camel/quarkus/component/xml/it/XmlRouteBuilder.java b/integration-tests/xml/src/main/java/org/apache/camel/quarkus/component/xml/it/XmlRouteBuilder.java
index 1e8dccf..4ce71d4 100644
--- a/integration-tests/xml/src/main/java/org/apache/camel/quarkus/component/xml/it/XmlRouteBuilder.java
+++ b/integration-tests/xml/src/main/java/org/apache/camel/quarkus/component/xml/it/XmlRouteBuilder.java
@@ -23,6 +23,8 @@ import org.apache.camel.builder.RouteBuilder;
 
 public class XmlRouteBuilder extends RouteBuilder {
     public static final String DIRECT_HTML_TO_DOM = "direct:html-to-dom";
+    public static final String DIRECT_HTML_TRANSFORM = "direct:html-transfrom";
+    public static final String DIRECT_HTML_TO_TEXT = "direct:html-to-text";
 
     @Override
     public void configure() {
@@ -50,5 +52,15 @@ public class XmlRouteBuilder extends RouteBuilder {
                     }
                     exchange.getIn().setBody(text.getTextContent());
                 });
+
+        from(DIRECT_HTML_TRANSFORM)
+                .unmarshal().tidyMarkup()
+                // tagSoup produces DOM that is then consumed by XSLT
+                .to("xslt:xslt/html-transform.xsl");
+
+        from(DIRECT_HTML_TO_TEXT)
+                .unmarshal().tidyMarkup()
+                // tagSoup produces DOM that is then consumed by XSLT
+                .to("xslt:xslt/html-to-text.xsl");
     }
 }
diff --git a/integration-tests/xml/src/main/resources/application.properties b/integration-tests/xml/src/main/resources/application.properties
index 58fc158..68f4e99 100644
--- a/integration-tests/xml/src/main/resources/application.properties
+++ b/integration-tests/xml/src/main/resources/application.properties
@@ -23,4 +23,4 @@ quarkus.log.category."org.apache.camel.quarkus.core.deployment".level = INFO
 #
 # Quarkus - Camel
 #
-quarkus.camel.xslt.sources = xslt/classpath-transform.xsl
+quarkus.camel.xslt.sources = xslt/classpath-transform.xsl,xslt/html-transform.xsl,xslt/html-to-text.xsl
diff --git a/integration-tests/xml/src/main/resources/xslt/html-to-text.xsl b/integration-tests/xml/src/main/resources/xslt/html-to-text.xsl
new file mode 100644
index 0000000..0a5a196
--- /dev/null
+++ b/integration-tests/xml/src/main/resources/xslt/html-to-text.xsl
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!--
+
+    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.
+
+-->
+<xsl:stylesheet
+        xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
+        version='1.0'>
+
+    <xsl:output method="text"/>
+
+    <xsl:template match="/">
+= <xsl:value-of select="/html/head/title"/><xsl:text>&#xa;</xsl:text>
+<xsl:text>&#xa;</xsl:text>
+<xsl:value-of select="/html/body/p"/>
+    </xsl:template>
+
+</xsl:stylesheet>
diff --git a/integration-tests/xml/src/main/resources/xslt/html-transform.xsl b/integration-tests/xml/src/main/resources/xslt/html-transform.xsl
new file mode 100644
index 0000000..fed0d6c
--- /dev/null
+++ b/integration-tests/xml/src/main/resources/xslt/html-transform.xsl
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!--
+
+    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.
+
+-->
+<xsl:stylesheet
+        xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
+        version='1.0'>
+
+    <xsl:output method="html" indent="yes" encoding="ISO-8859-1"/>
+
+    <xsl:template match="/">
+        <html>
+        <head>
+            <title><xsl:value-of select="/html/head/title"/></title>
+        </head>
+        <body>
+            <h1><xsl:value-of select="/html/head/title"/></h1>
+            <p>Paragraph Contents</p>
+        </body>
+        </html>
+    </xsl:template>
+
+</xsl:stylesheet>
diff --git a/integration-tests/xml/src/test/java/org/apache/camel/quarkus/component/xml/it/XmlTest.java b/integration-tests/xml/src/test/java/org/apache/camel/quarkus/component/xml/it/XmlTest.java
index 02da424..08861fa 100644
--- a/integration-tests/xml/src/test/java/org/apache/camel/quarkus/component/xml/it/XmlTest.java
+++ b/integration-tests/xml/src/test/java/org/apache/camel/quarkus/component/xml/it/XmlTest.java
@@ -60,4 +60,42 @@ class XmlTest {
                 actual);
     }
 
+    @Test
+    public void htmlTransform() throws Exception {
+        String html = IOUtils.toString(getClass().getResourceAsStream("/test.html"), Charset.forName("UTF-8"));
+
+        final String actual = RestAssured.given()
+                .contentType(ContentType.HTML)
+                .accept(ContentType.TEXT)
+                .body(html)
+                .post("/xml/html-transform")
+                .then()
+                .statusCode(200)
+                .extract().body().asString().trim().replaceAll(">\\s+<", "><");
+
+        Assertions.assertEquals(
+                "<html><head><META http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"><title>Title</title></head><body><h1>Title</h1><p>Paragraph Contents</p></body></html>",
+                actual);
+    }
+
+    @Test
+    public void htmlToText() throws Exception {
+        String html = IOUtils.toString(getClass().getResourceAsStream("/test.html"), Charset.forName("UTF-8"));
+
+        final String actual = RestAssured.given()
+                .contentType(ContentType.HTML)
+                .accept(ContentType.TEXT)
+                .body(html)
+                .post("/xml/html-to-text")
+                .then()
+                .statusCode(200)
+                .extract().body().asString().trim();
+
+        Assertions.assertEquals(
+                "= Title\n"
+                        + "\n"
+                        + "Paragraph Contents",
+                actual);
+    }
+
 }