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