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 import static org.testng.Assert.assertFalse;
  28 import static org.testng.Assert.assertTrue;
  29 
  30 import java.io.File;
  31 import java.io.IOException;
  32 
  33 import javax.xml.parsers.ParserConfigurationException;
  34 import javax.xml.parsers.SAXParser;
  35 import javax.xml.parsers.SAXParserFactory;
  36 
  37 import org.testng.annotations.DataProvider;
  38 import org.testng.annotations.Test;
  39 import org.xml.sax.SAXException;
  40 
  41 /**
  42  * Class contains the test cases for SAXParser API
  43  */
  44 public class SAXParserTest03 {
  45 
  46     /**
  47      * Provide SAXParserFactory.
  48      *
  49      * @throws Exception
  50      */
  51     @DataProvider(name = "input-provider")
  52     public Object[][] getFactory() {
  53         SAXParserFactory spf = SAXParserFactory.newInstance();
  54         spf.setValidating(true);
  55         MyErrorHandler handler = MyErrorHandler.newInstance();
  56         return new Object[][] { { spf, handler } };
  57     }
  58 
  59     /**
  60      * parsertest.xml holds a valid document. This method tests the validating
  61      * parser.
  62      */
  63     @Test(dataProvider = "input-provider")
  64     public void testParseValidate01(SAXParserFactory spf, MyErrorHandler handler) {
  65         try {
  66             SAXParser saxparser = spf.newSAXParser();
  67             saxparser.parse(new File(TestUtils.XML_DIR, "parsertest.xml"), handler);
  68             assertFalse(handler.errorOccured);
  69         } catch (ParserConfigurationException | SAXException | IOException e) {
  70             failUnexpected(e);
  71         }
  72     }
  73 
  74     /**
  75      * validns.xml holds a valid document with XML namespaces in it. This method
  76      * tests the Validating parser with namespace processing on.
  77      */
  78     @Test(dataProvider = "input-provider")
  79     public void testParseValidate02(SAXParserFactory spf, MyErrorHandler handler) {
  80         try {
  81             spf.setNamespaceAware(true);
  82             SAXParser saxparser = spf.newSAXParser();
  83             saxparser.parse(new File(TestUtils.XML_DIR, "validns.xml"), handler);
  84             assertFalse(handler.errorOccured);
  85         } catch (ParserConfigurationException | SAXException | IOException e) {
  86             failUnexpected(e);
  87         }
  88     }
  89 
  90     /**
  91      * invalidns.xml holds an invalid document with XML namespaces in it. This
  92      * method tests the validating parser with namespace processing on. It
  93      * should throw validation error.
  94      */
  95     @Test(dataProvider = "input-provider")
  96     public void testParseValidate03(SAXParserFactory spf, MyErrorHandler handler) {
  97         try {
  98             spf.setNamespaceAware(true);
  99             SAXParser saxparser = spf.newSAXParser();
 100             saxparser.parse(new File(TestUtils.XML_DIR, "invalidns.xml"), handler);
 101             failUnexpected(new RuntimeException());
 102         } catch (ParserConfigurationException | SAXException | IOException e) {
 103             if (e instanceof SAXException) {
 104                 assertTrue(handler.errorOccured);
 105             }
 106         }
 107     }
 108 
 109 }