1 /* 2 * Copyright (c) 2016, 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.Listeners; 33 import org.testng.annotations.Test; 34 import org.w3c.dom.Document; 35 import org.w3c.dom.DOMImplementation; 36 import org.w3c.dom.Element; 37 import org.xml.sax.SAXException; 38 39 /* 40 * @bug 8135283 8138721 41 * @summary Tests for the Element Traversal interface. 42 */ 43 44 @Listeners({jaxp.library.FilePolicy.class}) 45 public class ElementTraversal { 46 /* 47 Verifies that ElementTraversal is supported. 48 */ 49 @Test(dataProvider = "doc") 50 public void testHasFeature(Document doc) { 51 DOMImplementation di = doc.getImplementation(); 52 53 //return false if feasure == null 54 Assert.assertFalse(di.hasFeature(null, null)); 55 56 //A feature is supported without specifying version 57 Assert.assertTrue(di.hasFeature("ElementTraversal", null)); 58 Assert.assertTrue(di.hasFeature("ElementTraversal", "")); 59 60 //ElementTraversal Version 1.0 is supported 61 Assert.assertTrue(di.hasFeature("ElementTraversal", "1.0")); 62 } 63 64 /* 65 Verifies the ElementTraversal interface by exercising all of its five 66 methods while reading through the xml document. 67 */ 68 @Test(dataProvider = "doc") 69 public void test(Document doc) { 70 org.w3c.dom.ElementTraversal et = (org.w3c.dom.ElementTraversal)doc.getDocumentElement(); 71 //4 toys are listed 72 Assert.assertEquals(et.getChildElementCount(), 4); 73 74 //The 1st is the Martian 75 Element toy1 = et.getFirstElementChild(); 76 verify(toy1, "1", "The Martian"); 77 78 //toy1 has no previous element 79 Element noE = ((org.w3c.dom.ElementTraversal)toy1).getPreviousElementSibling(); 80 Assert.assertEquals(noE, null); 81 82 //The 1st toy's next element is toy2, the Doll 83 Element toy2 = ((org.w3c.dom.ElementTraversal)toy1).getNextElementSibling(); 84 verify(toy2, "2", "The Doll"); 85 86 //The last toy is toy4, the Spaceship 87 Element toy4 = et.getLastElementChild(); 88 verify(toy4, "4", "The Spaceship"); 89 90 //toy4 has no next element 91 noE = ((org.w3c.dom.ElementTraversal)toy4).getNextElementSibling(); 92 Assert.assertEquals(noE, null); 93 94 //toy4's previous element is toy3, Transformer X 95 //toy3 is also an EntityReference 96 Element toy3 = ((org.w3c.dom.ElementTraversal)toy4).getPreviousElementSibling(); 97 verify(toy3, "3", "Transformer X"); 98 } 99 100 /** 101 * Verifies that the values matches the specified element. 102 * @param id the value of the id attribute 103 * @param name the value of its name element 104 */ 105 void verify(Element e, String id, String name) { 106 Assert.assertEquals(e.getAttribute("id"), id); 107 Element toyName = ((org.w3c.dom.ElementTraversal)e).getFirstElementChild(); 108 Assert.assertEquals(toyName.getTextContent(), name); 109 } 110 111 112 /* 113 * DataProvider: a Document object 114 */ 115 @DataProvider(name = "doc") 116 Object[][] getXPath() { 117 return new Object[][]{{getDoc()}}; 118 } 119 Document getDoc() { 120 InputStream xmlFile = getClass().getResourceAsStream("ElementTraversal.xml"); 121 Document doc = null; 122 try { 123 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 124 dbf.setExpandEntityReferences(false); 125 DocumentBuilder db = dbf.newDocumentBuilder(); 126 doc = db.parse(xmlFile); 127 } catch (ParserConfigurationException | SAXException | IOException e) { 128 System.out.println("fail: " + e.getMessage()); 129 } 130 131 return doc; 132 } 133 }