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