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 tests.xml; 18 19import dalvik.annotation.BrokenTest; 20import dalvik.annotation.TestTargets; 21import dalvik.annotation.TestLevel; 22import dalvik.annotation.TestTargetNew; 23import dalvik.annotation.TestTargetClass; 24 25import junit.framework.TestCase; 26 27import org.w3c.dom.Comment; 28import org.w3c.dom.Document; 29import org.w3c.dom.Element; 30import org.w3c.dom.NamedNodeMap; 31import org.w3c.dom.Node; 32import org.w3c.dom.NodeList; 33import org.w3c.dom.ProcessingInstruction; 34import org.w3c.dom.Text; 35 36import javax.xml.parsers.DocumentBuilder; 37import javax.xml.parsers.DocumentBuilderFactory; 38 39@TestTargetClass(DocumentBuilder.class) 40public class SimpleBuilderTest extends TestCase { 41 42 private DocumentBuilder builder; 43 44 protected void setUp() throws Exception { 45 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 46 factory.setValidating(false); 47 factory.setNamespaceAware(true); 48 49 builder = factory.newDocumentBuilder(); 50 } 51 52 protected void tearDown() throws Exception { 53 builder = null; 54 } 55 56 private String getTextContent(Node node) { 57 String result = (node instanceof Text ? ((Text) node).getData() : ""); 58 59 Node child = node.getFirstChild(); 60 while (child != null) { 61 result = result + getTextContent(child); 62 child = child.getNextSibling(); 63 } 64 65 return result; 66 } 67 @TestTargetNew( 68 level = TestLevel.PARTIAL, 69 notes = "Regression test.", 70 method = "parse", 71 args = {java.io.InputStream.class} 72 ) 73 public void testGoodFile1() throws Exception { 74 Document document = builder.parse(getClass().getResourceAsStream( 75 "/SimpleBuilderTest.xml")); 76 77 Element root = document.getDocumentElement(); 78 assertNotNull(root); 79 assertEquals("http://www.foo.bar", root.getNamespaceURI()); 80 assertEquals("t", root.getPrefix()); 81 assertEquals("stuff", root.getLocalName()); 82 83 NodeList list = root.getElementsByTagName("nestedStuff"); 84 assertNotNull(list); 85 assertEquals(list.getLength(), 4); 86 87 Element one = (Element) list.item(0); 88 Element two = (Element) list.item(1); 89 Element three = (Element) list.item(2); 90 Element four = (Element) list.item(3); 91 92 assertEquals("This space intentionally left blank.", 93 getTextContent(one)); 94 assertEquals("Nothing to see here - please get along!", 95 getTextContent(two)); 96 assertEquals("Rent this space!", getTextContent(three)); 97 assertEquals("", getTextContent(four)); 98 99 assertEquals("eins", one.getAttribute("one")); 100 assertEquals("zwei", two.getAttribute("two")); 101 assertEquals("drei", three.getAttribute("three")); 102 103 assertEquals("vier", four.getAttribute("t:four")); 104 assertEquals("vier", four.getAttributeNS("http://www.foo.bar", "four")); 105 106 list = document.getChildNodes(); 107 assertNotNull(list); 108 109 String proinst = ""; 110 String comment = ""; 111 112 for (int i = 0; i < list.getLength(); i++) { 113 Node node = list.item(i); 114 115 if (node instanceof ProcessingInstruction) { 116 proinst = proinst + node.getNodeValue(); 117 } else if (node instanceof Comment) { 118 comment = comment + node.getNodeValue(); 119 } 120 } 121 122 assertEquals("The quick brown fox jumps over the lazy dog.", proinst); 123 assertEquals(" Fragile! Handle me with care! ", comment); 124 } 125 @TestTargetNew( 126 level = TestLevel.ADDITIONAL, 127 method = "!todo parse", 128 args = {java.io.InputStream.class} 129 ) 130 @BrokenTest("Doesn't verify anything.") 131 public void testGoodFile2() throws Exception { 132 Document document = builder.parse(getClass().getResourceAsStream( 133 "/staffNS.xml")); 134 135 Element root = document.getDocumentElement(); 136 assertNotNull(root); 137 138 // dump("", root); 139 } 140 141 private void dump(String prefix, Element element) { 142 System.out.print(prefix + "<" + element.getTagName()); 143 144 NamedNodeMap attrs = element.getAttributes(); 145 for (int i = 0; i < attrs.getLength(); i++) { 146 Node item = attrs.item(i); 147 System.out.print(" " + item.getNodeName() + "=" + item.getNodeValue()); 148 } 149 150 System.out.println(">"); 151 152 NodeList children = element.getChildNodes(); 153 for (int i = 0; i < children.getLength(); i++) { 154 Node item = children.item(i); 155 if (item instanceof Element) { 156 dump(prefix + " ", (Element)item); 157 } 158 } 159 160 System.out.println(prefix + "</" + element.getTagName() + ">"); 161 } 162} 163