ClasspathUriResolver is now also an LSInputResolver so it can be used for both XSLT...
[utils] / support / general / src / test / java / org / wamblee / xml / ClasspathUriResolverTest.java
1 /*
2  * Copyright 2005-2010 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 junit.framework.TestCase;
19
20 import org.w3c.dom.ls.LSInput;
21 import org.w3c.dom.ls.LSResourceResolver;
22 import org.wamblee.io.ClassPathResource;
23 import org.wamblee.io.FileSystemUtils;
24
25 import java.io.IOException;
26
27 import javax.xml.transform.Source;
28 import javax.xml.transform.TransformerException;
29 import javax.xml.transform.URIResolver;
30 import javax.xml.transform.stream.StreamSource;
31
32 /**
33  * Tests for {@link org.wamblee.xml.ClasspathUriResolver}.
34  * 
35  * @author Erik Brakkee
36  */
37 public class ClasspathUriResolverTest extends TestCase {
38     private static final String EXISTING_RESOURCE = "org/wamblee/xml/reportToHtml.xsl";
39     private static final String NON_EXISTING_RESOURCE = "org/wamblee/xml/reportToHtml-nonexisting.xsl";
40     private ClasspathUriResolver resolver;
41
42     /*
43      * (non-Javadoc)
44      * 
45      * @see junit.framework.TestCase#setUp()
46      */
47     @Override
48     protected void setUp() throws Exception {
49         resolver = new ClasspathUriResolver();
50     }
51
52     /**
53      * Resolves an existing file. Verifies the file is resolved correctly.
54      * 
55      * @throws TransformerException
56      * @throws IOException
57      */
58     public void testResolveExistingFileURIResolver()
59         throws TransformerException, IOException {
60         Source source = resolver
61             .resolve(EXISTING_RESOURCE, "");
62         assertTrue(source instanceof StreamSource);
63
64         String resolved = FileSystemUtils.read(((StreamSource) source)
65             .getInputStream());
66
67         ClassPathResource resource = new ClassPathResource(
68             EXISTING_RESOURCE);
69         String expected = FileSystemUtils.read(resource.getInputStream());
70         assertEquals(expected, resolved);
71     }
72
73     /**
74      * Resolves a non-existing file. Verifies that a TransformerException is
75      * thrown.
76      * 
77      */
78     public void testResolveNonExistingFileURIResolver() {
79         try {
80             resolver
81                 .resolve(NON_EXISTING_RESOURCE, "");
82         } catch (TransformerException e) {
83             return; // ok
84         }
85
86         fail();
87     }
88
89     /**
90      * Resolves an existing file. Verifies the file is resolved correctly.
91      * 
92      * @throws TransformerException
93      * @throws IOException
94      */
95     public void testResolveExistingFileLSResourceResolver()
96         throws TransformerException, IOException {
97         LSInput source = resolver.resolveResource(null, null, "kees",
98             EXISTING_RESOURCE, null);
99         assertNotNull(source);
100         
101         assertEquals("kees", source.getPublicId());
102         assertEquals(EXISTING_RESOURCE, source.getSystemId());
103
104         String resolved = FileSystemUtils.read(source.getByteStream());
105
106         ClassPathResource resource = new ClassPathResource(
107             EXISTING_RESOURCE);
108         String expected = FileSystemUtils.read(resource.getInputStream());
109         assertEquals(expected, resolved);
110     }
111
112     /**
113      * Resolves a non-existing file. Verifies that a TransformerException is
114      * thrown.
115      * 
116      */
117     public void testResolveNonExistingFileLSResourceResolver() {
118         LSInput input = resolver.resolveResource(null, null, "kees",
119             NON_EXISTING_RESOURCE, null);
120         assertNull(input);
121     }
122 }