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