6e30e5fd3f0fcb90a45b0434789a5afd9874309b
[utils] / support / general / src / test / java / org / wamblee / xml / XmlDocumentTest.java
1 /*
2  * Copyright 2005-2011 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.xml;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.ByteArrayOutputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.net.URI;
23 import java.net.URISyntaxException;
24
25 import org.junit.Test;
26 import org.w3c.dom.Document;
27 import org.w3c.dom.Element;
28 import org.w3c.dom.Node;
29
30 import static junit.framework.TestCase.*;
31
32 import static org.wamblee.xml.XMLDocument.*;
33
34 public class XmlDocumentTest {
35     
36     private InputStream getResource(String aResource) { 
37         return getClass().getResourceAsStream(aResource);
38     }
39
40     @Test
41     public void testReadWriteFromInputStream() throws Exception { 
42         XMLDocument xmldoc = xmldocument("test.xml", getResource("test.xml"));
43         
44         Document doc = xmldoc.getDocument();
45         Element element = doc.getDocumentElement();
46         assertEquals("http://wamblee.org/test", element.getNamespaceURI());
47         
48         String val = xmldoc.print(false);
49         // parse the written document
50         Document doc2 = xmldocument("val.xml", val).getDocument();
51         XmlUtils.assertEquals("", doc, doc2);
52     }
53     
54     @Test
55     public void testReadWriteFromUri() throws XMLException, URISyntaxException, IOException { 
56         XMLDocument xmldoc = xmldocument( getClass().getResource("test.xml").toURI());
57         Node node = xmldoc.getDocument().getFirstChild();
58         assertEquals("root", node.getLocalName());
59     }
60     
61     @Test
62     public void testReadWriteFromClassPath() throws XMLException { 
63         XMLDocument xmldoc = xmldocument("org/wamblee/xml/test.xml", new ClasspathUriResolver());
64         Node node = xmldoc.getDocument().getFirstChild();
65         assertEquals("root", node.getLocalName());
66     }
67     
68     @Test(expected = XMLException.class)
69     public void testReadNotWellFormed() throws Exception { 
70         xmldocument("testNotWellFormed.xml", getResource("testNotWellFormed.xml"));
71     }
72 }