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