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;
32 import org.wamblee.io.ClassPathResource;
33 import org.wamblee.io.FileSystemUtils;
34 import org.wamblee.io.InputResource;
36 import org.w3c.dom.Document;
39 * Tests the XSL transformer.
41 * @author Erik Brakkee
43 public class XslTransformerTest extends TestCase {
45 private static final String INCLUDED_XSL_FILE = "utilities.xsl";
47 private static final String REPORT_XML = "report.xml";
49 private static final String REPORT_TO_HTML_XSLT = "reportToHtml.xsl";
51 private static final String REPORT_TO_HTML2_XSLT = "reportToHtml2.xsl";
53 private static final String REPORT_TO_HTML_INVALID_XSLT = "reportToHtml-invalid.xsl";
55 private static final String REPORT_TO_HTML_NONWELLFORMED_XSLT = "reportToHtml-nonwellformed.xsl";
57 private static final String REPORT_TO_TEXT_XSLT = "reportToText.xsl";
59 private String getResourcePath(String aResource) {
60 return getClass().getPackage().getName().replaceAll("\\.", "/") + "/" + aResource;
64 * Transforms a file while using the default resolver, where the included
65 * file can be found. Verifies the transformation is done correctly.
68 public void testTransformUsingDefaultResolver() throws Exception {
69 XslTransformer transformer = new XslTransformer();
71 InputResource xmlResource = new ClassPathResource(getResourcePath(REPORT_XML));
73 Source xslt = new StreamSource(new ClassPathResource(getResourcePath(
74 REPORT_TO_HTML_XSLT)).getInputStream());
76 byte[] documentData = FileSystemUtils
77 .read(xmlResource.getInputStream()).getBytes();
78 DocumentBuilder builder = DocumentBuilderFactory.newInstance()
79 .newDocumentBuilder();
80 Document document = builder.parse(xmlResource.getInputStream());
81 Source documentSource = new StreamSource(xmlResource.getInputStream());
83 Document expected = DomUtils.read(new ClassPathResource(getResourcePath(
84 "output-reportToHtml-report.xml")).getInputStream());
86 Document output1 = transformer.transform(documentData, xslt);
87 XmlUtils.assertEquals("byte[] transform", expected, output1);
89 xslt = new StreamSource(new ClassPathResource(getResourcePath(
90 REPORT_TO_HTML_XSLT)).getInputStream());
91 Document output2 = transformer.transform(document, xslt);
92 XmlUtils.assertEquals("document transform", expected, output2);
94 ByteArrayOutputStream os = new ByteArrayOutputStream();
95 Result output = new StreamResult(os);
97 xslt = new StreamSource(new ClassPathResource(getResourcePath(
98 REPORT_TO_HTML_XSLT)).getInputStream());
99 transformer.transform(documentSource, output, xslt);
100 XmlUtils.assertEquals("document source transform", expected, DomUtils
101 .read(os.toString()));
103 xslt = new StreamSource(new ClassPathResource(getResourcePath(
104 REPORT_TO_HTML_XSLT)).getInputStream());
105 String result = transformer.textTransform(documentData, xslt);
107 .assertEquals("text transform", expected, DomUtils.read(result));
111 * Transforms a file using the default resolver where the included file
112 * cannot be found. Verifies that a TransformerException is thrown.
115 public void testTransformUsingDefaultResolverFails() throws IOException {
116 XslTransformer transformer = new XslTransformer();
118 InputResource xmlResource =
119 new ClassPathResource(getResourcePath(REPORT_XML));
120 Source xslt = new StreamSource(
121 new ClassPathResource(getResourcePath(
122 REPORT_TO_HTML2_XSLT)).getInputStream());
124 byte[] documentData = FileSystemUtils
125 .read(xmlResource.getInputStream()).getBytes();
127 Document output1 = transformer.transform(documentData, xslt);
128 } 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(
145 new ClassPathResource(getResourcePath(REPORT_TO_HTML_INVALID_XSLT)).getInputStream());
147 byte[] documentData = FileSystemUtils
148 .read(xmlResource.getInputStream()).getBytes();
150 Document output1 = transformer.transform(documentData, xslt);
151 } 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(
168 new ClassPathResource(getResourcePath(
169 REPORT_TO_HTML_NONWELLFORMED_XSLT)).getInputStream());
171 byte[] documentData = FileSystemUtils
172 .read(xmlResource.getInputStream()).getBytes();
174 Document output1 = transformer.transform(documentData, xslt);
175 } catch (TransformerException e) {
182 * Transforms a file using a class path resolver.
185 public void testTransformUsingClassPathResolver() throws Exception {
186 XslTransformer transformer = new XslTransformer(new ClasspathUriResolver());
188 InputResource xmlResource = new ClassPathResource(getResourcePath(
190 Source xslt = new StreamSource(new ClassPathResource(
191 getResourcePath(REPORT_TO_HTML2_XSLT)).getInputStream());
193 byte[] documentData = FileSystemUtils
194 .read(xmlResource.getInputStream()).getBytes();
196 Document output1 = transformer.transform(documentData, xslt);
197 Document expected = DomUtils.read(new ClassPathResource(
198 getResourcePath("output-reportToHtml-report.xml"))
200 XmlUtils.assertEquals("doc", expected, output1);
204 * Transforms a file to text output. Verifies the file is transformed
208 public void testTransformToTextOutput() throws Exception {
209 XslTransformer transformer = new XslTransformer(new ClasspathUriResolver());
211 InputResource xmlResource = new ClassPathResource(
212 getResourcePath(REPORT_XML));
213 Source xslt = new StreamSource(
214 new ClassPathResource(getResourcePath(REPORT_TO_TEXT_XSLT)).getInputStream());
216 byte[] documentData = FileSystemUtils
217 .read(xmlResource.getInputStream()).getBytes();
219 String result = transformer.textTransform(documentData, xslt);
220 String expected = "Hello world!";
221 assertEquals("text transform", expected, result);
225 * Tests resolving a file using {@link XslTransformer#resolve(String)} with the
226 * default resolver where the file does not exist.
229 public void testResolveWithDefaultResolverFileNotFound() {
230 XslTransformer transformer = new XslTransformer();
232 Source source = transformer.resolve("org/wamblee/xml/utilities-nonexistent.xsl");
233 } catch (TransformerException e) {
241 * Tests resolving a file using {@link XslTransformer#resolve(String)} with the
245 public void testResolveWithClasspathResolver() throws Exception {
246 XslTransformer transformer = new XslTransformer(new ClasspathUriResolver());
247 Source source = transformer.resolve(getResourcePath(INCLUDED_XSL_FILE));
248 assert(source instanceof StreamSource);
249 StreamSource ssource = (StreamSource)source;
250 String data = FileSystemUtils.read(ssource.getInputStream());
251 String expected = FileSystemUtils.read(new ClassPathResource(getResourcePath(INCLUDED_XSL_FILE)).getInputStream());
252 assertEquals(expected, data);