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