X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Fgeneral%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fxml%2FXmlDocumentTest.java;fp=support%2Fgeneral%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fxml%2FXmlDocumentTest.java;h=6e30e5fd3f0fcb90a45b0434789a5afd9874309b;hb=2136b85d7bde33a277d1dfd58b048ee6e5f5db8b;hp=0000000000000000000000000000000000000000;hpb=df850821cf26ca07ef2474d4d2cabb61e9104291;p=utils diff --git a/support/general/src/test/java/org/wamblee/xml/XmlDocumentTest.java b/support/general/src/test/java/org/wamblee/xml/XmlDocumentTest.java new file mode 100644 index 00000000..6e30e5fd --- /dev/null +++ b/support/general/src/test/java/org/wamblee/xml/XmlDocumentTest.java @@ -0,0 +1,72 @@ +/* + * Copyright 2005-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * 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 java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.net.URISyntaxException; + +import org.junit.Test; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; + +import static junit.framework.TestCase.*; + +import static org.wamblee.xml.XMLDocument.*; + +public class XmlDocumentTest { + + private InputStream getResource(String aResource) { + return getClass().getResourceAsStream(aResource); + } + + @Test + public void testReadWriteFromInputStream() throws Exception { + XMLDocument xmldoc = xmldocument("test.xml", getResource("test.xml")); + + Document doc = xmldoc.getDocument(); + Element element = doc.getDocumentElement(); + assertEquals("http://wamblee.org/test", element.getNamespaceURI()); + + String val = xmldoc.print(false); + // parse the written document + Document doc2 = xmldocument("val.xml", val).getDocument(); + XmlUtils.assertEquals("", doc, doc2); + } + + @Test + public void testReadWriteFromUri() throws XMLException, URISyntaxException, IOException { + XMLDocument xmldoc = xmldocument( getClass().getResource("test.xml").toURI()); + Node node = xmldoc.getDocument().getFirstChild(); + assertEquals("root", node.getLocalName()); + } + + @Test + public void testReadWriteFromClassPath() throws XMLException { + XMLDocument xmldoc = xmldocument("org/wamblee/xml/test.xml", new ClasspathUriResolver()); + Node node = xmldoc.getDocument().getFirstChild(); + assertEquals("root", node.getLocalName()); + } + + @Test(expected = XMLException.class) + public void testReadNotWellFormed() throws Exception { + xmldocument("testNotWellFormed.xml", getResource("testNotWellFormed.xml")); + } +}