1 /* 2 * Copyright (c) 1999, 2016, 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.validation.ptests; 24 25 import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI; 26 import static javax.xml.validation.ptests.ValidationTestConst.XML_DIR; 27 import static org.testng.Assert.assertNotNull; 28 import static org.testng.Assert.assertNull; 29 import static org.testng.Assert.assertSame; 30 import static org.testng.Assert.assertTrue; 31 32 import java.io.ByteArrayInputStream; 33 import java.io.File; 34 import java.io.IOException; 35 import java.io.InputStream; 36 import java.net.URL; 37 import java.nio.file.Files; 38 import java.nio.file.Paths; 39 40 import javax.xml.parsers.DocumentBuilder; 41 import javax.xml.parsers.DocumentBuilderFactory; 42 import javax.xml.parsers.ParserConfigurationException; 43 import javax.xml.stream.XMLInputFactory; 44 import javax.xml.stream.XMLStreamException; 45 import javax.xml.transform.Source; 46 import javax.xml.transform.dom.DOMSource; 47 import javax.xml.transform.sax.SAXSource; 48 import javax.xml.transform.stax.StAXSource; 49 import javax.xml.transform.stream.StreamSource; 50 import javax.xml.validation.Schema; 51 import javax.xml.validation.SchemaFactory; 52 53 import jaxp.library.JAXPDataProvider; 54 55 import org.testng.annotations.BeforeClass; 56 import org.testng.annotations.DataProvider; 57 import org.testng.annotations.Listeners; 58 import org.testng.annotations.Test; 59 import org.w3c.dom.Document; 60 import org.xml.sax.ErrorHandler; 61 import org.xml.sax.InputSource; 62 import org.xml.sax.SAXException; 63 import org.xml.sax.SAXNotRecognizedException; 64 import org.xml.sax.SAXNotSupportedException; 65 import org.xml.sax.SAXParseException; 66 67 /* 68 * @test 69 * @bug 8080907 70 * @library /javax/xml/jaxp/libs 71 * @run testng/othervm -DrunSecMngr=true javax.xml.validation.ptests.SchemaFactoryTest 72 * @run testng/othervm javax.xml.validation.ptests.SchemaFactoryTest 73 * @summary Class containing the test cases for SchemaFactory 74 */ 75 @Test(singleThreaded = true) 76 @Listeners({jaxp.library.FilePolicy.class}) 77 public class SchemaFactoryTest { 78 79 @BeforeClass 80 public void setup() throws SAXException, IOException, ParserConfigurationException { 81 sf = newSchemaFactory(); 82 assertNotNull(sf); 83 84 ifac = XMLInputFactory.newInstance(); 85 86 xsd1 = Files.readAllBytes(Paths.get(XML_DIR + "test.xsd")); 87 xsd2 = Files.readAllBytes(Paths.get(XML_DIR + "test1.xsd")); 88 89 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 90 dbf.setNamespaceAware(true); 91 DocumentBuilder db = dbf.newDocumentBuilder(); 92 xsdDoc1 = db.parse(newInputStream(xsd1)); 93 xsdDoc2 = db.parse(newInputStream(xsd2)); 94 95 xml = Files.readAllBytes(Paths.get(XML_DIR + "test.xml")); 96 } 97 98 99 @DataProvider(name = "parameters") 100 public Object[][] getValidateParameters() { 101 return new Object[][] { { W3C_XML_SCHEMA_NS_URI, SCHEMA_FACTORY_CLASSNAME, null }, 102 { W3C_XML_SCHEMA_NS_URI, SCHEMA_FACTORY_CLASSNAME, this.getClass().getClassLoader() } }; 103 } 104 105 /* 106 * test for SchemaFactory.newInstance(java.lang.String schemaLanguage, 107 * java.lang.String factoryClassName, java.lang.ClassLoader classLoader) 108 * factoryClassName points to correct implementation of 109 * javax.xml.validation.SchemaFactory , should return newInstance of 110 * SchemaFactory 111 */ 112 @Test(dataProvider = "parameters") 113 public void testNewInstance(String schemaLanguage, String factoryClassName, ClassLoader classLoader) throws SAXException { 114 SchemaFactory sf = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI, SCHEMA_FACTORY_CLASSNAME, null); 115 Schema schema = sf.newSchema(); 116 assertNotNull(schema); 117 } 118 119 /* 120 * test for SchemaFactory.newInstance(java.lang.String schemaLanguage, 121 * java.lang.String factoryClassName, java.lang.ClassLoader classLoader) 122 * factoryClassName is null , should throw IllegalArgumentException 123 */ 124 @Test(expectedExceptions = IllegalArgumentException.class, dataProvider = "new-instance-neg", dataProviderClass = JAXPDataProvider.class) 125 public void testNewInstanceWithNullFactoryClassName(String factoryClassName, ClassLoader classLoader) { 126 127 SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI, factoryClassName, classLoader); 128 } 129 130 /* 131 * test for SchemaFactory.newInstance(java.lang.String schemaLanguage, 132 * java.lang.String factoryClassName, java.lang.ClassLoader classLoader) 133 * schemaLanguage is null , should throw NPE 134 */ 135 @Test(expectedExceptions = NullPointerException.class) 136 public void testNewInstanceWithNullSchemaLanguage() { 137 SchemaFactory.newInstance(null, SCHEMA_FACTORY_CLASSNAME, this.getClass().getClassLoader()); 138 } 139 140 /* 141 * test for SchemaFactory.newInstance(java.lang.String schemaLanguage, 142 * java.lang.String factoryClassName, java.lang.ClassLoader classLoader) 143 * schemaLanguage is empty , should throw IllegalArgumentException 144 */ 145 @Test(expectedExceptions = IllegalArgumentException.class) 146 public void testNewInstanceWithEmptySchemaLanguage() { 147 SchemaFactory.newInstance("", SCHEMA_FACTORY_CLASSNAME, this.getClass().getClassLoader()); 148 } 149 150 151 @Test(expectedExceptions = SAXParseException.class) 152 public void testNewSchemaDefault() throws SAXException, IOException { 153 validate(sf.newSchema()); 154 } 155 156 @Test 157 public void testNewSchemaWithFile() throws SAXException, IOException { 158 validate(sf.newSchema(new File(XML_DIR + "test.xsd"))); 159 } 160 161 @Test(expectedExceptions = NullPointerException.class) 162 public void testNewSchemaWithNullFile() throws SAXException { 163 sf.newSchema((File) null); 164 } 165 166 @DataProvider(name = "valid-source") 167 public Object[][] getValidSource() throws XMLStreamException { 168 return new Object[][] { 169 { streamSource(xsd1) }, 170 { saxSource(xsd1) }, 171 { domSource(xsdDoc1) }, 172 { staxStreamSource(xsd1) }, 173 { staxEventSource(xsd1) } }; 174 175 } 176 177 @Test(dataProvider = "valid-source") 178 public void testNewSchemaWithValidSource(Source schema) throws SAXException, IOException { 179 validate(sf.newSchema(schema)); 180 } 181 182 @DataProvider(name = "invalid-source") 183 public Object[][] getInvalidSource() { 184 return new Object[][] { 185 { nullStreamSource() }, 186 { nullSaxSource() } }; 187 } 188 189 @Test(dataProvider = "invalid-source", expectedExceptions = SAXParseException.class) 190 public void testNewSchemaWithInvalidSource(Source schema) throws SAXException { 191 sf.newSchema(schema); 192 } 193 194 @Test(expectedExceptions = NullPointerException.class) 195 public void testNewSchemaWithNullSource() throws SAXException { 196 sf.newSchema((Source)null); 197 } 198 199 @DataProvider(name = "valid-sources") 200 public Object[][] getValidSources() { 201 return new Object[][] { 202 { streamSource(xsd1), streamSource(xsd2) }, 203 { saxSource(xsd1), saxSource(xsd2) }, 204 { domSource(xsdDoc1), domSource(xsdDoc2) } }; 205 206 } 207 208 @Test(dataProvider = "valid-sources") 209 public void testNewSchemaWithValidSourceArray(Source schema1, Source schema2) throws SAXException, IOException { 210 validate(sf.newSchema(new Source[] { schema1, schema2 })); 211 } 212 213 @DataProvider(name = "invalid-sources") 214 public Object[][] getInvalidSources() { 215 return new Object[][] { 216 { streamSource(xsd1), nullStreamSource() }, 217 { nullStreamSource(), nullStreamSource() }, 218 { saxSource(xsd1), nullSaxSource() }, 219 { nullSaxSource(), nullSaxSource() } }; 220 } 221 222 @Test(dataProvider = "invalid-sources", expectedExceptions = SAXParseException.class) 223 public void testNewSchemaWithInvalidSourceArray(Source schema1, Source schema2) throws SAXException { 224 sf.newSchema(new Source[] { schema1, schema2 }); 225 } 226 227 @DataProvider(name = "null-sources") 228 public Object[][] getNullSources() { 229 return new Object[][] { 230 { new Source[] { domSource(xsdDoc1), null } }, 231 { new Source[] { null, null } }, 232 { null } }; 233 234 } 235 236 @Test(dataProvider = "null-sources", expectedExceptions = NullPointerException.class) 237 public void testNewSchemaWithNullSourceArray(Source[] schemas) throws SAXException { 238 sf.newSchema(schemas); 239 } 240 241 @Test(expectedExceptions = NullPointerException.class) 242 public void testNewSchemaWithNullUrl() throws SAXException { 243 sf.newSchema((URL) null); 244 } 245 246 247 @Test 248 public void testErrorHandler() { 249 SchemaFactory sf = newSchemaFactory(); 250 assertNull(sf.getErrorHandler(), "When SchemaFactory is created, initially ErrorHandler should not be set."); 251 252 ErrorHandler handler = new MyErrorHandler(); 253 sf.setErrorHandler(handler); 254 assertSame(sf.getErrorHandler(), handler); 255 256 sf.setErrorHandler(null); 257 assertNull(sf.getErrorHandler()); 258 } 259 260 @Test(expectedExceptions = SAXNotRecognizedException.class) 261 public void testGetUnrecognizedProperty() throws SAXNotRecognizedException, SAXNotSupportedException { 262 SchemaFactory sf = newSchemaFactory(); 263 sf.getProperty(UNRECOGNIZED_NAME); 264 265 } 266 267 @Test(expectedExceptions = SAXNotRecognizedException.class) 268 public void testSetUnrecognizedProperty() throws SAXNotRecognizedException, SAXNotSupportedException { 269 SchemaFactory sf = newSchemaFactory(); 270 sf.setProperty(UNRECOGNIZED_NAME, "test"); 271 } 272 273 @Test(expectedExceptions = NullPointerException.class) 274 public void testGetNullProperty() throws SAXNotRecognizedException, SAXNotSupportedException { 275 SchemaFactory sf = newSchemaFactory(); 276 assertNotNull(sf); 277 sf.getProperty(null); 278 279 } 280 281 @Test(expectedExceptions = NullPointerException.class) 282 public void testSetNullProperty() throws SAXNotRecognizedException, SAXNotSupportedException { 283 SchemaFactory sf = newSchemaFactory(); 284 assertNotNull(sf); 285 sf.setProperty(null, "test"); 286 } 287 288 @Test(expectedExceptions = SAXNotRecognizedException.class) 289 public void testGetUnrecognizedFeature() throws SAXNotRecognizedException, SAXNotSupportedException { 290 SchemaFactory sf = newSchemaFactory(); 291 sf.getFeature(UNRECOGNIZED_NAME); 292 293 } 294 295 @Test(expectedExceptions = SAXNotRecognizedException.class) 296 public void testSetUnrecognizedFeature() throws SAXNotRecognizedException, SAXNotSupportedException { 297 SchemaFactory sf = newSchemaFactory(); 298 sf.setFeature(UNRECOGNIZED_NAME, true); 299 } 300 301 @Test(expectedExceptions = NullPointerException.class) 302 public void testGetNullFeature() throws SAXNotRecognizedException, SAXNotSupportedException { 303 SchemaFactory sf = newSchemaFactory(); 304 assertNotNull(sf); 305 sf.getFeature(null); 306 307 } 308 309 @Test(expectedExceptions = NullPointerException.class) 310 public void testSetNullFeature() throws SAXNotRecognizedException, SAXNotSupportedException { 311 SchemaFactory sf = newSchemaFactory(); 312 assertNotNull(sf); 313 sf.setFeature(null, true); 314 } 315 316 @DataProvider(name = "source-feature") 317 public Object[][] getSourceFeature() { 318 return new Object[][] { 319 { StreamSource.FEATURE }, 320 { SAXSource.FEATURE }, 321 { DOMSource.FEATURE }, 322 { DOMSource.FEATURE } }; 323 324 } 325 326 /* 327 * Return true for each of the JAXP Source features to indicate that this 328 * SchemaFactory supports all of the built-in JAXP Source types. 329 */ 330 @Test(dataProvider = "source-feature") 331 public void testSourceFeatureGet(String sourceFeature) throws Exception { 332 assertTrue(newSchemaFactory().getFeature(sourceFeature)); 333 } 334 335 /* 336 * JAXP Source features are read-only because this SchemaFactory always 337 * supports all JAXP Source types. 338 */ 339 @Test(dataProvider = "source-feature", expectedExceptions = SAXNotSupportedException.class) 340 public void testSourceFeatureSet(String sourceFeature) throws Exception { 341 newSchemaFactory().setFeature(sourceFeature, false); 342 } 343 344 @Test(expectedExceptions = IllegalArgumentException.class) 345 public void testInvalidSchemaLanguage() { 346 final String INVALID_SCHEMA_LANGUAGE = "http://relaxng.org/ns/structure/1.0"; 347 SchemaFactory.newInstance(INVALID_SCHEMA_LANGUAGE); 348 } 349 350 @Test(expectedExceptions = NullPointerException.class) 351 public void testNullSchemaLanguage() { 352 SchemaFactory.newInstance(null); 353 } 354 355 private void validate(Schema schema) throws SAXException, IOException { 356 schema.newValidator().validate(new StreamSource(new ByteArrayInputStream(xml))); 357 } 358 private InputStream newInputStream(byte[] xsd) { 359 return new ByteArrayInputStream(xsd); 360 } 361 362 private Source streamSource(byte[] xsd) { 363 return new StreamSource(newInputStream(xsd)); 364 } 365 366 private Source nullStreamSource() { 367 return new StreamSource((InputStream) null); 368 } 369 370 private Source saxSource(byte[] xsd) { 371 return new SAXSource(new InputSource(newInputStream(xsd))); 372 } 373 374 private Source nullSaxSource() { 375 return new SAXSource(new InputSource((InputStream) null)); 376 } 377 378 private Source domSource(Document xsdDoc) { 379 return new DOMSource(xsdDoc); 380 } 381 382 private Source staxStreamSource(byte[] xsd) throws XMLStreamException { 383 return new StAXSource(ifac.createXMLStreamReader(newInputStream(xsd))); 384 } 385 386 private Source staxEventSource(byte[] xsd) throws XMLStreamException { 387 return new StAXSource(ifac.createXMLEventReader(newInputStream(xsd))); 388 } 389 390 391 private SchemaFactory newSchemaFactory() { 392 return SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI); 393 } 394 395 private static final String UNRECOGNIZED_NAME = "http://xml.org/sax/features/namespace-prefixes"; 396 397 private static final String SCHEMA_FACTORY_CLASSNAME = "com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory"; 398 399 private SchemaFactory sf; 400 private XMLInputFactory ifac; 401 private byte[] xsd1; 402 private byte[] xsd2; 403 private Document xsdDoc1; 404 private Document xsdDoc2; 405 private byte[] xml; 406 } 407