1/* 2 * Copyright (C) 2007 The Android Open Source Project 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 17package libcore.xml; 18 19import javax.xml.parsers.DocumentBuilder; 20import javax.xml.parsers.DocumentBuilderFactory; 21import junit.framework.TestCase; 22import org.w3c.dom.Comment; 23import org.w3c.dom.Document; 24import org.w3c.dom.Element; 25import org.w3c.dom.Node; 26import org.w3c.dom.NodeList; 27import org.w3c.dom.ProcessingInstruction; 28import org.w3c.dom.Text; 29 30public class SimpleBuilderTest extends TestCase { 31 32 private DocumentBuilder builder; 33 34 protected void setUp() throws Exception { 35 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 36 factory.setValidating(false); 37 factory.setNamespaceAware(true); 38 39 builder = factory.newDocumentBuilder(); 40 } 41 42 protected void tearDown() throws Exception { 43 builder = null; 44 } 45 46 private String getTextContent(Node node) { 47 String result = (node instanceof Text ? ((Text) node).getData() : ""); 48 49 Node child = node.getFirstChild(); 50 while (child != null) { 51 result = result + getTextContent(child); 52 child = child.getNextSibling(); 53 } 54 55 return result; 56 } 57 public void testGoodFile1() throws Exception { 58 Document document = builder.parse(getClass().getResourceAsStream( 59 "/SimpleBuilderTest.xml")); 60 61 Element root = document.getDocumentElement(); 62 assertNotNull(root); 63 assertEquals("http://www.foo.bar", root.getNamespaceURI()); 64 assertEquals("t", root.getPrefix()); 65 assertEquals("stuff", root.getLocalName()); 66 67 NodeList list = root.getElementsByTagName("nestedStuff"); 68 assertNotNull(list); 69 assertEquals(list.getLength(), 4); 70 71 Element one = (Element) list.item(0); 72 Element two = (Element) list.item(1); 73 Element three = (Element) list.item(2); 74 Element four = (Element) list.item(3); 75 76 assertEquals("This space intentionally left blank.", 77 getTextContent(one)); 78 assertEquals("Nothing to see here - please get along!", 79 getTextContent(two)); 80 assertEquals("Rent this space!", getTextContent(three)); 81 assertEquals("", getTextContent(four)); 82 83 assertEquals("eins", one.getAttribute("one")); 84 assertEquals("zwei", two.getAttribute("two")); 85 assertEquals("drei", three.getAttribute("three")); 86 87 assertEquals("vier", four.getAttribute("t:four")); 88 assertEquals("vier", four.getAttributeNS("http://www.foo.bar", "four")); 89 90 list = document.getChildNodes(); 91 assertNotNull(list); 92 93 String proinst = ""; 94 String comment = ""; 95 96 for (int i = 0; i < list.getLength(); i++) { 97 Node node = list.item(i); 98 99 if (node instanceof ProcessingInstruction) { 100 proinst = proinst + node.getNodeValue(); 101 } else if (node instanceof Comment) { 102 comment = comment + node.getNodeValue(); 103 } 104 } 105 106 assertEquals("The quick brown fox jumps over the lazy dog.", proinst); 107 assertEquals(" Fragile! Handle me with care! ", comment); 108 } 109 110 public void testGoodFile2() throws Exception { 111 Document document = builder.parse(getClass().getResourceAsStream( 112 "/staffNS.xml")); 113 114 Element root = document.getDocumentElement(); 115 assertNotNull(root); 116 } 117} 118