X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Fgeneral%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fxml%2FXSLTransformationTest.java;fp=support%2Fgeneral%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fxml%2FXSLTransformationTest.java;h=3cc494cee416bdf4747418480940149865844f8b;hb=2136b85d7bde33a277d1dfd58b048ee6e5f5db8b;hp=0000000000000000000000000000000000000000;hpb=df850821cf26ca07ef2474d4d2cabb61e9104291;p=utils diff --git a/support/general/src/test/java/org/wamblee/xml/XSLTransformationTest.java b/support/general/src/test/java/org/wamblee/xml/XSLTransformationTest.java new file mode 100644 index 00000000..3cc494ce --- /dev/null +++ b/support/general/src/test/java/org/wamblee/xml/XSLTransformationTest.java @@ -0,0 +1,164 @@ +/* + * Copyright 2005-2011 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. + * 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.wamblee.xml; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +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.junit.Test; +import org.w3c.dom.Document; +import org.wamblee.io.ClassPathResource; +import org.wamblee.io.FileSystemUtils; +import org.wamblee.io.InputResource; + +import static org.wamblee.xml.XMLDocument.*; + +import static junit.framework.TestCase.*; + +/** + * Tests the XSL transformation + * + * @author Erik Brakkee + */ +public class XSLTransformationTest { + private static final String PREFIX = "org/wamblee/xml/"; + private static final String INCLUDED_XSL_FILE = "utilities.xsl"; + 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; + } + + /** + * Transforms a file while using the default resolver, where the included + * file can be found. Verifies the transformation is done correctly. + * + */ + @Test + public void testTransformUsingDefaultResolver() throws Exception { + XMLDocument xmldoc = xmldocument(PREFIX + REPORT_XML, new ClasspathUriResolver()); + XMLDocument transformed = xmldoc.transform(getClass().getResource(REPORT_TO_HTML_XSLT).toURI()); + + Document expected = xmldocument(getClass().getResource("output-reportToHtml-report.xml").toURI()).getDocument(); + + Document output = transformed.getDocument(); + XmlUtils.assertEquals("byte[] transform", expected, output); + } + + @Test + public void testTransformUsingDefaultResolverTextTransform() throws Exception { + XMLDocument xmldoc = xmldocument(PREFIX + REPORT_XML, new ClasspathUriResolver()); + String transformed = xmldoc.write(new XSLTransformation(getClass().getResource(REPORT_TO_HTML_XSLT).toURI())); + + XMLDocument xmldocTransformed = xmldocument("read-transformed-text", new ByteArrayInputStream(transformed.getBytes())); + Document expected = xmldocument("output-reportToHtml-report.xml", + new ClassPathResource( + getResourcePath("output-reportToHtml-report.xml")) + .getInputStream()).getDocument(); + XmlUtils + .assertEquals("text transform", expected, xmldocTransformed.getDocument()); + } + + /** + * Transforms a file using the default resolver where the included file + * cannot be found. Verifies that a TransformerException is thrown. + * + */ + @Test(expected = XMLException.class) + public void testTransformUsingDefaultResolverFails() throws Exception { + XMLDocument xmldoc = xmldocument(PREFIX + REPORT_XML, new ClasspathUriResolver()); + XMLDocument transformed = xmldoc.transform(getClass().getResource(REPORT_TO_HTML2_XSLT).toURI()); + } + + /** + * Transforms a file using an invalid Xslt. Verifies that a + * TransformerException is thrown. + * + */ + @Test(expected = XMLException.class) + public void testTransformInvalidXslt() throws Exception { + XMLDocument xmldoc = xmldocument(PREFIX + REPORT_XML, new ClasspathUriResolver()); + XMLDocument transformed = xmldoc.transform(getClass().getResource(REPORT_TO_HTML_INVALID_XSLT).toURI()); + } + + /** + * Transforms a file using a non-well formed xslt. Verifies that a + * TransformerException is thrown. + * + */ + @Test(expected=XMLException.class) + public void testTransformNonWellformedXslt() throws Exception { + XMLDocument xmldoc = xmldocument(PREFIX + REPORT_XML, new ClasspathUriResolver()); + XMLDocument transformed = xmldoc.transform(getClass().getResource(REPORT_TO_HTML_NONWELLFORMED_XSLT).toURI()); + } + + /** + * Transforms a file using a class path resolver. + * + */ + @Test + public void testTransformUsingClassPathResolver() throws Exception { + XMLDocument xmldoc = xmldocument(PREFIX + REPORT_XML, new ClasspathUriResolver()); + XMLDocument transformed = xmldoc.transform(PREFIX + REPORT_TO_HTML2_XSLT, new ClasspathUriResolver()); + + Document output1 = transformed.getDocument(); + + Document expected = xmldocument(getClass().getResource("output-reportToHtml-report.xml").toURI()).getDocument(); + XmlUtils.assertEquals("doc", expected, output1); + } + + /** + * Transforms a file to text output. Verifies the file is transformed + * correctly. + * + */ + @Test + public void testTransformToTextOutput() throws Exception { + XMLDocument xmldoc = xmldocument(PREFIX + REPORT_XML, new ClasspathUriResolver()); + String transformed = xmldoc.write(new XSLTransformation(PREFIX + REPORT_TO_TEXT_XSLT, new ClasspathUriResolver())); + + String expected = "Hello world!"; + assertEquals("text transform", expected, transformed); + } + + /** + * Tests resolving a file using {@link XslTransformer#resolve(String)} with + * the default resolver where the file does not exist. + * + */ + @Test(expected = XMLException.class) + public void testResolveWithDefaultResolverFileNotFound() throws Exception { + XMLDocument xmldoc = xmldocument(PREFIX + REPORT_XML, new ClasspathUriResolver()); + XMLDocument transformed = xmldoc.transform(PREFIX + "utilities-nonexistent.xsl", new ClasspathUriResolver()); + } +}