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