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