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 */ 65 @Test(expectedExceptions = IllegalArgumentException.class, 66 dataProvider = "parser-provider") 67 public void testParse01(SAXParser saxparser) throws Exception { 68 FileInputStream instream = null; 69 saxparser.parse(instream, new HandlerBase()); 70 } 71 72 /** 73 * Test with by setting URI as null, parsing should fail and throw 74 * IllegalArgumentException. 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 * 154 * @param saxparser a SAXParser instance. 155 * @throws Exception If any errors occur. 156 */ 157 @Test(expectedExceptions = IllegalArgumentException.class, 158 dataProvider = "parser-provider") 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 }