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 */ 71 @Test(expectedExceptions = IllegalArgumentException.class, 72 dataProvider = "parser-provider") 73 public void testParse01(SAXParser saxparser) throws Exception { 74 FileInputStream instream = null; 75 saxparser.parse(instream, new HandlerBase()); 76 } 77 78 /** 79 * Test with by setting URI as null, parsing should fail and throw 80 * IllegalArgumentException. 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 * 148 * @param saxparser a SAXParser instance. 149 * @throws Exception If any errors occur. 150 */ 151 @Test(expectedExceptions = IllegalArgumentException.class, 152 dataProvider = "parser-provider") 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 }