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