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