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.FILE_SEP;
  27 import static jaxp.library.JAXPTestUtilities.USER_DIR;
  28 import static jaxp.library.JAXPTestUtilities.failUnexpected;
  29 
  30 import java.io.File;
  31 import java.io.FileInputStream;
  32 import java.io.IOException;
  33 
  34 import javax.xml.parsers.ParserConfigurationException;
  35 import javax.xml.parsers.SAXParser;
  36 import javax.xml.parsers.SAXParserFactory;
  37 
  38 import org.testng.annotations.BeforeTest;
  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 {
  49 
  50     /**
  51      * Copy necessary dtd files to user directory before any test run.
  52      */
  53     @BeforeTest
  54     protected void setup() throws IOException {
  55         TestUtils.copyFiles(TestUtils.XML_DIR, USER_DIR, "firstdtd.dtd");
  56     }
  57 
  58     /**
  59      * Test case with FileInputStream null, parsing should fail and throw
  60      * IllegalArgumentException.
  61      *
  62      * @throws IllegalArgumentException
  63      */
  64     @Test(expectedExceptions = IllegalArgumentException.class)
  65     public void testParse01() throws IllegalArgumentException {
  66         try {
  67             FileInputStream instream = null;
  68             HandlerBase handler = new HandlerBase();
  69             SAXParserFactory spf = SAXParserFactory.newInstance();
  70             SAXParser saxparser = spf.newSAXParser();
  71             saxparser.parse(instream, handler);
  72         } catch (ParserConfigurationException | SAXException | IOException e) {
  73             failUnexpected(e);
  74         }
  75     }
  76 
  77     /**
  78      * Testcase with an error in xml file, parsing should fail and throw
  79      * SAXException.
  80      *
  81      * @throws SAXException
  82      */
  83     @Test(expectedExceptions = SAXException.class)
  84     public void testParse02() throws SAXException {
  85         try {
  86             SAXParserFactory spf = SAXParserFactory.newInstance();
  87             SAXParser saxparser = null;
  88             try {
  89                 saxparser = spf.newSAXParser();
  90             } catch (SAXException e) {
  91                 failUnexpected(e);
  92             }
  93             HandlerBase handler = new HandlerBase();
  94             FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "invalid.xml"));
  95             saxparser.parse(instream, handler);
  96         } catch (ParserConfigurationException | IOException e) {
  97             failUnexpected(e);
  98         }
  99     }
 100 
 101     /**
 102      * Testcase with a valid in xml file, parser should parse the xml document.
 103      */
 104     @Test
 105     public void testParse03() {
 106         try {
 107             SAXParserFactory spf = SAXParserFactory.newInstance();
 108             SAXParser saxparser = spf.newSAXParser();
 109             HandlerBase handler = new HandlerBase();
 110             FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "parsertest.xml"));
 111             saxparser.parse(instream, handler);
 112         } catch (ParserConfigurationException | IOException | SAXException e) {
 113             failUnexpected(e);
 114         }
 115     }
 116 
 117     /**
 118      * Testcase with valid input stream, parser should parse the xml document
 119      * successfully.
 120      */
 121     @Test
 122     public void testParse04() {
 123         try {
 124             SAXParserFactory spf = SAXParserFactory.newInstance();
 125             SAXParser saxparser = spf.newSAXParser();
 126             HandlerBase handler = new HandlerBase();
 127             FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "correct.xml"));
 128             saxparser.parse(instream, handler);
 129         } catch (ParserConfigurationException | SAXException | IOException e) {
 130             failUnexpected(e);
 131         }
 132     }
 133 
 134     /**
 135      * Testcase with valid input source, parser should parse the xml document
 136      * successfully.
 137      */
 138     @Test
 139     public void testParse05() {
 140         try {
 141             SAXParserFactory spf = SAXParserFactory.newInstance();
 142             SAXParser saxparser = spf.newSAXParser();
 143             HandlerBase handler = new HandlerBase();
 144             String xmlPath1 = new File(TestUtils.XML_DIR).getAbsolutePath() + FILE_SEP;
 145             FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "parsertest.xml"));
 146             saxparser.parse(instream, handler, "file:///" + xmlPath1);
 147         } catch (ParserConfigurationException | SAXException | IOException e) {
 148             failUnexpected(e);
 149         }
 150     }
 151 
 152     /**
 153      * Testcase with valid input source, parser should parse the xml document
 154      * successfully.
 155      */
 156     @Test
 157     public void testParse06() {
 158         try {
 159             SAXParserFactory spf = SAXParserFactory.newInstance();
 160             SAXParser saxparser = spf.newSAXParser();
 161             HandlerBase handler = new HandlerBase();
 162             FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "parsertest.xml"));
 163             saxparser.parse(instream, handler, null);
 164         } catch (ParserConfigurationException | SAXException | IOException e) {
 165             failUnexpected(e);
 166         }
 167     }
 168 
 169     /**
 170      * Testcase with uri null, parsing should fail and throw
 171      * IllegalArgumentException.
 172      *
 173      * @throws IllegalArgumentException
 174      */
 175     @Test(expectedExceptions = IllegalArgumentException.class)
 176     public void testParse07() throws IllegalArgumentException {
 177         try {
 178             String uri = null;
 179             SAXParserFactory spf = SAXParserFactory.newInstance();
 180             SAXParser saxparser = spf.newSAXParser();
 181             HandlerBase handler = new HandlerBase();
 182             saxparser.parse(uri, handler);
 183         } catch (ParserConfigurationException | SAXException | IOException e) {
 184             failUnexpected(e);
 185         }
 186     }
 187 
 188     /**
 189      * Testcase with non-existant uri, parsing should fail and throw
 190      * IOException.
 191      *
 192      * @throws SAXException
 193      * @throws IOException
 194      */
 195     @Test(expectedExceptions = { SAXException.class, IOException.class })
 196     public void testParse08() throws SAXException, IOException {
 197         try {
 198             String uri = " ";
 199             SAXParserFactory spf = SAXParserFactory.newInstance();
 200             SAXParser saxparser = null;
 201             try {
 202                 saxparser = spf.newSAXParser();
 203             } catch (SAXException e) {
 204                 failUnexpected(e);
 205             }
 206             HandlerBase handler = new HandlerBase();
 207             saxparser.parse(uri, handler);
 208         } catch (ParserConfigurationException e) {
 209             failUnexpected(e);
 210         }
 211     }
 212 
 213     /**
 214      * Testcase with proper uri, parser should parse successfully.
 215      */
 216     @Test
 217     public void testParse09() {
 218         try {
 219             File file = new File(TestUtils.XML_DIR, "correct.xml");
 220             String Absolutepath = file.getAbsolutePath();
 221             String newAbsolutePath = Absolutepath;
 222             if (File.separatorChar == '\\')
 223                 newAbsolutePath = Absolutepath.replace('\\', '/');
 224             String uri = "file:///" + newAbsolutePath;
 225             HandlerBase handler = null;
 226             SAXParserFactory spf = SAXParserFactory.newInstance();
 227             SAXParser saxparser = spf.newSAXParser();
 228             handler = new HandlerBase();
 229             saxparser.parse(uri, handler);
 230         } catch (ParserConfigurationException | SAXException | IOException e) {
 231             failUnexpected(e);
 232         }
 233     }
 234 
 235     /**
 236      * Testcase with File null, parsing should fail and throw
 237      * IllegalArgumentException.
 238      *
 239      * @throws IllegalArgumentException
 240      */
 241     @Test(expectedExceptions = IllegalArgumentException.class)
 242     public void testParse10() throws IllegalArgumentException {
 243         try {
 244             File file = null;
 245             SAXParserFactory spf = SAXParserFactory.newInstance();
 246             SAXParser saxparser = spf.newSAXParser();
 247             HandlerBase handler = new HandlerBase();
 248             saxparser.parse(file, handler);
 249         } catch (ParserConfigurationException | SAXException | IOException e) {
 250             failUnexpected(e);
 251         }
 252     }
 253 
 254     /**
 255      * Testcase with empty string as File, parsing should fail and throw
 256      * SAXException.
 257      *
 258      * @throws SAXException
 259      */
 260     @Test(expectedExceptions = SAXException.class)
 261     public void testParse11() throws SAXException {
 262         try {
 263             SAXParserFactory spf = SAXParserFactory.newInstance();
 264             SAXParser saxparser = null;
 265             try {
 266                 saxparser = spf.newSAXParser();
 267             } catch (SAXException e) {
 268                 failUnexpected(e);
 269             }
 270             HandlerBase handler = new HandlerBase();
 271             File file = new File("");
 272             saxparser.parse(file, handler);
 273         } catch (ParserConfigurationException | IOException e) {
 274             failUnexpected(e);
 275         }
 276     }
 277 
 278     /**
 279      * Testcase with xml file that has errors parsing should fail and throw
 280      * SAXException.
 281      *
 282      * @throws SAXException
 283      */
 284     @Test(expectedExceptions = SAXException.class)
 285     public void testParse12() throws SAXException {
 286         try {
 287             SAXParserFactory spf = SAXParserFactory.newInstance();
 288             SAXParser saxparser = null;
 289             try {
 290                 saxparser = spf.newSAXParser();
 291             } catch (SAXException e) {
 292                 failUnexpected(e);
 293             }
 294             HandlerBase handler = new HandlerBase();
 295             File file = new File(TestUtils.XML_DIR, "valid.xml");
 296             saxparser.parse(file, handler);
 297         } catch (ParserConfigurationException | IOException e) {
 298             failUnexpected(e);
 299         }
 300     }
 301 
 302     /**
 303      * Testcase with xml file that has no errors Parser should successfully
 304      * parse the xml document.
 305      */
 306     @Test
 307     public void testParse13() {
 308         try {
 309             SAXParserFactory spf = SAXParserFactory.newInstance();
 310             SAXParser saxparser = spf.newSAXParser();
 311             HandlerBase handler = new HandlerBase();
 312             File file = new File(TestUtils.XML_DIR, "correct.xml");
 313             saxparser.parse(file, handler);
 314         } catch (ParserConfigurationException | SAXException | IOException e) {
 315             failUnexpected(e);
 316         }
 317 
 318     }
 319 
 320     /**
 321      * Testcase with input source null, parsing should fail and throw
 322      * IllegalArgumentException.
 323      *
 324      * @throws IllegalArgumentException
 325      */
 326     @Test(expectedExceptions = IllegalArgumentException.class)
 327     public void testParse14() throws IllegalArgumentException {
 328         try {
 329             InputSource is = null;
 330             SAXParserFactory spf = SAXParserFactory.newInstance();
 331             SAXParser saxparser = spf.newSAXParser();
 332             HandlerBase handler = new HandlerBase();
 333             saxparser.parse(is, handler);
 334         } catch (ParserConfigurationException | SAXException | IOException e) {
 335             failUnexpected(e);
 336         }
 337     }
 338 
 339     /**
 340      * Testcase with input source attached an invaild xml, parsing should fail
 341      * and throw SAXException.
 342      *
 343      * @throws SAXException
 344      */
 345     @Test(expectedExceptions = SAXException.class)
 346     public void testParse15() throws SAXException {
 347         try {
 348             SAXParserFactory spf = SAXParserFactory.newInstance();
 349             SAXParser saxparser = null;
 350             try {
 351                 saxparser = spf.newSAXParser();
 352             } catch (SAXException e) {
 353                 failUnexpected(e);
 354             }
 355             HandlerBase handler = new HandlerBase();
 356             FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "invalid.xml"));
 357             InputSource is = new InputSource(instream);
 358             saxparser.parse(is, handler);
 359         } catch (ParserConfigurationException | IOException e) {
 360             failUnexpected(e);
 361         }
 362     }
 363 
 364     /**
 365      * Testcase with input source attached an vaild xml, parser should
 366      * successfully parse the xml document.
 367      */
 368     @Test
 369     public void testParse16() {
 370         try {
 371             SAXParserFactory spf = SAXParserFactory.newInstance();
 372             SAXParser saxparser = spf.newSAXParser();
 373             HandlerBase handler = new HandlerBase();
 374             FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "correct.xml"));
 375             InputSource is = new InputSource(instream);
 376             saxparser.parse(is, handler);
 377         } catch (ParserConfigurationException | SAXException | IOException e) {
 378             failUnexpected(e);
 379         }
 380     }
 381 
 382     /**
 383      * Testcase with FileInputStream null, parsing should fail and throw
 384      * IllegalArgumentException.
 385      *
 386      * @throws IllegalArgumentException
 387      */
 388     @Test(expectedExceptions = IllegalArgumentException.class)
 389     public void testParse17() throws IllegalArgumentException {
 390         try {
 391             FileInputStream instream = null;
 392             SAXParserFactory spf = SAXParserFactory.newInstance();
 393             SAXParser saxparser = spf.newSAXParser();
 394             DefaultHandler handler = new DefaultHandler();
 395             saxparser.parse(instream, handler);
 396         } catch (ParserConfigurationException | SAXException | IOException e) {
 397             failUnexpected(e);
 398         }
 399     }
 400 
 401     /**
 402      * Testcase with an error in xml file, parsing should fail and throw
 403      * SAXException.
 404      *
 405      * @throws SAXException
 406      */
 407     @Test(expectedExceptions = SAXException.class)
 408     public void testParse18() throws SAXException {
 409         try {
 410             SAXParserFactory spf = SAXParserFactory.newInstance();
 411             SAXParser saxparser = null;
 412             try {
 413                 saxparser = spf.newSAXParser();
 414             } catch (SAXException e) {
 415                 failUnexpected(e);
 416             }
 417             DefaultHandler handler = new DefaultHandler();
 418             FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "invalid.xml"));
 419             saxparser.parse(instream, handler);
 420         } catch (ParserConfigurationException | IOException e) {
 421             failUnexpected(e);
 422         }
 423     }
 424 
 425     /**
 426      * Testcase with valid input stream, parser should parse the xml document
 427      * successfully.
 428      */
 429     @Test
 430     public void testParse19() {
 431         try {
 432             SAXParserFactory spf = SAXParserFactory.newInstance();
 433             SAXParser saxparser = spf.newSAXParser();
 434             DefaultHandler handler = new DefaultHandler();
 435             FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "parsertest.xml"));
 436             saxparser.parse(instream, handler);
 437         } catch (ParserConfigurationException | IOException | SAXException e) {
 438             failUnexpected(e);
 439         }
 440     }
 441 
 442     /**
 443      * Testcase with valid input stream, parser should parse the xml document
 444      * successfully.
 445      */
 446     @Test
 447     public void testParse20() {
 448         try {
 449             SAXParserFactory spf = SAXParserFactory.newInstance();
 450             SAXParser saxparser = spf.newSAXParser();
 451             DefaultHandler handler = new DefaultHandler();
 452             FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "correct.xml"));
 453             saxparser.parse(instream, handler);
 454         } catch (ParserConfigurationException | SAXException | IOException e) {
 455             failUnexpected(e);
 456         }
 457     }
 458 
 459     /**
 460      * Testcase with valid input source, parser should parse the xml document
 461      * successfully.
 462      */
 463     @Test
 464     public void testParse21() {
 465         try {
 466             SAXParserFactory spf = SAXParserFactory.newInstance();
 467             SAXParser saxparser = spf.newSAXParser();
 468             DefaultHandler handler = new DefaultHandler();
 469             String xmlPath1 = new File(TestUtils.XML_DIR).getAbsolutePath() + FILE_SEP;
 470             FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "parsertest.xml"));
 471             saxparser.parse(instream, handler, "file:///" + xmlPath1);
 472         } catch (ParserConfigurationException | SAXException | IOException e) {
 473             failUnexpected(e);
 474         }
 475 
 476     }
 477 
 478     /**
 479      * Testcase with valid input source, parser should parse the xml document
 480      * successfully.
 481      */
 482     @Test
 483     public void testParse22() {
 484         try {
 485             SAXParserFactory spf = SAXParserFactory.newInstance();
 486             SAXParser saxparser = spf.newSAXParser();
 487             DefaultHandler handler = new DefaultHandler();
 488             FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "parsertest.xml"));
 489             saxparser.parse(instream, handler, null);
 490         } catch (ParserConfigurationException | IOException | SAXException e) {
 491             failUnexpected(e);
 492         }
 493     }
 494 
 495     /**
 496      * Testcase with uri null, parsing should fail and throw
 497      * IllegalArgumentException.
 498      *
 499      * @throws IllegalArgumentException
 500      */
 501     @Test(expectedExceptions = IllegalArgumentException.class)
 502     public void testParse23() throws IllegalArgumentException {
 503         try {
 504             String uri = null;
 505             SAXParserFactory spf = SAXParserFactory.newInstance();
 506             SAXParser saxparser = spf.newSAXParser();
 507             DefaultHandler handler = new DefaultHandler();
 508             saxparser.parse(uri, handler);
 509         } catch (ParserConfigurationException | SAXException | IOException e) {
 510             failUnexpected(e);
 511         }
 512     }
 513 
 514     /**
 515      * Testcase with non-existant uri, parsing should fail and throw
 516      * SAXException or IOException.
 517      *
 518      * @throws SAXException
 519      * @throws IOException
 520      */
 521     @Test(expectedExceptions = { SAXException.class, IOException.class })
 522     public void testParse24() throws SAXException, IOException {
 523         try {
 524             SAXParserFactory spf = SAXParserFactory.newInstance();
 525             SAXParser saxparser = null;
 526             String uri = " ";
 527             try {
 528                 saxparser = spf.newSAXParser();
 529             } catch (SAXException e) {
 530                 failUnexpected(e);
 531             }
 532             DefaultHandler handler = new DefaultHandler();
 533             saxparser.parse(uri, handler);
 534         } catch (ParserConfigurationException e) {
 535             failUnexpected(e);
 536         }
 537     }
 538 
 539     /**
 540      * Testcase with proper uri, parser should parse successfully.
 541      */
 542     @Test
 543     public void testParse25() {
 544         try {
 545             File file = new File(TestUtils.XML_DIR, "correct.xml");
 546             String Absolutepath = file.getAbsolutePath();
 547             String newAbsolutePath = Absolutepath;
 548             if (File.separatorChar == '\\')
 549                 newAbsolutePath = Absolutepath.replace('\\', '/');
 550             String uri = "file:///" + newAbsolutePath;
 551 
 552             SAXParserFactory spf = SAXParserFactory.newInstance();
 553             SAXParser saxparser = spf.newSAXParser();
 554             DefaultHandler handler = new DefaultHandler();
 555             saxparser.parse(uri, handler);
 556         } catch (ParserConfigurationException | SAXException | IOException e) {
 557             failUnexpected(e);
 558         }
 559     }
 560 
 561     /**
 562      * Testcase with File null, parsing should fail and throw
 563      * IllegalArgumentException.
 564      *
 565      * @throws IllegalArgumentException
 566      */
 567     @Test(expectedExceptions = IllegalArgumentException.class)
 568     public void testParse26() throws IllegalArgumentException {
 569         try {
 570             SAXParserFactory spf = SAXParserFactory.newInstance();
 571             SAXParser saxparser = spf.newSAXParser();
 572             DefaultHandler handler = new DefaultHandler();
 573             saxparser.parse((File) null, handler);
 574         } catch (ParserConfigurationException | SAXException | IOException e) {
 575             failUnexpected(e);
 576         }
 577     }
 578 
 579     /**
 580      * Testcase with empty string as File, parsing should fail and throw
 581      * SAXException.
 582      *
 583      * @throws SAXException
 584      */
 585     @Test(expectedExceptions = SAXException.class)
 586     public void testParse27() throws SAXException {
 587         try {
 588             SAXParserFactory spf = SAXParserFactory.newInstance();
 589             SAXParser saxparser = null;
 590             try {
 591                 saxparser = spf.newSAXParser();
 592             } catch (SAXException e) {
 593                 failUnexpected(e);
 594             }
 595             DefaultHandler handler = new DefaultHandler();
 596             File file = new File("");
 597             saxparser.parse(file, handler);
 598         } catch (ParserConfigurationException | IOException e) {
 599             failUnexpected(e);
 600         }
 601     }
 602 
 603     /**
 604      * Testcase with xml file that has errors, parsing should fail and throw
 605      * SAXException.
 606      *
 607      * @throws SAXException
 608      */
 609     @Test(expectedExceptions = SAXException.class)
 610     public void testParse28() throws SAXException {
 611         try {
 612             SAXParserFactory spf = SAXParserFactory.newInstance();
 613             SAXParser saxparser = null;
 614             try {
 615                 saxparser = spf.newSAXParser();
 616             } catch (SAXException e) {
 617                 failUnexpected(e);
 618             }
 619             DefaultHandler handler = new DefaultHandler();
 620             File file = new File(TestUtils.XML_DIR, "valid.xml");
 621             saxparser.parse(file, handler);
 622         } catch (ParserConfigurationException | IOException e) {
 623             failUnexpected(e);
 624         }
 625     }
 626 
 627     /**
 628      * Testcase with xml file that has no errors, parser should successfully
 629      * parse the xml document.
 630      */
 631     @Test
 632     public void testParse29() {
 633         try {
 634             SAXParserFactory spf = SAXParserFactory.newInstance();
 635             SAXParser saxparser = spf.newSAXParser();
 636             DefaultHandler handler = new DefaultHandler();
 637             File file = new File(TestUtils.XML_DIR, "correct.xml");
 638             saxparser.parse(file, handler);
 639         } catch (ParserConfigurationException | SAXException | IOException e) {
 640             failUnexpected(e);
 641         }
 642     }
 643 
 644     /**
 645      * Testcase with input source null, parsing should fail and throw
 646      * IllegalArgumentException.
 647      *
 648      * @throws IllegalArgumentException
 649      */
 650     @Test(expectedExceptions = IllegalArgumentException.class)
 651     public void testParse30() throws IllegalArgumentException {
 652         try {
 653             InputSource is = null;
 654             SAXParserFactory spf = SAXParserFactory.newInstance();
 655             SAXParser saxparser = spf.newSAXParser();
 656             DefaultHandler handler = new DefaultHandler();
 657             saxparser.parse(is, handler);
 658         } catch (ParserConfigurationException | SAXException | IOException e) {
 659             failUnexpected(e);
 660         }
 661     }
 662 
 663     /**
 664      * Testcase with an invalid xml file, parser should throw SAXException.
 665      *
 666      * @throws SAXException
 667      */
 668     @Test(expectedExceptions = SAXException.class)
 669     public void testParse31() throws SAXException {
 670         try {
 671             SAXParserFactory spf = SAXParserFactory.newInstance();
 672             SAXParser saxparser = null;
 673             try {
 674                 saxparser = spf.newSAXParser();
 675             } catch (SAXException e) {
 676                 failUnexpected(e);
 677             }
 678             DefaultHandler handler = new DefaultHandler();
 679             FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "invalid.xml"));
 680             InputSource is = new InputSource(instream);
 681             saxparser.parse(is, handler);
 682         } catch (ParserConfigurationException | IOException e) {
 683             failUnexpected(e);
 684         }
 685     }
 686 
 687     /**
 688      * Test case to parse an xml file that not use namespaces.
 689      */
 690     @Test
 691     public void testParse32() {
 692         try {
 693             SAXParserFactory spf = SAXParserFactory.newInstance();
 694             SAXParser saxparser = spf.newSAXParser();
 695             DefaultHandler handler = new DefaultHandler();
 696             FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "correct.xml"));
 697             InputSource is = new InputSource(instream);
 698             saxparser.parse(is, handler);
 699         } catch (ParserConfigurationException | SAXException | IOException e) {
 700             failUnexpected(e);
 701         }
 702     }
 703 
 704     /**
 705      * Test case to parse an xml file that uses namespaces.
 706      */
 707     @Test
 708     public void testParse33() {
 709         try {
 710             SAXParserFactory spf = SAXParserFactory.newInstance();
 711             spf.setNamespaceAware(true);
 712             SAXParser saxparser = spf.newSAXParser();
 713             HandlerBase handler = new HandlerBase();
 714             FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "ns4.xml"));
 715             saxparser.parse(instream, handler);
 716         } catch (ParserConfigurationException | SAXException | IOException e) {
 717             failUnexpected(e);
 718         }
 719     }
 720 }