(no commit message)
[utils] / support / test / org / wamblee / xml / XslTransformerTest.java
index 8352cb0d0d7a2a8a0f98685fd269a8b043d49468..5d9712909fc4f11082c2153add0afcc3f770cd54 100644 (file)
  * 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.wamblee.xml;
 
 import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.IOException;
 
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.transform.Result;
 import javax.xml.transform.Source;
+import javax.xml.transform.TransformerException;
 import javax.xml.transform.stream.StreamResult;
 import javax.xml.transform.stream.StreamSource;
 
 import junit.framework.TestCase;
 
+import org.springframework.core.io.ClassPathResource;
 import org.w3c.dom.Document;
 import org.wamblee.io.FileSystemUtils;
 import org.wamblee.io.InputResource;
@@ -37,77 +40,227 @@ import org.wamblee.io.TestResource;
  * Tests the XSL transformer.
  */
 public class XslTransformerTest extends TestCase {
-    
+
     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";
+
+
     /**
-     * Transforms a file while using the default resolver, 
-     * where the included file can be found. 
-     * Verifies the transformation is done correctly.
-     *
+     * Transforms a file while using the default resolver, where the included
+     * file can be found. Verifies the transformation is done correctly.
+     * 
      */
     public void testTransformUsingDefaultResolver() throws Exception {
         XslTransformer transformer = new XslTransformer();
-        
-        InputResource xmlResource = new TestResource(XslTransformerTest.class, REPORT_XML);
-        Source xslt = new StreamSource(new File( FileSystemUtils.getTestInputDir(XslTransformerTest.class), REPORT_TO_HTML_XSLT));
-        
-        byte[] documentData = FileSystemUtils.read(xmlResource.getInputStream()).getBytes();
-        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
+
+        InputResource xmlResource = new TestResource(XslTransformerTest.class,
+                REPORT_XML);
+        Source xslt = new StreamSource(
+                new File(FileSystemUtils
+                        .getTestInputDir(XslTransformerTest.class),
+                        REPORT_TO_HTML_XSLT));
+
+        byte[] documentData = FileSystemUtils
+                .read(xmlResource.getInputStream()).getBytes();
+        DocumentBuilder builder = DocumentBuilderFactory.newInstance()
+                .newDocumentBuilder();
         Document document = builder.parse(xmlResource.getInputStream());
         Source documentSource = new StreamSource(xmlResource.getInputStream());
-        
-        Document output1 = transformer.transform(documentData, xslt); 
+
+        Document expected = DomUtils.read(new TestResource(
+                XslTransformerTest.class, "output-reportToHtml-report.xml")
+                .getInputStream());
+
+        Document output1 = transformer.transform(documentData, xslt);
+        XmlUtils.assertEquals("byte[] transform", expected, output1);
+
         Document output2 = transformer.transform(document, xslt);
-        ByteArrayOutputStream os = new ByteArrayOutputStream(); 
+        XmlUtils.assertEquals("document transform", expected, output2);
+
+        ByteArrayOutputStream os = new ByteArrayOutputStream();
         Result output = new StreamResult(os);
         transformer.transform(documentSource, output, xslt);
-        transformer.textTransform(documentData, xslt);
+        XmlUtils.assertEquals("document source transform", expected, DomUtils
+                .read(os.toString()));
+
+        String result = transformer.textTransform(documentData, xslt);
+        XmlUtils
+                .assertEquals("text transform", expected, DomUtils.read(result));
     }
-    
+
     /**
-     * Transforms a file using the default resolver where the
-     * included file cannot be found. 
-     * Verifies that a TransformerException is thrown. 
-     *
+     * Transforms a file using the default resolver where the included file
+     * cannot be found. Verifies that a TransformerException is thrown.
+     * 
      */
-    public void testTransformUsingDefaultResolverFails() { 
-        fail(); 
+    public void testTransformUsingDefaultResolverFails() throws IOException {
+        XslTransformer transformer = new XslTransformer();
+
+        InputResource xmlResource = new TestResource(XslTransformerTest.class,
+                REPORT_XML);
+        Source xslt = new StreamSource(new File(FileSystemUtils
+                .getTestInputDir(XslTransformerTest.class),
+                REPORT_TO_HTML2_XSLT));
+
+        byte[] documentData = FileSystemUtils
+                .read(xmlResource.getInputStream()).getBytes();
+        try {
+            Document output1 = transformer.transform(documentData, xslt);
+        } catch (TransformerException e) {
+            return; // ok
+        }
+        fail();
     }
-    
+
     /**
-     * Transforms a file using an invalid Xslt. 
-     * Verifies that a TransformerException is thrown. 
-     *
+     * Transforms a file using an invalid Xslt. Verifies that a
+     * TransformerException is thrown.
+     * 
      */
-    public void testTransformInvalidXslt() { 
-        fail(); 
+    public void testTransformInvalidXslt() throws IOException {
+        XslTransformer transformer = new XslTransformer();
+
+        InputResource xmlResource = new TestResource(XslTransformerTest.class,
+                REPORT_XML);
+        Source xslt = new StreamSource(new File(FileSystemUtils
+                .getTestInputDir(XslTransformerTest.class),
+                REPORT_TO_HTML_INVALID_XSLT));
+
+        byte[] documentData = FileSystemUtils
+                .read(xmlResource.getInputStream()).getBytes();
+        try {
+            Document output1 = transformer.transform(documentData, xslt);
+        } catch (TransformerException e) {
+            return; // ok
+        }
+        fail();
     }
-    
+
     /**
-     * Transforms a file using a non-well formed xslt. 
-     * Verifies that a TransformerException is thrown. 
-     *
+     * Transforms a file using a non-well formed xslt. Verifies that a
+     * TransformerException is thrown.
+     * 
      */
-    public void testTransformNonWellformedXslt() { 
+    public void testTransformNonWellformedXslt() throws IOException {
+        XslTransformer transformer = new XslTransformer();
+
+        InputResource xmlResource = new TestResource(XslTransformerTest.class,
+                REPORT_XML);
+        Source xslt = new StreamSource(new File(FileSystemUtils
+                .getTestInputDir(XslTransformerTest.class),
+                REPORT_TO_HTML_NONWELLFORMED_XSLT));
+
+        byte[] documentData = FileSystemUtils
+                .read(xmlResource.getInputStream()).getBytes();
+        try {
+            Document output1 = transformer.transform(documentData, xslt);
+        } catch (TransformerException e) {
+            return; // ok
+        }
         fail();
     }
+
+    /**
+     * Transforms a file using a class path resolver.
+     * 
+     */
+    public void testTransformUsingClassPathResolver() throws Exception {
+        XslTransformer transformer = new XslTransformer(new ClasspathUriResolver());
+
+        InputResource xmlResource = new TestResource(XslTransformerTest.class,
+                REPORT_XML);
+        Source xslt = new StreamSource(new File(FileSystemUtils
+                .getTestInputDir(XslTransformerTest.class),
+                REPORT_TO_HTML2_XSLT));
+
+        byte[] documentData = FileSystemUtils
+                .read(xmlResource.getInputStream()).getBytes();
+        
+        Document output1 = transformer.transform(documentData, xslt);
+        Document expected = DomUtils.read(new TestResource(
+                XslTransformerTest.class, "output-reportToHtml-report.xml")
+                .getInputStream());
+        XmlUtils.assertEquals("doc", expected, output1);
+    }
+
+    /**
+     * Transforms a file to text output. Verifies the file is transformed
+     * correctly.
+     * 
+     */
+    public void testTransformToTextOutput() throws Exception {
+        XslTransformer transformer = new XslTransformer(new ClasspathUriResolver());
+
+        InputResource xmlResource = new TestResource(XslTransformerTest.class,
+                REPORT_XML);
+        Source xslt = new StreamSource(
+                new File(FileSystemUtils
+                        .getTestInputDir(XslTransformerTest.class),
+                        REPORT_TO_TEXT_XSLT));
+
+        byte[] documentData = FileSystemUtils
+                .read(xmlResource.getInputStream()).getBytes();
    
+        String result = transformer.textTransform(documentData, xslt);
+        String expected = "Hello world!";
+        assertEquals("text transform", expected, result);
+    }
+    
     /**
-     * Transforms a file using a class path resolver. 
+     * Tests resolving a file using {@link XslTransformer#resolve(String)}.
      *
      */
-    public void testTransformUsingClassPathResolver() { 
-        fail();    
+    public void testResolveWithDefaultResolver() throws Exception { 
+        XslTransformer transformer = new XslTransformer();
+        File utilities = new File(FileSystemUtils.getTestInputDir(XslTransformerTest.class), "utilities.xsl");
+        Source source = transformer.resolve(utilities.getAbsolutePath());
+        assert(source instanceof StreamSource);
+        StreamSource ssource = (StreamSource)source;
+        String data = FileSystemUtils.read(ssource.getInputStream());
+        String expected = FileSystemUtils.read(new ClassPathResource("org/wamblee/xml/utilities.xsl").getInputStream());
+        assertEquals(expected, data);
     }
-
     /**
-     * Transforms a file to text output. 
-     * Verifies the file is transformed correctly
+     * Tests resolving a file using {@link XslTransformer#resolve(String)} with the 
+     * default resolver where the file does not exist
      *
      */
-    public void testTransformToTextOutput() { 
+    public void testResolveWithDefaultResolverFileNotFound() { 
+        XslTransformer transformer = new XslTransformer();
+        try { 
+            Source source = 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. 
+     *
+     */
+    public void testResolveWithClasspathResolver() throws Exception { 
+        XslTransformer transformer = new XslTransformer(new ClasspathUriResolver());
+        Source source = transformer.resolve("org/wamblee/xml/utilities.xsl");
+        assert(source instanceof StreamSource);
+        StreamSource ssource = (StreamSource)source;
+        String data = FileSystemUtils.read(ssource.getInputStream());
+        String expected = FileSystemUtils.read(new ClassPathResource("org/wamblee/xml/utilities.xsl").getInputStream());
+        assertEquals(expected, data);
+    }
+    
 }
+
+    
\ No newline at end of file