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