8204329: Java API doc for XMLStreamReader.next() needs to be clarified for the exception thrown when hasNext() method returns false

Reviewed-by: lancea, rriggs
This commit is contained in:
Joe Wang 2018-07-02 13:54:01 -07:00
parent 9006ccdbbf
commit 977b9cae12
3 changed files with 73 additions and 5 deletions

View File

@ -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

View File

@ -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();
}
}

View File

@ -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).