Removed DOCUMENT ME comments that were generated and applied source code
[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  * Tests the XSL transformer.
40  * 
41  * @author Erik Brakkee
42  */
43 public class XslTransformerTest extends TestCase {
44     private static final String INCLUDED_XSL_FILE = "utilities.xsl";
45     private static final String REPORT_XML = "report.xml";
46     private static final String REPORT_TO_HTML_XSLT = "reportToHtml.xsl";
47     private static final String REPORT_TO_HTML2_XSLT = "reportToHtml2.xsl";
48     private static final String REPORT_TO_HTML_INVALID_XSLT = "reportToHtml-invalid.xsl";
49     private static final String REPORT_TO_HTML_NONWELLFORMED_XSLT = "reportToHtml-nonwellformed.xsl";
50     private static final String REPORT_TO_TEXT_XSLT = "reportToText.xsl";
51
52     private String getResourcePath(String aResource) {
53         return getClass().getPackage().getName().replaceAll("\\.", "/") + "/" +
54             aResource;
55     }
56
57     /**
58      * Transforms a file while using the default resolver, where the included
59      * file can be found. Verifies the transformation is done correctly.
60      * 
61      */
62     public void testTransformUsingDefaultResolver() throws Exception {
63         XslTransformer transformer = new XslTransformer();
64
65         InputResource xmlResource = new ClassPathResource(
66             getResourcePath(REPORT_XML));
67
68         Source xslt = new StreamSource(new ClassPathResource(
69             getResourcePath(REPORT_TO_HTML_XSLT)).getInputStream());
70
71         byte[] documentData = FileSystemUtils
72             .read(xmlResource.getInputStream()).getBytes();
73         DocumentBuilder builder = DocumentBuilderFactory.newInstance()
74             .newDocumentBuilder();
75         Document document = builder.parse(xmlResource.getInputStream());
76         Source documentSource = new StreamSource(xmlResource.getInputStream());
77
78         Document expected = DomUtils
79             .read(new ClassPathResource(
80                 getResourcePath("output-reportToHtml-report.xml"))
81                 .getInputStream());
82
83         Document output1 = transformer.transform(documentData, xslt);
84         XmlUtils.assertEquals("byte[] transform", expected, output1);
85
86         xslt = new StreamSource(new ClassPathResource(
87             getResourcePath(REPORT_TO_HTML_XSLT)).getInputStream());
88
89         Document output2 = transformer.transform(document, xslt);
90         XmlUtils.assertEquals("document transform", expected, output2);
91
92         ByteArrayOutputStream os = new ByteArrayOutputStream();
93         Result output = new StreamResult(os);
94
95         xslt = new StreamSource(new ClassPathResource(
96             getResourcePath(REPORT_TO_HTML_XSLT)).getInputStream());
97         transformer.transform(documentSource, output, xslt);
98         XmlUtils.assertEquals("document source transform", expected, DomUtils
99             .read(os.toString()));
100
101         xslt = new StreamSource(new ClassPathResource(
102             getResourcePath(REPORT_TO_HTML_XSLT)).getInputStream());
103
104         String result = transformer.textTransform(documentData, xslt);
105         XmlUtils
106             .assertEquals("text transform", expected, DomUtils.read(result));
107     }
108
109     /**
110      * Transforms a file using the default resolver where the included file
111      * cannot be found. Verifies that a TransformerException is thrown.
112      * 
113      */
114     public void testTransformUsingDefaultResolverFails() throws IOException {
115         XslTransformer transformer = new XslTransformer();
116
117         InputResource xmlResource = new ClassPathResource(
118             getResourcePath(REPORT_XML));
119         Source xslt = new StreamSource(new ClassPathResource(
120             getResourcePath(REPORT_TO_HTML2_XSLT)).getInputStream());
121
122         byte[] documentData = FileSystemUtils
123             .read(xmlResource.getInputStream()).getBytes();
124
125         try {
126             Document output1 = transformer.transform(documentData, xslt);
127         } catch (TransformerException e) {
128             return; // ok
129         }
130
131         fail();
132     }
133
134     /**
135      * Transforms a file using an invalid Xslt. Verifies that a
136      * TransformerException is thrown.
137      * 
138      */
139     public void testTransformInvalidXslt() throws IOException {
140         XslTransformer transformer = new XslTransformer();
141
142         InputResource xmlResource = new ClassPathResource(
143             getResourcePath(REPORT_XML));
144         Source xslt = new StreamSource(new ClassPathResource(
145             getResourcePath(REPORT_TO_HTML_INVALID_XSLT)).getInputStream());
146
147         byte[] documentData = FileSystemUtils
148             .read(xmlResource.getInputStream()).getBytes();
149
150         try {
151             Document output1 = transformer.transform(documentData, xslt);
152         } catch (TransformerException e) {
153             return; // ok
154         }
155
156         fail();
157     }
158
159     /**
160      * Transforms a file using a non-well formed xslt. Verifies that a
161      * TransformerException is thrown.
162      * 
163      */
164     public void testTransformNonWellformedXslt() throws IOException {
165         XslTransformer transformer = new XslTransformer();
166
167         InputResource xmlResource = new ClassPathResource(
168             getResourcePath(REPORT_XML));
169         Source xslt = new StreamSource(new ClassPathResource(
170             getResourcePath(REPORT_TO_HTML_NONWELLFORMED_XSLT))
171             .getInputStream());
172
173         byte[] documentData = FileSystemUtils
174             .read(xmlResource.getInputStream()).getBytes();
175
176         try {
177             Document output1 = transformer.transform(documentData, xslt);
178         } catch (TransformerException e) {
179             return; // ok
180         }
181
182         fail();
183     }
184
185     /**
186      * Transforms a file using a class path resolver.
187      * 
188      */
189     public void testTransformUsingClassPathResolver() throws Exception {
190         XslTransformer transformer = new XslTransformer(
191             new ClasspathUriResolver());
192
193         InputResource xmlResource = new ClassPathResource(
194             getResourcePath(REPORT_XML));
195         Source xslt = new StreamSource(new ClassPathResource(
196             getResourcePath(REPORT_TO_HTML2_XSLT)).getInputStream());
197
198         byte[] documentData = FileSystemUtils
199             .read(xmlResource.getInputStream()).getBytes();
200
201         Document output1 = transformer.transform(documentData, xslt);
202         Document expected = DomUtils
203             .read(new ClassPathResource(
204                 getResourcePath("output-reportToHtml-report.xml"))
205                 .getInputStream());
206         XmlUtils.assertEquals("doc", expected, output1);
207     }
208
209     /**
210      * Transforms a file to text output. Verifies the file is transformed
211      * correctly.
212      * 
213      */
214     public void testTransformToTextOutput() throws Exception {
215         XslTransformer transformer = new XslTransformer(
216             new ClasspathUriResolver());
217
218         InputResource xmlResource = new ClassPathResource(
219             getResourcePath(REPORT_XML));
220         Source xslt = new StreamSource(new ClassPathResource(
221             getResourcePath(REPORT_TO_TEXT_XSLT)).getInputStream());
222
223         byte[] documentData = FileSystemUtils
224             .read(xmlResource.getInputStream()).getBytes();
225
226         String result = transformer.textTransform(documentData, xslt);
227         String expected = "Hello world!";
228         assertEquals("text transform", expected, result);
229     }
230
231     /**
232      * Tests resolving a file using {@link XslTransformer#resolve(String)} with
233      * the default resolver where the file does not exist.
234      * 
235      */
236     public void testResolveWithDefaultResolverFileNotFound() {
237         XslTransformer transformer = new XslTransformer();
238
239         try {
240             Source source = transformer
241                 .resolve("org/wamblee/xml/utilities-nonexistent.xsl");
242         } catch (TransformerException e) {
243             return; // ok
244         }
245
246         fail();
247     }
248
249     /**
250      * Tests resolving a file using {@link XslTransformer#resolve(String)} with
251      * the default resolver.
252      * 
253      */
254     public void testResolveWithClasspathResolver() throws Exception {
255         XslTransformer transformer = new XslTransformer(
256             new ClasspathUriResolver());
257         Source source = transformer.resolve(getResourcePath(INCLUDED_XSL_FILE));
258         assert (source instanceof StreamSource);
259
260         StreamSource ssource = (StreamSource) source;
261         String data = FileSystemUtils.read(ssource.getInputStream());
262         String expected = FileSystemUtils.read(new ClassPathResource(
263             getResourcePath(INCLUDED_XSL_FILE)).getInputStream());
264         assertEquals(expected, data);
265     }
266 }