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.FilePermission;
  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 import static javax.xml.parsers.ptests.TestUtils.XML_DIR;
  38 import jaxp.library.JAXPBaseTest;
  39 import org.testng.annotations.AfterGroups;
  40 import org.testng.annotations.BeforeGroups;
  41 
  42 import org.testng.annotations.DataProvider;
  43 import org.testng.annotations.Test;
  44 import org.xml.sax.SAXException;
  45 
  46 /**
  47  * Class contains the test cases for SAXParser API
  48  */
  49 public class SAXParserTest03 extends JAXPBaseTest {
  50 
  51     /**
  52      * Provide SAXParserFactory.
  53      *
  54      * @return a dimensional contains.
  55      */
  56     @DataProvider(name = "input-provider")
  57     public Object[][] getFactory() {
  58         SAXParserFactory spf = SAXParserFactory.newInstance();
  59         spf.setValidating(true);
  60         return new Object[][] { { spf, MyErrorHandler.newInstance() } };
  61     }
  62     
  63     /**
  64      * Save system property for restoring.
  65      */
  66     @BeforeGroups (groups = {"readLocalFiles"})
  67     public void setFilePermissions() {
  68         setPermissions(new FilePermission(XML_DIR + "/-",
  69                 "read"));
  70     }
  71     
  72     /**
  73      * Restore the system property.
  74      */
  75     @AfterGroups (groups = {"readLocalFiles"})
  76     public void restoreFilePermissions() {
  77         setPermissions();
  78     }
  79 
  80     /**
  81      * parsertest.xml holds a valid document. This method tests the validating
  82      * parser.
  83      * 
  84      * @param spf a Parser factory.
  85      * @param handler an error handler for capturing events.
  86      * @throws SAXException If any parse errors occur.
  87      * @throws ParserConfigurationException in case of ServiceConfigurationError
  88      * service configuration error or if the implementation is not available or
  89      * cannot be instantiated.
  90      * @throws IOException if any I/O operation error.
  91      */
  92     @Test(groups = {"readLocalFiles"}, dataProvider = "input-provider")
  93     public void testParseValidate01(SAXParserFactory spf, MyErrorHandler handler) 
  94             throws ParserConfigurationException, SAXException, IOException {
  95             spf.newSAXParser().parse(new File(XML_DIR, "parsertest.xml"), handler);
  96             assertFalse(handler.isErrorOccured());




  97     }
  98 
  99     /**
 100      * validns.xml holds a valid document with XML namespaces in it. This method
 101      * tests the Validating parser with namespace processing on.
 102      * 
 103      * @param spf a Parser factory.
 104      * @param handler an error handler for capturing events.
 105      * @throws SAXException If any parse errors occur.
 106      * @throws ParserConfigurationException in case of ServiceConfigurationError
 107      * service configuration error or if the implementation is not available or
 108      * cannot be instantiated.
 109      * @throws IOException if any I/O operation error.
 110      */
 111     @Test(groups = {"readLocalFiles"}, dataProvider = "input-provider")
 112     public void testParseValidate02(SAXParserFactory spf, MyErrorHandler handler) 
 113             throws ParserConfigurationException, SAXException, IOException {
 114             spf.setNamespaceAware(true);
 115             spf.newSAXParser().parse(new File(XML_DIR, "validns.xml"), handler);
 116             assertFalse(handler.isErrorOccured());




 117     }
 118 
 119     /**
 120      * invalidns.xml holds an invalid document with XML namespaces in it. This
 121      * method tests the validating parser with namespace processing on. It
 122      * should throw validation error.
 123      * 
 124      * @param spf a Parser factory.
 125      * @param handler an error handler for capturing events.
 126      */
 127     @Test(groups = {"readLocalFiles"}, dataProvider = "input-provider")
 128     public void testParseValidate03(SAXParserFactory spf, MyErrorHandler handler) {
 129         try {
 130             spf.setNamespaceAware(true);
 131             SAXParser saxparser = spf.newSAXParser();
 132             saxparser.parse(new File(XML_DIR, "invalidns.xml"), handler);
 133             failUnexpected(new RuntimeException());
 134         } catch (ParserConfigurationException | SAXException | IOException e) {
 135             if (e instanceof SAXException) {
 136                 assertTrue(handler.isErrorOccured());
 137             }
 138         }
 139     }
 140 
 141 }
--- EOF ---