(no commit message)
[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 static org.wamblee.xml.XMLDocument.*;
19 import junit.framework.TestCase;
20
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;
27
28 import java.io.IOException;
29
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;
34
35 import static junit.framework.TestCase.*;
36
37 /**
38  * Tests for {@link org.wamblee.xml.ClasspathUriResolver}.
39  * 
40  * @author Erik Brakkee
41  */
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;
48
49     /*
50      * (non-Javadoc)
51      * 
52      * @see junit.framework.TestCase#setUp()
53      */
54     @Before
55     public void setUp() throws Exception {
56         resolver = new ClasspathUriResolver();
57     }
58
59     /**
60      * Resolves a non-existing file. Verifies that a TransformerException is
61      * thrown.
62      * 
63      */
64     @Test
65     public void testResolveNonExistingFileURIResolver() {
66         try {
67             resolver
68                 .resolve(NON_EXISTING_RESOURCE, "");
69         } catch (TransformerException e) {
70             return; // ok
71         }
72
73         fail();
74     }
75
76     /**
77      * Resolves an existing file. Verifies the file is resolved correctly.
78      * 
79      * @throws TransformerException
80      * @throws IOException
81      */
82     @Test
83     public void testResolveExistingFileLSResourceResolver()
84         throws TransformerException, IOException {
85         LSInput source = resolver.resolveResource(null, null, "kees",
86             EXISTING_RESOURCE, null);
87         assertNotNull(source);
88         
89         assertEquals("kees", source.getPublicId());
90         assertEquals(EXISTING_RESOURCE, source.getSystemId());
91
92         String resolved = FileSystemUtils.read(source.getByteStream());
93
94         ClassPathResource resource = new ClassPathResource(
95             EXISTING_RESOURCE);
96         String expected = FileSystemUtils.read(resource.getInputStream());
97         assertEquals(expected, resolved);
98     }
99     
100     /**
101      * Resolves an existing file using a base path. Verifies the file is resolved correctly.
102      * 
103      * @throws TransformerException
104      * @throws IOException
105      */
106     @Test
107     public void testResolveExistingFileLSResourceResolverUsingBasePath()
108         throws TransformerException, IOException {
109         
110         resolver = new ClasspathUriResolver(PREFIX);
111         LSInput source = resolver.resolveResource(null, null, "kees",
112             REPORT_TO_HTML_XSL, null);
113         assertNotNull(source);
114         
115         assertEquals("kees", source.getPublicId());
116         assertEquals(REPORT_TO_HTML_XSL, source.getSystemId());
117
118         String resolved = FileSystemUtils.read(source.getByteStream());
119
120         ClassPathResource resource = new ClassPathResource(
121             EXISTING_RESOURCE);
122         String expected = FileSystemUtils.read(resource.getInputStream());
123         assertEquals(expected, resolved);
124     }
125
126     /**
127      * Resolves a non-existing file. Verifies that a TransformerException is
128      * thrown.
129      * 
130      */
131     @Test
132     public void testResolveNonExistingFileLSResourceResolver() {
133         LSInput input = resolver.resolveResource(null, null, "kees",
134             NON_EXISTING_RESOURCE, null);
135         assertNull(input);
136     }
137 }