2 * Copyright 2005 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.wamblee.xml;
19 import java.io.ByteArrayOutputStream;
21 import java.io.IOException;
23 import javax.xml.parsers.DocumentBuilder;
24 import javax.xml.parsers.DocumentBuilderFactory;
25 import javax.xml.transform.Result;
26 import javax.xml.transform.Source;
27 import javax.xml.transform.TransformerException;
28 import javax.xml.transform.stream.StreamResult;
29 import javax.xml.transform.stream.StreamSource;
31 import junit.framework.TestCase;
33 import org.springframework.core.io.ClassPathResource;
34 import org.w3c.dom.Document;
35 import org.wamblee.io.FileSystemUtils;
36 import org.wamblee.io.InputResource;
37 import org.wamblee.io.TestResource;
40 * Tests the XSL transformer.
42 public class XslTransformerTest extends TestCase {
44 private static final String REPORT_XML = "report.xml";
46 private static final String REPORT_TO_HTML_XSLT = "reportToHtml.xsl";
48 private static final String REPORT_TO_HTML2_XSLT = "reportToHtml2.xsl";
50 private static final String REPORT_TO_HTML_INVALID_XSLT = "reportToHtml-invalid.xsl";
52 private static final String REPORT_TO_HTML_NONWELLFORMED_XSLT = "reportToHtml-nonwellformed.xsl";
54 private static final String REPORT_TO_TEXT_XSLT = "reportToText.xsl";
58 * Transforms a file while using the default resolver, where the included
59 * file can be found. Verifies the transformation is done correctly.
62 public void testTransformUsingDefaultResolver() throws Exception {
63 XslTransformer transformer = new XslTransformer();
65 InputResource xmlResource = new TestResource(XslTransformerTest.class,
67 Source xslt = new StreamSource(
68 new File(FileSystemUtils
69 .getTestInputDir(XslTransformerTest.class),
70 REPORT_TO_HTML_XSLT));
72 byte[] documentData = FileSystemUtils
73 .read(xmlResource.getInputStream()).getBytes();
74 DocumentBuilder builder = DocumentBuilderFactory.newInstance()
75 .newDocumentBuilder();
76 Document document = builder.parse(xmlResource.getInputStream());
77 Source documentSource = new StreamSource(xmlResource.getInputStream());
79 Document expected = DomUtils.read(new TestResource(
80 XslTransformerTest.class, "output-reportToHtml-report.xml")
83 Document output1 = transformer.transform(documentData, xslt);
84 XmlUtils.assertEquals("byte[] transform", expected, output1);
86 Document output2 = transformer.transform(document, xslt);
87 XmlUtils.assertEquals("document transform", expected, output2);
89 ByteArrayOutputStream os = new ByteArrayOutputStream();
90 Result output = new StreamResult(os);
91 transformer.transform(documentSource, output, xslt);
92 XmlUtils.assertEquals("document source transform", expected, DomUtils
93 .read(os.toString()));
95 String result = transformer.textTransform(documentData, xslt);
97 .assertEquals("text transform", expected, DomUtils.read(result));
101 * Transforms a file using the default resolver where the included file
102 * cannot be found. Verifies that a TransformerException is thrown.
105 public void testTransformUsingDefaultResolverFails() throws IOException {
106 XslTransformer transformer = new XslTransformer();
108 InputResource xmlResource = new TestResource(XslTransformerTest.class,
110 Source xslt = new StreamSource(new File(FileSystemUtils
111 .getTestInputDir(XslTransformerTest.class),
112 REPORT_TO_HTML2_XSLT));
114 byte[] documentData = FileSystemUtils
115 .read(xmlResource.getInputStream()).getBytes();
117 Document output1 = transformer.transform(documentData, xslt);
118 } catch (TransformerException e) {
125 * Transforms a file using an invalid Xslt. Verifies that a
126 * TransformerException is thrown.
129 public void testTransformInvalidXslt() throws IOException {
130 XslTransformer transformer = new XslTransformer();
132 InputResource xmlResource = new TestResource(XslTransformerTest.class,
134 Source xslt = new StreamSource(new File(FileSystemUtils
135 .getTestInputDir(XslTransformerTest.class),
136 REPORT_TO_HTML_INVALID_XSLT));
138 byte[] documentData = FileSystemUtils
139 .read(xmlResource.getInputStream()).getBytes();
141 Document output1 = transformer.transform(documentData, xslt);
142 } catch (TransformerException e) {
149 * Transforms a file using a non-well formed xslt. Verifies that a
150 * TransformerException is thrown.
153 public void testTransformNonWellformedXslt() throws IOException {
154 XslTransformer transformer = new XslTransformer();
156 InputResource xmlResource = new TestResource(XslTransformerTest.class,
158 Source xslt = new StreamSource(new File(FileSystemUtils
159 .getTestInputDir(XslTransformerTest.class),
160 REPORT_TO_HTML_NONWELLFORMED_XSLT));
162 byte[] documentData = FileSystemUtils
163 .read(xmlResource.getInputStream()).getBytes();
165 Document output1 = transformer.transform(documentData, xslt);
166 } catch (TransformerException e) {
173 * Transforms a file using a class path resolver.
176 public void testTransformUsingClassPathResolver() throws Exception {
177 XslTransformer transformer = new XslTransformer(new ClasspathUriResolver());
179 InputResource xmlResource = new TestResource(XslTransformerTest.class,
181 Source xslt = new StreamSource(new File(FileSystemUtils
182 .getTestInputDir(XslTransformerTest.class),
183 REPORT_TO_HTML2_XSLT));
185 byte[] documentData = FileSystemUtils
186 .read(xmlResource.getInputStream()).getBytes();
188 Document output1 = transformer.transform(documentData, xslt);
189 Document expected = DomUtils.read(new TestResource(
190 XslTransformerTest.class, "output-reportToHtml-report.xml")
192 XmlUtils.assertEquals("doc", expected, output1);
196 * Transforms a file to text output. Verifies the file is transformed
200 public void testTransformToTextOutput() throws Exception {
201 XslTransformer transformer = new XslTransformer(new ClasspathUriResolver());
203 InputResource xmlResource = new TestResource(XslTransformerTest.class,
205 Source xslt = new StreamSource(
206 new File(FileSystemUtils
207 .getTestInputDir(XslTransformerTest.class),
208 REPORT_TO_TEXT_XSLT));
210 byte[] documentData = FileSystemUtils
211 .read(xmlResource.getInputStream()).getBytes();
213 String result = transformer.textTransform(documentData, xslt);
214 String expected = "Hello world!";
215 assertEquals("text transform", expected, result);
219 * Tests resolving a file using {@link XslTransformer#resolve(String)}.
222 public void testResolveWithDefaultResolver() throws Exception {
223 XslTransformer transformer = new XslTransformer();
224 File utilities = new File(FileSystemUtils.getTestInputDir(XslTransformerTest.class), "utilities.xsl");
225 Source source = transformer.resolve(utilities.getAbsolutePath());
226 assert(source instanceof StreamSource);
227 StreamSource ssource = (StreamSource)source;
228 String data = FileSystemUtils.read(ssource.getInputStream());
229 String expected = FileSystemUtils.read(new ClassPathResource("org/wamblee/xml/utilities.xsl").getInputStream());
230 assertEquals(expected, data);
234 * Tests resolving a file using {@link XslTransformer#resolve(String)} with the
235 * default resolver where the file does not exist.
238 public void testResolveWithDefaultResolverFileNotFound() {
239 XslTransformer transformer = new XslTransformer();
241 Source source = transformer.resolve("org/wamblee/xml/utilities-nonexistent.xsl");
242 } catch (TransformerException e) {
250 * Tests resolving a file using {@link XslTransformer#resolve(String)} with the
254 public void testResolveWithClasspathResolver() throws Exception {
255 XslTransformer transformer = new XslTransformer(new ClasspathUriResolver());
256 Source source = transformer.resolve("org/wamblee/xml/utilities.xsl");
257 assert(source instanceof StreamSource);
258 StreamSource ssource = (StreamSource)source;
259 String data = FileSystemUtils.read(ssource.getInputStream());
260 String expected = FileSystemUtils.read(new ClassPathResource("org/wamblee/xml/utilities.xsl").getInputStream());
261 assertEquals(expected, data);