2 * Copyright 2005-2011 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.xml;
18 import java.io.ByteArrayInputStream;
19 import java.io.ByteArrayOutputStream;
20 import java.io.IOException;
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;
30 import junit.framework.TestCase;
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;
38 import static org.wamblee.xml.XMLDocument.*;
40 import static junit.framework.TestCase.*;
43 * Tests the XSL transformation
45 * @author Erik Brakkee
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";
57 private String getResourcePath(String aResource) {
58 return getClass().getPackage().getName().replaceAll("\\.", "/") + "/" +
63 * Transforms a file while using the default resolver, where the included
64 * file can be found. Verifies the transformation is done correctly.
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());
72 Document expected = xmldocument(getClass().getResource("output-reportToHtml-report.xml").toURI()).getDocument();
74 Document output = transformed.getDocument();
75 XmlUtils.assertEquals("byte[] transform", expected, output);
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()));
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();
89 .assertEquals("text transform", expected, xmldocTransformed.getDocument());
93 * Transforms a file using the default resolver where the included file
94 * cannot be found. Verifies that a TransformerException is thrown.
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());
104 * Transforms a file using an invalid Xslt. Verifies that a
105 * TransformerException is thrown.
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());
115 * Transforms a file using a non-well formed xslt. Verifies that a
116 * TransformerException is thrown.
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());
126 * Transforms a file using a class path resolver.
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());
134 Document output1 = transformed.getDocument();
136 Document expected = xmldocument(getClass().getResource("output-reportToHtml-report.xml").toURI()).getDocument();
137 XmlUtils.assertEquals("doc", expected, output1);
141 * Transforms a file to text output. Verifies the file is transformed
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()));
150 String expected = "Hello world!";
151 assertEquals("text transform", expected, transformed);
155 * Tests resolving a file with
156 * the default resolver where the file does not exist.
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());