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.XMLStreamFilterTest;
  25 
  26 import javax.xml.stream.StreamFilter;
  27 import javax.xml.stream.XMLInputFactory;
  28 import javax.xml.stream.XMLStreamException;
  29 import javax.xml.stream.XMLStreamReader;
  30 import javax.xml.stream.events.XMLEvent;
  31 
  32 import org.testng.Assert;
  33 import org.testng.annotations.Listeners;
  34 import org.testng.annotations.Test;
  35 
  36 /*
  37  * @summary Test Filtered XMLStreamReader hasNext() always return the correct value if repeat to call it.
  38  */
  39 @Listeners({jaxp.library.FilePolicy.class})
  40 public class HasNextTest {
  41 
  42     private static String INPUT_FILE = "HasNextTest.xml";
  43 
  44     private HasNextTypeFilter createFilter() {
  45 
  46         HasNextTypeFilter f = new HasNextTypeFilter();
  47 
  48         f.addType(XMLEvent.START_ELEMENT);
  49         f.addType(XMLEvent.END_ELEMENT);
  50         f.addType(XMLEvent.PROCESSING_INSTRUCTION);
  51         f.addType(XMLEvent.CHARACTERS);
  52         f.addType(XMLEvent.COMMENT);
  53         f.addType(XMLEvent.SPACE);
  54         f.addType(XMLEvent.START_DOCUMENT);
  55         f.addType(XMLEvent.END_DOCUMENT);
  56         return f;
  57     }
  58 
  59     private XMLStreamReader createStreamReader(HasNextTypeFilter f) {
  60 
  61         try {
  62             XMLInputFactory factory = XMLInputFactory.newInstance();
  63             factory = XMLInputFactory.newInstance();
  64             return factory.createFilteredReader(factory.createXMLStreamReader(this.getClass().getResourceAsStream(INPUT_FILE)), (StreamFilter) f);
  65         } catch (Exception e) {
  66             e.printStackTrace();
  67             Assert.fail("Unexpected Exception: " + e.getMessage());
  68             return null;
  69         }
  70     }
  71 
  72     private void checkHasNext(XMLStreamReader r1) throws XMLStreamException {
  73 
  74         // try asking 3 times, insure all results are the same
  75         boolean hasNext_1 = r1.hasNext();
  76         boolean hasNext_2 = r1.hasNext();
  77         boolean hasNext_3 = r1.hasNext();
  78 
  79         System.out.println("XMLStreamReader.hasNext() (1): " + hasNext_1);
  80         System.out.println("XMLStreamReader.hasNext() (2): " + hasNext_2);
  81         System.out.println("XMLStreamReader.hasNext() (3): " + hasNext_3);
  82 
  83         Assert.assertTrue((hasNext_1 == hasNext_2) && (hasNext_1 == hasNext_3),
  84                 "XMLStreamReader.hasNext() returns inconsistent values for each subsequent call: " + hasNext_1 + ", " + hasNext_2 + ", " + hasNext_3);
  85     }
  86 
  87     @Test
  88     public void testFilterUsingNextTag() {
  89 
  90         try {
  91             HasNextTypeFilter f = createFilter();
  92             XMLStreamReader r1 = createStreamReader(f);
  93 
  94             while (r1.hasNext()) {
  95                 try {
  96                     r1.nextTag();
  97                 } catch (Exception e) {
  98                     System.err.println("Expected Exception: " + e.getMessage());
  99                     e.printStackTrace();
 100                 }
 101 
 102                 checkHasNext(r1);
 103             }
 104 
 105         } catch (XMLStreamException e) {
 106             System.err.println("Unexpected Exception: " + e.getMessage());
 107             e.printStackTrace();
 108             Assert.fail("Unexpected Exception: " + e.toString());
 109         } catch (Exception e) {
 110             // if this is END_DOCUMENT, it is expected
 111             if (e.toString().indexOf("END_DOCUMENT") != -1) {
 112                 // expected
 113                 System.err.println("Expected Exception:");
 114                 e.printStackTrace();
 115             } else {
 116                 // unexpected
 117                 System.err.println("Unexpected Exception: " + e.getMessage());
 118                 e.printStackTrace();
 119                 Assert.fail("Unexpected Exception: " + e.toString());
 120             }
 121         }
 122     }
 123 
 124     @Test
 125     public void testFilterUsingNext() {
 126 
 127         try {
 128             HasNextTypeFilter f = createFilter();
 129             XMLStreamReader r1 = createStreamReader(f);
 130 
 131             while (r1.hasNext()) {
 132                 r1.next();
 133                 checkHasNext(r1);
 134             }
 135 
 136         } catch (Exception e) {
 137             // unexpected
 138             System.err.println("Unexpected Exception: " + e.getMessage());
 139             e.printStackTrace();
 140             Assert.fail("Unexpected Exception: " + e.toString());
 141         }
 142     }
 143 }