(no commit message)
[utils] / support / general / src / test / java / org / wamblee / xml / XslTransformerTest.java
index d30e60a6327b10b0911efe694244e9874e718c8e..c3cf78b06775ff1964deb40eced8ba89c2510026 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2005 the original author or authors.
+ * Copyright 2005-2010 the original author or authors.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.wamblee.xml;
 
 import java.io.ByteArrayOutputStream;
-import java.io.File;
 import java.io.IOException;
 
 import javax.xml.parsers.DocumentBuilder;
@@ -29,35 +27,29 @@ import javax.xml.transform.stream.StreamResult;
 import javax.xml.transform.stream.StreamSource;
 
 import junit.framework.TestCase;
+
+import org.w3c.dom.Document;
 import org.wamblee.io.ClassPathResource;
 import org.wamblee.io.FileSystemUtils;
 import org.wamblee.io.InputResource;
 
-import org.w3c.dom.Document;
-
 /**
  * Tests the XSL transformer.
- *
+ * 
  * @author Erik Brakkee
  */
 public class XslTransformerTest extends TestCase {
-
     private static final String INCLUDED_XSL_FILE = "utilities.xsl";
-
-       private static final String REPORT_XML = "report.xml";
-
+    private static final String REPORT_XML = "report.xml";
     private static final String REPORT_TO_HTML_XSLT = "reportToHtml.xsl";
-
     private static final String REPORT_TO_HTML2_XSLT = "reportToHtml2.xsl";
-    
     private static final String REPORT_TO_HTML_INVALID_XSLT = "reportToHtml-invalid.xsl";
-    
     private static final String REPORT_TO_HTML_NONWELLFORMED_XSLT = "reportToHtml-nonwellformed.xsl";
-    
     private static final String REPORT_TO_TEXT_XSLT = "reportToText.xsl";
 
-    private String getResourcePath(String aResource) { 
-       return getClass().getPackage().getName().replaceAll("\\.", "/") + "/" + aResource;
+    private String getResourcePath(String aResource) {
+        return getClass().getPackage().getName().replaceAll("\\.", "/") + "/" +
+            aResource;
     }
 
     /**
@@ -68,43 +60,48 @@ public class XslTransformerTest extends TestCase {
     public void testTransformUsingDefaultResolver() throws Exception {
         XslTransformer transformer = new XslTransformer();
 
-        InputResource xmlResource = new ClassPathResource(getResourcePath(REPORT_XML));
-        
-        Source xslt = new StreamSource(new ClassPathResource(getResourcePath(
-                       REPORT_TO_HTML_XSLT)).getInputStream());
+        InputResource xmlResource = new ClassPathResource(
+            getResourcePath(REPORT_XML));
+
+        Source xslt = new StreamSource(new ClassPathResource(
+            getResourcePath(REPORT_TO_HTML_XSLT)).getInputStream());
 
         byte[] documentData = FileSystemUtils
-                .read(xmlResource.getInputStream()).getBytes();
+            .read(xmlResource.getInputStream()).getBytes();
         DocumentBuilder builder = DocumentBuilderFactory.newInstance()
-                .newDocumentBuilder();
+            .newDocumentBuilder();
         Document document = builder.parse(xmlResource.getInputStream());
         Source documentSource = new StreamSource(xmlResource.getInputStream());
 
-        Document expected = DomUtils.read(new ClassPathResource(getResourcePath(
-                       "output-reportToHtml-report.xml")).getInputStream());
+        Document expected = DomUtils
+            .read(new ClassPathResource(
+                getResourcePath("output-reportToHtml-report.xml"))
+                .getInputStream());
 
         Document output1 = transformer.transform(documentData, xslt);
         XmlUtils.assertEquals("byte[] transform", expected, output1);
 
-        xslt = new StreamSource(new ClassPathResource(getResourcePath(
-                       REPORT_TO_HTML_XSLT)).getInputStream());
+        xslt = new StreamSource(new ClassPathResource(
+            getResourcePath(REPORT_TO_HTML_XSLT)).getInputStream());
+
         Document output2 = transformer.transform(document, xslt);
         XmlUtils.assertEquals("document transform", expected, output2);
 
         ByteArrayOutputStream os = new ByteArrayOutputStream();
         Result output = new StreamResult(os);
-        
-        xslt = new StreamSource(new ClassPathResource(getResourcePath(
-                       REPORT_TO_HTML_XSLT)).getInputStream());
+
+        xslt = new StreamSource(new ClassPathResource(
+            getResourcePath(REPORT_TO_HTML_XSLT)).getInputStream());
         transformer.transform(documentSource, output, xslt);
         XmlUtils.assertEquals("document source transform", expected, DomUtils
-                .read(os.toString()));
+            .read(os.toString()));
+
+        xslt = new StreamSource(new ClassPathResource(
+            getResourcePath(REPORT_TO_HTML_XSLT)).getInputStream());
 
-        xslt = new StreamSource(new ClassPathResource(getResourcePath(
-                       REPORT_TO_HTML_XSLT)).getInputStream());
         String result = transformer.textTransform(documentData, xslt);
         XmlUtils
-                .assertEquals("text transform", expected, DomUtils.read(result));
+            .assertEquals("text transform", expected, DomUtils.read(result));
     }
 
     /**
@@ -115,19 +112,20 @@ public class XslTransformerTest extends TestCase {
     public void testTransformUsingDefaultResolverFails() throws IOException {
         XslTransformer transformer = new XslTransformer();
 
-        InputResource xmlResource = 
-                       new ClassPathResource(getResourcePath(REPORT_XML));
-        Source xslt = new StreamSource(
-                       new ClassPathResource(getResourcePath(
-                                       REPORT_TO_HTML2_XSLT)).getInputStream());
+        InputResource xmlResource = new ClassPathResource(
+            getResourcePath(REPORT_XML));
+        Source xslt = new StreamSource(new ClassPathResource(
+            getResourcePath(REPORT_TO_HTML2_XSLT)).getInputStream());
 
         byte[] documentData = FileSystemUtils
-                .read(xmlResource.getInputStream()).getBytes();
+            .read(xmlResource.getInputStream()).getBytes();
+
         try {
-            Document output1 = transformer.transform(documentData, xslt);
+            transformer.transform(documentData, xslt);
         } catch (TransformerException e) {
             return; // ok
         }
+
         fail();
     }
 
@@ -140,17 +138,19 @@ public class XslTransformerTest extends TestCase {
         XslTransformer transformer = new XslTransformer();
 
         InputResource xmlResource = new ClassPathResource(
-                       getResourcePath(REPORT_XML));
-        Source xslt = new StreamSource(
-                       new ClassPathResource(getResourcePath(REPORT_TO_HTML_INVALID_XSLT)).getInputStream());
+            getResourcePath(REPORT_XML));
+        Source xslt = new StreamSource(new ClassPathResource(
+            getResourcePath(REPORT_TO_HTML_INVALID_XSLT)).getInputStream());
 
         byte[] documentData = FileSystemUtils
-                .read(xmlResource.getInputStream()).getBytes();
+            .read(xmlResource.getInputStream()).getBytes();
+
         try {
-            Document output1 = transformer.transform(documentData, xslt);
+            transformer.transform(documentData, xslt);
         } catch (TransformerException e) {
             return; // ok
         }
+
         fail();
     }
 
@@ -163,18 +163,20 @@ public class XslTransformerTest extends TestCase {
         XslTransformer transformer = new XslTransformer();
 
         InputResource xmlResource = new ClassPathResource(
-                       getResourcePath(REPORT_XML));
-        Source xslt = new StreamSource(
-                       new ClassPathResource(getResourcePath(
-                                       REPORT_TO_HTML_NONWELLFORMED_XSLT)).getInputStream());
+            getResourcePath(REPORT_XML));
+        Source xslt = new StreamSource(new ClassPathResource(
+            getResourcePath(REPORT_TO_HTML_NONWELLFORMED_XSLT))
+            .getInputStream());
 
         byte[] documentData = FileSystemUtils
-                .read(xmlResource.getInputStream()).getBytes();
+            .read(xmlResource.getInputStream()).getBytes();
+
         try {
-            Document output1 = transformer.transform(documentData, xslt);
+            transformer.transform(documentData, xslt);
         } catch (TransformerException e) {
             return; // ok
         }
+
         fail();
     }
 
@@ -183,19 +185,21 @@ public class XslTransformerTest extends TestCase {
      * 
      */
     public void testTransformUsingClassPathResolver() throws Exception {
-        XslTransformer transformer = new XslTransformer(new ClasspathUriResolver());
+        XslTransformer transformer = new XslTransformer(
+            new ClasspathUriResolver());
 
-        InputResource xmlResource = new ClassPathResource(getResourcePath(
-                       REPORT_XML));
+        InputResource xmlResource = new ClassPathResource(
+            getResourcePath(REPORT_XML));
         Source xslt = new StreamSource(new ClassPathResource(
-                       getResourcePath(REPORT_TO_HTML2_XSLT)).getInputStream());
+            getResourcePath(REPORT_TO_HTML2_XSLT)).getInputStream());
 
         byte[] documentData = FileSystemUtils
-                .read(xmlResource.getInputStream()).getBytes();
-        
+            .read(xmlResource.getInputStream()).getBytes();
+
         Document output1 = transformer.transform(documentData, xslt);
-        Document expected = DomUtils.read(new ClassPathResource(
-                       getResourcePath("output-reportToHtml-report.xml"))
+        Document expected = DomUtils
+            .read(new ClassPathResource(
+                getResourcePath("output-reportToHtml-report.xml"))
                 .getInputStream());
         XmlUtils.assertEquals("doc", expected, output1);
     }
@@ -206,52 +210,54 @@ public class XslTransformerTest extends TestCase {
      * 
      */
     public void testTransformToTextOutput() throws Exception {
-        XslTransformer transformer = new XslTransformer(new ClasspathUriResolver());
+        XslTransformer transformer = new XslTransformer(
+            new ClasspathUriResolver());
 
         InputResource xmlResource = new ClassPathResource(
-                       getResourcePath(REPORT_XML));
-        Source xslt = new StreamSource(
-                       new ClassPathResource(getResourcePath(REPORT_TO_TEXT_XSLT)).getInputStream());
+            getResourcePath(REPORT_XML));
+        Source xslt = new StreamSource(new ClassPathResource(
+            getResourcePath(REPORT_TO_TEXT_XSLT)).getInputStream());
 
         byte[] documentData = FileSystemUtils
-                .read(xmlResource.getInputStream()).getBytes();
-   
+            .read(xmlResource.getInputStream()).getBytes();
+
         String result = transformer.textTransform(documentData, xslt);
         String expected = "Hello world!";
         assertEquals("text transform", expected, result);
     }
+
     /**
-     * Tests resolving a file using {@link XslTransformer#resolve(String)} with the 
-     * default resolver where the file does not exist. 
-     *
+     * Tests resolving a file using {@link XslTransformer#resolve(String)} with
+     * the default resolver where the file does not exist.
+     * 
      */
-    public void testResolveWithDefaultResolverFileNotFound() { 
+    public void testResolveWithDefaultResolverFileNotFound() {
         XslTransformer transformer = new XslTransformer();
-        try { 
-            Source source = transformer.resolve("org/wamblee/xml/utilities-nonexistent.xsl");
-        } catch (TransformerException e) { 
+
+        try {
+            transformer.resolve("org/wamblee/xml/utilities-nonexistent.xsl");
+        } catch (TransformerException e) {
             return; // ok
         }
+
         fail();
     }
-    
-    
+
     /**
-     * Tests resolving a file using {@link XslTransformer#resolve(String)} with the 
-     * default resolver. 
-     *
+     * Tests resolving a file using {@link XslTransformer#resolve(String)} with
+     * the default resolver.
+     * 
      */
-    public void testResolveWithClasspathResolver() throws Exception { 
-        XslTransformer transformer = new XslTransformer(new ClasspathUriResolver());
+    public void testResolveWithClasspathResolver() throws Exception {
+        XslTransformer transformer = new XslTransformer(
+            new ClasspathUriResolver());
         Source source = transformer.resolve(getResourcePath(INCLUDED_XSL_FILE));
-        assert(source instanceof StreamSource);
-        StreamSource ssource = (StreamSource)source;
+        assert (source instanceof StreamSource);
+
+        StreamSource ssource = (StreamSource) source;
         String data = FileSystemUtils.read(ssource.getInputStream());
-        String expected = FileSystemUtils.read(new ClassPathResource(getResourcePath(INCLUDED_XSL_FILE)).getInputStream());
+        String expected = FileSystemUtils.read(new ClassPathResource(
+            getResourcePath(INCLUDED_XSL_FILE)).getInputStream());
         assertEquals(expected, data);
     }
-    
 }
-
-