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 
  24 package stream.XMLStreamReaderTest;
  25 
  26 import javax.xml.stream.XMLInputFactory;
  27 import javax.xml.stream.XMLStreamReader;
  28 import javax.xml.stream.events.XMLEvent;
  29 
  30 import org.testng.Assert;
  31 import org.testng.annotations.Listeners;
  32 import org.testng.annotations.Test;
  33 
  34 /*
  35  * @bug 6440324
  36  * @summary Test StAX can accept non-existent DTD if IS_VALIDATING if false.
  37  */
  38 @Listeners({jaxp.library.FilePolicy.class})
  39 public class IsValidatingTest {
  40 
  41     /**
  42      * File with non-existent DTD.
  43      */
  44     private static final String INPUT_FILE = "IsValidatingTest.xml";
  45     /**
  46      * File with internal subset and non-existent DTD.
  47      */
  48     private static final String INPUT_FILE_INTERNAL_SUBSET = "IsValidatingTestInternalSubset.xml";
  49 
  50     /**
  51      * Test StAX with IS_VALIDATING = false and a non-existent DTD.
  52      * Test should pass.
  53      *
  54      * Try to parse an XML file that references a a non-existent DTD.
  55      * Desired behavior:
  56      *     If IS_VALIDATING == false, then continue processing.
  57      *
  58      * Note that an attempt is made to read the DTD even if IS_VALIDATING == false.
  59      * This is not required for DTD validation, but for entity resolution.
  60      * The XML specification allows the optional reading of an external DTD
  61      * even for non-validating processors.
  62      *
  63      */
  64     @Test
  65     public void testStAXIsValidatingFalse() {
  66 
  67         XMLStreamReader reader = null;
  68         Boolean isValidating = null;
  69         String propertyValues = null;
  70         boolean dtdEventOccured = false;
  71 
  72         XMLInputFactory xif = XMLInputFactory.newInstance();
  73         xif.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
  74 
  75         try {
  76             reader = xif.createXMLStreamReader(this.getClass().getResource(INPUT_FILE).toExternalForm(), this.getClass().getResourceAsStream(INPUT_FILE));
  77 
  78             isValidating = (Boolean) reader.getProperty(XMLInputFactory.IS_VALIDATING);
  79             propertyValues = "IS_VALIDATING=" + isValidating;
  80 
  81             while (reader.hasNext()) {
  82                 int e = reader.next();
  83                 if (e == XMLEvent.DTD) {
  84                     dtdEventOccured = true;
  85                     System.out.println("testStAXIsValidatingFalse(): " + "reader.getText() with Event == DTD: " + reader.getText());
  86                 }
  87             }
  88 
  89             // expected success
  90 
  91             // should have see DTD Event
  92             if (!dtdEventOccured) {
  93                 Assert.fail("Unexpected failure: did not see DTD event");
  94             }
  95         } catch (Exception e) {
  96             // unexpected failure
  97             System.err.println("Exception with reader.getEventType(): " + reader.getEventType());
  98             e.printStackTrace();
  99             Assert.fail("Unexpected failure with " + propertyValues + ", " + e.toString());
 100         }
 101     }
 102 
 103     /**
 104      * Test StAX with IS_VALIDATING = false, an internal subset and a
 105      * non-existent DTD.
 106      *
 107      * Test should pass.
 108      */
 109     @Test
 110     public void testStAXIsValidatingFalseInternalSubset() {
 111 
 112         XMLStreamReader reader = null;
 113         Boolean isValidating = null;
 114         String propertyValues = null;
 115         boolean dtdEventOccured = false;
 116         boolean entityReferenceEventOccured = false;
 117 
 118         XMLInputFactory xif = XMLInputFactory.newInstance();
 119         xif.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
 120         xif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE);
 121 
 122         try {
 123             reader = xif.createXMLStreamReader(this.getClass().getResource(INPUT_FILE).toExternalForm(),
 124                     this.getClass().getResourceAsStream(INPUT_FILE_INTERNAL_SUBSET));
 125 
 126             isValidating = (Boolean) reader.getProperty(XMLInputFactory.IS_VALIDATING);
 127             propertyValues = "IS_VALIDATING=" + isValidating;
 128 
 129             while (reader.hasNext()) {
 130                 int e = reader.next();
 131                 if (e == XMLEvent.DTD) {
 132                     dtdEventOccured = true;
 133                     System.out.println("testStAXIsValidatingFalseInternalSubset(): " + "reader.getText() with Event == DTD: " + reader.getText());
 134                 } else if (e == XMLEvent.ENTITY_REFERENCE) {
 135                     // expected ENTITY_REFERENCE values?
 136                     if (reader.getLocalName().equals("foo") && reader.getText().equals("bar")) {
 137                         entityReferenceEventOccured = true;
 138                     }
 139 
 140                     System.out.println("testStAXIsValidatingFalseInternalSubset(): " + "reader.get(LocalName, Text)() with Event " + " == ENTITY_REFERENCE: "
 141                             + reader.getLocalName() + " = " + reader.getText());
 142                 }
 143             }
 144 
 145             // expected success
 146 
 147             // should have see DTD Event
 148             if (!dtdEventOccured) {
 149                 Assert.fail("Unexpected failure: did not see DTD event");
 150             }
 151 
 152             // should have seen an ENITY_REFERENCE Event
 153             if (!entityReferenceEventOccured) {
 154                 Assert.fail("Unexpected failure: did not see ENTITY_REFERENCE event");
 155             }
 156         } catch (Exception e) {
 157             // unexpected failure
 158             System.err.println("Exception with reader.getEventType(): " + reader.getEventType());
 159             e.printStackTrace();
 160             Assert.fail("Unexpected failure with " + propertyValues + ", " + e.toString());
 161         }
 162     }
 163 }