1 /* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package dom; 24 25 import java.io.IOException; 26 import java.io.InputStream; 27 import javax.xml.parsers.DocumentBuilder; 28 import javax.xml.parsers.DocumentBuilderFactory; 29 import javax.xml.parsers.ParserConfigurationException; 30 import org.testng.Assert; 31 import org.testng.annotations.DataProvider; 32 import org.testng.annotations.Test; 33 import org.w3c.dom.Document; 34 import org.w3c.dom.Element; 35 import org.xml.sax.SAXException; 36 37 /* 38 * @bug 8135283 39 * @summary Tests for the Element Traversal interface. 40 */ 41 42 public class ElementTraversal { 43 /* 44 Verifies the ElementTraversal interface by exercising all of its five 45 methods while reading through the xml document. 46 */ 47 @Test(dataProvider = "doc") 48 public void test(Document doc) { 49 org.w3c.dom.ElementTraversal et = (org.w3c.dom.ElementTraversal)doc.getDocumentElement(); 50 //4 toys are listed 51 Assert.assertEquals(et.getChildElementCount(), 4); 52 53 //The 1st is the Martian 54 Element toy1 = et.getFirstElementChild(); 55 verify(toy1, "1", "The Martian"); 56 57 //toy1 has no previous element 58 Element noE = ((org.w3c.dom.ElementTraversal)toy1).getPreviousElementSibling(); 59 Assert.assertEquals(noE, null); 60 61 //The 1st toy's next element is toy2, the Doll 62 Element toy2 = ((org.w3c.dom.ElementTraversal)toy1).getNextElementSibling(); 63 verify(toy2, "2", "The Doll"); 64 65 //The last toy is toy4, the Spaceship 66 Element toy4 = et.getLastElementChild(); 67 verify(toy4, "4", "The Spaceship"); 68 69 //toy4 has no next element 70 noE = ((org.w3c.dom.ElementTraversal)toy4).getNextElementSibling(); 71 Assert.assertEquals(noE, null); 72 73 //toy4's previous element is toy3, Transformer X 74 //toy3 is also an EntityReference 75 Element toy3 = ((org.w3c.dom.ElementTraversal)toy4).getPreviousElementSibling(); 76 verify(toy3, "3", "Transformer X"); 77 } 78 79 /** 80 * Verifies that the values matches the specified element. 81 * @param id the value of the id attribute 82 * @param name the value of its name element 83 */ 84 void verify(Element e, String id, String name) { 85 Assert.assertEquals(e.getAttribute("id"), id); 86 Element toyName = ((org.w3c.dom.ElementTraversal)e).getFirstElementChild(); 87 Assert.assertEquals(toyName.getTextContent(), name); 88 } 89 90 91 /* 92 * DataProvider: a Document object 93 */ 94 @DataProvider(name = "doc") 95 Object[][] getXPath() { 96 return new Object[][]{{getDoc()}}; 97 } 98 Document getDoc() { 99 InputStream xmlFile = getClass().getResourceAsStream("ElementTraversal.xml"); 100 Document doc = null; 101 try { 102 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 103 dbf.setExpandEntityReferences(false); 104 DocumentBuilder db = dbf.newDocumentBuilder(); 105 doc = db.parse(xmlFile); 106 } catch (ParserConfigurationException | SAXException | IOException e) { 107 System.out.println("fail: " + e.getMessage()); 108 } 109 110 return doc; 111 } 112 }