1 /*
   2  * Copyright (c) 2014, 2016, 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;
  25 
  26 import javax.xml.namespace.QName;
  27 import javax.xml.stream.EventFilter;
  28 import javax.xml.stream.XMLEventReader;
  29 import javax.xml.stream.XMLInputFactory;
  30 import javax.xml.stream.XMLStreamException;
  31 import javax.xml.stream.events.XMLEvent;
  32 
  33 import org.testng.Assert;
  34 import org.testng.annotations.Listeners;
  35 import org.testng.annotations.Test;
  36 
  37 /*
  38  * @bug 6976938
  39  * @summary Test StAX parser won't throw StackOverflowError while reading valid XML file, in case the text content of an XML element contains many lines like "< ... >".
  40  */
  41 @Listeners({jaxp.library.FilePolicy.class})
  42 public class Bug6976938Test {
  43 
  44     private static final String INPUT_FILE = "Bug6976938.xml";
  45 
  46     public static final String VF_GENERIC_TT_NAMESPACE = "http://www.vodafone.com/oss/xml/TroubleTicket";
  47 
  48     public static final QName ATTACHMENT_NAME = new QName(VF_GENERIC_TT_NAMESPACE, "attachment");
  49 
  50     @Test
  51     public void testEventReader() {
  52         XMLInputFactory xif = XMLInputFactory.newInstance();
  53         xif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
  54         eventReaderTest(xif);
  55     }
  56 
  57     @Test
  58     public void testEventReader1() {
  59         XMLInputFactory xif = XMLInputFactory.newInstance();
  60         eventReaderTest(xif);
  61     }
  62 
  63     public void eventReaderTest(XMLInputFactory xif) {
  64         XMLEventReader eventReader = null;
  65         try {
  66             eventReader = xif.createXMLEventReader(this.getClass().getResourceAsStream(INPUT_FILE));
  67             XMLEventReader filteredEventReader = xif.createFilteredReader(eventReader, new EventFilter() {
  68                 public boolean accept(XMLEvent event) {
  69                     if (!event.isStartElement()) {
  70                         return false;
  71                     }
  72                     QName elementQName = event.asStartElement().getName();
  73                     if ((elementQName.getLocalPart().equals(ATTACHMENT_NAME.getLocalPart()) || elementQName.getLocalPart().equals("Attachment"))
  74                             && elementQName.getNamespaceURI().equals(VF_GENERIC_TT_NAMESPACE)) {
  75                         return true;
  76                     }
  77                     return false;
  78                 }
  79             });
  80             if (filteredEventReader.hasNext()) {
  81                 System.out.println("containsAttachments() returns true");
  82             }
  83         } catch (Exception e) {
  84             e.printStackTrace();
  85             Assert.fail(e.getMessage());
  86 
  87         } finally {
  88             if (eventReader != null) {
  89                 try {
  90                     eventReader.close();
  91                 } catch (XMLStreamException xse) {
  92                     // Ignored by intention
  93                 }
  94             }
  95         }
  96     }
  97 
  98 }