From 977b9cae127fdb4e008a1c384d317f239aa7bfdd Mon Sep 17 00:00:00 2001
From: Joe Wang <joehw@openjdk.org>
Date: Mon, 2 Jul 2018 13:54:01 -0700
Subject: [PATCH] 8204329: Java API doc for XMLStreamReader.next() needs to be
 clarified for the exception thrown when hasNext() method returns false

Reviewed-by: lancea, rriggs
---
 .../javax/xml/stream/XMLStreamReader.java     |  3 +-
 .../XMLEventReaderTest/EventReaderTest.java   | 54 +++++++++++++++++++
 .../XMLStreamReaderTest/StreamReaderTest.java | 21 ++++++--
 3 files changed, 73 insertions(+), 5 deletions(-)
 create mode 100644 test/jaxp/javax/xml/jaxp/unittest/stream/XMLEventReaderTest/EventReaderTest.java

diff --git a/src/java.xml/share/classes/javax/xml/stream/XMLStreamReader.java b/src/java.xml/share/classes/javax/xml/stream/XMLStreamReader.java
index ac8362b1bf2..a16e2a1f570 100644
--- a/src/java.xml/share/classes/javax/xml/stream/XMLStreamReader.java
+++ b/src/java.xml/share/classes/javax/xml/stream/XMLStreamReader.java
@@ -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 @@ public interface XMLStreamReader extends XMLStreamConstants {
    *  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
diff --git a/test/jaxp/javax/xml/jaxp/unittest/stream/XMLEventReaderTest/EventReaderTest.java b/test/jaxp/javax/xml/jaxp/unittest/stream/XMLEventReaderTest/EventReaderTest.java
new file mode 100644
index 00000000000..ccee3232bb0
--- /dev/null
+++ b/test/jaxp/javax/xml/jaxp/unittest/stream/XMLEventReaderTest/EventReaderTest.java
@@ -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();
+    }
+}
diff --git a/test/jaxp/javax/xml/jaxp/unittest/stream/XMLStreamReaderTest/StreamReaderTest.java b/test/jaxp/javax/xml/jaxp/unittest/stream/XMLStreamReaderTest/StreamReaderTest.java
index 2a8a75ec1fc..cc8ac7bbd9f 100644
--- a/test/jaxp/javax/xml/jaxp/unittest/stream/XMLStreamReaderTest/StreamReaderTest.java
+++ b/test/jaxp/javax/xml/jaxp/unittest/stream/XMLStreamReaderTest/StreamReaderTest.java
@@ -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 @@ import org.testng.annotations.Test;
 
 /*
  * @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 @@ import org.testng.annotations.Test;
  */
 @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).