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