1 /*
   2  * Copyright (c) 2014, 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 
  24 package dom;
  25 
  26 import java.io.ByteArrayInputStream;
  27 
  28 import javax.xml.parsers.DocumentBuilder;
  29 import javax.xml.parsers.DocumentBuilderFactory;
  30 import javax.xml.xpath.XPath;
  31 import javax.xml.xpath.XPathConstants;
  32 import javax.xml.xpath.XPathExpression;
  33 import javax.xml.xpath.XPathFactory;
  34 
  35 import org.testng.Assert;
  36 import org.testng.annotations.Listeners;
  37 import org.testng.annotations.Test;
  38 import org.w3c.dom.Document;
  39 import org.w3c.dom.NodeList;
  40 
  41 /*
  42  * @bug 6333993
  43  * @summary Test NodeList.item(valid index) returns value after NodeList.item(NodeList.getLength()).
  44  */
  45 @Listeners({jaxp.library.BasePolicy.class})
  46 public class CR6333993Test {
  47 
  48     @Test
  49     public void testNodeList() {
  50         int n = 5;
  51         while (0 != (n--))
  52             ;
  53         System.out.println("n=" + n);
  54         try {
  55             String testXML = "<root>" + "  <node/>" + "  <node/>" + "  <node/>" + "  <node/>" + "</root>\n";
  56             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  57             // dbf.setNamespaceAware(true);
  58             DocumentBuilder builder = dbf.newDocumentBuilder();
  59             ByteArrayInputStream bis = new ByteArrayInputStream(testXML.getBytes());
  60             Document testDoc = builder.parse(bis);
  61             XPathFactory xpathFactory = XPathFactory.newInstance();
  62             XPath xpath = xpathFactory.newXPath();
  63             XPathExpression expr = xpath.compile("/root/node");
  64             NodeList testNodes = (NodeList) expr.evaluate(testDoc, XPathConstants.NODESET);
  65             // Node list appears to work correctly
  66             System.out.println("testNodes.getLength() = " + testNodes.getLength());
  67             System.out.println("testNodes[0] = " + testNodes.item(0));
  68             System.out.println("testNodes[0] = " + testNodes.item(0));
  69             System.out.println("testNodes.getLength() = " + testNodes.getLength());
  70             // Access past the end of the NodeList correctly returns null
  71             System.out.println("testNodes[testNodes.getLength()] = " + testNodes.item(testNodes.getLength()));
  72             // BUG! First access of valid node after accessing past the end
  73             // incorrectly returns null
  74             if (testNodes.item(0) == null) {
  75                 System.out.println("testNodes[0] = null");
  76                 Assert.fail("First access of valid node after accessing past the end incorrectly returns null");
  77             }
  78             // Subsequent access of valid node correctly returns the node
  79             System.out.println("testNodes[0] = " + testNodes.item(0));
  80         } catch (Exception ex) {
  81             ex.printStackTrace();
  82         }
  83 
  84     }
  85 
  86 }