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