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 java.io.File;
  27 
  28 import javax.xml.stream.XMLEventReader;
  29 import javax.xml.stream.XMLInputFactory;
  30 import javax.xml.stream.XMLOutputFactory;
  31 import javax.xml.stream.events.XMLEvent;
  32 
  33 import org.testng.Assert;
  34 import org.testng.annotations.Test;
  35 
  36 /*
  37  * @bug 6668115
  38  * @summary Test XMLEventReader.getElementText() shall update last event even if no peek.
  39  */
  40 public class Bug6668115Test {
  41 
  42     public java.io.File input;
  43     public final String filesDir = "./";
  44     protected XMLInputFactory inputFactory;
  45     protected XMLOutputFactory outputFactory;
  46 
  47     /**
  48      * The reason the following call sequence is a problem is that with a
  49      * peekevent, getElementText calls nextEvent which does properly update the
  50      * lastEvent
  51      */
  52     @Test
  53     public void testNextTag() {
  54         try {
  55             XMLEventReader er = getReader();
  56             er.nextTag();
  57             er.nextTag();
  58 
  59             System.out.println(er.getElementText());
  60             er.nextTag();
  61             System.out.println(er.getElementText());
  62 
  63         } catch (Exception e) {
  64             System.out.println(e.getMessage());
  65             e.printStackTrace();
  66             Assert.fail(e.getMessage());
  67         }
  68     }
  69 
  70     @Test
  71     public void testNextTagWPeek() {
  72         try {
  73             XMLEventReader er = getReader();
  74             er.nextTag();
  75             er.nextTag();
  76 
  77             XMLEvent event = er.peek();
  78             System.out.println(er.getElementText());
  79             er.nextTag();
  80             System.out.println(er.getElementText());
  81 
  82         } catch (Exception e) {
  83             System.out.println(e.getMessage());
  84             e.printStackTrace();
  85             Assert.fail(e.getMessage());
  86         }
  87     }
  88 
  89     private XMLEventReader getReader() throws Exception {
  90         inputFactory = XMLInputFactory.newInstance();
  91         input = new File(getClass().getResource("play2.xml").getFile());
  92         // Check if event reader returns the correct event
  93         XMLEventReader er = inputFactory.createXMLEventReader(inputFactory.createXMLStreamReader(new java.io.FileInputStream(input), "UTF-8"));
  94         return er;
  95     }
  96 
  97 }