2 * Copyright 2005 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.
17 package org.wamblee.xml;
19 import java.io.ByteArrayOutputStream;
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.stream.StreamResult;
27 import javax.xml.transform.stream.StreamSource;
29 import junit.framework.TestCase;
31 import org.w3c.dom.Document;
32 import org.wamblee.io.FileSystemUtils;
33 import org.wamblee.io.InputResource;
34 import org.wamblee.io.TestResource;
37 * Tests the XSL transformer.
39 public class XslTransformerTest extends TestCase {
41 private static final String REPORT_XML = "report.xml";
42 private static final String REPORT_TO_HTML_XSLT = "reportToHtml.xsl";
45 * Transforms a file while using the default resolver,
46 * where the included file can be found.
47 * Verifies the transformation is done correctly.
50 public void testTransformUsingDefaultResolver() throws Exception {
51 XslTransformer transformer = new XslTransformer();
53 InputResource xmlResource = new TestResource(XslTransformerTest.class, REPORT_XML);
54 Source xslt = new StreamSource(new File( FileSystemUtils.getTestInputDir(XslTransformerTest.class), REPORT_TO_HTML_XSLT));
56 byte[] documentData = FileSystemUtils.read(xmlResource.getInputStream()).getBytes();
57 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
58 Document document = builder.parse(xmlResource.getInputStream());
59 Source documentSource = new StreamSource(xmlResource.getInputStream());
61 Document output1 = transformer.transform(documentData, xslt);
62 Document output2 = transformer.transform(document, xslt);
63 XmlUtils.assertEquals("output", output1, output2);
64 ByteArrayOutputStream os = new ByteArrayOutputStream();
65 Result output = new StreamResult(os);
66 transformer.transform(documentSource, output, xslt);
67 transformer.textTransform(documentData, xslt);
71 * Transforms a file using the default resolver where the
72 * included file cannot be found.
73 * Verifies that a TransformerException is thrown.
76 public void testTransformUsingDefaultResolverFails() {
81 * Transforms a file using an invalid Xslt.
82 * Verifies that a TransformerException is thrown.
85 public void testTransformInvalidXslt() {
90 * Transforms a file using a non-well formed xslt.
91 * Verifies that a TransformerException is thrown.
94 public void testTransformNonWellformedXslt() {
99 * Transforms a file using a class path resolver.
102 public void testTransformUsingClassPathResolver() {
107 * Transforms a file to text output.
108 * Verifies the file is transformed correctly.
111 public void testTransformToTextOutput() {