added unit tests for schema validation and a test for not wellformed XML.
authorErik Brakkee <erik@brakkee.org>
Tue, 22 Feb 2011 20:15:17 +0000 (20:15 +0000)
committerErik Brakkee <erik@brakkee.org>
Tue, 22 Feb 2011 20:15:17 +0000 (20:15 +0000)
support/general/src/test/java/org/wamblee/xml/DomUtilsTest.java
support/general/src/test/resources/org/wamblee/xml/test.xsd [new file with mode: 0644]
support/general/src/test/resources/org/wamblee/xml/testInvalid.xml [new file with mode: 0644]
support/general/src/test/resources/org/wamblee/xml/testNotWellFormed.xml [new file with mode: 0644]

index 86fe91596fa93ae01fb9bdde85ac665b5cfce3a1..d85f44646b0ed5751e6ad1c3f51a278117695aa8 100644 (file)
@@ -16,6 +16,7 @@
 package org.wamblee.xml;
 
 import java.io.ByteArrayInputStream;
+import java.io.InputStream;
 
 import org.junit.Test;
 import org.w3c.dom.Document;
@@ -24,10 +25,14 @@ import org.w3c.dom.Element;
 import static junit.framework.TestCase.*;
 
 public class DomUtilsTest {
+    
+    private InputStream getResource(String aResource) { 
+        return getClass().getResourceAsStream(aResource);
+    }
 
     @Test
     public void testReadWrite() throws Exception { 
-        Document doc = DomUtils.read(getClass().getResourceAsStream("test.xml"));
+        Document doc = DomUtils.read(getResource("test.xml"));
         Element element = doc.getDocumentElement();
         assertEquals("http://wamblee.org/test", element.getNamespaceURI());
         String val = DomUtils.serialize(doc);
@@ -35,4 +40,25 @@ public class DomUtilsTest {
         Document doc2 = DomUtils.read(new ByteArrayInputStream(val.getBytes()));
         XmlUtils.assertEquals("", doc, doc2);
     }
+    
+    @Test(expected = XMLException.class)
+    public void testReadNotWellFormed() throws Exception { 
+        Document doc = DomUtils.read(getResource("testNotWellFormed.xml"));
+    }
+    
+    @Test
+    public void testReadAndValidateOk() throws Exception { 
+        Document doc = DomUtils.readAndValidate(getResource("test.xml"), getResource("test.xsd"));
+        Element element = doc.getDocumentElement();
+        assertEquals("http://wamblee.org/test", element.getNamespaceURI());
+        String val = DomUtils.serialize(doc);
+        // parse the written document
+        Document doc2 = DomUtils.read(new ByteArrayInputStream(val.getBytes()));
+        XmlUtils.assertEquals("", doc, doc2);
+    }
+    
+    @Test(expected = XMLException.class)
+    public void testReadAndValidateNotOk() throws Exception { 
+        Document doc = DomUtils.readAndValidate(getResource("testInvalid.xml"), getResource("test.xsd"));
+    }
 }
diff --git a/support/general/src/test/resources/org/wamblee/xml/test.xsd b/support/general/src/test/resources/org/wamblee/xml/test.xsd
new file mode 100644 (file)
index 0000000..547837e
--- /dev/null
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://wamblee.org/test"
+    elementFormDefault="qualified">
+    
+    <xs:element name="root">
+        <xs:complexType>
+            <xs:sequence>
+                <xs:element name="elem"/>
+            </xs:sequence>
+        </xs:complexType>
+    </xs:element>
+    
+</xs:schema>
diff --git a/support/general/src/test/resources/org/wamblee/xml/testInvalid.xml b/support/general/src/test/resources/org/wamblee/xml/testInvalid.xml
new file mode 100644 (file)
index 0000000..9770b31
--- /dev/null
@@ -0,0 +1,3 @@
+<x:root xmlns:x="http://wamblee.org/test">
+    <x:elem2 attr="5">hallo</x:elem2>
+</x:root>
\ No newline at end of file
diff --git a/support/general/src/test/resources/org/wamblee/xml/testNotWellFormed.xml b/support/general/src/test/resources/org/wamblee/xml/testNotWellFormed.xml
new file mode 100644 (file)
index 0000000..836db2d
--- /dev/null
@@ -0,0 +1,3 @@
+<x:root xmlns:x="http://wamblee.org/test">
+    <x:elem attr="5">hallo</x:ele>
+</x:root>
\ No newline at end of file