From a825c512f2f9fadcb2276eb09044bb927663fca5 Mon Sep 17 00:00:00 2001 From: erik Date: Thu, 18 Jan 2007 16:28:07 +0000 Subject: [PATCH] --- .../org/wamblee/xml/XslTransformerTest.java | 107 ++++++++---------- 1 file changed, 48 insertions(+), 59 deletions(-) diff --git a/support/src/test/java/org/wamblee/xml/XslTransformerTest.java b/support/src/test/java/org/wamblee/xml/XslTransformerTest.java index 5d971290..b9effeec 100644 --- a/support/src/test/java/org/wamblee/xml/XslTransformerTest.java +++ b/support/src/test/java/org/wamblee/xml/XslTransformerTest.java @@ -29,19 +29,20 @@ 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.ClassPathResource; import org.wamblee.io.FileSystemUtils; import org.wamblee.io.InputResource; -import org.wamblee.io.TestResource; + +import org.w3c.dom.Document; /** * Tests the XSL transformer. */ public class XslTransformerTest extends TestCase { - private static final String REPORT_XML = "report.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"; @@ -53,6 +54,9 @@ public class XslTransformerTest extends TestCase { 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 @@ -62,12 +66,10 @@ public class XslTransformerTest extends TestCase { 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)); + 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(); @@ -76,22 +78,28 @@ public class XslTransformerTest extends TestCase { Document document = builder.parse(xmlResource.getInputStream()); Source documentSource = new StreamSource(xmlResource.getInputStream()); - Document expected = DomUtils.read(new TestResource( - XslTransformerTest.class, "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()); 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()); transformer.transform(documentSource, output, xslt); XmlUtils.assertEquals("document source transform", expected, DomUtils .read(os.toString())); + 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)); @@ -105,11 +113,11 @@ public class XslTransformerTest extends TestCase { 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)); + 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(); @@ -129,11 +137,10 @@ public class XslTransformerTest extends TestCase { 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)); + 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(); @@ -153,11 +160,11 @@ public class XslTransformerTest extends TestCase { 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)); + 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(); @@ -176,18 +183,17 @@ public class XslTransformerTest extends TestCase { 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)); + 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(); Document output1 = transformer.transform(documentData, xslt); - Document expected = DomUtils.read(new TestResource( - XslTransformerTest.class, "output-reportToHtml-report.xml") + Document expected = DomUtils.read(new ClassPathResource( + getResourcePath("output-reportToHtml-report.xml")) .getInputStream()); XmlUtils.assertEquals("doc", expected, output1); } @@ -200,12 +206,10 @@ public class XslTransformerTest extends TestCase { public void testTransformToTextOutput() throws Exception { XslTransformer transformer = new XslTransformer(new ClasspathUriResolver()); - InputResource xmlResource = new TestResource(XslTransformerTest.class, - REPORT_XML); + InputResource xmlResource = new ClassPathResource( + getResourcePath(REPORT_XML)); Source xslt = new StreamSource( - new File(FileSystemUtils - .getTestInputDir(XslTransformerTest.class), - REPORT_TO_TEXT_XSLT)); + new ClassPathResource(getResourcePath(REPORT_TO_TEXT_XSLT)).getInputStream()); byte[] documentData = FileSystemUtils .read(xmlResource.getInputStream()).getBytes(); @@ -214,21 +218,6 @@ public class XslTransformerTest extends TestCase { String expected = "Hello world!"; assertEquals("text transform", expected, result); } - - /** - * Tests resolving a file using {@link XslTransformer#resolve(String)}. - * - */ - 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); - } /** * Tests resolving a file using {@link XslTransformer#resolve(String)} with the @@ -253,14 +242,14 @@ public class XslTransformerTest extends TestCase { */ public void testResolveWithClasspathResolver() throws Exception { XslTransformer transformer = new XslTransformer(new ClasspathUriResolver()); - Source source = transformer.resolve("org/wamblee/xml/utilities.xsl"); + 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("org/wamblee/xml/utilities.xsl").getInputStream()); + String expected = FileSystemUtils.read(new ClassPathResource(getResourcePath(INCLUDED_XSL_FILE)).getInputStream()); assertEquals(expected, data); } } - \ No newline at end of file + -- 2.31.1