jaxp/test/javax/xml/jaxp/unittest/dom/ElementTraversal.java
changeset 32791 ed81078b4e7f
child 32901 42af6bc4d36a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jaxp/test/javax/xml/jaxp/unittest/dom/ElementTraversal.java	Fri Sep 25 16:42:19 2015 -0700
@@ -0,0 +1,112 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package dom;
+
+import java.io.IOException;
+import java.io.InputStream;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import org.testng.Assert;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.xml.sax.SAXException;
+
+/*
+ * @bug 8135283
+ * @summary Tests for the Element Traversal interface.
+ */
+
+public class ElementTraversal {
+    /*
+       Verifies the ElementTraversal interface by exercising all of its five
+       methods while reading through the xml document.
+     */
+    @Test(dataProvider = "doc")
+    public void test(Document doc) {
+        org.w3c.dom.ElementTraversal et = (org.w3c.dom.ElementTraversal)doc.getDocumentElement();
+        //4 toys are listed
+        Assert.assertEquals(et.getChildElementCount(), 4);
+
+        //The 1st is the Martian
+        Element toy1 = et.getFirstElementChild();
+        verify(toy1, "1", "The Martian");
+
+        //toy1 has no previous element
+        Element noE = ((org.w3c.dom.ElementTraversal)toy1).getPreviousElementSibling();
+        Assert.assertEquals(noE, null);
+
+        //The 1st toy's next element is toy2, the Doll
+        Element toy2 = ((org.w3c.dom.ElementTraversal)toy1).getNextElementSibling();
+        verify(toy2, "2", "The Doll");
+
+        //The last toy is toy4, the Spaceship
+        Element toy4 = et.getLastElementChild();
+        verify(toy4, "4", "The Spaceship");
+
+        //toy4 has no next element
+        noE = ((org.w3c.dom.ElementTraversal)toy4).getNextElementSibling();
+        Assert.assertEquals(noE, null);
+
+        //toy4's previous element is toy3, Transformer X
+        //toy3 is also an EntityReference
+        Element toy3 = ((org.w3c.dom.ElementTraversal)toy4).getPreviousElementSibling();
+        verify(toy3, "3", "Transformer X");
+    }
+
+    /**
+     * Verifies that the values matches the specified element.
+     * @param id the value of the id attribute
+     * @param name the value of its name element
+     */
+    void verify(Element e, String id, String name) {
+        Assert.assertEquals(e.getAttribute("id"), id);
+        Element toyName = ((org.w3c.dom.ElementTraversal)e).getFirstElementChild();
+        Assert.assertEquals(toyName.getTextContent(), name);
+    }
+
+
+    /*
+     * DataProvider: a Document object
+     */
+    @DataProvider(name = "doc")
+    Object[][] getXPath() {
+        return new Object[][]{{getDoc()}};
+    }
+    Document getDoc() {
+        InputStream xmlFile = getClass().getResourceAsStream("ElementTraversal.xml");
+        Document doc = null;
+        try {
+            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+            dbf.setExpandEntityReferences(false);
+            DocumentBuilder db = dbf.newDocumentBuilder();
+            doc = db.parse(xmlFile);
+        } catch (ParserConfigurationException | SAXException | IOException e) {
+            System.out.println("fail: " + e.getMessage());
+        }
+
+        return doc;
+    }
+}