package org.wamblee.xml;
import java.io.ByteArrayInputStream;
+import java.io.InputStream;
import org.junit.Test;
import org.w3c.dom.Document;
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);
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"));
+ }
}