X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Fgeneral%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fxml%2FXslTransformerTest.java;h=8b06f01d683c8bfb47aca058dceb5f2555af1a2f;hb=8de36ff0206c996baf3ee4adc3e2293b12ff5f39;hp=c43214cd38470cb540affb770f9758faa7b98fab;hpb=89c06d4d52b46c154128c97d6e758fa1f4fc7a6e;p=utils diff --git a/support/general/src/test/java/org/wamblee/xml/XslTransformerTest.java b/support/general/src/test/java/org/wamblee/xml/XslTransformerTest.java index c43214cd..8b06f01d 100644 --- a/support/general/src/test/java/org/wamblee/xml/XslTransformerTest.java +++ b/support/general/src/test/java/org/wamblee/xml/XslTransformerTest.java @@ -35,10 +35,9 @@ import javax.xml.transform.TransformerException; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; - /** * Tests the XSL transformer. - * + * * @author Erik Brakkee */ public class XslTransformerTest extends TestCase { @@ -52,38 +51,40 @@ public class XslTransformerTest extends TestCase { private String getResourcePath(String aResource) { return getClass().getPackage().getName().replaceAll("\\.", "/") + "/" + - aResource; + aResource; } /** * 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 ClassPathResource(getResourcePath( - REPORT_XML)); + InputResource xmlResource = new ClassPathResource( + getResourcePath(REPORT_XML)); - Source xslt = new StreamSource(new ClassPathResource(getResourcePath( - REPORT_TO_HTML_XSLT)).getInputStream()); + Source xslt = new StreamSource(new ClassPathResource( + getResourcePath(REPORT_TO_HTML_XSLT)).getInputStream()); - byte[] documentData = FileSystemUtils.read(xmlResource.getInputStream()) - .getBytes(); + byte[] documentData = FileSystemUtils + .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); @@ -91,35 +92,35 @@ public class XslTransformerTest extends TestCase { 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())); + XmlUtils.assertEquals("document source transform", expected, DomUtils + .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)); + 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. - * + * */ - public void testTransformUsingDefaultResolverFails() - throws IOException { + 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(); + byte[] documentData = FileSystemUtils + .read(xmlResource.getInputStream()).getBytes(); try { Document output1 = transformer.transform(documentData, xslt); @@ -133,18 +134,18 @@ public class XslTransformerTest extends TestCase { /** * Transforms a file using an invalid Xslt. Verifies that a * TransformerException is thrown. - * + * */ public void testTransformInvalidXslt() throws IOException { XslTransformer transformer = new XslTransformer(); - InputResource xmlResource = new ClassPathResource(getResourcePath( - REPORT_XML)); - Source xslt = new StreamSource(new ClassPathResource(getResourcePath( - REPORT_TO_HTML_INVALID_XSLT)).getInputStream()); + InputResource xmlResource = new ClassPathResource( + getResourcePath(REPORT_XML)); + Source xslt = new StreamSource(new ClassPathResource( + getResourcePath(REPORT_TO_HTML_INVALID_XSLT)).getInputStream()); - byte[] documentData = FileSystemUtils.read(xmlResource.getInputStream()) - .getBytes(); + byte[] documentData = FileSystemUtils + .read(xmlResource.getInputStream()).getBytes(); try { Document output1 = transformer.transform(documentData, xslt); @@ -158,18 +159,19 @@ public class XslTransformerTest extends TestCase { /** * Transforms a file using a non-well formed xslt. Verifies that a * TransformerException is thrown. - * + * */ public void testTransformNonWellformedXslt() throws IOException { XslTransformer transformer = new XslTransformer(); - InputResource xmlResource = new ClassPathResource(getResourcePath( - REPORT_XML)); - Source xslt = new StreamSource(new ClassPathResource(getResourcePath( - REPORT_TO_HTML_NONWELLFORMED_XSLT)).getInputStream()); + InputResource xmlResource = new ClassPathResource( + getResourcePath(REPORT_XML)); + Source xslt = new StreamSource(new ClassPathResource( + getResourcePath(REPORT_TO_HTML_NONWELLFORMED_XSLT)) + .getInputStream()); - byte[] documentData = FileSystemUtils.read(xmlResource.getInputStream()) - .getBytes(); + byte[] documentData = FileSystemUtils + .read(xmlResource.getInputStream()).getBytes(); try { Document output1 = transformer.transform(documentData, xslt); @@ -182,40 +184,44 @@ public class XslTransformerTest extends TestCase { /** * Transforms a file using a class path resolver. - * + * */ public void testTransformUsingClassPathResolver() 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_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(); + byte[] documentData = FileSystemUtils + .read(xmlResource.getInputStream()).getBytes(); Document output1 = transformer.transform(documentData, xslt); - Document expected = DomUtils.read(new ClassPathResource(getResourcePath( - "output-reportToHtml-report.xml")).getInputStream()); + Document expected = DomUtils + .read(new ClassPathResource( + getResourcePath("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()); + 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()); + InputResource xmlResource = new ClassPathResource( + getResourcePath(REPORT_XML)); + Source xslt = new StreamSource(new ClassPathResource( + getResourcePath(REPORT_TO_TEXT_XSLT)).getInputStream()); - byte[] documentData = FileSystemUtils.read(xmlResource.getInputStream()) - .getBytes(); + byte[] documentData = FileSystemUtils + .read(xmlResource.getInputStream()).getBytes(); String result = transformer.textTransform(documentData, xslt); String expected = "Hello world!"; @@ -223,16 +229,16 @@ public class XslTransformerTest extends TestCase { } /** - * 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() { XslTransformer transformer = new XslTransformer(); try { - Source source = transformer.resolve( - "org/wamblee/xml/utilities-nonexistent.xsl"); + Source source = transformer + .resolve("org/wamblee/xml/utilities-nonexistent.xsl"); } catch (TransformerException e) { return; // ok } @@ -241,19 +247,20 @@ public class XslTransformerTest extends TestCase { } /** - * 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()); + XslTransformer transformer = new XslTransformer( + new ClasspathUriResolver()); Source source = transformer.resolve(getResourcePath(INCLUDED_XSL_FILE)); 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()); + getResourcePath(INCLUDED_XSL_FILE)).getInputStream()); assertEquals(expected, data); } }