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 package javax.xml.stream.XMLEventReaderTest;
  24 
  25 import java.io.StringReader;
  26 
  27 import javax.xml.stream.XMLEventReader;
  28 import javax.xml.stream.XMLInputFactory;
  29 import javax.xml.stream.events.EntityReference;
  30 import javax.xml.stream.events.XMLEvent;
  31 
  32 import org.testng.Assert;
  33 import org.testng.annotations.Test;
  34 
  35 /*
  36  * @bug 6555001
  37  * @summary Test StAX parser replaces the entity reference as setting.
  38  */
  39 public class Bug6555001 {
  40     private static final String XML = "" + "<!DOCTYPE doc SYSTEM 'file:///tmp/this/does/not/exist/but/that/is/ok' [" + "<!ENTITY def '<para/>'>" + "]>"
  41             + "<doc>&def;&undef;</doc>";
  42 
  43     @Test
  44     public void testReplacing() throws Exception {
  45         XMLInputFactory factory = XMLInputFactory.newInstance();
  46         factory.setProperty("javax.xml.stream.isReplacingEntityReferences", true);
  47 
  48         StringReader sr = new StringReader(XML);
  49         XMLEventReader reader = factory.createXMLEventReader(sr);
  50 
  51         boolean sawUndef = false;
  52         boolean sawDef = false;
  53 
  54         while (reader.hasNext()) {
  55             XMLEvent event = reader.nextEvent();
  56             // System.out.println("Event: " + event);
  57             if (event.isEntityReference()) {
  58                 EntityReference ref = (EntityReference) event;
  59                 if ("def".equals(ref.getName())) {
  60                     sawDef = true;
  61                 } else if ("undef".equals(ref.getName())) {
  62                     sawUndef = true;
  63                 } else {
  64                     throw new IllegalArgumentException("Unexpected entity name");
  65                 }
  66             }
  67         }
  68 
  69         Assert.assertEquals(false, sawDef);
  70         Assert.assertEquals(true, sawUndef);
  71         reader.close();
  72     }
  73 
  74     @Test
  75     public void testNotReplacing() throws Exception {
  76         XMLInputFactory factory = XMLInputFactory.newInstance();
  77         factory.setProperty("javax.xml.stream.isReplacingEntityReferences", false);
  78 
  79         StringReader sr = new StringReader(XML);
  80         XMLEventReader reader = factory.createXMLEventReader(sr);
  81 
  82         boolean sawUndef = false;
  83         boolean sawDef = false;
  84 
  85         while (reader.hasNext()) {
  86             XMLEvent event = reader.nextEvent();
  87             // System.out.println("Event: " + event);
  88             if (event.isEntityReference()) {
  89                 EntityReference ref = (EntityReference) event;
  90                 if ("def".equals(ref.getName())) {
  91                     sawDef = true;
  92                 } else if ("undef".equals(ref.getName())) {
  93                     sawUndef = true;
  94                 } else {
  95                     throw new IllegalArgumentException("Unexpected entity name");
  96                 }
  97             }
  98         }
  99 
 100         Assert.assertEquals(true, sawDef);
 101         Assert.assertEquals(true, sawUndef);
 102         reader.close();
 103     }
 104 }