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;
40 * Tests the XSL transformer.
42 * @author Erik Brakkee
44 public class XslTransformerTest extends TestCase {
45 private static final String INCLUDED_XSL_FILE = "utilities.xsl";
46 private static final String REPORT_XML = "report.xml";
47 private static final String REPORT_TO_HTML_XSLT = "reportToHtml.xsl";
48 private static final String REPORT_TO_HTML2_XSLT = "reportToHtml2.xsl";
49 private static final String REPORT_TO_HTML_INVALID_XSLT = "reportToHtml-invalid.xsl";
50 private static final String REPORT_TO_HTML_NONWELLFORMED_XSLT = "reportToHtml-nonwellformed.xsl";
51 private static final String REPORT_TO_TEXT_XSLT = "reportToText.xsl";
53 private String getResourcePath(String aResource) {
54 return getClass().getPackage().getName().replaceAll("\\.", "/") + "/" +
59 * Transforms a file while using the default resolver, where the included
60 * file can be found. Verifies the transformation is done correctly.
63 public void testTransformUsingDefaultResolver() throws Exception {
64 XslTransformer transformer = new XslTransformer();
66 InputResource xmlResource = new ClassPathResource(getResourcePath(
69 Source xslt = new StreamSource(new ClassPathResource(getResourcePath(
70 REPORT_TO_HTML_XSLT)).getInputStream());
72 byte[] documentData = FileSystemUtils.read(xmlResource.getInputStream())
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 ClassPathResource(getResourcePath(
80 "output-reportToHtml-report.xml")).getInputStream());
82 Document output1 = transformer.transform(documentData, xslt);
83 XmlUtils.assertEquals("byte[] transform", expected, output1);
85 xslt = new StreamSource(new ClassPathResource(getResourcePath(
86 REPORT_TO_HTML_XSLT)).getInputStream());
88 Document output2 = transformer.transform(document, xslt);
89 XmlUtils.assertEquals("document transform", expected, output2);
91 ByteArrayOutputStream os = new ByteArrayOutputStream();
92 Result output = new StreamResult(os);
94 xslt = new StreamSource(new ClassPathResource(getResourcePath(
95 REPORT_TO_HTML_XSLT)).getInputStream());
96 transformer.transform(documentSource, output, xslt);
97 XmlUtils.assertEquals("document source transform", expected,
98 DomUtils.read(os.toString()));
100 xslt = new StreamSource(new ClassPathResource(getResourcePath(
101 REPORT_TO_HTML_XSLT)).getInputStream());
103 String result = transformer.textTransform(documentData, xslt);
104 XmlUtils.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()
114 XslTransformer transformer = new XslTransformer();
116 InputResource xmlResource = new ClassPathResource(getResourcePath(
118 Source xslt = new StreamSource(new ClassPathResource(getResourcePath(
119 REPORT_TO_HTML2_XSLT)).getInputStream());
121 byte[] documentData = FileSystemUtils.read(xmlResource.getInputStream())
125 Document output1 = transformer.transform(documentData, xslt);
126 } catch (TransformerException e) {
134 * Transforms a file using an invalid Xslt. Verifies that a
135 * TransformerException is thrown.
138 public void testTransformInvalidXslt() throws IOException {
139 XslTransformer transformer = new XslTransformer();
141 InputResource xmlResource = new ClassPathResource(getResourcePath(
143 Source xslt = new StreamSource(new ClassPathResource(getResourcePath(
144 REPORT_TO_HTML_INVALID_XSLT)).getInputStream());
146 byte[] documentData = FileSystemUtils.read(xmlResource.getInputStream())
150 Document output1 = transformer.transform(documentData, xslt);
151 } catch (TransformerException e) {
159 * Transforms a file using a non-well formed xslt. Verifies that a
160 * TransformerException is thrown.
163 public void testTransformNonWellformedXslt() throws IOException {
164 XslTransformer transformer = new XslTransformer();
166 InputResource xmlResource = new ClassPathResource(getResourcePath(
168 Source xslt = new StreamSource(new ClassPathResource(getResourcePath(
169 REPORT_TO_HTML_NONWELLFORMED_XSLT)).getInputStream());
171 byte[] documentData = FileSystemUtils.read(xmlResource.getInputStream())
175 Document output1 = 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(new ClasspathUriResolver());
190 InputResource xmlResource = new ClassPathResource(getResourcePath(
192 Source xslt = new StreamSource(new ClassPathResource(getResourcePath(
193 REPORT_TO_HTML2_XSLT)).getInputStream());
195 byte[] documentData = FileSystemUtils.read(xmlResource.getInputStream())
198 Document output1 = transformer.transform(documentData, xslt);
199 Document expected = DomUtils.read(new ClassPathResource(getResourcePath(
200 "output-reportToHtml-report.xml")).getInputStream());
201 XmlUtils.assertEquals("doc", expected, output1);
205 * Transforms a file to text output. Verifies the file is transformed
209 public void testTransformToTextOutput() throws Exception {
210 XslTransformer transformer = new XslTransformer(new ClasspathUriResolver());
212 InputResource xmlResource = new ClassPathResource(getResourcePath(
214 Source xslt = new StreamSource(new ClassPathResource(getResourcePath(
215 REPORT_TO_TEXT_XSLT)).getInputStream());
217 byte[] documentData = FileSystemUtils.read(xmlResource.getInputStream())
220 String result = transformer.textTransform(documentData, xslt);
221 String expected = "Hello world!";
222 assertEquals("text transform", expected, result);
226 * Tests resolving a file using {@link XslTransformer#resolve(String)} with the
227 * default resolver where the file does not exist.
230 public void testResolveWithDefaultResolverFileNotFound() {
231 XslTransformer transformer = new XslTransformer();
234 Source source = transformer.resolve(
235 "org/wamblee/xml/utilities-nonexistent.xsl");
236 } catch (TransformerException e) {
244 * Tests resolving a file using {@link XslTransformer#resolve(String)} with the
248 public void testResolveWithClasspathResolver() throws Exception {
249 XslTransformer transformer = new XslTransformer(new ClasspathUriResolver());
250 Source source = transformer.resolve(getResourcePath(INCLUDED_XSL_FILE));
251 assert (source instanceof StreamSource);
253 StreamSource ssource = (StreamSource) source;
254 String data = FileSystemUtils.read(ssource.getInputStream());
255 String expected = FileSystemUtils.read(new ClassPathResource(
256 getResourcePath(INCLUDED_XSL_FILE)).getInputStream());
257 assertEquals(expected, data);