1 /* 2 * Copyright (C) 2009 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 17 package tests.xml; 18 19 import dalvik.annotation.TestTargetClass; 20 import junit.framework.TestCase; 21 import org.w3c.dom.Document; 22 import org.w3c.dom.Element; 23 import org.w3c.dom.Node; 24 import org.w3c.dom.NodeList; 25 import tests.support.resource.Support_Resources; 26 27 import javax.xml.parsers.DocumentBuilder; 28 import javax.xml.parsers.DocumentBuilderFactory; 29 import java.io.ByteArrayInputStream; 30 import java.io.File; 31 import java.util.ArrayList; 32 import java.util.List; 33 34 @TestTargetClass(Node.class) 35 public class NodeTest extends TestCase { 36 37 /** 38 * For bug 779: Node#getNextSibling throws IndexOutOfBoundsException. 39 */ test_getNextSibling()40 public void test_getNextSibling() throws Exception { 41 // Calling getNextSibling when there is no next sibling should return null. 42 // From http://code.google.com/p/android/issues/detail?id=779. 43 ByteArrayInputStream bis = new ByteArrayInputStream("<root/>".getBytes()); 44 Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(bis); 45 Node root = document.getDocumentElement(); 46 assertNull(root.getNextSibling()); 47 } 48 testGetBaseUri()49 public void testGetBaseUri() throws Exception { 50 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 51 File file = Support_Resources.resourceToTempFile("/simple.xml"); 52 Document document = builder.parse(file); 53 54 assertFileUriEquals(file, document.getBaseURI()); 55 56 Element documentElement = document.getDocumentElement(); 57 for (Node node : flattenSubtree(documentElement)) { 58 if (node.getNodeType() == Node.ELEMENT_NODE 59 || node.getNodeType() == Node.DOCUMENT_NODE) { 60 assertFileUriEquals(file, node.getBaseURI()); 61 } else { 62 assertNull("Unexpected base URI for " + node, node.getBaseURI()); 63 } 64 } 65 66 // TODO: test other node types 67 // TODO: test resolution of relative paths 68 // TODO: test URI sanitization 69 } 70 assertFileUriEquals(File expectedFile, String actual)71 private void assertFileUriEquals(File expectedFile, String actual) { 72 assertTrue("Expected URI for: " + expectedFile + " but was " + actual + ". ", 73 actual.equals("file:" + expectedFile) 74 || actual.equals("file://" + expectedFile)); 75 } 76 flattenSubtree(Node subtree)77 private List<Node> flattenSubtree(Node subtree) { 78 List<Node> result = new ArrayList<Node>(); 79 traverse(subtree, result); 80 return result; 81 } 82 traverse(Node node, List<Node> sink)83 private void traverse(Node node, List<Node> sink) { 84 sink.add(node); 85 86 NodeList children = node.getChildNodes(); 87 for (int i = 0; i < children.getLength(); i++) { 88 traverse(children.item(i), sink); 89 } 90 } 91 } 92