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