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 public class XslTransformerTest extends TestCase {
43 private static final String INCLUDED_XSL_FILE = "utilities.xsl";
45 private static final String REPORT_XML = "report.xml";
47 private static final String REPORT_TO_HTML_XSLT = "reportToHtml.xsl";
49 private static final String REPORT_TO_HTML2_XSLT = "reportToHtml2.xsl";
51 private static final String REPORT_TO_HTML_INVALID_XSLT = "reportToHtml-invalid.xsl";
53 private static final String REPORT_TO_HTML_NONWELLFORMED_XSLT = "reportToHtml-nonwellformed.xsl";
55 private static final String REPORT_TO_TEXT_XSLT = "reportToText.xsl";
57 private String getResourcePath(String aResource) {
58 return getClass().getPackage().getName().replaceAll("\\.", "/") + "/" + aResource;
62 * Transforms a file while using the default resolver, where the included
63 * file can be found. Verifies the transformation is done correctly.
66 public void testTransformUsingDefaultResolver() throws Exception {
67 XslTransformer transformer = new XslTransformer();
69 InputResource xmlResource = new ClassPathResource(getResourcePath(REPORT_XML));
71 Source xslt = new StreamSource(new ClassPathResource(getResourcePath(
72 REPORT_TO_HTML_XSLT)).getInputStream());
74 byte[] documentData = FileSystemUtils
75 .read(xmlResource.getInputStream()).getBytes();
76 DocumentBuilder builder = DocumentBuilderFactory.newInstance()
77 .newDocumentBuilder();
78 Document document = builder.parse(xmlResource.getInputStream());
79 Source documentSource = new StreamSource(xmlResource.getInputStream());
81 Document expected = DomUtils.read(new ClassPathResource(getResourcePath(
82 "output-reportToHtml-report.xml")).getInputStream());
84 Document output1 = transformer.transform(documentData, xslt);
85 XmlUtils.assertEquals("byte[] transform", expected, output1);
87 xslt = new StreamSource(new ClassPathResource(getResourcePath(
88 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(getResourcePath(
96 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(getResourcePath(
102 REPORT_TO_HTML_XSLT)).getInputStream());
103 String result = transformer.textTransform(documentData, xslt);
105 .assertEquals("text transform", expected, DomUtils.read(result));
109 * Transforms a file using the default resolver where the included file
110 * cannot be found. Verifies that a TransformerException is thrown.
113 public void testTransformUsingDefaultResolverFails() throws IOException {
114 XslTransformer transformer = new XslTransformer();
116 InputResource xmlResource =
117 new ClassPathResource(getResourcePath(REPORT_XML));
118 Source xslt = new StreamSource(
119 new ClassPathResource(getResourcePath(
120 REPORT_TO_HTML2_XSLT)).getInputStream());
122 byte[] documentData = FileSystemUtils
123 .read(xmlResource.getInputStream()).getBytes();
125 Document output1 = transformer.transform(documentData, xslt);
126 } 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(
143 new ClassPathResource(getResourcePath(REPORT_TO_HTML_INVALID_XSLT)).getInputStream());
145 byte[] documentData = FileSystemUtils
146 .read(xmlResource.getInputStream()).getBytes();
148 Document output1 = transformer.transform(documentData, xslt);
149 } catch (TransformerException e) {
156 * Transforms a file using a non-well formed xslt. Verifies that a
157 * TransformerException is thrown.
160 public void testTransformNonWellformedXslt() throws IOException {
161 XslTransformer transformer = new XslTransformer();
163 InputResource xmlResource = new ClassPathResource(
164 getResourcePath(REPORT_XML));
165 Source xslt = new StreamSource(
166 new ClassPathResource(getResourcePath(
167 REPORT_TO_HTML_NONWELLFORMED_XSLT)).getInputStream());
169 byte[] documentData = FileSystemUtils
170 .read(xmlResource.getInputStream()).getBytes();
172 Document output1 = transformer.transform(documentData, xslt);
173 } catch (TransformerException e) {
180 * Transforms a file using a class path resolver.
183 public void testTransformUsingClassPathResolver() throws Exception {
184 XslTransformer transformer = new XslTransformer(new ClasspathUriResolver());
186 InputResource xmlResource = new ClassPathResource(getResourcePath(
188 Source xslt = new StreamSource(new ClassPathResource(
189 getResourcePath(REPORT_TO_HTML2_XSLT)).getInputStream());
191 byte[] documentData = FileSystemUtils
192 .read(xmlResource.getInputStream()).getBytes();
194 Document output1 = transformer.transform(documentData, xslt);
195 Document expected = DomUtils.read(new ClassPathResource(
196 getResourcePath("output-reportToHtml-report.xml"))
198 XmlUtils.assertEquals("doc", expected, output1);
202 * Transforms a file to text output. Verifies the file is transformed
206 public void testTransformToTextOutput() throws Exception {
207 XslTransformer transformer = new XslTransformer(new ClasspathUriResolver());
209 InputResource xmlResource = new ClassPathResource(
210 getResourcePath(REPORT_XML));
211 Source xslt = new StreamSource(
212 new ClassPathResource(getResourcePath(REPORT_TO_TEXT_XSLT)).getInputStream());
214 byte[] documentData = FileSystemUtils
215 .read(xmlResource.getInputStream()).getBytes();
217 String result = transformer.textTransform(documentData, xslt);
218 String expected = "Hello world!";
219 assertEquals("text transform", expected, result);
223 * Tests resolving a file using {@link XslTransformer#resolve(String)} with the
224 * default resolver where the file does not exist.
227 public void testResolveWithDefaultResolverFileNotFound() {
228 XslTransformer transformer = new XslTransformer();
230 Source source = transformer.resolve("org/wamblee/xml/utilities-nonexistent.xsl");
231 } catch (TransformerException e) {
239 * Tests resolving a file using {@link XslTransformer#resolve(String)} with the
243 public void testResolveWithClasspathResolver() throws Exception {
244 XslTransformer transformer = new XslTransformer(new ClasspathUriResolver());
245 Source source = transformer.resolve(getResourcePath(INCLUDED_XSL_FILE));
246 assert(source instanceof StreamSource);
247 StreamSource ssource = (StreamSource)source;
248 String data = FileSystemUtils.read(ssource.getInputStream());
249 String expected = FileSystemUtils.read(new ClassPathResource(getResourcePath(INCLUDED_XSL_FILE)).getInputStream());
250 assertEquals(expected, data);