changeset 50905:67f6158279d8

8204329: Java API doc for XMLStreamReader.next() needs to be clarified for the exception thrown when hasNext() method returns false Reviewed-by: lancea, rriggs
author joehw
date Mon, 02 Jul 2018 13:54:01 -0700
parents b9c7eb8d8972
children c30c35118303
files src/java.xml/share/classes/javax/xml/stream/XMLStreamReader.java test/jaxp/javax/xml/jaxp/unittest/stream/XMLEventReaderTest/EventReaderTest.java test/jaxp/javax/xml/jaxp/unittest/stream/XMLStreamReaderTest/StreamReaderTest.java
diffstat 3 files changed, 73 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.xml/share/classes/javax/xml/stream/XMLStreamReader.java	Mon Jul 02 16:28:09 2018 -0400
+++ b/src/java.xml/share/classes/javax/xml/stream/XMLStreamReader.java	Mon Jul 02 13:54:01 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -197,7 +197,6 @@
    *  with  two separate events: START_ELEMENT, END_ELEMENT - This preserves
    *   parsing equivalency of empty element to {@code <tag></tag>}.
    *
-   * This method will throw an IllegalStateException if it is called after hasNext() returns false.
    * @see javax.xml.stream.events.XMLEvent
    * @return the integer code corresponding to the current parse event
    * @throws java.util.NoSuchElementException if this is called when hasNext() returns false
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jaxp/javax/xml/jaxp/unittest/stream/XMLEventReaderTest/EventReaderTest.java	Mon Jul 02 13:54:01 2018 -0700
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package stream.XMLEventReaderTest;
+
+import java.io.StringReader;
+import java.util.NoSuchElementException;
+import javax.xml.stream.XMLEventReader;
+
+import javax.xml.stream.XMLInputFactory;
+import org.testng.annotations.Listeners;
+import org.testng.annotations.Test;
+
+/*
+ * @test
+ * @bug 8204329
+ * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
+ * @run testng stream.XMLEventReaderTest.EventReaderTest
+ * @summary Tests XMLEventReader
+ */
+@Listeners({jaxp.library.BasePolicy.class})
+public class EventReaderTest {
+    @Test(expectedExceptions = NoSuchElementException.class)
+    public void testNextEvent() throws Exception {
+        XMLEventReader eventReader = XMLInputFactory.newFactory().createXMLEventReader(
+                new StringReader("<?xml version='1.0'?><foo/>"));
+
+        while (eventReader.hasNext()) {
+            eventReader.nextEvent();
+        }
+        // no more event
+        eventReader.nextEvent();
+    }
+}
--- a/test/jaxp/javax/xml/jaxp/unittest/stream/XMLStreamReaderTest/StreamReaderTest.java	Mon Jul 02 16:28:09 2018 -0400
+++ b/test/jaxp/javax/xml/jaxp/unittest/stream/XMLStreamReaderTest/StreamReaderTest.java	Mon Jul 02 13:54:01 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -20,9 +20,10 @@
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
-
 package stream.XMLStreamReaderTest;
 
+import java.io.StringReader;
+import java.util.NoSuchElementException;
 import javax.xml.stream.XMLInputFactory;
 import javax.xml.stream.XMLStreamConstants;
 import javax.xml.stream.XMLStreamReader;
@@ -34,7 +35,7 @@
 
 /*
  * @test
- * @bug 8167340
+ * @bug 8167340 8204329
  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  * @run testng/othervm -DrunSecMngr=true stream.XMLStreamReaderTest.StreamReaderTest
  * @run testng/othervm stream.XMLStreamReaderTest.StreamReaderTest
@@ -42,6 +43,20 @@
  */
 @Listeners({jaxp.library.FilePolicy.class})
 public class StreamReaderTest {
+    @Test(expectedExceptions = NoSuchElementException.class)
+    public void testNext() throws Exception {
+        XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
+        XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(
+                new StringReader("<?xml version='1.0'?><foo/>"));
+
+        while (xmlStreamReader.hasNext()) {
+            int event = xmlStreamReader.next();
+        }
+        // no more event
+        xmlStreamReader.next();
+    }
+
+
     /**
      * Verifies that after switching to a different XML Version (1.1), the parser
      * is initialized properly (the listener was not registered in this case).