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.junit.Before;
22 import org.junit.Test;
23 import org.w3c.dom.ls.LSInput;
24 import org.w3c.dom.ls.LSResourceResolver;
25 import org.wamblee.io.ClassPathResource;
26 import org.wamblee.io.FileSystemUtils;
28 import java.io.IOException;
30 import javax.xml.transform.Source;
31 import javax.xml.transform.TransformerException;
32 import javax.xml.transform.URIResolver;
33 import javax.xml.transform.stream.StreamSource;
35 import static junit.framework.TestCase.*;
38 * Tests for {@link org.wamblee.xml.ClasspathUriResolver}.
40 * @author Erik Brakkee
42 public class ClasspathUriResolverTest {
43 private static final String REPORT_TO_HTML_XSL = "reportToHtml.xsl";
44 private static final String PREFIX = "org/wamblee/xml";
45 private static final String EXISTING_RESOURCE = PREFIX + "/" + REPORT_TO_HTML_XSL;
46 private static final String NON_EXISTING_RESOURCE = "org/wamblee/xml/reportToHtml-nonexisting.xsl";
47 private ClasspathUriResolver resolver;
52 * @see junit.framework.TestCase#setUp()
55 public void setUp() throws Exception {
56 resolver = new ClasspathUriResolver();
60 * Resolves a non-existing file. Verifies that a TransformerException is
65 public void testResolveNonExistingFileURIResolver() {
68 .resolve(NON_EXISTING_RESOURCE, "");
69 } catch (TransformerException e) {
77 * Resolves an existing file. Verifies the file is resolved correctly.
79 * @throws TransformerException
83 public void testResolveExistingFileLSResourceResolver()
84 throws TransformerException, IOException {
85 LSInput source = resolver.resolveResource(null, null, "kees",
86 EXISTING_RESOURCE, null);
87 assertNotNull(source);
89 assertEquals("kees", source.getPublicId());
90 assertEquals(EXISTING_RESOURCE, source.getSystemId());
92 String resolved = FileSystemUtils.read(source.getByteStream());
94 ClassPathResource resource = new ClassPathResource(
96 String expected = FileSystemUtils.read(resource.getInputStream());
97 assertEquals(expected, resolved);
101 * Resolves an existing file using a base path. Verifies the file is resolved correctly.
103 * @throws TransformerException
104 * @throws IOException
107 public void testResolveExistingFileLSResourceResolverUsingBasePath()
108 throws TransformerException, IOException {
110 resolver = new ClasspathUriResolver(PREFIX);
111 LSInput source = resolver.resolveResource(null, null, "kees",
112 REPORT_TO_HTML_XSL, null);
113 assertNotNull(source);
115 assertEquals("kees", source.getPublicId());
116 assertEquals(REPORT_TO_HTML_XSL, source.getSystemId());
118 String resolved = FileSystemUtils.read(source.getByteStream());
120 ClassPathResource resource = new ClassPathResource(
122 String expected = FileSystemUtils.read(resource.getInputStream());
123 assertEquals(expected, resolved);
127 * Resolves a non-existing file. Verifies that a TransformerException is
132 public void testResolveNonExistingFileLSResourceResolver() {
133 LSInput input = resolver.resolveResource(null, null, "kees",
134 NON_EXISTING_RESOURCE, null);