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