c43214cd38470cb540affb770f9758faa7b98fab
[utils] / support / general / src / test / java / org / wamblee / xml / XslTransformerTest.java
1 /*
2  * Copyright 2005 the original author or authors.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.wamblee.xml;
17
18 import junit.framework.TestCase;
19
20 import org.w3c.dom.Document;
21
22 import org.wamblee.io.ClassPathResource;
23 import org.wamblee.io.FileSystemUtils;
24 import org.wamblee.io.InputResource;
25
26 import java.io.ByteArrayOutputStream;
27 import java.io.File;
28 import java.io.IOException;
29
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;
37
38
39 /**
40  * Tests the XSL transformer.
41  *
42  * @author Erik Brakkee
43  */
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";
52
53     private String getResourcePath(String aResource) {
54         return getClass().getPackage().getName().replaceAll("\\.", "/") + "/" +
55         aResource;
56     }
57
58     /**
59      * Transforms a file while using the default resolver, where the included
60      * file can be found. Verifies the transformation is done correctly.
61      *
62      */
63     public void testTransformUsingDefaultResolver() throws Exception {
64         XslTransformer transformer = new XslTransformer();
65
66         InputResource xmlResource = new ClassPathResource(getResourcePath(
67                     REPORT_XML));
68
69         Source xslt = new StreamSource(new ClassPathResource(getResourcePath(
70                         REPORT_TO_HTML_XSLT)).getInputStream());
71
72         byte[] documentData = FileSystemUtils.read(xmlResource.getInputStream())
73                                              .getBytes();
74         DocumentBuilder builder = DocumentBuilderFactory.newInstance()
75                                                         .newDocumentBuilder();
76         Document document = builder.parse(xmlResource.getInputStream());
77         Source documentSource = new StreamSource(xmlResource.getInputStream());
78
79         Document expected = DomUtils.read(new ClassPathResource(getResourcePath(
80                         "output-reportToHtml-report.xml")).getInputStream());
81
82         Document output1 = transformer.transform(documentData, xslt);
83         XmlUtils.assertEquals("byte[] transform", expected, output1);
84
85         xslt = new StreamSource(new ClassPathResource(getResourcePath(
86                         REPORT_TO_HTML_XSLT)).getInputStream());
87
88         Document output2 = transformer.transform(document, xslt);
89         XmlUtils.assertEquals("document transform", expected, output2);
90
91         ByteArrayOutputStream os = new ByteArrayOutputStream();
92         Result output = new StreamResult(os);
93
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()));
99
100         xslt = new StreamSource(new ClassPathResource(getResourcePath(
101                         REPORT_TO_HTML_XSLT)).getInputStream());
102
103         String result = transformer.textTransform(documentData, xslt);
104         XmlUtils.assertEquals("text transform", expected, DomUtils.read(result));
105     }
106
107     /**
108      * Transforms a file using the default resolver where the included file
109      * cannot be found. Verifies that a TransformerException is thrown.
110      *
111      */
112     public void testTransformUsingDefaultResolverFails()
113         throws IOException {
114         XslTransformer transformer = new XslTransformer();
115
116         InputResource xmlResource = new ClassPathResource(getResourcePath(
117                     REPORT_XML));
118         Source xslt = new StreamSource(new ClassPathResource(getResourcePath(
119                         REPORT_TO_HTML2_XSLT)).getInputStream());
120
121         byte[] documentData = FileSystemUtils.read(xmlResource.getInputStream())
122                                              .getBytes();
123
124         try {
125             Document output1 = transformer.transform(documentData, xslt);
126         } catch (TransformerException e) {
127             return; // ok
128         }
129
130         fail();
131     }
132
133     /**
134      * Transforms a file using an invalid Xslt. Verifies that a
135      * TransformerException is thrown.
136      *
137      */
138     public void testTransformInvalidXslt() throws IOException {
139         XslTransformer transformer = new XslTransformer();
140
141         InputResource xmlResource = new ClassPathResource(getResourcePath(
142                     REPORT_XML));
143         Source xslt = new StreamSource(new ClassPathResource(getResourcePath(
144                         REPORT_TO_HTML_INVALID_XSLT)).getInputStream());
145
146         byte[] documentData = FileSystemUtils.read(xmlResource.getInputStream())
147                                              .getBytes();
148
149         try {
150             Document output1 = transformer.transform(documentData, xslt);
151         } catch (TransformerException e) {
152             return; // ok
153         }
154
155         fail();
156     }
157
158     /**
159      * Transforms a file using a non-well formed xslt. Verifies that a
160      * TransformerException is thrown.
161      *
162      */
163     public void testTransformNonWellformedXslt() throws IOException {
164         XslTransformer transformer = new XslTransformer();
165
166         InputResource xmlResource = new ClassPathResource(getResourcePath(
167                     REPORT_XML));
168         Source xslt = new StreamSource(new ClassPathResource(getResourcePath(
169                         REPORT_TO_HTML_NONWELLFORMED_XSLT)).getInputStream());
170
171         byte[] documentData = FileSystemUtils.read(xmlResource.getInputStream())
172                                              .getBytes();
173
174         try {
175             Document output1 = transformer.transform(documentData, xslt);
176         } catch (TransformerException e) {
177             return; // ok
178         }
179
180         fail();
181     }
182
183     /**
184      * Transforms a file using a class path resolver.
185      *
186      */
187     public void testTransformUsingClassPathResolver() throws Exception {
188         XslTransformer transformer = new XslTransformer(new ClasspathUriResolver());
189
190         InputResource xmlResource = new ClassPathResource(getResourcePath(
191                     REPORT_XML));
192         Source xslt = new StreamSource(new ClassPathResource(getResourcePath(
193                         REPORT_TO_HTML2_XSLT)).getInputStream());
194
195         byte[] documentData = FileSystemUtils.read(xmlResource.getInputStream())
196                                              .getBytes();
197
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);
202     }
203
204     /**
205      * Transforms a file to text output. Verifies the file is transformed
206      * correctly.
207      *
208      */
209     public void testTransformToTextOutput() throws Exception {
210         XslTransformer transformer = new XslTransformer(new ClasspathUriResolver());
211
212         InputResource xmlResource = new ClassPathResource(getResourcePath(
213                     REPORT_XML));
214         Source xslt = new StreamSource(new ClassPathResource(getResourcePath(
215                         REPORT_TO_TEXT_XSLT)).getInputStream());
216
217         byte[] documentData = FileSystemUtils.read(xmlResource.getInputStream())
218                                              .getBytes();
219
220         String result = transformer.textTransform(documentData, xslt);
221         String expected = "Hello world!";
222         assertEquals("text transform", expected, result);
223     }
224
225     /**
226      * Tests resolving a file using {@link XslTransformer#resolve(String)} with the
227      * default resolver where the file does not exist.
228      *
229      */
230     public void testResolveWithDefaultResolverFileNotFound() {
231         XslTransformer transformer = new XslTransformer();
232
233         try {
234             Source source = transformer.resolve(
235                     "org/wamblee/xml/utilities-nonexistent.xsl");
236         } catch (TransformerException e) {
237             return; // ok
238         }
239
240         fail();
241     }
242
243     /**
244      * Tests resolving a file using {@link XslTransformer#resolve(String)} with the
245      * default resolver.
246      *
247      */
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);
252
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);
258     }
259 }