01c904795151574e7c1ad6848c7e200f729e4e78
[utils] / support / general / src / test / java / org / wamblee / xml / ClasspathUriResolverTest.java
1 /*
2  * Copyright 2005 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.wamblee.io.ClassPathResource;
21 import org.wamblee.io.FileSystemUtils;
22
23 import java.io.IOException;
24
25 import javax.xml.transform.Source;
26 import javax.xml.transform.TransformerException;
27 import javax.xml.transform.URIResolver;
28 import javax.xml.transform.stream.StreamSource;
29
30
31 /**
32  * Tests for {@link org.wamblee.xml.ClasspathUriResolver}.
33  *
34  * @author Erik Brakkee
35  */
36 public class ClasspathUriResolverTest extends TestCase {
37     private URIResolver resolver;
38
39     /* (non-Javadoc)
40      * @see junit.framework.TestCase#setUp()
41      */
42     @Override
43     protected void setUp() throws Exception {
44         resolver = new ClasspathUriResolver();
45     }
46
47     /**
48      * Resolves an existing file. Verifies the file is resolved correctly.
49      * @throws TransformerException
50      * @throws IOException
51      */
52     public void testResolveExistingFile()
53         throws TransformerException, IOException {
54         Source source = resolver.resolve("org/wamblee/xml/reportToHtml.xsl", "");
55         assertTrue(source instanceof StreamSource);
56
57         String resolved = FileSystemUtils.read(((StreamSource) source).getInputStream());
58
59         ClassPathResource resource = new ClassPathResource(
60                 "org/wamblee/xml/reportToHtml.xsl");
61         String expected = FileSystemUtils.read(resource.getInputStream());
62         assertEquals(expected, resolved);
63     }
64
65     /**
66      * Resolves a non-existing file. Verifies that a TransformerException is thrown.
67      *
68      */
69     public void testResolveNonExistingFile() {
70         try {
71             Source source = resolver.resolve("org/wamblee/xml/reportToHtml-nonexisting.xsl",
72                     "");
73         } catch (TransformerException e) {
74             return; // ok
75         }
76
77         fail();
78     }
79 }