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 package org.xml.sax.ptests;
  24 
  25 import javax.xml.parsers.ParserConfigurationException;
  26 import javax.xml.parsers.SAXParser;
  27 import javax.xml.parsers.SAXParserFactory;
  28 import jaxp.library.JAXPBaseTest;
  29 import static org.testng.Assert.assertFalse;
  30 import static org.testng.Assert.assertTrue;
  31 import org.testng.annotations.Test;
  32 import org.xml.sax.SAXException;
  33 import org.xml.sax.XMLReader;
  34 
  35 /**
  36  * Class containing the test cases for Namespace Table defined at
  37  * http://www.megginson.com/SAX/Java/namespaces.html
  38  */
  39 public class NSTableTest01 extends JAXPBaseTest {
  40     private static final String NAMESPACES =
  41                         "http://xml.org/sax/features/namespaces";
  42     private static final String NAMESPACE_PREFIXES =
  43                         "http://xml.org/sax/features/namespace-prefixes";
  44 
  45     /**
  46      * Here namespace processing and namespace-prefixes are enabled.
  47      * The testcase tests XMLReader for this.
  48      * 
  49      * @throws SAXException If any parse errors occur.
  50      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
  51      *         created which satisfies the configuration requested.
  52      */
  53     @Test
  54     public void xrNSTable01() throws ParserConfigurationException, SAXException {
  55         SAXParserFactory spf = SAXParserFactory.newInstance();
  56         spf.setNamespaceAware(true);
  57         SAXParser saxParser = spf.newSAXParser();
  58 
  59         XMLReader xmlReader = saxParser.getXMLReader();
  60         xmlReader.setFeature(NAMESPACE_PREFIXES, true);
  61 
  62         assertTrue(xmlReader.getFeature(NAMESPACES));
  63         assertTrue(xmlReader.getFeature(NAMESPACE_PREFIXES));
  64     }
  65 
  66     /**
  67      * Here namespace processing is enabled. This will make namespace-prefixes
  68      * disabled. The testcase tests XMLReader for this.
  69      * 
  70      * @throws SAXException If any parse errors occur.
  71      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
  72      *         created which satisfies the configuration requested.
  73      */
  74     @Test
  75     public void xrNSTable02() throws ParserConfigurationException, SAXException {
  76         SAXParserFactory spf = SAXParserFactory.newInstance();
  77         spf.setNamespaceAware(true);
  78 
  79         XMLReader xmlReader = spf.newSAXParser().getXMLReader();
  80         assertTrue(xmlReader.getFeature(NAMESPACES));
  81         assertFalse(xmlReader.getFeature(NAMESPACE_PREFIXES));
  82     }
  83 
  84     /**
  85      * Here namespace processing is disabled. This will make namespace-prefixes
  86      * enabled. The testcase tests XMLReader for this.
  87      * 
  88      * @throws SAXException If any parse errors occur.
  89      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
  90      *         created which satisfies the configuration requested.
  91      */
  92     @Test
  93     public void xrNSTable03() throws ParserConfigurationException, SAXException {
  94         XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
  95         assertFalse(xmlReader.getFeature(NAMESPACES));
  96         assertTrue(xmlReader.getFeature(NAMESPACE_PREFIXES));
  97     }
  98 
  99     /**
 100      * Here namespace processing is disabled, and namespace-prefixes is
 101      * disabled. This will make namespace processing on.The testcase tests
 102      * XMLReader for this.  This behavior only apply to crimson, not
 103      * XERCES.
 104      * 
 105      * @throws SAXException If any parse errors occur.
 106      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 107      *         created which satisfies the configuration requested.
 108      */
 109     @Test
 110     public void xrNSTable04() throws SAXException, ParserConfigurationException {
 111         XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
 112         xmlReader.setFeature(NAMESPACE_PREFIXES, false);
 113         assertFalse(xmlReader.getFeature(NAMESPACE_PREFIXES));
 114     }
 115 
 116     /**
 117      * Here namespace processing and namespace-prefixes are enabled.
 118      * The testcase tests SAXParserFactory for this.
 119      * 
 120      * @throws SAXException If any parse errors occur.
 121      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 122      *         created which satisfies the configuration requested.
 123      */
 124     @Test
 125     public void spNSTable01() throws ParserConfigurationException, SAXException {
 126         SAXParserFactory spf = SAXParserFactory.newInstance();
 127         spf.setNamespaceAware(true);
 128         spf.setFeature(NAMESPACE_PREFIXES,true);
 129         assertTrue(spf.getFeature(NAMESPACES));
 130         assertTrue(spf.getFeature(NAMESPACE_PREFIXES));
 131     }
 132 
 133     /**
 134      * Here namespace processing is enabled. This will make namespace-prefixes
 135      * disabled. The testcase tests SAXParserFactory for this.
 136      * 
 137      * @throws SAXException If any parse errors occur.
 138      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 139      *         created which satisfies the configuration requested.
 140      */
 141     @Test
 142     public void spNSTable02() throws ParserConfigurationException, SAXException {
 143         SAXParserFactory spf = SAXParserFactory.newInstance();
 144         spf.setNamespaceAware(true);
 145         assertTrue(spf.getFeature(NAMESPACES));
 146         assertFalse(spf.getFeature(NAMESPACE_PREFIXES));
 147     }
 148 
 149     /**
 150      * Here namespace processing is disabled. This will make namespace-prefixes
 151      * enabled. The testcase tests SAXParserFactory for this.
 152      * 
 153      * @throws SAXException If any parse errors occur.
 154      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 155      *         created which satisfies the configuration requested.
 156      */
 157     @Test
 158     public void spNSTable03() throws ParserConfigurationException, SAXException {
 159         SAXParserFactory spf = SAXParserFactory.newInstance();
 160         assertFalse(spf.getFeature(NAMESPACES));
 161         assertTrue(spf.getFeature(NAMESPACE_PREFIXES));
 162     }
 163     /**
 164      * Here namespace processing is disabled, and namespace-prefixes is
 165      * disabled. This will make namespace processing on.The testcase tests
 166      * SAXParserFactory for this.  This behavior only apply to crimson,
 167      * not xerces.
 168      * 
 169      * @throws SAXException If any parse errors occur.
 170      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 171      *         created which satisfies the configuration requested.
 172      */
 173     @Test
 174     public void spNSTable04() throws ParserConfigurationException, SAXException {
 175         SAXParserFactory spf = SAXParserFactory.newInstance();
 176         spf.setFeature(NAMESPACE_PREFIXES, false);
 177         assertFalse(spf.getFeature(NAMESPACE_PREFIXES));
 178     }
 179 }