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