1 /*
   2  * Copyright (c) 2014, 2015, 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.EventsTest;
  25 
  26 import java.io.StringReader;
  27 import java.util.Iterator;
  28 import java.util.List;
  29 
  30 import javax.xml.stream.XMLEventReader;
  31 import javax.xml.stream.XMLInputFactory;
  32 import javax.xml.stream.XMLOutputFactory;
  33 import javax.xml.stream.XMLStreamConstants;
  34 import javax.xml.stream.events.DTD;
  35 import javax.xml.stream.events.EntityDeclaration;
  36 import javax.xml.stream.events.NotationDeclaration;
  37 import javax.xml.stream.events.XMLEvent;
  38 
  39 import org.testng.Assert;
  40 import org.testng.annotations.Test;
  41 
  42 /*
  43  * @bug 6620632
  44  * @summary Test XMLEventReader can parse notation and entity information from DTD Event.
  45  */
  46 public class Issue48Test {
  47 
  48     public java.io.File input;
  49     public final String filesDir = "./";
  50     protected XMLInputFactory inputFactory;
  51     protected XMLOutputFactory outputFactory;
  52 
  53     /**
  54      * DTDEvent instances constructed via event reader are missing the notation
  55      * and entity declaration information
  56      */
  57     @Test
  58     public void testDTDEvent() {
  59         String XML = "<?xml version='1.0' ?>" + "<!DOCTYPE root [\n" + "<!ENTITY intEnt 'internal'>\n" + "<!ENTITY extParsedEnt SYSTEM 'url:dummy'>\n"
  60                 + "<!NOTATION notation PUBLIC 'notation-public-id'>\n" + "<!NOTATION notation2 SYSTEM 'url:dummy'>\n"
  61                 + "<!ENTITY extUnparsedEnt SYSTEM 'url:dummy2' NDATA notation>\n" + "]>" + "<root />";
  62 
  63         try {
  64             XMLEventReader er = getReader(XML);
  65             XMLEvent evt = er.nextEvent(); // StartDocument
  66             evt = er.nextEvent(); // DTD
  67             if (evt.getEventType() != XMLStreamConstants.DTD) {
  68                 Assert.fail("Expected DTD event");
  69             }
  70             DTD dtd = (DTD) evt;
  71             List entities = dtd.getEntities();
  72             if (entities == null) {
  73                 Assert.fail("No entity found. Expected 3.");
  74             } else {
  75                 Assert.assertEquals(entities.size(), 3);
  76             }
  77             // Let's also verify they are all of right type...
  78             testListElems(entities, EntityDeclaration.class);
  79 
  80             List notations = dtd.getNotations();
  81             if (notations == null) {
  82                 Assert.fail("No notation found. Expected 2.");
  83             } else {
  84                 Assert.assertEquals(notations.size(), 2);
  85             }
  86             // Let's also verify they are all of right type...
  87             testListElems(notations, NotationDeclaration.class);
  88         } catch (Exception e) {
  89             Assert.fail(e.getMessage());
  90         }
  91     }
  92 
  93     private XMLEventReader getReader(String XML) throws Exception {
  94         inputFactory = XMLInputFactory.newInstance();
  95 
  96         // Check if event reader returns the correct event
  97         XMLEventReader er = inputFactory.createXMLEventReader(new StringReader(XML));
  98         return er;
  99     }
 100 
 101 
 102     private void testListElems(List l, Class expType) {
 103         Iterator it = l.iterator();
 104         while (it.hasNext()) {
 105             Object o = it.next();
 106             Assert.assertNotNull(o);
 107             Assert.assertTrue(expType.isAssignableFrom(o.getClass()));
 108         }
 109     }
 110 
 111 }