Issue40Test.java revision 968:874082a9b565
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 24package stream.XMLEventReaderTest; 25 26import java.io.File; 27 28import javax.xml.stream.XMLEventReader; 29import javax.xml.stream.XMLInputFactory; 30import javax.xml.stream.XMLOutputFactory; 31import javax.xml.stream.XMLStreamConstants; 32import javax.xml.stream.events.XMLEvent; 33 34import org.testng.Assert; 35import org.testng.annotations.Listeners; 36import org.testng.annotations.Test; 37 38/* 39 * @test 40 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest 41 * @run testng/othervm -DrunSecMngr=true stream.XMLEventReaderTest.Issue40Test 42 * @run testng/othervm stream.XMLEventReaderTest.Issue40Test 43 * @summary Test XMLEventReader.getElementText() works after calling peek(). 44 */ 45@Listeners({jaxp.library.FilePolicy.class}) 46public class Issue40Test { 47 48 public java.io.File input; 49 public final String filesDir = "./"; 50 protected XMLInputFactory inputFactory; 51 protected XMLOutputFactory outputFactory; 52 53 /** 54 * test without peek 55 */ 56 @Test 57 public void testWOPeek() { 58 try { 59 XMLEventReader er = getReader(); 60 XMLEvent e = er.nextEvent(); 61 Assert.assertEquals(e.getEventType(), XMLStreamConstants.START_DOCUMENT); 62 // we have two start elements in this file 63 Assert.assertEquals(er.nextEvent().getEventType(), XMLStreamConstants.START_ELEMENT); 64 Assert.assertEquals(er.nextEvent().getEventType(), XMLStreamConstants.START_ELEMENT); 65 System.out.println(er.getElementText()); 66 67 } catch (Exception e) { 68 Assert.fail(e.getMessage()); 69 } 70 } 71 72 /** 73 * test with peek 74 */ 75 @Test 76 public void testWPeek() { 77 try { 78 XMLEventReader er = getReader(); 79 XMLEvent e = er.nextEvent(); 80 Assert.assertEquals(e.getEventType(), XMLStreamConstants.START_DOCUMENT); 81 // we have two start elements in this file 82 while (er.peek().getEventType() == XMLStreamConstants.START_ELEMENT) { 83 e = er.nextEvent(); 84 } 85 Assert.assertEquals(e.getEventType(), XMLStreamConstants.START_ELEMENT); 86 System.out.println(er.getElementText()); 87 88 } catch (Exception e) { 89 Assert.fail(e.getMessage()); 90 } 91 } 92 93 private XMLEventReader getReader() throws Exception { 94 inputFactory = XMLInputFactory.newInstance(); 95 input = new File(getClass().getResource("play.xml").getFile()); 96 97 // Check if event reader returns the correct event 98 XMLEventReader er = inputFactory.createXMLEventReader(inputFactory.createXMLStreamReader(new java.io.FileInputStream(input), "UTF-8")); 99 return er; 100 } 101 102} 103 104