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 2015/09/07 16:22:41 UTC

[2/2] camel git commit: CAMEL-9119: xslt should throw exception if error/fatal parsing xslt source. Thanks to metatech for the patch.

CAMEL-9119: xslt should throw exception if error/fatal parsing xslt source. Thanks to metatech for the patch.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/ce747783
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/ce747783
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/ce747783

Branch: refs/heads/camel-2.15.x
Commit: ce747783684f66d21ebe52f99e631f24777f3028
Parents: bffedde
Author: Claus Ibsen <da...@apache.org>
Authored: Mon Sep 7 16:20:46 2015 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Mon Sep 7 16:23:32 2015 +0200

----------------------------------------------------------------------
 .../camel/builder/xml/XsltErrorListener.java    | 11 +++--
 .../xslt/XsltRouteXsltWithErrorTest.java        | 52 ++++++++++++++++++++
 .../component/xslt/transform-with-error.xsl     | 28 +++++++++++
 3 files changed, 87 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/ce747783/camel-core/src/main/java/org/apache/camel/builder/xml/XsltErrorListener.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/builder/xml/XsltErrorListener.java b/camel-core/src/main/java/org/apache/camel/builder/xml/XsltErrorListener.java
index fadff65..30fb968 100644
--- a/camel-core/src/main/java/org/apache/camel/builder/xml/XsltErrorListener.java
+++ b/camel-core/src/main/java/org/apache/camel/builder/xml/XsltErrorListener.java
@@ -23,7 +23,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * {@link ErrorListener} which logs the errors.
+ * {@link ErrorListener} which logs the errors and rethrow the exceptions for error and fatal conditions.
  */
 public class XsltErrorListener implements ErrorListener {
 
@@ -31,16 +31,19 @@ public class XsltErrorListener implements ErrorListener {
 
     @Override
     public void warning(TransformerException e) throws TransformerException {
-        LOG.warn(e.getMessageAndLocation());
+        // just log warning
+        LOG.warn("Warning parsing XSLT file: " + e.getMessageAndLocation());
     }
 
     @Override
     public void error(TransformerException e) throws TransformerException {
-        LOG.error(e.getMessageAndLocation(), e);
+        LOG.error("Error parsing XSLT file: " + e.getMessageAndLocation());
+        throw e;
     }
 
     @Override
     public void fatalError(TransformerException e) throws TransformerException {
-        LOG.error(e.getMessageAndLocation(), e);
+        LOG.error("Fatal error parsing XSLT file: " + e.getMessageAndLocation());
+        throw e;
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/ce747783/camel-core/src/test/java/org/apache/camel/component/xslt/XsltRouteXsltWithErrorTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/xslt/XsltRouteXsltWithErrorTest.java b/camel-core/src/test/java/org/apache/camel/component/xslt/XsltRouteXsltWithErrorTest.java
new file mode 100644
index 0000000..a3ab5d3
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/component/xslt/XsltRouteXsltWithErrorTest.java
@@ -0,0 +1,52 @@
+/**
+ * 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.xslt;
+
+import javax.xml.transform.TransformerConfigurationException;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.FailedToCreateRouteException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.util.ObjectHelper;
+
+public class XsltRouteXsltWithErrorTest extends ContextTestSupport {
+
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+
+    public void testXsltWithError() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                        .to("xslt:org/apache/camel/component/xslt/transform-with-error.xsl");
+            }
+        });
+        try {
+            context.start();
+            fail("Should have thrown exception");
+        } catch (FailedToCreateRouteException e) {
+            TransformerConfigurationException cause = ObjectHelper.getException(TransformerConfigurationException.class, e);
+            assertNotNull(cause);
+            // not sure if XSLT errors may be i18n and not english always so just check for the spelling mistake of select -> slect
+            assertTrue(cause.getMessage().contains("slect"));
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/ce747783/camel-core/src/test/resources/org/apache/camel/component/xslt/transform-with-error.xsl
----------------------------------------------------------------------
diff --git a/camel-core/src/test/resources/org/apache/camel/component/xslt/transform-with-error.xsl b/camel-core/src/test/resources/org/apache/camel/component/xslt/transform-with-error.xsl
new file mode 100644
index 0000000..8cb5ced
--- /dev/null
+++ b/camel-core/src/test/resources/org/apache/camel/component/xslt/transform-with-error.xsl
@@ -0,0 +1,28 @@
+<?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:template match="/">
+    <goodbye>
+      <xsl:value-of slect="/hello"/>
+    </goodbye>
+  </xsl:template>
+
+</xsl:stylesheet>