added XMLDocument, XMLSchema, and XSLTransformation classes.
[utils] / support / general / src / test / java / org / wamblee / xml / XSLTransformationTest.java
1 /*
2  * Copyright 2005-2011 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 java.io.ByteArrayInputStream;
19 import java.io.ByteArrayOutputStream;
20 import java.io.IOException;
21
22 import javax.xml.parsers.DocumentBuilder;
23 import javax.xml.parsers.DocumentBuilderFactory;
24 import javax.xml.transform.Result;
25 import javax.xml.transform.Source;
26 import javax.xml.transform.TransformerException;
27 import javax.xml.transform.stream.StreamResult;
28 import javax.xml.transform.stream.StreamSource;
29
30 import junit.framework.TestCase;
31
32 import org.junit.Test;
33 import org.w3c.dom.Document;
34 import org.wamblee.io.ClassPathResource;
35 import org.wamblee.io.FileSystemUtils;
36 import org.wamblee.io.InputResource;
37
38 import static org.wamblee.xml.XMLDocument.*;
39
40 import static junit.framework.TestCase.*;
41
42 /**
43  * Tests the XSL transformation
44  * 
45  * @author Erik Brakkee
46  */
47 public class XSLTransformationTest  {
48     private static final String PREFIX = "org/wamblee/xml/";
49     private static final String INCLUDED_XSL_FILE = "utilities.xsl";
50     private static final String REPORT_XML = "report.xml";
51     private static final String REPORT_TO_HTML_XSLT = "reportToHtml.xsl";
52     private static final String REPORT_TO_HTML2_XSLT = "reportToHtml2.xsl";
53     private static final String REPORT_TO_HTML_INVALID_XSLT = "reportToHtml-invalid.xsl";
54     private static final String REPORT_TO_HTML_NONWELLFORMED_XSLT = "reportToHtml-nonwellformed.xsl";
55     private static final String REPORT_TO_TEXT_XSLT = "reportToText.xsl";
56
57     private String getResourcePath(String aResource) {
58         return getClass().getPackage().getName().replaceAll("\\.", "/") + "/" +
59             aResource;
60     }
61
62     /**
63      * Transforms a file while using the default resolver, where the included
64      * file can be found. Verifies the transformation is done correctly.
65      * 
66      */
67     @Test
68     public void testTransformUsingDefaultResolver() throws Exception {
69         XMLDocument xmldoc = xmldocument(PREFIX + REPORT_XML, new ClasspathUriResolver());
70         XMLDocument transformed = xmldoc.transform(getClass().getResource(REPORT_TO_HTML_XSLT).toURI());
71        
72         Document expected = xmldocument(getClass().getResource("output-reportToHtml-report.xml").toURI()).getDocument();
73
74         Document output = transformed.getDocument();
75         XmlUtils.assertEquals("byte[] transform", expected, output);
76     }
77     
78     @Test
79     public void testTransformUsingDefaultResolverTextTransform() throws Exception {
80         XMLDocument xmldoc = xmldocument(PREFIX + REPORT_XML, new ClasspathUriResolver());
81         String transformed = xmldoc.write(new XSLTransformation(getClass().getResource(REPORT_TO_HTML_XSLT).toURI()));
82        
83         XMLDocument xmldocTransformed = xmldocument("read-transformed-text", new ByteArrayInputStream(transformed.getBytes()));
84         Document expected = xmldocument("output-reportToHtml-report.xml", 
85             new ClassPathResource(
86                 getResourcePath("output-reportToHtml-report.xml"))
87                 .getInputStream()).getDocument();
88         XmlUtils
89             .assertEquals("text transform", expected, xmldocTransformed.getDocument());
90     }
91    
92     /**
93      * Transforms a file using the default resolver where the included file
94      * cannot be found. Verifies that a TransformerException is thrown.
95      * 
96      */
97     @Test(expected = XMLException.class)
98     public void testTransformUsingDefaultResolverFails() throws Exception {
99         XMLDocument xmldoc = xmldocument(PREFIX + REPORT_XML, new ClasspathUriResolver());
100         XMLDocument transformed = xmldoc.transform(getClass().getResource(REPORT_TO_HTML2_XSLT).toURI());
101     }
102
103     /**
104      * Transforms a file using an invalid Xslt. Verifies that a
105      * TransformerException is thrown.
106      * 
107      */
108     @Test(expected = XMLException.class)
109     public void testTransformInvalidXslt() throws Exception {
110         XMLDocument xmldoc = xmldocument(PREFIX + REPORT_XML, new ClasspathUriResolver());
111         XMLDocument transformed = xmldoc.transform(getClass().getResource(REPORT_TO_HTML_INVALID_XSLT).toURI());
112     }
113
114     /**
115      * Transforms a file using a non-well formed xslt. Verifies that a
116      * TransformerException is thrown.
117      * 
118      */
119     @Test(expected=XMLException.class)
120     public void testTransformNonWellformedXslt() throws Exception {
121         XMLDocument xmldoc = xmldocument(PREFIX + REPORT_XML, new ClasspathUriResolver());
122         XMLDocument transformed = xmldoc.transform(getClass().getResource(REPORT_TO_HTML_NONWELLFORMED_XSLT).toURI());
123     }
124
125     /**
126      * Transforms a file using a class path resolver.
127      * 
128      */
129     @Test
130     public void testTransformUsingClassPathResolver() throws Exception {
131         XMLDocument xmldoc = xmldocument(PREFIX + REPORT_XML, new ClasspathUriResolver());
132         XMLDocument transformed = xmldoc.transform(PREFIX + REPORT_TO_HTML2_XSLT, new ClasspathUriResolver());
133  
134         Document output1 = transformed.getDocument();
135         
136         Document expected = xmldocument(getClass().getResource("output-reportToHtml-report.xml").toURI()).getDocument();
137         XmlUtils.assertEquals("doc", expected, output1);
138     }
139
140     /**
141      * Transforms a file to text output. Verifies the file is transformed
142      * correctly.
143      * 
144      */
145     @Test
146     public void testTransformToTextOutput() throws Exception {
147         XMLDocument xmldoc = xmldocument(PREFIX + REPORT_XML, new ClasspathUriResolver());
148         String transformed = xmldoc.write(new XSLTransformation(PREFIX + REPORT_TO_TEXT_XSLT, new ClasspathUriResolver()));
149      
150         String expected = "Hello world!";
151         assertEquals("text transform", expected, transformed);
152     }
153
154     /**
155      * Tests resolving a file using {@link XslTransformer#resolve(String)} with
156      * the default resolver where the file does not exist.
157      * 
158      */
159     @Test(expected = XMLException.class)
160     public void testResolveWithDefaultResolverFileNotFound() throws Exception {
161         XMLDocument xmldoc = xmldocument(PREFIX + REPORT_XML, new ClasspathUriResolver());
162         XMLDocument transformed = xmldoc.transform(PREFIX + "utilities-nonexistent.xsl", new ClasspathUriResolver());
163     }
164 }