2 * Copyright 2005-2010 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 java.io.ByteArrayOutputStream;
19 import java.io.IOException;
21 import javax.xml.parsers.DocumentBuilder;
22 import javax.xml.parsers.DocumentBuilderFactory;
23 import javax.xml.transform.Result;
24 import javax.xml.transform.Source;
25 import javax.xml.transform.TransformerException;
26 import javax.xml.transform.stream.StreamResult;
27 import javax.xml.transform.stream.StreamSource;
29 import junit.framework.TestCase;
31 import org.w3c.dom.Document;
32 import org.wamblee.io.ClassPathResource;
33 import org.wamblee.io.FileSystemUtils;
34 import org.wamblee.io.InputResource;
37 * Tests the XSL transformer.
39 * @author Erik Brakkee
41 public class XslTransformerTest extends TestCase {
42 private static final String INCLUDED_XSL_FILE = "utilities.xsl";
43 private static final String REPORT_XML = "report.xml";
44 private static final String REPORT_TO_HTML_XSLT = "reportToHtml.xsl";
45 private static final String REPORT_TO_HTML2_XSLT = "reportToHtml2.xsl";
46 private static final String REPORT_TO_HTML_INVALID_XSLT = "reportToHtml-invalid.xsl";
47 private static final String REPORT_TO_HTML_NONWELLFORMED_XSLT = "reportToHtml-nonwellformed.xsl";
48 private static final String REPORT_TO_TEXT_XSLT = "reportToText.xsl";
50 private String getResourcePath(String aResource) {
51 return getClass().getPackage().getName().replaceAll("\\.", "/") + "/" +
56 * Transforms a file while using the default resolver, where the included
57 * file can be found. Verifies the transformation is done correctly.
60 public void testTransformUsingDefaultResolver() throws Exception {
61 XslTransformer transformer = new XslTransformer();
63 InputResource xmlResource = new ClassPathResource(
64 getResourcePath(REPORT_XML));
66 Source xslt = new StreamSource(new ClassPathResource(
67 getResourcePath(REPORT_TO_HTML_XSLT)).getInputStream());
69 byte[] documentData = FileSystemUtils
70 .read(xmlResource.getInputStream()).getBytes();
71 DocumentBuilder builder = DocumentBuilderFactory.newInstance()
72 .newDocumentBuilder();
73 Document document = builder.parse(xmlResource.getInputStream());
74 Source documentSource = new StreamSource(xmlResource.getInputStream());
76 Document expected = DomUtils
77 .read(new ClassPathResource(
78 getResourcePath("output-reportToHtml-report.xml"))
81 Document output1 = transformer.transform(documentData, xslt);
82 XmlUtils.assertEquals("byte[] transform", expected, output1);
84 xslt = new StreamSource(new ClassPathResource(
85 getResourcePath(REPORT_TO_HTML_XSLT)).getInputStream());
87 Document output2 = transformer.transform(document, xslt);
88 XmlUtils.assertEquals("document transform", expected, output2);
90 ByteArrayOutputStream os = new ByteArrayOutputStream();
91 Result output = new StreamResult(os);
93 xslt = new StreamSource(new ClassPathResource(
94 getResourcePath(REPORT_TO_HTML_XSLT)).getInputStream());
95 transformer.transform(documentSource, output, xslt);
96 XmlUtils.assertEquals("document source transform", expected, DomUtils
97 .read(os.toString()));
99 xslt = new StreamSource(new ClassPathResource(
100 getResourcePath(REPORT_TO_HTML_XSLT)).getInputStream());
102 String result = transformer.textTransform(documentData, xslt);
104 .assertEquals("text transform", expected, DomUtils.read(result));
108 * Transforms a file using the default resolver where the included file
109 * cannot be found. Verifies that a TransformerException is thrown.
112 public void testTransformUsingDefaultResolverFails() throws IOException {
113 XslTransformer transformer = new XslTransformer();
115 InputResource xmlResource = new ClassPathResource(
116 getResourcePath(REPORT_XML));
117 Source xslt = new StreamSource(new ClassPathResource(
118 getResourcePath(REPORT_TO_HTML2_XSLT)).getInputStream());
120 byte[] documentData = FileSystemUtils
121 .read(xmlResource.getInputStream()).getBytes();
124 transformer.transform(documentData, xslt);
125 } catch (TransformerException e) {
133 * Transforms a file using an invalid Xslt. Verifies that a
134 * TransformerException is thrown.
137 public void testTransformInvalidXslt() throws IOException {
138 XslTransformer transformer = new XslTransformer();
140 InputResource xmlResource = new ClassPathResource(
141 getResourcePath(REPORT_XML));
142 Source xslt = new StreamSource(new ClassPathResource(
143 getResourcePath(REPORT_TO_HTML_INVALID_XSLT)).getInputStream());
145 byte[] documentData = FileSystemUtils
146 .read(xmlResource.getInputStream()).getBytes();
149 transformer.transform(documentData, xslt);
150 } catch (TransformerException e) {
158 * Transforms a file using a non-well formed xslt. Verifies that a
159 * TransformerException is thrown.
162 public void testTransformNonWellformedXslt() throws IOException {
163 XslTransformer transformer = new XslTransformer();
165 InputResource xmlResource = new ClassPathResource(
166 getResourcePath(REPORT_XML));
167 Source xslt = new StreamSource(new ClassPathResource(
168 getResourcePath(REPORT_TO_HTML_NONWELLFORMED_XSLT))
171 byte[] documentData = FileSystemUtils
172 .read(xmlResource.getInputStream()).getBytes();
175 transformer.transform(documentData, xslt);
176 } catch (TransformerException e) {
184 * Transforms a file using a class path resolver.
187 public void testTransformUsingClassPathResolver() throws Exception {
188 XslTransformer transformer = new XslTransformer(
189 new ClasspathUriResolver());
191 InputResource xmlResource = new ClassPathResource(
192 getResourcePath(REPORT_XML));
193 Source xslt = new StreamSource(new ClassPathResource(
194 getResourcePath(REPORT_TO_HTML2_XSLT)).getInputStream());
196 byte[] documentData = FileSystemUtils
197 .read(xmlResource.getInputStream()).getBytes();
199 Document output1 = transformer.transform(documentData, xslt);
200 Document expected = DomUtils
201 .read(new ClassPathResource(
202 getResourcePath("output-reportToHtml-report.xml"))
204 XmlUtils.assertEquals("doc", expected, output1);
208 * Transforms a file to text output. Verifies the file is transformed
212 public void testTransformToTextOutput() throws Exception {
213 XslTransformer transformer = new XslTransformer(
214 new ClasspathUriResolver());
216 InputResource xmlResource = new ClassPathResource(
217 getResourcePath(REPORT_XML));
218 Source xslt = new StreamSource(new ClassPathResource(
219 getResourcePath(REPORT_TO_TEXT_XSLT)).getInputStream());
221 byte[] documentData = FileSystemUtils
222 .read(xmlResource.getInputStream()).getBytes();
224 String result = transformer.textTransform(documentData, xslt);
225 String expected = "Hello world!";
226 assertEquals("text transform", expected, result);
230 * Tests resolving a file using {@link XslTransformer#resolve(String)} with
231 * the default resolver where the file does not exist.
234 public void testResolveWithDefaultResolverFileNotFound() {
235 XslTransformer transformer = new XslTransformer();
238 transformer.resolve("org/wamblee/xml/utilities-nonexistent.xsl");
239 } catch (TransformerException e) {
247 * Tests resolving a file using {@link XslTransformer#resolve(String)} with
248 * the default resolver.
251 public void testResolveWithClasspathResolver() throws Exception {
252 XslTransformer transformer = new XslTransformer(
253 new ClasspathUriResolver());
254 Source source = transformer.resolve(getResourcePath(INCLUDED_XSL_FILE));
255 assert (source instanceof StreamSource);
257 StreamSource ssource = (StreamSource) source;
258 String data = FileSystemUtils.read(ssource.getInputStream());
259 String expected = FileSystemUtils.read(new ClassPathResource(
260 getResourcePath(INCLUDED_XSL_FILE)).getInputStream());
261 assertEquals(expected, data);