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