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 @Test(expectedExceptions = SAXParseException.class) 85 public void testNewSchemaDefault() throws SAXException, IOException { 86 validate(sf.newSchema()); 87 } 88 89 @Test 90 public void testNewSchemaWithFile() throws SAXException, IOException { 91 validate(sf.newSchema(new File(XML_DIR + "test.xsd"))); 92 } 93 94 @Test(expectedExceptions = NullPointerException.class) 95 public void testNewSchemaWithNullFile() throws SAXException { 96 sf.newSchema((File) null); 97 } 98 99 @DataProvider(name = "valid-source") 100 public Object[][] getValidSource() { 101 return new Object[][] { 102 { streamSource(xsd1) }, 103 { saxSource(xsd1) }, 104 { domSource(xsdDoc1) } }; 105 106 } 107 108 @Test(dataProvider = "valid-source") 109 public void testNewSchemaWithValidSource(Source schema) throws SAXException, IOException { 110 validate(sf.newSchema(schema)); 111 } 112 113 @DataProvider(name = "invalid-source") 114 public Object[][] getInvalidSource() { 115 return new Object[][] { 116 { nullStreamSource() }, 117 { nullSaxSource() } }; 118 } 119 120 @Test(dataProvider = "invalid-source", expectedExceptions = SAXParseException.class) 121 public void testNewSchemaWithInvalidSource(Source schema) throws SAXException { 122 sf.newSchema(schema); 123 } 124 125 @Test(expectedExceptions = NullPointerException.class) 126 public void testNewSchemaWithNullSource() throws SAXException { 127 sf.newSchema((Source)null); 128 } 129 130 @DataProvider(name = "valid-sources") 131 public Object[][] getValidSources() { 132 return new Object[][] { 133 { streamSource(xsd1), streamSource(xsd2) }, 134 { saxSource(xsd1), saxSource(xsd2) }, 135 { domSource(xsdDoc1), domSource(xsdDoc2) } }; 136 137 } 138 139 @Test(dataProvider = "valid-sources") 140 public void testNewSchemaWithValidSourceArray(Source schema1, Source schema2) throws SAXException, IOException { 141 validate(sf.newSchema(new Source[] { schema1, schema2 })); 142 } 143 144 @DataProvider(name = "invalid-sources") 145 public Object[][] getInvalidSources() { 146 return new Object[][] { 147 { streamSource(xsd1), nullStreamSource() }, 148 { nullStreamSource(), nullStreamSource() }, 149 { saxSource(xsd1), nullSaxSource() }, 150 { nullSaxSource(), nullSaxSource() } }; 151 } 152 153 @Test(dataProvider = "invalid-sources", expectedExceptions = SAXParseException.class) 154 public void testNewSchemaWithInvalidSourceArray(Source schema1, Source schema2) throws SAXException { 155 sf.newSchema(new Source[] { schema1, schema2 }); 156 } 157 158 @DataProvider(name = "null-sources") 159 public Object[][] getNullSources() { 160 return new Object[][] { 161 { new Source[] { domSource(xsdDoc1), null } }, 162 { new Source[] { null, null } }, 163 { null } }; 164 165 } 166 167 @Test(dataProvider = "null-sources", expectedExceptions = NullPointerException.class) 168 public void testNewSchemaWithNullSourceArray(Source[] schemas) throws SAXException { 169 sf.newSchema(schemas); 170 } 171 172 @Test(expectedExceptions = NullPointerException.class) 173 public void testNewSchemaWithNullUrl() throws SAXException { 174 sf.newSchema((URL) null); 175 } 176 177 178 @Test 179 public void testErrorHandler() { 180 SchemaFactory sf = newSchemaFactory(); 181 assertNull(sf.getErrorHandler(), "When SchemaFactory is created, initially ErrorHandler should not be set."); 182 183 ErrorHandler handler = new MyErrorHandler(); 184 sf.setErrorHandler(handler); 185 assertSame(sf.getErrorHandler(), handler); 186 187 sf.setErrorHandler(null); 188 assertNull(sf.getErrorHandler()); 189 } 190 191 @Test(expectedExceptions = SAXNotRecognizedException.class) 192 public void testGetUnrecognizedProperty() throws SAXNotRecognizedException, SAXNotSupportedException { 193 SchemaFactory sf = newSchemaFactory(); 194 sf.getProperty(UNRECOGNIZED_NAME); 195 196 } 197 198 @Test(expectedExceptions = SAXNotRecognizedException.class) 199 public void testSetUnrecognizedProperty() throws SAXNotRecognizedException, SAXNotSupportedException { 200 SchemaFactory sf = newSchemaFactory(); 201 sf.setProperty(UNRECOGNIZED_NAME, "test"); 202 } 203 204 @Test(expectedExceptions = NullPointerException.class) 205 public void testGetNullProperty() throws SAXNotRecognizedException, SAXNotSupportedException { 206 SchemaFactory sf = newSchemaFactory(); 207 assertNotNull(sf); 208 sf.getProperty(null); 209 210 } 211 212 @Test(expectedExceptions = NullPointerException.class) 213 public void testSetNullProperty() throws SAXNotRecognizedException, SAXNotSupportedException { 214 SchemaFactory sf = newSchemaFactory(); 215 assertNotNull(sf); 216 sf.setProperty(null, "test"); 217 } 218 219 @Test(expectedExceptions = SAXNotRecognizedException.class) 220 public void testGetUnrecognizedFeature() throws SAXNotRecognizedException, SAXNotSupportedException { 221 SchemaFactory sf = newSchemaFactory(); 222 sf.getFeature(UNRECOGNIZED_NAME); 223 224 } 225 226 @Test(expectedExceptions = SAXNotRecognizedException.class) 227 public void testSetUnrecognizedFeature() throws SAXNotRecognizedException, SAXNotSupportedException { 228 SchemaFactory sf = newSchemaFactory(); 229 sf.setFeature(UNRECOGNIZED_NAME, true); 230 } 231 232 @Test(expectedExceptions = NullPointerException.class) 233 public void testGetNullFeature() throws SAXNotRecognizedException, SAXNotSupportedException { 234 SchemaFactory sf = newSchemaFactory(); 235 assertNotNull(sf); 236 sf.getFeature(null); 237 238 } 239 240 @Test(expectedExceptions = NullPointerException.class) 241 public void testSetNullFeature() throws SAXNotRecognizedException, SAXNotSupportedException { 242 SchemaFactory sf = newSchemaFactory(); 243 assertNotNull(sf); 244 sf.setFeature(null, true); 245 } 246 247 @Test(expectedExceptions = IllegalArgumentException.class) 248 public void testInvalidSchemaLanguage() { 249 final String INVALID_SCHEMA_LANGUAGE = "http://relaxng.org/ns/structure/1.0"; 250 SchemaFactory.newInstance(INVALID_SCHEMA_LANGUAGE); 251 } 252 253 @Test(expectedExceptions = NullPointerException.class) 254 public void testNullSchemaLanguage() { 255 SchemaFactory.newInstance(null); 256 } 257 258 private void validate(Schema schema) throws SAXException, IOException { 259 schema.newValidator().validate(new StreamSource(new ByteArrayInputStream(xml))); 260 } 261 private InputStream newInputStream(byte[] xsd) { 262 return new ByteArrayInputStream(xsd); 263 } 264 265 private Source streamSource(byte[] xsd) { 266 return new StreamSource(newInputStream(xsd)); 267 } 268 269 private Source nullStreamSource() { 270 return new StreamSource((InputStream) null); 271 } 272 273 private Source saxSource(byte[] xsd) { 274 return new SAXSource(new InputSource(newInputStream(xsd))); 275 } 276 277 private Source nullSaxSource() { 278 return new SAXSource(new InputSource((InputStream) null)); 279 } 280 281 private Source domSource(Document xsdDoc) { 282 return new DOMSource(xsdDoc); 283 } 284 285 private SchemaFactory newSchemaFactory() { 286 return SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI); 287 } 288 289 private static final String UNRECOGNIZED_NAME = "http://xml.org/sax/features/namespace-prefixes"; 290 291 private SchemaFactory sf; 292 private byte[] xsd1; 293 private byte[] xsd2; 294 private Document xsdDoc1; 295 private Document xsdDoc2; 296 private byte[] xml; 297 }