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