(no commit message)
[utils] / support / 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
17 package org.wamblee.xml;
18
19 import java.io.ByteArrayOutputStream;
20 import java.io.File;
21 import java.io.IOException;
22
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;
30
31 import junit.framework.TestCase;
32 import org.wamblee.io.ClassPathResource;
33 import org.wamblee.io.FileSystemUtils;
34 import org.wamblee.io.InputResource;
35
36 import org.w3c.dom.Document;
37
38 /**
39  * Tests the XSL transformer.
40  */
41 public class XslTransformerTest extends TestCase {
42
43     private static final String INCLUDED_XSL_FILE = "utilities.xsl";
44
45         private static final String REPORT_XML = "report.xml";
46
47     private static final String REPORT_TO_HTML_XSLT = "reportToHtml.xsl";
48
49     private static final String REPORT_TO_HTML2_XSLT = "reportToHtml2.xsl";
50     
51     private static final String REPORT_TO_HTML_INVALID_XSLT = "reportToHtml-invalid.xsl";
52     
53     private static final String REPORT_TO_HTML_NONWELLFORMED_XSLT = "reportToHtml-nonwellformed.xsl";
54     
55     private static final String REPORT_TO_TEXT_XSLT = "reportToText.xsl";
56
57     private String getResourcePath(String aResource) { 
58         return getClass().getPackage().getName().replaceAll("\\.", "/") + "/" + aResource;
59     }
60
61     /**
62      * Transforms a file while using the default resolver, where the included
63      * file can be found. Verifies the transformation is done correctly.
64      * 
65      */
66     public void testTransformUsingDefaultResolver() throws Exception {
67         XslTransformer transformer = new XslTransformer();
68
69         InputResource xmlResource = new ClassPathResource(getResourcePath(REPORT_XML));
70         
71         Source xslt = new StreamSource(new ClassPathResource(getResourcePath(
72                         REPORT_TO_HTML_XSLT)).getInputStream());
73
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());
80
81         Document expected = DomUtils.read(new ClassPathResource(getResourcePath(
82                         "output-reportToHtml-report.xml")).getInputStream());
83
84         Document output1 = transformer.transform(documentData, xslt);
85         XmlUtils.assertEquals("byte[] transform", expected, output1);
86
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);
91
92         ByteArrayOutputStream os = new ByteArrayOutputStream();
93         Result output = new StreamResult(os);
94         
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()));
100
101         xslt = new StreamSource(new ClassPathResource(getResourcePath(
102                         REPORT_TO_HTML_XSLT)).getInputStream());
103         String result = transformer.textTransform(documentData, xslt);
104         XmlUtils
105                 .assertEquals("text transform", expected, DomUtils.read(result));
106     }
107
108     /**
109      * Transforms a file using the default resolver where the included file
110      * cannot be found. Verifies that a TransformerException is thrown.
111      * 
112      */
113     public void testTransformUsingDefaultResolverFails() throws IOException {
114         XslTransformer transformer = new XslTransformer();
115
116         InputResource xmlResource = 
117                         new ClassPathResource(getResourcePath(REPORT_XML));
118         Source xslt = new StreamSource(
119                         new ClassPathResource(getResourcePath(
120                                         REPORT_TO_HTML2_XSLT)).getInputStream());
121
122         byte[] documentData = FileSystemUtils
123                 .read(xmlResource.getInputStream()).getBytes();
124         try {
125             Document output1 = transformer.transform(documentData, xslt);
126         } catch (TransformerException e) {
127             return; // ok
128         }
129         fail();
130     }
131
132     /**
133      * Transforms a file using an invalid Xslt. Verifies that a
134      * TransformerException is thrown.
135      * 
136      */
137     public void testTransformInvalidXslt() throws IOException {
138         XslTransformer transformer = new XslTransformer();
139
140         InputResource xmlResource = new ClassPathResource(
141                         getResourcePath(REPORT_XML));
142         Source xslt = new StreamSource(
143                         new ClassPathResource(getResourcePath(REPORT_TO_HTML_INVALID_XSLT)).getInputStream());
144
145         byte[] documentData = FileSystemUtils
146                 .read(xmlResource.getInputStream()).getBytes();
147         try {
148             Document output1 = transformer.transform(documentData, xslt);
149         } catch (TransformerException e) {
150             return; // ok
151         }
152         fail();
153     }
154
155     /**
156      * Transforms a file using a non-well formed xslt. Verifies that a
157      * TransformerException is thrown.
158      * 
159      */
160     public void testTransformNonWellformedXslt() throws IOException {
161         XslTransformer transformer = new XslTransformer();
162
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());
168
169         byte[] documentData = FileSystemUtils
170                 .read(xmlResource.getInputStream()).getBytes();
171         try {
172             Document output1 = transformer.transform(documentData, xslt);
173         } catch (TransformerException e) {
174             return; // ok
175         }
176         fail();
177     }
178
179     /**
180      * Transforms a file using a class path resolver.
181      * 
182      */
183     public void testTransformUsingClassPathResolver() throws Exception {
184         XslTransformer transformer = new XslTransformer(new ClasspathUriResolver());
185
186         InputResource xmlResource = new ClassPathResource(getResourcePath(
187                         REPORT_XML));
188         Source xslt = new StreamSource(new ClassPathResource(
189                         getResourcePath(REPORT_TO_HTML2_XSLT)).getInputStream());
190
191         byte[] documentData = FileSystemUtils
192                 .read(xmlResource.getInputStream()).getBytes();
193         
194         Document output1 = transformer.transform(documentData, xslt);
195         Document expected = DomUtils.read(new ClassPathResource(
196                         getResourcePath("output-reportToHtml-report.xml"))
197                 .getInputStream());
198         XmlUtils.assertEquals("doc", expected, output1);
199     }
200
201     /**
202      * Transforms a file to text output. Verifies the file is transformed
203      * correctly.
204      * 
205      */
206     public void testTransformToTextOutput() throws Exception {
207         XslTransformer transformer = new XslTransformer(new ClasspathUriResolver());
208
209         InputResource xmlResource = new ClassPathResource(
210                         getResourcePath(REPORT_XML));
211         Source xslt = new StreamSource(
212                         new ClassPathResource(getResourcePath(REPORT_TO_TEXT_XSLT)).getInputStream());
213
214         byte[] documentData = FileSystemUtils
215                 .read(xmlResource.getInputStream()).getBytes();
216    
217         String result = transformer.textTransform(documentData, xslt);
218         String expected = "Hello world!";
219         assertEquals("text transform", expected, result);
220     }
221  
222     /**
223      * Tests resolving a file using {@link XslTransformer#resolve(String)} with the 
224      * default resolver where the file does not exist. 
225      *
226      */
227     public void testResolveWithDefaultResolverFileNotFound() { 
228         XslTransformer transformer = new XslTransformer();
229         try { 
230             Source source = transformer.resolve("org/wamblee/xml/utilities-nonexistent.xsl");
231         } catch (TransformerException e) { 
232             return; // ok
233         }
234         fail();
235     }
236     
237     
238     /**
239      * Tests resolving a file using {@link XslTransformer#resolve(String)} with the 
240      * default resolver. 
241      *
242      */
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);
251     }
252     
253 }
254
255