< prev index next >

test/javax/xml/jaxp/unittest/stream/XMLEventReaderTest/Bug6555001.java

Print this page




   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 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 }


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