added XMLDocument, XMLSchema, and XSLTransformation classes.
[utils] / support / general / src / test / java / org / wamblee / xml / ClasspathUriResolverTest.java
index e48b5cc3662b97bd549399a67b852411c43aff27..f406c69d5820bbc261e677160747194985c59c70 100644 (file)
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
- */ 
+ */
 package org.wamblee.xml;
 
+import static org.wamblee.xml.XMLDocument.*;
 import junit.framework.TestCase;
 
+import org.w3c.dom.ls.LSInput;
+import org.w3c.dom.ls.LSResourceResolver;
 import org.wamblee.io.ClassPathResource;
 import org.wamblee.io.FileSystemUtils;
 
@@ -33,7 +36,11 @@ import javax.xml.transform.stream.StreamSource;
  * @author Erik Brakkee
  */
 public class ClasspathUriResolverTest extends TestCase {
-    private URIResolver resolver;
+    private static final String REPORT_TO_HTML_XSL = "reportToHtml.xsl";
+    private static final String PREFIX = "org/wamblee/xml";
+    private static final String EXISTING_RESOURCE = PREFIX + "/" + REPORT_TO_HTML_XSL;
+    private static final String NON_EXISTING_RESOURCE = "org/wamblee/xml/reportToHtml-nonexisting.xsl";
+    private ClasspathUriResolver resolver;
 
     /*
      * (non-Javadoc)
@@ -45,23 +52,66 @@ public class ClasspathUriResolverTest extends TestCase {
         resolver = new ClasspathUriResolver();
     }
 
+    /**
+     * Resolves a non-existing file. Verifies that a TransformerException is
+     * thrown.
+     * 
+     */
+    public void testResolveNonExistingFileURIResolver() {
+        try {
+            resolver
+                .resolve(NON_EXISTING_RESOURCE, "");
+        } catch (TransformerException e) {
+            return; // ok
+        }
+
+        fail();
+    }
+
     /**
      * Resolves an existing file. Verifies the file is resolved correctly.
      * 
      * @throws TransformerException
      * @throws IOException
      */
-    public void testResolveExistingFile() throws TransformerException,
-        IOException {
-        Source source = resolver
-            .resolve("org/wamblee/xml/reportToHtml.xsl", "");
-        assertTrue(source instanceof StreamSource);
+    public void testResolveExistingFileLSResourceResolver()
+        throws TransformerException, IOException {
+        LSInput source = resolver.resolveResource(null, null, "kees",
+            EXISTING_RESOURCE, null);
+        assertNotNull(source);
+        
+        assertEquals("kees", source.getPublicId());
+        assertEquals(EXISTING_RESOURCE, source.getSystemId());
 
-        String resolved = FileSystemUtils.read(((StreamSource) source)
-            .getInputStream());
+        String resolved = FileSystemUtils.read(source.getByteStream());
 
         ClassPathResource resource = new ClassPathResource(
-            "org/wamblee/xml/reportToHtml.xsl");
+            EXISTING_RESOURCE);
+        String expected = FileSystemUtils.read(resource.getInputStream());
+        assertEquals(expected, resolved);
+    }
+    
+    /**
+     * Resolves an existing file using a base path. Verifies the file is resolved correctly.
+     * 
+     * @throws TransformerException
+     * @throws IOException
+     */
+    public void testResolveExistingFileLSResourceResolverUsingBasePath()
+        throws TransformerException, IOException {
+        
+        resolver = new ClasspathUriResolver(PREFIX);
+        LSInput source = resolver.resolveResource(null, null, "kees",
+            REPORT_TO_HTML_XSL, null);
+        assertNotNull(source);
+        
+        assertEquals("kees", source.getPublicId());
+        assertEquals(REPORT_TO_HTML_XSL, source.getSystemId());
+
+        String resolved = FileSystemUtils.read(source.getByteStream());
+
+        ClassPathResource resource = new ClassPathResource(
+            EXISTING_RESOURCE);
         String expected = FileSystemUtils.read(resource.getInputStream());
         assertEquals(expected, resolved);
     }
@@ -71,14 +121,9 @@ public class ClasspathUriResolverTest extends TestCase {
      * thrown.
      * 
      */
-    public void testResolveNonExistingFile() {
-        try {
-            Source source = resolver.resolve(
-                "org/wamblee/xml/reportToHtml-nonexisting.xsl", "");
-        } catch (TransformerException e) {
-            return; // ok
-        }
-
-        fail();
+    public void testResolveNonExistingFileLSResourceResolver() {
+        LSInput input = resolver.resolveResource(null, null, "kees",
+            NON_EXISTING_RESOURCE, null);
+        assertNull(input);
     }
 }