jaxp/test/javax/xml/jaxp/functional/javax/xml/parsers/ptests/SAXParserFactTest.java
changeset 28445 5a87f52ca380
parent 27983 f5f19fe0e83b
child 40223 64662417aa2d
equal deleted inserted replaced
28344:722378bc599e 28445:5a87f52ca380
     1 /*
     1 /*
     2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    20  * or visit www.oracle.com if you need additional information or have any
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 package javax.xml.parsers.ptests;
    24 package javax.xml.parsers.ptests;
    25 
       
    26 import static jaxp.library.JAXPTestUtilities.failUnexpected;
       
    27 import static org.testng.Assert.assertFalse;
    25 import static org.testng.Assert.assertFalse;
    28 import static org.testng.Assert.assertTrue;
    26 import static org.testng.Assert.assertTrue;
    29 
       
    30 import javax.xml.parsers.ParserConfigurationException;
       
    31 import javax.xml.parsers.SAXParser;
       
    32 import javax.xml.parsers.SAXParserFactory;
    27 import javax.xml.parsers.SAXParserFactory;
    33 
    28 import jaxp.library.JAXPBaseTest;
    34 import org.testng.annotations.Test;
    29 import org.testng.annotations.Test;
    35 import org.xml.sax.SAXException;
       
    36 import org.xml.sax.SAXNotRecognizedException;
       
    37 import org.xml.sax.SAXNotSupportedException;
       
    38 
    30 
    39 /**
    31 /**
    40  * Class containing the test cases for SAXParserFactory API
    32  * Class containing the test cases for SAXParserFactory API.
    41  */
    33  */
    42 public class SAXParserFactTest {
    34 public class SAXParserFactTest extends JAXPBaseTest {
    43 
    35 
    44     private static final String NAMESPACES = "http://xml.org/sax/features/namespaces";
    36     private static final String NAMESPACES = "http://xml.org/sax/features/namespaces";
    45     private static final String NAMESPACE_PREFIXES = "http://xml.org/sax/features/namespace-prefixes";
    37     private static final String NAMESPACE_PREFIXES = "http://xml.org/sax/features/namespace-prefixes";
    46     private static final String STRING_INTERNING = "http://xml.org/sax/features/string-interning";
    38     private static final String STRING_INTERNING = "http://xml.org/sax/features/string-interning";
    47     private static final String VALIDATION = "http://xml.org/sax/features/validation";
    39     private static final String VALIDATION = "http://xml.org/sax/features/validation";
    48     private static final String EXTERNAL_G_ENTITIES = "http://xml.org/sax/features/external-general-entities";
    40     private static final String EXTERNAL_G_ENTITIES = "http://xml.org/sax/features/external-general-entities";
    49     private static final String EXTERNAL_P_ENTITIES = "http://xml.org/sax/features/external-parameter-entities";
    41     private static final String EXTERNAL_P_ENTITIES = "http://xml.org/sax/features/external-parameter-entities";
    50 
    42 
    51     /**
    43     /**
    52      * Testcase to test if newSAXParser() method returns SAXParser.
    44      * Test if newSAXParser() method returns SAXParser.
    53      */
    45      * @throws Exception If any errors occur.
    54     @Test
    46      */
    55     public void testParser01() {
    47     @Test
    56         try {
    48     public void testParser01() throws Exception {
    57             SAXParserFactory spf = SAXParserFactory.newInstance();
    49         SAXParserFactory spf = SAXParserFactory.newInstance();
    58             SAXParser saxparser = spf.newSAXParser();
    50         spf.newSAXParser();
    59         } catch (ParserConfigurationException | SAXException e) {
    51     }
    60             failUnexpected(e);
    52 
    61         }
    53     /**
    62     }
    54      * Test the default functionality (No validation) of the parser.
    63 
       
    64     /**
       
    65      * Testcase to test the default functionality (No validation) of the parser.
       
    66      */
    55      */
    67     @Test
    56     @Test
    68     public void testValidate01() {
    57     public void testValidate01() {
    69         SAXParserFactory spf = SAXParserFactory.newInstance();
    58         SAXParserFactory spf = SAXParserFactory.newInstance();
    70         assertFalse(spf.isValidating());
    59         assertFalse(spf.isValidating());
    71     }
    60     }
    72 
    61 
    73     /**
    62     /**
    74      * Testcase to test the functionality of setValidating and isvalidating
    63      * Test the functionality of setValidating and isvalidating
    75      * methods.
    64      * methods.
    76      */
    65      */
    77     @Test
    66     @Test
    78     public void testValidate02() {
    67     public void testValidate02() {
    79         SAXParserFactory spf = SAXParserFactory.newInstance();
    68         SAXParserFactory spf = SAXParserFactory.newInstance();
    80         spf.setValidating(true);
    69         spf.setValidating(true);
    81         assertTrue(spf.isValidating());
    70         assertTrue(spf.isValidating());
    82     }
    71     }
    83 
    72 
    84     /**
    73     /**
    85      * Parser should not be namespaceaware by default.
    74      * Parser should not be namespace-aware by default.
    86      */
    75      */
    87     @Test
    76     @Test
    88     public void testNamespace01() {
    77     public void testNamespace01() {
    89         SAXParserFactory spf = SAXParserFactory.newInstance();
    78         SAXParserFactory spf = SAXParserFactory.newInstance();
    90         assertFalse(spf.isNamespaceAware());
    79         assertFalse(spf.isNamespaceAware());
    91     }
    80     }
    92 
    81 
    93     /**
    82     /**
    94      * Testcase to test the functionality of setNamespaceAware and
    83      * Test the functionality of setNamespaceAware and
    95      * isNamespaceAware methods.
    84      * isNamespaceAware methods.
    96      */
    85      */
    97     @Test
    86     @Test
    98     public void testNamespace02() {
    87     public void testNamespace02() {
    99         SAXParserFactory spf = SAXParserFactory.newInstance();
    88         SAXParserFactory spf = SAXParserFactory.newInstance();
   100         spf.setNamespaceAware(true);
    89         spf.setNamespaceAware(true);
   101         assertTrue(spf.isNamespaceAware());
    90         assertTrue(spf.isNamespaceAware());
   102     }
    91     }
   103 
    92 
   104     /**
    93     /**
   105      * Testcase to test the functionality of setNamespaceAware and getFeature()
    94      * Test the functionality of setNamespaceAware and getFeature()
   106      * methods for namespaces property.
    95      * methods for namespaces property.
   107      */
    96      * @throws Exception If any errors occur.
   108     @Test
    97      */
   109     public void testFeature01() {
    98     @Test
   110         try {
    99     public void testFeature01() throws Exception {
   111             SAXParserFactory spf = SAXParserFactory.newInstance();
   100         SAXParserFactory spf = SAXParserFactory.newInstance();
   112             assertFalse(spf.getFeature(NAMESPACES));
   101         assertFalse(spf.getFeature(NAMESPACES));
   113 
   102 
   114             spf.setNamespaceAware(true);
   103         spf.setNamespaceAware(true);
   115             assertTrue(spf.getFeature(NAMESPACES));
   104         assertTrue(spf.getFeature(NAMESPACES));
   116         } catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) {
   105     }
   117             failUnexpected(e);
   106 
   118         }
   107     /**
   119     }
   108      * Test the functionality of setFeature and getFeature methods
   120 
       
   121     /**
       
   122      * Testcase to test the functionality of setFeature and getFeature methods
       
   123      * for namespaces property.
   109      * for namespaces property.
   124      */
   110      * @throws Exception If any errors occur.
   125     @Test
   111      */
   126     public void testFeature02() {
   112     @Test
   127         try {
   113     public void testFeature02() throws Exception {
   128             SAXParserFactory spf = SAXParserFactory.newInstance();
   114         SAXParserFactory spf = SAXParserFactory.newInstance();
   129 
   115 
   130             spf.setFeature(NAMESPACES, true);
   116         spf.setFeature(NAMESPACES, true);
   131             assertTrue(spf.getFeature(NAMESPACES));
   117         assertTrue(spf.getFeature(NAMESPACES));
   132 
   118 
   133             spf.setFeature(NAMESPACES, false);
   119         spf.setFeature(NAMESPACES, false);
   134             assertFalse(spf.getFeature(NAMESPACES));
   120         assertFalse(spf.getFeature(NAMESPACES));
   135         } catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) {
   121     }
   136             failUnexpected(e);
   122 
   137         }
   123     /**
   138     }
   124      * Test the functionality of setFeature and getFeature methods
   139 
       
   140     /**
       
   141      * Testcase to test the functionality of setFeature and getFeature methods
       
   142      * for namespace-prefixes property.
   125      * for namespace-prefixes property.
   143      */
   126      * @throws Exception If any errors occur.
   144     @Test
   127      */
   145     public void testFeature03() {
   128     @Test
   146         try {
   129     public void testFeature03() throws Exception {
   147             SAXParserFactory spf = SAXParserFactory.newInstance();
   130         SAXParserFactory spf = SAXParserFactory.newInstance();
   148 
   131 
   149             spf.setFeature(NAMESPACE_PREFIXES, true);
   132         spf.setFeature(NAMESPACE_PREFIXES, true);
   150             assertTrue(spf.getFeature(NAMESPACE_PREFIXES));
   133         assertTrue(spf.getFeature(NAMESPACE_PREFIXES));
   151 
   134 
   152             spf.setFeature(NAMESPACE_PREFIXES, false);
   135         spf.setFeature(NAMESPACE_PREFIXES, false);
   153             assertFalse(spf.getFeature(NAMESPACE_PREFIXES));
   136         assertFalse(spf.getFeature(NAMESPACE_PREFIXES));
   154         } catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) {
   137     }
   155             failUnexpected(e);
   138 
   156         }
   139     /**
   157     }
   140      * Test the functionality of getFeature method for
   158 
       
   159     /**
       
   160      * Testcase to test the functionality of getFeature method for
       
   161      * string-interning property.
   141      * string-interning property.
   162      */
   142      * @throws Exception If any errors occur.
   163     @Test
   143      */
   164     public void testFeature04() {
   144     @Test
   165         try {
   145     public void testFeature04() throws Exception {
   166             SAXParserFactory spf = SAXParserFactory.newInstance();
   146         SAXParserFactory spf = SAXParserFactory.newInstance();
   167             assertTrue(spf.getFeature(STRING_INTERNING));
   147         assertTrue(spf.getFeature(STRING_INTERNING));
   168         } catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) {
   148     }
   169             failUnexpected(e);
   149 
   170         }
   150     /**
   171     }
   151      * Test the functionality of getFeature and setValidating
   172 
       
   173     /**
       
   174      * Testcase to test the functionality of getFeature and setValidating
       
   175      * methods for validation property.
   152      * methods for validation property.
   176      */
   153      * @throws Exception If any errors occur.
   177     @Test
   154      */
   178     public void testFeature05() {
   155     @Test
   179         try {
   156     public void testFeature05() throws Exception {
   180             SAXParserFactory spf = SAXParserFactory.newInstance();
   157         SAXParserFactory spf = SAXParserFactory.newInstance();
   181             assertFalse(spf.getFeature(VALIDATION));
   158         assertFalse(spf.getFeature(VALIDATION));
   182             spf.setValidating(true);
   159         spf.setValidating(true);
   183             assertTrue(spf.getFeature(VALIDATION));
   160         assertTrue(spf.getFeature(VALIDATION));
   184         } catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) {
   161     }
   185             failUnexpected(e);
   162 
   186         }
   163     /**
   187 
   164      * Test the functionality of setFeature and getFeature methods
   188     }
       
   189 
       
   190     /**
       
   191      * Testcase to test the functionality of setFeature and getFeature methods
       
   192      * for validation property.
   165      * for validation property.
   193      */
   166      * @throws Exception If any errors occur.
   194     @Test
   167      */
   195     public void testFeature06() {
   168     @Test
   196         try {
   169     public void testFeature06() throws Exception {
   197             SAXParserFactory spf = SAXParserFactory.newInstance();
   170         SAXParserFactory spf = SAXParserFactory.newInstance();
   198 
   171         spf.setFeature(VALIDATION, true);
   199             spf.setFeature(VALIDATION, true);
   172         assertTrue(spf.getFeature(VALIDATION));
   200             assertTrue(spf.getFeature(VALIDATION));
   173         spf.setFeature(VALIDATION, false);
   201 
   174         assertFalse(spf.getFeature(VALIDATION));
   202             spf.setFeature(VALIDATION, false);
   175     }
   203             assertFalse(spf.getFeature(VALIDATION));
   176 
   204         } catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) {
   177     /**
   205             failUnexpected(e);
   178      * Test the functionality of getFeature method for
   206         }
       
   207 
       
   208     }
       
   209 
       
   210     /**
       
   211      * Testcase to test the functionality of getFeature method for
       
   212      * external-general-entities property.
   179      * external-general-entities property.
   213      */
   180      * @throws Exception If any errors occur.
   214     @Test
   181      */
   215     public void testFeature07() {
   182     @Test
   216         try {
   183     public void testFeature07() throws Exception {
   217             SAXParserFactory spf = SAXParserFactory.newInstance();
   184         SAXParserFactory spf = SAXParserFactory.newInstance();
   218             assertTrue(spf.getFeature(EXTERNAL_G_ENTITIES));
   185         assertTrue(spf.getFeature(EXTERNAL_G_ENTITIES));
   219         } catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) {
   186     }
   220             failUnexpected(e);
   187 
   221         }
   188     /**
   222 
   189      * Test the functionality of setFeature and getFeature methods
   223     }
       
   224 
       
   225     /**
       
   226      * Testcase to test the functionality of setFeature and getFeature methods
       
   227      * for external-general-entities property.
   190      * for external-general-entities property.
   228      */
   191      * @throws Exception If any errors occur.
   229     @Test
   192      */
   230     public void testFeature08() {
   193     @Test
   231         try {
   194     public void testFeature08() throws Exception {
   232             SAXParserFactory spf = SAXParserFactory.newInstance();
   195         SAXParserFactory spf = SAXParserFactory.newInstance();
   233             spf.setFeature(EXTERNAL_G_ENTITIES, false);
   196         spf.setFeature(EXTERNAL_G_ENTITIES, false);
   234             assertFalse(spf.getFeature(EXTERNAL_G_ENTITIES));
   197         assertFalse(spf.getFeature(EXTERNAL_G_ENTITIES));
   235         } catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) {
   198     }
   236             failUnexpected(e);
   199 
   237         }
   200     /**
   238     }
   201      * Test the functionality of getFeature method for
   239 
       
   240     /**
       
   241      * Testcase to test the functionality of getFeature method for
       
   242      * external-parameter-entities property.
   202      * external-parameter-entities property.
   243      */
   203      * @throws Exception If any errors occur.
   244     @Test
   204      */
   245     public void testFeature09() {
   205     @Test
   246         try {
   206     public void testFeature09() throws Exception {
   247             SAXParserFactory spf = SAXParserFactory.newInstance();
   207         SAXParserFactory spf = SAXParserFactory.newInstance();
   248             assertTrue(spf.getFeature(EXTERNAL_P_ENTITIES));
   208         assertTrue(spf.getFeature(EXTERNAL_P_ENTITIES));
   249         } catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) {
   209     }
   250             failUnexpected(e);
   210 
   251         }
   211     /**
   252     }
   212      * Test the functionality of setFeature method for
   253 
       
   254     /**
       
   255      * Testcase to test the functionality of setFeature method for
       
   256      * external-parameter-entitie property.
   213      * external-parameter-entitie property.
   257      */
   214      * @throws Exception If any errors occur.
   258     @Test
   215      */
   259     public void testFeature10() {
   216     @Test
   260         try {
   217     public void testFeature10() throws Exception {
   261             SAXParserFactory spf = SAXParserFactory.newInstance();
   218         SAXParserFactory spf = SAXParserFactory.newInstance();
   262             spf.setFeature(EXTERNAL_P_ENTITIES, false);
   219         spf.setFeature(EXTERNAL_P_ENTITIES, false);
   263         } catch (ParserConfigurationException | SAXNotRecognizedException | SAXNotSupportedException e) {
   220         assertFalse(spf.getFeature(EXTERNAL_P_ENTITIES));
   264             failUnexpected(e);
       
   265         }
       
   266 
       
   267     }
   221     }
   268 }
   222 }