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