2 * Copyright 2005-2010 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 static org.wamblee.xml.XMLDocument.*;
19 import junit.framework.TestCase;
21 import org.w3c.dom.ls.LSInput;
22 import org.w3c.dom.ls.LSResourceResolver;
23 import org.wamblee.io.ClassPathResource;
24 import org.wamblee.io.FileSystemUtils;
26 import java.io.IOException;
28 import javax.xml.transform.Source;
29 import javax.xml.transform.TransformerException;
30 import javax.xml.transform.URIResolver;
31 import javax.xml.transform.stream.StreamSource;
34 * Tests for {@link org.wamblee.xml.ClasspathUriResolver}.
36 * @author Erik Brakkee
38 public class ClasspathUriResolverTest extends TestCase {
39 private static final String REPORT_TO_HTML_XSL = "reportToHtml.xsl";
40 private static final String PREFIX = "org/wamblee/xml";
41 private static final String EXISTING_RESOURCE = PREFIX + "/" + REPORT_TO_HTML_XSL;
42 private static final String NON_EXISTING_RESOURCE = "org/wamblee/xml/reportToHtml-nonexisting.xsl";
43 private ClasspathUriResolver resolver;
48 * @see junit.framework.TestCase#setUp()
51 protected void setUp() throws Exception {
52 resolver = new ClasspathUriResolver();
56 * Resolves a non-existing file. Verifies that a TransformerException is
60 public void testResolveNonExistingFileURIResolver() {
63 .resolve(NON_EXISTING_RESOURCE, "");
64 } catch (TransformerException e) {
72 * Resolves an existing file. Verifies the file is resolved correctly.
74 * @throws TransformerException
77 public void testResolveExistingFileLSResourceResolver()
78 throws TransformerException, IOException {
79 LSInput source = resolver.resolveResource(null, null, "kees",
80 EXISTING_RESOURCE, null);
81 assertNotNull(source);
83 assertEquals("kees", source.getPublicId());
84 assertEquals(EXISTING_RESOURCE, source.getSystemId());
86 String resolved = FileSystemUtils.read(source.getByteStream());
88 ClassPathResource resource = new ClassPathResource(
90 String expected = FileSystemUtils.read(resource.getInputStream());
91 assertEquals(expected, resolved);
95 * Resolves an existing file using a base path. Verifies the file is resolved correctly.
97 * @throws TransformerException
100 public void testResolveExistingFileLSResourceResolverUsingBasePath()
101 throws TransformerException, IOException {
103 resolver = new ClasspathUriResolver(PREFIX);
104 LSInput source = resolver.resolveResource(null, null, "kees",
105 REPORT_TO_HTML_XSL, null);
106 assertNotNull(source);
108 assertEquals("kees", source.getPublicId());
109 assertEquals(REPORT_TO_HTML_XSL, source.getSystemId());
111 String resolved = FileSystemUtils.read(source.getByteStream());
113 ClassPathResource resource = new ClassPathResource(
115 String expected = FileSystemUtils.read(resource.getInputStream());
116 assertEquals(expected, resolved);
120 * Resolves a non-existing file. Verifies that a TransformerException is
124 public void testResolveNonExistingFileLSResourceResolver() {
125 LSInput input = resolver.resolveResource(null, null, "kees",
126 NON_EXISTING_RESOURCE, null);