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 javax.xml.stream.XMLEventReaderTest;
  25 
  26 import org.testng.annotations.Test;
  27 import org.testng.Assert;
  28 import javax.xml.namespace.QName;
  29 import javax.xml.stream.XMLEventReader;
  30 import javax.xml.stream.XMLInputFactory;
  31 import javax.xml.stream.XMLStreamConstants;
  32 import javax.xml.stream.XMLStreamException;
  33 import javax.xml.stream.events.XMLEvent;
  34 
  35 /*
  36  * @bug 6613059
  37  * @summary Test XMLEventReader.nextTag() shall update internal event state, same as 6586466.
  38  */
  39 public class Bug6613059Test {
  40 
  41     @Test
  42     public void test() {
  43         String xmlFile = "bug6613059.xml";
  44         XMLEventReader xer = null;
  45         XMLInputFactory xif = XMLInputFactory.newInstance();
  46         try {
  47             xer = xif.createXMLEventReader(xif.createXMLStreamReader(getClass().getResource(xmlFile).getFile(), getClass().getResourceAsStream(xmlFile)));
  48         } catch (XMLStreamException e) {
  49             System.out.println("Error while reading XML: " + e.getClass().getName() + " " + e.getMessage());
  50         }
  51 
  52         try {
  53             while (xer.hasNext()) {
  54                 XMLEvent event = xer.nextTag();
  55                 if (event.isEndElement() && event.asEndElement().getName().equals(new QName("menubar"))) {
  56                     break;
  57                 }
  58 
  59                 if (event.asStartElement().getName().equals(new QName("menu"))) {
  60                     // nextTag should be used when processing element-only
  61                     // content, assuming "addMenu" in
  62                     // the user's code handles the menu part properly
  63                     addMenu(xer, event);
  64                 }
  65 
  66             }
  67         } catch (XMLStreamException e) {
  68             Assert.fail("Exception while reading " + xmlFile + ": " + e.getClass().getName() + " " + e.getMessage());
  69         }
  70     }
  71 
  72     void addMenu(XMLEventReader xer, XMLEvent event) throws XMLStreamException {
  73         // user did not submit this part of code, just jump to the end of menu
  74         // element
  75         int eventType = 0;
  76         while (true) {
  77             event = xer.nextEvent();
  78             // System.out.println("event: " + event);
  79             eventType = event.getEventType();
  80             if (eventType == XMLStreamConstants.END_ELEMENT && event.asEndElement().getName().equals(new QName("menu"))) {
  81                 break;
  82             }
  83         }
  84     }
  85 }