1 /*
   2  * Copyright (c) 2003, 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 package org.xml.sax.ptests;
  24 
  25 import static org.testng.Assert.assertFalse;
  26 import static org.testng.Assert.assertTrue;
  27 
  28 import javax.xml.parsers.SAXParser;
  29 import javax.xml.parsers.SAXParserFactory;
  30 
  31 import org.testng.annotations.Listeners;
  32 import org.testng.annotations.Test;
  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 @Listeners({jaxp.library.BasePolicy.class})
  40 public class NSTableTest {
  41     private static final String NAMESPACES =
  42                         "http://xml.org/sax/features/namespaces";
  43     private static final String NAMESPACE_PREFIXES =
  44                         "http://xml.org/sax/features/namespace-prefixes";
  45 
  46     /**
  47      * Here namespace processing and namespace-prefixes are enabled.
  48      * The testcase tests XMLReader for this.
  49      *
  50      * @throws Exception If any errors occur.
  51      */
  52     @Test
  53     public void xrNSTable01() throws Exception {
  54         SAXParserFactory spf = SAXParserFactory.newInstance();
  55         spf.setNamespaceAware(true);
  56         SAXParser saxParser = spf.newSAXParser();
  57 
  58         XMLReader xmlReader = saxParser.getXMLReader();
  59         xmlReader.setFeature(NAMESPACE_PREFIXES, true);
  60 
  61         assertTrue(xmlReader.getFeature(NAMESPACES));
  62         assertTrue(xmlReader.getFeature(NAMESPACE_PREFIXES));
  63     }
  64 
  65     /**
  66      * Here namespace processing is enabled. This will make namespace-prefixes
  67      * disabled. The testcase tests XMLReader for this.
  68      *
  69      * @throws Exception If any errors occur.
  70      */
  71     @Test
  72     public void xrNSTable02() throws Exception {
  73         SAXParserFactory spf = SAXParserFactory.newInstance();
  74         spf.setNamespaceAware(true);
  75 
  76         XMLReader xmlReader = spf.newSAXParser().getXMLReader();
  77         assertTrue(xmlReader.getFeature(NAMESPACES));
  78         assertFalse(xmlReader.getFeature(NAMESPACE_PREFIXES));
  79     }
  80 
  81     /**
  82      * Here namespace processing is disabled. This will make namespace-prefixes
  83      * enabled. The testcase tests XMLReader for this.
  84      *
  85      * @throws Exception If any errors occur.
  86      */
  87     @Test
  88     public void xrNSTable03() throws Exception {
  89         XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
  90         assertFalse(xmlReader.getFeature(NAMESPACES));
  91         assertTrue(xmlReader.getFeature(NAMESPACE_PREFIXES));
  92     }
  93 
  94     /**
  95      * Here namespace processing is disabled, and namespace-prefixes is
  96      * disabled. This will make namespace processing on.The testcase tests
  97      * XMLReader for this.  This behavior only apply to crimson, not
  98      * XERCES.
  99      *
 100      * @throws Exception If any errors occur.
 101      */
 102     @Test
 103     public void xrNSTable04() throws Exception {
 104         XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
 105         xmlReader.setFeature(NAMESPACE_PREFIXES, false);
 106         assertFalse(xmlReader.getFeature(NAMESPACE_PREFIXES));
 107     }
 108 
 109     /**
 110      * Here namespace processing and namespace-prefixes are enabled.
 111      * The testcase tests SAXParserFactory for this.
 112      *
 113      * @throws Exception If any errors occur.
 114      */
 115     @Test
 116     public void spNSTable01() throws Exception {
 117         SAXParserFactory spf = SAXParserFactory.newInstance();
 118         spf.setNamespaceAware(true);
 119         spf.setFeature(NAMESPACE_PREFIXES,true);
 120         assertTrue(spf.getFeature(NAMESPACES));
 121         assertTrue(spf.getFeature(NAMESPACE_PREFIXES));
 122     }
 123 
 124     /**
 125      * Here namespace processing is enabled. This will make namespace-prefixes
 126      * disabled. The testcase tests SAXParserFactory for this.
 127      *
 128      * @throws Exception If any errors occur.
 129      */
 130     @Test
 131     public void spNSTable02() throws Exception {
 132         SAXParserFactory spf = SAXParserFactory.newInstance();
 133         spf.setNamespaceAware(true);
 134         assertTrue(spf.getFeature(NAMESPACES));
 135         assertFalse(spf.getFeature(NAMESPACE_PREFIXES));
 136     }
 137 
 138     /**
 139      * Here namespace processing is disabled. This will make namespace-prefixes
 140      * enabled. The testcase tests SAXParserFactory for this.
 141      *
 142      * @throws Exception If any errors occur.
 143      */
 144     @Test
 145     public void spNSTable03() throws Exception {
 146         SAXParserFactory spf = SAXParserFactory.newInstance();
 147         assertFalse(spf.getFeature(NAMESPACES));
 148         assertTrue(spf.getFeature(NAMESPACE_PREFIXES));
 149     }
 150     /**
 151      * Here namespace processing is disabled, and namespace-prefixes is
 152      * disabled. This will make namespace processing on.The testcase tests
 153      * SAXParserFactory for this.  This behavior only apply to crimson,
 154      * not xerces.
 155      *
 156      * @throws Exception If any errors occur.
 157      */
 158     @Test
 159     public void spNSTable04() throws Exception {
 160         SAXParserFactory spf = SAXParserFactory.newInstance();
 161         spf.setFeature(NAMESPACE_PREFIXES, false);
 162         assertFalse(spf.getFeature(NAMESPACE_PREFIXES));
 163     }
 164 }