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