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