1 /*
   2  * Copyright (c) 1999, 2016, 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 import static org.testng.Assert.assertFalse;
  26 import static org.testng.Assert.assertTrue;
  27 
  28 import javax.xml.parsers.SAXParserFactory;
  29 
  30 import org.testng.annotations.Listeners;
  31 import org.testng.annotations.Test;
  32 
  33 /**
  34  * Class containing the test cases for SAXParserFactory API.
  35  */
  36 /*
  37  * @test
  38  * @library /javax/xml/jaxp/libs
  39  * @run testng/othervm -DrunSecMngr=true javax.xml.parsers.ptests.SAXParserFactTest
  40  * @run testng/othervm javax.xml.parsers.ptests.SAXParserFactTest
  41  */
  42 @Listeners({jaxp.library.BasePolicy.class})
  43 public class SAXParserFactTest {
  44 
  45     private static final String NAMESPACES = "http://xml.org/sax/features/namespaces";
  46     private static final String NAMESPACE_PREFIXES = "http://xml.org/sax/features/namespace-prefixes";
  47     private static final String STRING_INTERNING = "http://xml.org/sax/features/string-interning";
  48     private static final String VALIDATION = "http://xml.org/sax/features/validation";
  49     private static final String EXTERNAL_G_ENTITIES = "http://xml.org/sax/features/external-general-entities";
  50     private static final String EXTERNAL_P_ENTITIES = "http://xml.org/sax/features/external-parameter-entities";
  51 
  52     /**
  53      * Test if newSAXParser() method returns SAXParser.
  54      * @throws Exception If any errors occur.
  55      */
  56     @Test
  57     public void testParser01() throws Exception {
  58         SAXParserFactory spf = SAXParserFactory.newInstance();
  59         spf.newSAXParser();
  60     }
  61 
  62     /**
  63      * Test the default functionality (No validation) of the parser.
  64      */
  65     @Test
  66     public void testValidate01() {
  67         SAXParserFactory spf = SAXParserFactory.newInstance();
  68         assertFalse(spf.isValidating());
  69     }
  70 
  71     /**
  72      * Test the functionality of setValidating and isvalidating
  73      * methods.
  74      */
  75     @Test
  76     public void testValidate02() {
  77         SAXParserFactory spf = SAXParserFactory.newInstance();
  78         spf.setValidating(true);
  79         assertTrue(spf.isValidating());
  80     }
  81 
  82     /**
  83      * Parser should not be namespace-aware by default.
  84      */
  85     @Test
  86     public void testNamespace01() {
  87         SAXParserFactory spf = SAXParserFactory.newInstance();
  88         assertFalse(spf.isNamespaceAware());
  89     }
  90 
  91     /**
  92      * Test the functionality of setNamespaceAware and
  93      * isNamespaceAware methods.
  94      */
  95     @Test
  96     public void testNamespace02() {
  97         SAXParserFactory spf = SAXParserFactory.newInstance();
  98         spf.setNamespaceAware(true);
  99         assertTrue(spf.isNamespaceAware());
 100     }
 101 
 102     /**
 103      * Test the functionality of setNamespaceAware and getFeature()
 104      * methods for namespaces property.
 105      * @throws Exception If any errors occur.
 106      */
 107     @Test
 108     public void testFeature01() throws Exception {
 109         SAXParserFactory spf = SAXParserFactory.newInstance();
 110         assertFalse(spf.getFeature(NAMESPACES));
 111 
 112         spf.setNamespaceAware(true);
 113         assertTrue(spf.getFeature(NAMESPACES));
 114     }
 115 
 116     /**
 117      * Test the functionality of setFeature and getFeature methods
 118      * for namespaces property.
 119      * @throws Exception If any errors occur.
 120      */
 121     @Test
 122     public void testFeature02() throws Exception {
 123         SAXParserFactory spf = SAXParserFactory.newInstance();
 124 
 125         spf.setFeature(NAMESPACES, true);
 126         assertTrue(spf.getFeature(NAMESPACES));
 127 
 128         spf.setFeature(NAMESPACES, false);
 129         assertFalse(spf.getFeature(NAMESPACES));
 130     }
 131 
 132     /**
 133      * Test the functionality of setFeature and getFeature methods
 134      * for namespace-prefixes property.
 135      * @throws Exception If any errors occur.
 136      */
 137     @Test
 138     public void testFeature03() throws Exception {
 139         SAXParserFactory spf = SAXParserFactory.newInstance();
 140 
 141         spf.setFeature(NAMESPACE_PREFIXES, true);
 142         assertTrue(spf.getFeature(NAMESPACE_PREFIXES));
 143 
 144         spf.setFeature(NAMESPACE_PREFIXES, false);
 145         assertFalse(spf.getFeature(NAMESPACE_PREFIXES));
 146     }
 147 
 148     /**
 149      * Test the functionality of getFeature method for
 150      * string-interning property.
 151      * @throws Exception If any errors occur.
 152      */
 153     @Test
 154     public void testFeature04() throws Exception {
 155         SAXParserFactory spf = SAXParserFactory.newInstance();
 156         assertTrue(spf.getFeature(STRING_INTERNING));
 157     }
 158 
 159     /**
 160      * Test the functionality of getFeature and setValidating
 161      * methods for validation property.
 162      * @throws Exception If any errors occur.
 163      */
 164     @Test
 165     public void testFeature05() throws Exception {
 166         SAXParserFactory spf = SAXParserFactory.newInstance();
 167         assertFalse(spf.getFeature(VALIDATION));
 168         spf.setValidating(true);
 169         assertTrue(spf.getFeature(VALIDATION));
 170     }
 171 
 172     /**
 173      * Test the functionality of setFeature and getFeature methods
 174      * for validation property.
 175      * @throws Exception If any errors occur.
 176      */
 177     @Test
 178     public void testFeature06() throws Exception {
 179         SAXParserFactory spf = SAXParserFactory.newInstance();
 180         spf.setFeature(VALIDATION, true);
 181         assertTrue(spf.getFeature(VALIDATION));
 182         spf.setFeature(VALIDATION, false);
 183         assertFalse(spf.getFeature(VALIDATION));
 184     }
 185 
 186     /**
 187      * Test the functionality of getFeature method for
 188      * external-general-entities property.
 189      * @throws Exception If any errors occur.
 190      */
 191     @Test
 192     public void testFeature07() throws Exception {
 193         SAXParserFactory spf = SAXParserFactory.newInstance();
 194         assertTrue(spf.getFeature(EXTERNAL_G_ENTITIES));
 195     }
 196 
 197     /**
 198      * Test the functionality of setFeature and getFeature methods
 199      * for external-general-entities property.
 200      * @throws Exception If any errors occur.
 201      */
 202     @Test
 203     public void testFeature08() throws Exception {
 204         SAXParserFactory spf = SAXParserFactory.newInstance();
 205         spf.setFeature(EXTERNAL_G_ENTITIES, false);
 206         assertFalse(spf.getFeature(EXTERNAL_G_ENTITIES));
 207     }
 208 
 209     /**
 210      * Test the functionality of getFeature method for
 211      * external-parameter-entities property.
 212      * @throws Exception If any errors occur.
 213      */
 214     @Test
 215     public void testFeature09() throws Exception {
 216         SAXParserFactory spf = SAXParserFactory.newInstance();
 217         assertTrue(spf.getFeature(EXTERNAL_P_ENTITIES));
 218     }
 219 
 220     /**
 221      * Test the functionality of setFeature method for
 222      * external-parameter-entitie property.
 223      * @throws Exception If any errors occur.
 224      */
 225     @Test
 226     public void testFeature10() throws Exception {
 227         SAXParserFactory spf = SAXParserFactory.newInstance();
 228         spf.setFeature(EXTERNAL_P_ENTITIES, false);
 229         assertFalse(spf.getFeature(EXTERNAL_P_ENTITIES));
 230     }
 231 }