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