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