From: Erik Brakkee Date: Tue, 22 Feb 2011 20:15:17 +0000 (+0000) Subject: added unit tests for schema validation and a test for not wellformed XML. X-Git-Tag: wamblee-utils-0.7~34 X-Git-Url: http://wamblee.org/gitweb/?a=commitdiff_plain;h=ed3ce3eb0c855255c91e974f07aef94f38009d70;p=utils added unit tests for schema validation and a test for not wellformed XML. --- diff --git a/support/general/src/test/java/org/wamblee/xml/DomUtilsTest.java b/support/general/src/test/java/org/wamblee/xml/DomUtilsTest.java index 86fe9159..d85f4464 100644 --- a/support/general/src/test/java/org/wamblee/xml/DomUtilsTest.java +++ b/support/general/src/test/java/org/wamblee/xml/DomUtilsTest.java @@ -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 index 00000000..547837e8 --- /dev/null +++ b/support/general/src/test/resources/org/wamblee/xml/test.xsd @@ -0,0 +1,13 @@ + + + + + + + + + + + + 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 index 00000000..9770b31d --- /dev/null +++ b/support/general/src/test/resources/org/wamblee/xml/testInvalid.xml @@ -0,0 +1,3 @@ + + hallo + \ 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 index 00000000..836db2dc --- /dev/null +++ b/support/general/src/test/resources/org/wamblee/xml/testNotWellFormed.xml @@ -0,0 +1,3 @@ + + hallo + \ No newline at end of file