(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 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  * Tests for {@link org.wamblee.xml.ClasspathUriResolver}.
32  * 
33  * @author Erik Brakkee
34  */
35 public class ClasspathUriResolverTest extends TestCase {
36     private URIResolver resolver;
37
38     /*
39      * (non-Javadoc)
40      * 
41      * @see junit.framework.TestCase#setUp()
42      */
43     @Override
44     protected void setUp() throws Exception {
45         resolver = new ClasspathUriResolver();
46     }
47
48     /**
49      * Resolves an existing file. Verifies the file is resolved correctly.
50      * 
51      * @throws TransformerException
52      * @throws IOException
53      */
54     public void testResolveExistingFile() throws TransformerException,
55         IOException {
56         Source source = resolver
57             .resolve("org/wamblee/xml/reportToHtml.xsl", "");
58         assertTrue(source instanceof StreamSource);
59
60         String resolved = FileSystemUtils.read(((StreamSource) source)
61             .getInputStream());
62
63         ClassPathResource resource = new ClassPathResource(
64             "org/wamblee/xml/reportToHtml.xsl");
65         String expected = FileSystemUtils.read(resource.getInputStream());
66         assertEquals(expected, resolved);
67     }
68
69     /**
70      * Resolves a non-existing file. Verifies that a TransformerException is
71      * thrown.
72      * 
73      */
74     public void testResolveNonExistingFile() {
75         try {
76             resolver
77                 .resolve("org/wamblee/xml/reportToHtml-nonexisting.xsl", "");
78         } catch (TransformerException e) {
79             return; // ok
80         }
81
82         fail();
83     }
84 }