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.
16 package org.wamblee.xml;
18 import junit.framework.TestCase;
20 import org.w3c.dom.Document;
22 import org.wamblee.io.ClassPathResource;
23 import org.wamblee.io.FileSystemUtils;
24 import org.wamblee.io.InputResource;
26 import java.io.ByteArrayOutputStream;
28 import java.io.IOException;
30 import javax.xml.parsers.DocumentBuilder;
31 import javax.xml.parsers.DocumentBuilderFactory;
32 import javax.xml.transform.Result;
33 import javax.xml.transform.Source;
34 import javax.xml.transform.TransformerException;
35 import javax.xml.transform.stream.StreamResult;
36 import javax.xml.transform.stream.StreamSource;
39 * Tests the XSL transformer.
41 * @author Erik Brakkee
43 public class XslTransformerTest extends TestCase {
44 private static final String INCLUDED_XSL_FILE = "utilities.xsl";
45 private static final String REPORT_XML = "report.xml";
46 private static final String REPORT_TO_HTML_XSLT = "reportToHtml.xsl";
47 private static final String REPORT_TO_HTML2_XSLT = "reportToHtml2.xsl";
48 private static final String REPORT_TO_HTML_INVALID_XSLT = "reportToHtml-invalid.xsl";
49 private static final String REPORT_TO_HTML_NONWELLFORMED_XSLT = "reportToHtml-nonwellformed.xsl";
50 private static final String REPORT_TO_TEXT_XSLT = "reportToText.xsl";
52 private String getResourcePath(String aResource) {
53 return getClass().getPackage().getName().replaceAll("\\.", "/") + "/" +
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 ClassPathResource(
66 getResourcePath(REPORT_XML));
68 Source xslt = new StreamSource(new ClassPathResource(
69 getResourcePath(REPORT_TO_HTML_XSLT)).getInputStream());
71 byte[] documentData = FileSystemUtils
72 .read(xmlResource.getInputStream()).getBytes();
73 DocumentBuilder builder = DocumentBuilderFactory.newInstance()
74 .newDocumentBuilder();
75 Document document = builder.parse(xmlResource.getInputStream());
76 Source documentSource = new StreamSource(xmlResource.getInputStream());
78 Document expected = DomUtils
79 .read(new ClassPathResource(
80 getResourcePath("output-reportToHtml-report.xml"))
83 Document output1 = transformer.transform(documentData, xslt);
84 XmlUtils.assertEquals("byte[] transform", expected, output1);
86 xslt = new StreamSource(new ClassPathResource(
87 getResourcePath(REPORT_TO_HTML_XSLT)).getInputStream());
89 Document output2 = transformer.transform(document, xslt);
90 XmlUtils.assertEquals("document transform", expected, output2);
92 ByteArrayOutputStream os = new ByteArrayOutputStream();
93 Result output = new StreamResult(os);
95 xslt = new StreamSource(new ClassPathResource(
96 getResourcePath(REPORT_TO_HTML_XSLT)).getInputStream());
97 transformer.transform(documentSource, output, xslt);
98 XmlUtils.assertEquals("document source transform", expected, DomUtils
99 .read(os.toString()));
101 xslt = new StreamSource(new ClassPathResource(
102 getResourcePath(REPORT_TO_HTML_XSLT)).getInputStream());
104 String result = transformer.textTransform(documentData, xslt);
106 .assertEquals("text transform", expected, DomUtils.read(result));
110 * Transforms a file using the default resolver where the included file
111 * cannot be found. Verifies that a TransformerException is thrown.
114 public void testTransformUsingDefaultResolverFails() throws IOException {
115 XslTransformer transformer = new XslTransformer();
117 InputResource xmlResource = new ClassPathResource(
118 getResourcePath(REPORT_XML));
119 Source xslt = new StreamSource(new ClassPathResource(
120 getResourcePath(REPORT_TO_HTML2_XSLT)).getInputStream());
122 byte[] documentData = FileSystemUtils
123 .read(xmlResource.getInputStream()).getBytes();
126 Document output1 = transformer.transform(documentData, xslt);
127 } catch (TransformerException e) {
135 * Transforms a file using an invalid Xslt. Verifies that a
136 * TransformerException is thrown.
139 public void testTransformInvalidXslt() throws IOException {
140 XslTransformer transformer = new XslTransformer();
142 InputResource xmlResource = new ClassPathResource(
143 getResourcePath(REPORT_XML));
144 Source xslt = new StreamSource(new ClassPathResource(
145 getResourcePath(REPORT_TO_HTML_INVALID_XSLT)).getInputStream());
147 byte[] documentData = FileSystemUtils
148 .read(xmlResource.getInputStream()).getBytes();
151 Document output1 = transformer.transform(documentData, xslt);
152 } catch (TransformerException e) {
160 * Transforms a file using a non-well formed xslt. Verifies that a
161 * TransformerException is thrown.
164 public void testTransformNonWellformedXslt() throws IOException {
165 XslTransformer transformer = new XslTransformer();
167 InputResource xmlResource = new ClassPathResource(
168 getResourcePath(REPORT_XML));
169 Source xslt = new StreamSource(new ClassPathResource(
170 getResourcePath(REPORT_TO_HTML_NONWELLFORMED_XSLT))
173 byte[] documentData = FileSystemUtils
174 .read(xmlResource.getInputStream()).getBytes();
177 Document output1 = transformer.transform(documentData, xslt);
178 } catch (TransformerException e) {
186 * Transforms a file using a class path resolver.
189 public void testTransformUsingClassPathResolver() throws Exception {
190 XslTransformer transformer = new XslTransformer(
191 new ClasspathUriResolver());
193 InputResource xmlResource = new ClassPathResource(
194 getResourcePath(REPORT_XML));
195 Source xslt = new StreamSource(new ClassPathResource(
196 getResourcePath(REPORT_TO_HTML2_XSLT)).getInputStream());
198 byte[] documentData = FileSystemUtils
199 .read(xmlResource.getInputStream()).getBytes();
201 Document output1 = transformer.transform(documentData, xslt);
202 Document expected = DomUtils
203 .read(new ClassPathResource(
204 getResourcePath("output-reportToHtml-report.xml"))
206 XmlUtils.assertEquals("doc", expected, output1);
210 * Transforms a file to text output. Verifies the file is transformed
214 public void testTransformToTextOutput() throws Exception {
215 XslTransformer transformer = new XslTransformer(
216 new ClasspathUriResolver());
218 InputResource xmlResource = new ClassPathResource(
219 getResourcePath(REPORT_XML));
220 Source xslt = new StreamSource(new ClassPathResource(
221 getResourcePath(REPORT_TO_TEXT_XSLT)).getInputStream());
223 byte[] documentData = FileSystemUtils
224 .read(xmlResource.getInputStream()).getBytes();
226 String result = transformer.textTransform(documentData, xslt);
227 String expected = "Hello world!";
228 assertEquals("text transform", expected, result);
232 * Tests resolving a file using {@link XslTransformer#resolve(String)} with
233 * the default resolver where the file does not exist.
236 public void testResolveWithDefaultResolverFileNotFound() {
237 XslTransformer transformer = new XslTransformer();
240 Source source = transformer
241 .resolve("org/wamblee/xml/utilities-nonexistent.xsl");
242 } catch (TransformerException e) {
250 * Tests resolving a file using {@link XslTransformer#resolve(String)} with
251 * the default resolver.
254 public void testResolveWithClasspathResolver() throws Exception {
255 XslTransformer transformer = new XslTransformer(
256 new ClasspathUriResolver());
257 Source source = transformer.resolve(getResourcePath(INCLUDED_XSL_FILE));
258 assert (source instanceof StreamSource);
260 StreamSource ssource = (StreamSource) source;
261 String data = FileSystemUtils.read(ssource.getInputStream());
262 String expected = FileSystemUtils.read(new ClassPathResource(
263 getResourcePath(INCLUDED_XSL_FILE)).getInputStream());
264 assertEquals(expected, data);