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 24 package javax.xml.parsers.ptests; 25 26 import java.io.File; 27 import java.io.FileInputStream; 28 import java.io.FilePermission; 29 import java.io.IOException; 30 import javax.xml.parsers.SAXParser; 31 import javax.xml.parsers.SAXParserFactory; 32 import static javax.xml.parsers.ptests.ParserTestConst.XML_DIR; 33 import jaxp.library.JAXPFileReadOnlyBaseTest; 34 import org.testng.annotations.DataProvider; 35 import org.testng.annotations.Test; 36 import org.xml.sax.HandlerBase; 37 import org.xml.sax.InputSource; 38 import org.xml.sax.SAXException; 39 import org.xml.sax.helpers.DefaultHandler; 40 41 /** 42 * Class contains the test cases for SAXParser API 43 */ 44 public class SAXParserTest extends JAXPFileReadOnlyBaseTest { 45 /** 46 * Provide SAXParser. 47 * 48 * @return a data provider contains a SAXParser instance. 49 * @throws Exception If any errors occur. 50 */ 51 @DataProvider(name = "parser-provider") 52 public Object[][] getParser() throws Exception { 53 SAXParserFactory spf = SAXParserFactory.newInstance(); 54 SAXParser saxparser = spf.newSAXParser(); 55 return new Object[][] { { saxparser } }; 56 } 57 58 /** 59 * Test case with FileInputStream null, parsing should fail and throw 60 * IllegalArgumentException. 61 * 62 * @param saxparser a SAXParser instance. 63 * @throws Exception If any errors occur. 64 */ 75 * 76 * @param saxparser a SAXParser instance. 77 * @throws Exception If any errors occur. 78 */ 79 @Test(expectedExceptions = IllegalArgumentException.class, 80 dataProvider = "parser-provider") 81 public void testParse02(SAXParser saxparser) throws Exception { 82 String uri = null; 83 saxparser.parse(uri, new HandlerBase()); 84 } 85 86 /** 87 * Test with non-existence URI, parsing should fail and throw IOException. 88 * 89 * @param saxparser a SAXParser instance. 90 * @throws Exception If any errors occur. 91 */ 92 @Test(expectedExceptions = { SAXException.class }, 93 dataProvider = "parser-provider") 94 public void testParse03(SAXParser saxparser) throws Exception { 95 String workingDir = getSystemProperty("user.dir"); 96 setPermissions(new FilePermission(workingDir, "read")); 97 try { 98 saxparser.parse("", new HandlerBase()); 99 } finally { 100 setPermissions(); 101 } 102 } 103 104 /** 105 * Test with File null, parsing should fail and throw 106 * IllegalArgumentException. 107 * 108 * @param saxparser a SAXParser instance. 109 * @throws Exception If any errors occur. 110 */ 111 @Test(expectedExceptions = IllegalArgumentException.class, 112 dataProvider = "parser-provider") 113 public void testParse04(SAXParser saxparser) throws Exception { 114 File file = null; 115 saxparser.parse(file, new HandlerBase()); 116 } 117 118 /** 119 * Test with empty string as File, parsing should fail and throw 120 * SAXException. 121 * 122 * @param saxparser a SAXParser instance. 123 * @throws Exception If any errors occur. 124 */ 125 @Test(expectedExceptions = SAXException.class, dataProvider = "parser-provider") 126 public void testParse05(SAXParser saxparser) throws Exception { 127 String workingDir = getSystemProperty("user.dir"); 128 setPermissions(new FilePermission(workingDir, "read")); 129 try { 130 saxparser.parse(new File(""), new HandlerBase()); 131 } finally { 132 setPermissions(); 133 } 134 } 135 136 /** 137 * Test with input source null, parsing should fail and throw 138 * IllegalArgumentException. 139 * 140 * @param saxparser a SAXParser instance. 141 * @throws Exception If any errors occur. 142 */ 143 @Test(expectedExceptions = IllegalArgumentException.class, 144 dataProvider = "parser-provider") 145 public void testParse06(SAXParser saxparser) throws Exception { 146 InputSource is = null; 147 saxparser.parse(is, new HandlerBase()); 148 } 149 150 /** 151 * Test with FileInputStream null, parsing should fail and throw 152 * IllegalArgumentException. 153 * 159 public void testParse07(SAXParser saxparser) throws Exception { 160 FileInputStream instream = null; 161 saxparser.parse(instream, new DefaultHandler()); 162 } 163 164 /** 165 * Test with URI null, parsing should fail and throw 166 * IllegalArgumentException. 167 * 168 * @param saxparser a SAXParser instance. 169 * @throws Exception If any errors occur. 170 */ 171 @Test(expectedExceptions = IllegalArgumentException.class, 172 dataProvider = "parser-provider") 173 public void testParse08(SAXParser saxparser) throws Exception { 174 String uri = null; 175 saxparser.parse(uri, new DefaultHandler()); 176 } 177 178 /** 179 * Test with non-existence URI, parsing should fail and throw 180 * SAXException or IOException. 181 * 182 * @param saxparser a SAXParser instance. 183 * @throws Exception If any errors occur. 184 */ 185 @Test(expectedExceptions = { SAXException.class, IOException.class }, 186 dataProvider = "parser-provider") 187 public void testParse09(SAXParser saxparser) throws Exception { 188 String workingDir = getSystemProperty("user.dir"); 189 setPermissions(new FilePermission(workingDir + "/../-", "read")); 190 String uri = " "; 191 try { 192 saxparser.parse(uri, new DefaultHandler()); 193 } finally { 194 setPermissions(); 195 } 196 } 197 198 /** 199 * Test with empty string as File, parsing should fail and throw 200 * SAXException. 201 * 202 * @param saxparser a SAXParser instance. 203 * @throws Exception If any errors occur. 204 */ 205 @Test(expectedExceptions = SAXException.class, dataProvider = "parser-provider") 206 public void testParse10(SAXParser saxparser) throws Exception { 207 String workingDir = getSystemProperty("user.dir"); 208 setPermissions(new FilePermission(workingDir, "read")); 209 File file = new File(""); 210 try { 211 saxparser.parse(file, new DefaultHandler()); 212 } finally { 213 setPermissions(); 214 } 215 } 216 217 /** 218 * Test with File null, parsing should fail and throw 219 * IllegalArgumentException. 220 * 221 * @param saxparser a SAXParser instance. 222 * @throws Exception If any errors occur. 223 */ 224 @Test(expectedExceptions = IllegalArgumentException.class, 225 dataProvider = "parser-provider") 226 public void testParse11(SAXParser saxparser) throws Exception { 227 saxparser.parse((File) null, new DefaultHandler()); 228 } 229 230 /** 231 * Test with input source null, parsing should fail and throw 232 * IllegalArgumentException. 233 * 234 * @param saxparser a SAXParser instance. 235 * @throws Exception If any errors occur. 236 */ 237 @Test(expectedExceptions = IllegalArgumentException.class, 238 dataProvider = "parser-provider") 239 public void testParse12(SAXParser saxparser) throws Exception { 240 InputSource is = null; 241 saxparser.parse(is, new DefaultHandler()); 242 } 243 244 /** 245 * Test with an error in XML file, parsing should fail and throw 246 * SAXException. 247 * 248 * @param saxparser a SAXParser instance. 249 * @throws Exception If any errors occur. 250 */ 251 @Test(groups = {"readLocalFiles"}, expectedExceptions = SAXException.class, 252 dataProvider = "parser-provider") 253 public void testParse13(SAXParser saxparser) throws Exception { 254 try (FileInputStream instream = new FileInputStream(new File( 255 XML_DIR, "invalid.xml"))) { 256 saxparser.parse(instream, new HandlerBase()); 257 } 258 } 259 260 /** 261 * Test with a valid in XML file, parser should parse the XML document. 262 * 263 * @param saxparser a SAXParser instance. 264 * @throws Exception If any errors occur. 265 */ 266 @Test(groups = {"readLocalFiles"}, dataProvider = "parser-provider") 267 public void testParse14(SAXParser saxparser) throws Exception { 268 saxparser.parse(new File(XML_DIR, "parsertest.xml"), 269 new HandlerBase()); 270 } 271 272 /** 273 * Test with valid input stream, parser should parse the XML document 274 * successfully. 275 * 276 * @param saxparser a SAXParser instance. 277 * @throws Exception If any errors occur. 278 */ 279 @Test(groups = {"readLocalFiles"}, dataProvider = "parser-provider") 280 public void testParse15(SAXParser saxparser) throws Exception { 281 try (FileInputStream instream = new FileInputStream(new File(XML_DIR, 282 "correct.xml"))) { 283 saxparser.parse(instream, new HandlerBase()); 284 } 285 } 286 287 /** 288 * Test with valid input source, parser should parse the XML document 289 * successfully. 290 * 291 * @param saxparser a SAXParser instance. 292 * @throws Exception If any errors occur. 293 */ 294 @Test(groups = {"readLocalFiles"}, dataProvider = "parser-provider") 295 public void testParse16(SAXParser saxparser) throws Exception { 296 try (FileInputStream instream = new FileInputStream( 297 new File(XML_DIR, "parsertest.xml"))) { 298 saxparser.parse(instream, new HandlerBase(), 299 new File(XML_DIR).toURI().toASCIIString()); 300 } 301 } 302 303 /** 304 * Test with proper URI, parser should parse successfully. 305 * 306 * @param saxparser a SAXParser instance. 307 * @throws Exception If any errors occur. 308 */ 309 @Test(groups = {"readLocalFiles"}, dataProvider = "parser-provider") 310 public void testParse17(SAXParser saxparser) throws Exception { 311 File file = new File(XML_DIR, "correct.xml"); 312 saxparser.parse(file.toURI().toASCIIString(), new HandlerBase()); 313 } 314 315 /** 316 * Test with XML file that has errors parsing should fail and throw 317 * SAXException. 318 * 319 * @param saxparser a SAXParser instance. 320 * @throws Exception If any errors occur. 321 */ 322 @Test(groups = {"readLocalFiles"}, expectedExceptions = SAXException.class, 323 dataProvider = "parser-provider") 324 public void testParse18(SAXParser saxparser) throws Exception { 325 saxparser.parse(new File(XML_DIR, "valid.xml"), new HandlerBase()); 326 } 327 328 /** 329 * Test with XML file that has no errors Parser should successfully 330 * parse the XML document. 331 * 332 * @param saxparser a SAXParser instance. 333 * @throws Exception If any errors occur. 334 */ 335 @Test(groups = {"readLocalFiles"}, dataProvider = "parser-provider") 336 public void testParse19(SAXParser saxparser) throws Exception { 337 saxparser.parse(new File(XML_DIR, "correct.xml"), new HandlerBase()); 338 } 339 340 /** 341 * Test with input source attached an invalid XML, parsing should fail 342 * and throw SAXException. 343 * 344 * @param saxparser a SAXParser instance. 345 * @throws Exception If any errors occur. 346 */ 347 @Test(groups = {"readLocalFiles"}, expectedExceptions = SAXException.class, 348 dataProvider = "parser-provider") 349 public void testParse20(SAXParser saxparser) throws Exception { 350 try(FileInputStream instream = new FileInputStream(new File(XML_DIR, 351 "invalid.xml"))) { 352 saxparser.parse(new InputSource(instream), new HandlerBase()); 353 } 354 } 355 356 /** 357 * Test with input source attached an valid XML, parser should 358 * successfully parse the XML document. 359 * 360 * @param saxparser a SAXParser instance. 361 * @throws Exception If any errors occur. 362 */ 363 @Test(groups = {"readLocalFiles"}, dataProvider = "parser-provider") 364 public void testParse21(SAXParser saxparser) throws Exception { 365 try (FileInputStream instream = new FileInputStream(new File(XML_DIR, 366 "correct.xml"))) { 367 saxparser.parse(new InputSource(instream), new HandlerBase()); 368 } 369 } 370 371 /** 372 * Test with an error in xml file, parsing should fail and throw 373 * SAXException. 374 * 375 * @param saxparser a SAXParser instance. 376 * @throws Exception If any errors occur. 377 */ 378 @Test(groups = {"readLocalFiles"}, expectedExceptions = SAXException.class, 379 dataProvider = "parser-provider") 380 public void testParse22(SAXParser saxparser) throws Exception { 381 try (FileInputStream instream = new FileInputStream( 382 new File(XML_DIR, "invalid.xml"))) { 383 saxparser.parse(instream, new DefaultHandler()); 384 } 385 } 386 387 /** 388 * Test with valid input stream, parser should parse the XML document 389 * successfully. 390 * 391 * @param saxparser a SAXParser instance. 392 * @throws Exception If any errors occur. 393 */ 394 @Test(groups = {"readLocalFiles"}, dataProvider = "parser-provider") 395 public void testParse23(SAXParser saxparser) throws Exception { 396 DefaultHandler handler = new DefaultHandler(); 397 saxparser.parse(new File(XML_DIR, "parsertest.xml"), handler); 398 } 399 400 /** 401 * Test with valid input stream, parser should parse the XML document 402 * successfully. 403 * 404 * @param saxparser a SAXParser instance. 405 * @throws Exception If any errors occur. 406 */ 407 @Test(groups = {"readLocalFiles"}, dataProvider = "parser-provider") 408 public void testParse24(SAXParser saxparser) throws Exception { 409 try (FileInputStream instream = new FileInputStream(new File(XML_DIR, 410 "correct.xml"))) { 411 DefaultHandler handler = new DefaultHandler(); 412 saxparser.parse(instream, handler); 413 } 414 } 415 416 /** 417 * Test with valid input source, parser should parse the XML document 418 * successfully. 419 * 420 * @param saxparser a SAXParser instance. 421 * @throws Exception If any errors occur. 422 */ 423 @Test(groups = {"readLocalFiles"}, dataProvider = "parser-provider") 424 public void testParse25(SAXParser saxparser) throws Exception { 425 try (FileInputStream instream = new FileInputStream( 426 new File(XML_DIR, "parsertest.xml"))) { 427 saxparser.parse(instream, new DefaultHandler(), 428 new File(XML_DIR).toURI().toASCIIString()); 429 } 430 } 431 432 /** 433 * Test with proper URI, parser should parse successfully. 434 * 435 * @param saxparser a SAXParser instance. 436 * @throws Exception If any errors occur. 437 */ 438 @Test(groups = {"readLocalFiles"}, dataProvider = "parser-provider") 439 public void testParse26(SAXParser saxparser) throws Exception { 440 File file = new File(XML_DIR, "correct.xml"); 441 saxparser.parse(file.toURI().toASCIIString(), new DefaultHandler()); 442 } 443 444 /** 445 * Test with XML file that has errors, parsing should fail and throw 446 * SAXException. 447 * 448 * @param saxparser a SAXParser instance. 449 * @throws Exception If any errors occur. 450 */ 451 @Test(groups = {"readLocalFiles"}, expectedExceptions = SAXException.class, 452 dataProvider = "parser-provider") 453 public void testParse27(SAXParser saxparser) throws Exception { 454 saxparser.parse(new File(XML_DIR, "valid.xml"), new DefaultHandler()); 455 } 456 457 /** 458 * Test with XML file that has no errors, parser should successfully 459 * parse the XML document. 460 * 461 * @param saxparser a SAXParser instance. 462 * @throws Exception If any errors occur. 463 */ 464 @Test(groups = {"readLocalFiles"}, dataProvider = "parser-provider") 465 public void testParse28(SAXParser saxparser) throws Exception { 466 saxparser.parse(new File(XML_DIR, "correct.xml"), new DefaultHandler()); 467 } 468 469 /** 470 * Test with an invalid XML file, parser should throw SAXException. 471 * 472 * @param saxparser a SAXParser instance. 473 * @throws Exception If any errors occur. 474 */ 475 @Test(groups = {"readLocalFiles"}, expectedExceptions = SAXException.class, 476 dataProvider = "parser-provider") 477 public void testParse29(SAXParser saxparser) throws Exception { 478 try (FileInputStream instream = new FileInputStream( 479 new File(XML_DIR, "invalid.xml"))) { 480 saxparser.parse(new InputSource(instream), new DefaultHandler()); 481 } 482 } 483 484 /** 485 * Test case to parse an XML file that not use namespaces. 486 * 487 * @param saxparser a SAXParser instance. 488 * @throws Exception If any errors occur. 489 */ 490 @Test(groups = {"readLocalFiles"}, dataProvider = "parser-provider") 491 public void testParse30(SAXParser saxparser) throws Exception { 492 try (FileInputStream instream = new FileInputStream( 493 new File(XML_DIR, "correct.xml"))) { 494 saxparser.parse(new InputSource(instream), new DefaultHandler()); 495 } 496 } 497 498 /** 499 * Test case to parse an XML file that uses namespaces. 500 * 501 * @throws Exception If any errors occur. 502 */ 503 @Test(groups = {"readLocalFiles"}) 504 public void testParse31() throws Exception { 505 try (FileInputStream instream = new FileInputStream( 506 new File(XML_DIR, "ns4.xml"))) { 507 SAXParserFactory spf = SAXParserFactory.newInstance(); 508 spf.setNamespaceAware(true); 509 spf.newSAXParser().parse(instream, new HandlerBase()); 510 } 511 } 512 } | 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 24 package javax.xml.parsers.ptests; 25 26 import static javax.xml.parsers.ptests.ParserTestConst.XML_DIR; 27 28 import java.io.File; 29 import java.io.FileInputStream; 30 import java.io.FilePermission; 31 import java.io.IOException; 32 33 import javax.xml.parsers.SAXParser; 34 import javax.xml.parsers.SAXParserFactory; 35 36 import jaxp.library.JAXPTestUtilities; 37 38 import org.testng.annotations.DataProvider; 39 import org.testng.annotations.Listeners; 40 import org.testng.annotations.Test; 41 import org.xml.sax.HandlerBase; 42 import org.xml.sax.InputSource; 43 import org.xml.sax.SAXException; 44 import org.xml.sax.helpers.DefaultHandler; 45 46 /** 47 * Class contains the test cases for SAXParser API 48 */ 49 @Listeners({jaxp.library.FilePolicy.class}) 50 public class SAXParserTest { 51 /** 52 * Provide SAXParser. 53 * 54 * @return a data provider contains a SAXParser instance. 55 * @throws Exception If any errors occur. 56 */ 57 @DataProvider(name = "parser-provider") 58 public Object[][] getParser() throws Exception { 59 SAXParserFactory spf = SAXParserFactory.newInstance(); 60 SAXParser saxparser = spf.newSAXParser(); 61 return new Object[][] { { saxparser } }; 62 } 63 64 /** 65 * Test case with FileInputStream null, parsing should fail and throw 66 * IllegalArgumentException. 67 * 68 * @param saxparser a SAXParser instance. 69 * @throws Exception If any errors occur. 70 */ 81 * 82 * @param saxparser a SAXParser instance. 83 * @throws Exception If any errors occur. 84 */ 85 @Test(expectedExceptions = IllegalArgumentException.class, 86 dataProvider = "parser-provider") 87 public void testParse02(SAXParser saxparser) throws Exception { 88 String uri = null; 89 saxparser.parse(uri, new HandlerBase()); 90 } 91 92 /** 93 * Test with non-existence URI, parsing should fail and throw IOException. 94 * 95 * @param saxparser a SAXParser instance. 96 * @throws Exception If any errors occur. 97 */ 98 @Test(expectedExceptions = { SAXException.class }, 99 dataProvider = "parser-provider") 100 public void testParse03(SAXParser saxparser) throws Exception { 101 saxparser.parse("", new HandlerBase()); 102 } 103 104 /** 105 * Test with File null, parsing should fail and throw 106 * IllegalArgumentException. 107 * 108 * @param saxparser a SAXParser instance. 109 * @throws Exception If any errors occur. 110 */ 111 @Test(expectedExceptions = IllegalArgumentException.class, 112 dataProvider = "parser-provider") 113 public void testParse04(SAXParser saxparser) throws Exception { 114 File file = null; 115 saxparser.parse(file, new HandlerBase()); 116 } 117 118 /** 119 * Test with empty string as File, parsing should fail and throw 120 * SAXException. 121 * 122 * @param saxparser a SAXParser instance. 123 * @throws Exception If any errors occur. 124 */ 125 @Test(expectedExceptions = SAXException.class, dataProvider = "parser-provider") 126 public void testParse05(SAXParser saxparser) throws Exception { 127 saxparser.parse(new File(""), new HandlerBase()); 128 } 129 130 /** 131 * Test with input source null, parsing should fail and throw 132 * IllegalArgumentException. 133 * 134 * @param saxparser a SAXParser instance. 135 * @throws Exception If any errors occur. 136 */ 137 @Test(expectedExceptions = IllegalArgumentException.class, 138 dataProvider = "parser-provider") 139 public void testParse06(SAXParser saxparser) throws Exception { 140 InputSource is = null; 141 saxparser.parse(is, new HandlerBase()); 142 } 143 144 /** 145 * Test with FileInputStream null, parsing should fail and throw 146 * IllegalArgumentException. 147 * 153 public void testParse07(SAXParser saxparser) throws Exception { 154 FileInputStream instream = null; 155 saxparser.parse(instream, new DefaultHandler()); 156 } 157 158 /** 159 * Test with URI null, parsing should fail and throw 160 * IllegalArgumentException. 161 * 162 * @param saxparser a SAXParser instance. 163 * @throws Exception If any errors occur. 164 */ 165 @Test(expectedExceptions = IllegalArgumentException.class, 166 dataProvider = "parser-provider") 167 public void testParse08(SAXParser saxparser) throws Exception { 168 String uri = null; 169 saxparser.parse(uri, new DefaultHandler()); 170 } 171 172 /** 173 * Test with non-existence URI, parsing should fail and throw SAXException 174 * or IOException. 175 * 176 * @param saxparser 177 * a SAXParser instance. 178 * @throws Exception 179 * If any errors occur. 180 */ 181 @Test(expectedExceptions = { SAXException.class, IOException.class }, dataProvider = "parser-provider") 182 public void testParse09(SAXParser saxparser) throws Exception { 183 JAXPTestUtilities.tryRunWithTmpPermission(() -> saxparser.parse(" ", new DefaultHandler()), 184 new FilePermission(System.getProperty("user.dir") + "/ ", "read")); 185 } 186 187 /** 188 * Test with empty string as File, parsing should fail and throw 189 * SAXException. 190 * 191 * @param saxparser a SAXParser instance. 192 * @throws Exception If any errors occur. 193 */ 194 @Test(expectedExceptions = SAXException.class, dataProvider = "parser-provider") 195 public void testParse10(SAXParser saxparser) throws Exception { 196 File file = new File(""); 197 saxparser.parse(file, new DefaultHandler()); 198 } 199 200 /** 201 * Test with File null, parsing should fail and throw 202 * IllegalArgumentException. 203 * 204 * @param saxparser a SAXParser instance. 205 * @throws Exception If any errors occur. 206 */ 207 @Test(expectedExceptions = IllegalArgumentException.class, 208 dataProvider = "parser-provider") 209 public void testParse11(SAXParser saxparser) throws Exception { 210 saxparser.parse((File) null, new DefaultHandler()); 211 } 212 213 /** 214 * Test with input source null, parsing should fail and throw 215 * IllegalArgumentException. 216 * 217 * @param saxparser a SAXParser instance. 218 * @throws Exception If any errors occur. 219 */ 220 @Test(expectedExceptions = IllegalArgumentException.class, 221 dataProvider = "parser-provider") 222 public void testParse12(SAXParser saxparser) throws Exception { 223 InputSource is = null; 224 saxparser.parse(is, new DefaultHandler()); 225 } 226 227 /** 228 * Test with an error in XML file, parsing should fail and throw 229 * SAXException. 230 * 231 * @param saxparser a SAXParser instance. 232 * @throws Exception If any errors occur. 233 */ 234 @Test(expectedExceptions = SAXException.class, 235 dataProvider = "parser-provider") 236 public void testParse13(SAXParser saxparser) throws Exception { 237 try (FileInputStream instream = new FileInputStream(new File( 238 XML_DIR, "invalid.xml"))) { 239 saxparser.parse(instream, new HandlerBase()); 240 } 241 } 242 243 /** 244 * Test with a valid in XML file, parser should parse the XML document. 245 * 246 * @param saxparser a SAXParser instance. 247 * @throws Exception If any errors occur. 248 */ 249 @Test(dataProvider = "parser-provider") 250 public void testParse14(SAXParser saxparser) throws Exception { 251 saxparser.parse(new File(XML_DIR, "parsertest.xml"), 252 new HandlerBase()); 253 } 254 255 /** 256 * Test with valid input stream, parser should parse the XML document 257 * successfully. 258 * 259 * @param saxparser a SAXParser instance. 260 * @throws Exception If any errors occur. 261 */ 262 @Test(dataProvider = "parser-provider") 263 public void testParse15(SAXParser saxparser) throws Exception { 264 try (FileInputStream instream = new FileInputStream(new File(XML_DIR, 265 "correct.xml"))) { 266 saxparser.parse(instream, new HandlerBase()); 267 } 268 } 269 270 /** 271 * Test with valid input source, parser should parse the XML document 272 * successfully. 273 * 274 * @param saxparser a SAXParser instance. 275 * @throws Exception If any errors occur. 276 */ 277 @Test(dataProvider = "parser-provider") 278 public void testParse16(SAXParser saxparser) throws Exception { 279 try (FileInputStream instream = new FileInputStream( 280 new File(XML_DIR, "parsertest.xml"))) { 281 saxparser.parse(instream, new HandlerBase(), 282 new File(XML_DIR).toURI().toASCIIString()); 283 } 284 } 285 286 /** 287 * Test with proper URI, parser should parse successfully. 288 * 289 * @param saxparser a SAXParser instance. 290 * @throws Exception If any errors occur. 291 */ 292 @Test(dataProvider = "parser-provider") 293 public void testParse17(SAXParser saxparser) throws Exception { 294 File file = new File(XML_DIR, "correct.xml"); 295 saxparser.parse(file.toURI().toASCIIString(), new HandlerBase()); 296 } 297 298 /** 299 * Test with XML file that has errors parsing should fail and throw 300 * SAXException. 301 * 302 * @param saxparser a SAXParser instance. 303 * @throws Exception If any errors occur. 304 */ 305 @Test(expectedExceptions = SAXException.class, 306 dataProvider = "parser-provider") 307 public void testParse18(SAXParser saxparser) throws Exception { 308 saxparser.parse(new File(XML_DIR, "valid.xml"), new HandlerBase()); 309 } 310 311 /** 312 * Test with XML file that has no errors Parser should successfully 313 * parse the XML document. 314 * 315 * @param saxparser a SAXParser instance. 316 * @throws Exception If any errors occur. 317 */ 318 @Test(dataProvider = "parser-provider") 319 public void testParse19(SAXParser saxparser) throws Exception { 320 saxparser.parse(new File(XML_DIR, "correct.xml"), new HandlerBase()); 321 } 322 323 /** 324 * Test with input source attached an invalid XML, parsing should fail 325 * and throw SAXException. 326 * 327 * @param saxparser a SAXParser instance. 328 * @throws Exception If any errors occur. 329 */ 330 @Test(expectedExceptions = SAXException.class, 331 dataProvider = "parser-provider") 332 public void testParse20(SAXParser saxparser) throws Exception { 333 try(FileInputStream instream = new FileInputStream(new File(XML_DIR, 334 "invalid.xml"))) { 335 saxparser.parse(new InputSource(instream), new HandlerBase()); 336 } 337 } 338 339 /** 340 * Test with input source attached an valid XML, parser should 341 * successfully parse the XML document. 342 * 343 * @param saxparser a SAXParser instance. 344 * @throws Exception If any errors occur. 345 */ 346 @Test(dataProvider = "parser-provider") 347 public void testParse21(SAXParser saxparser) throws Exception { 348 try (FileInputStream instream = new FileInputStream(new File(XML_DIR, 349 "correct.xml"))) { 350 saxparser.parse(new InputSource(instream), new HandlerBase()); 351 } 352 } 353 354 /** 355 * Test with an error in xml file, parsing should fail and throw 356 * SAXException. 357 * 358 * @param saxparser a SAXParser instance. 359 * @throws Exception If any errors occur. 360 */ 361 @Test(expectedExceptions = SAXException.class, 362 dataProvider = "parser-provider") 363 public void testParse22(SAXParser saxparser) throws Exception { 364 try (FileInputStream instream = new FileInputStream( 365 new File(XML_DIR, "invalid.xml"))) { 366 saxparser.parse(instream, new DefaultHandler()); 367 } 368 } 369 370 /** 371 * Test with valid input stream, parser should parse the XML document 372 * successfully. 373 * 374 * @param saxparser a SAXParser instance. 375 * @throws Exception If any errors occur. 376 */ 377 @Test(dataProvider = "parser-provider") 378 public void testParse23(SAXParser saxparser) throws Exception { 379 DefaultHandler handler = new DefaultHandler(); 380 saxparser.parse(new File(XML_DIR, "parsertest.xml"), handler); 381 } 382 383 /** 384 * Test with valid input stream, parser should parse the XML document 385 * successfully. 386 * 387 * @param saxparser a SAXParser instance. 388 * @throws Exception If any errors occur. 389 */ 390 @Test(dataProvider = "parser-provider") 391 public void testParse24(SAXParser saxparser) throws Exception { 392 try (FileInputStream instream = new FileInputStream(new File(XML_DIR, 393 "correct.xml"))) { 394 DefaultHandler handler = new DefaultHandler(); 395 saxparser.parse(instream, handler); 396 } 397 } 398 399 /** 400 * Test with valid input source, parser should parse the XML document 401 * successfully. 402 * 403 * @param saxparser a SAXParser instance. 404 * @throws Exception If any errors occur. 405 */ 406 @Test(dataProvider = "parser-provider") 407 public void testParse25(SAXParser saxparser) throws Exception { 408 try (FileInputStream instream = new FileInputStream( 409 new File(XML_DIR, "parsertest.xml"))) { 410 saxparser.parse(instream, new DefaultHandler(), 411 new File(XML_DIR).toURI().toASCIIString()); 412 } 413 } 414 415 /** 416 * Test with proper URI, parser should parse successfully. 417 * 418 * @param saxparser a SAXParser instance. 419 * @throws Exception If any errors occur. 420 */ 421 @Test(dataProvider = "parser-provider") 422 public void testParse26(SAXParser saxparser) throws Exception { 423 File file = new File(XML_DIR, "correct.xml"); 424 saxparser.parse(file.toURI().toASCIIString(), new DefaultHandler()); 425 } 426 427 /** 428 * Test with XML file that has errors, parsing should fail and throw 429 * SAXException. 430 * 431 * @param saxparser a SAXParser instance. 432 * @throws Exception If any errors occur. 433 */ 434 @Test(expectedExceptions = SAXException.class, 435 dataProvider = "parser-provider") 436 public void testParse27(SAXParser saxparser) throws Exception { 437 saxparser.parse(new File(XML_DIR, "valid.xml"), new DefaultHandler()); 438 } 439 440 /** 441 * Test with XML file that has no errors, parser should successfully 442 * parse the XML document. 443 * 444 * @param saxparser a SAXParser instance. 445 * @throws Exception If any errors occur. 446 */ 447 @Test(dataProvider = "parser-provider") 448 public void testParse28(SAXParser saxparser) throws Exception { 449 saxparser.parse(new File(XML_DIR, "correct.xml"), new DefaultHandler()); 450 } 451 452 /** 453 * Test with an invalid XML file, parser should throw SAXException. 454 * 455 * @param saxparser a SAXParser instance. 456 * @throws Exception If any errors occur. 457 */ 458 @Test(expectedExceptions = SAXException.class, 459 dataProvider = "parser-provider") 460 public void testParse29(SAXParser saxparser) throws Exception { 461 try (FileInputStream instream = new FileInputStream( 462 new File(XML_DIR, "invalid.xml"))) { 463 saxparser.parse(new InputSource(instream), new DefaultHandler()); 464 } 465 } 466 467 /** 468 * Test case to parse an XML file that not use namespaces. 469 * 470 * @param saxparser a SAXParser instance. 471 * @throws Exception If any errors occur. 472 */ 473 @Test(dataProvider = "parser-provider") 474 public void testParse30(SAXParser saxparser) throws Exception { 475 try (FileInputStream instream = new FileInputStream( 476 new File(XML_DIR, "correct.xml"))) { 477 saxparser.parse(new InputSource(instream), new DefaultHandler()); 478 } 479 } 480 481 /** 482 * Test case to parse an XML file that uses namespaces. 483 * 484 * @throws Exception If any errors occur. 485 */ 486 @Test 487 public void testParse31() throws Exception { 488 try (FileInputStream instream = new FileInputStream( 489 new File(XML_DIR, "ns4.xml"))) { 490 SAXParserFactory spf = SAXParserFactory.newInstance(); 491 spf.setNamespaceAware(true); 492 spf.newSAXParser().parse(instream, new HandlerBase()); 493 } 494 } 495 } |