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.assertNotNull;
  27 import static org.testng.Assert.assertTrue;
  28 import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
  29 
  30 import java.io.FileInputStream;
  31 
  32 import javax.xml.parsers.SAXParserFactory;
  33 
  34 import org.testng.annotations.Listeners;
  35 import org.testng.annotations.Test;
  36 import org.xml.sax.InputSource;
  37 import org.xml.sax.SAXNotRecognizedException;
  38 import org.xml.sax.XMLReader;
  39 import org.xml.sax.helpers.XMLFilterImpl;
  40 
  41 /**
  42  * Unit test for XMLFilter.
  43  */
  44 /*
  45  * @test
  46  * @library /javax/xml/jaxp/libs
  47  * @run testng/othervm -DrunSecMngr=true org.xml.sax.ptests.XMLFilterTest
  48  * @run testng/othervm org.xml.sax.ptests.XMLFilterTest
  49  */
  50 @Listeners({jaxp.library.FilePolicy.class})
  51 public class XMLFilterTest {
  52     /**
  53      * name spaces constant.
  54      */
  55     private static final String NAMESPACES =
  56                 "http://xml.org/sax/features/namespaces";
  57 
  58     /**
  59      * name spaces prefixes constant.
  60      */
  61     private static final String NAMESPACE_PREFIXES =
  62                 "http://xml.org/sax/features/namespace-prefixes";
  63 
  64     /**
  65      * No exception expected when set a correct content handler.
  66      */
  67     @Test
  68     public void contentHandler01() {
  69         XMLFilterImpl xmlFilter = new XMLFilterImpl();
  70         xmlFilter.setContentHandler(xmlFilter);
  71         assertNotNull(xmlFilter.getContentHandler());
  72     }
  73 
  74     /**
  75      * No exception is expected when set content handler as null.
  76      */
  77     @Test
  78     public void contentHandler02() {
  79         new XMLFilterImpl().setContentHandler(null);
  80     }
  81 
  82     /**
  83      * No exception expected when set a correct entity solver.
  84      */
  85     @Test
  86     public void entity01() {
  87         XMLFilterImpl xmlFilter = new XMLFilterImpl();
  88         xmlFilter.setEntityResolver(xmlFilter);
  89         assertNotNull(xmlFilter.getEntityResolver());
  90     }
  91 
  92     /**
  93      * No exception is expected when set entity resolver as null.
  94      */
  95     @Test
  96     public void entity02() {
  97         new XMLFilterImpl().setEntityResolver(null);
  98     }
  99 
 100     /**
 101      * No exception expected when set a correct DTD handler.
 102      */
 103     @Test
 104     public void dtdHandler01() {
 105         XMLFilterImpl xmlFilter = new XMLFilterImpl();
 106         xmlFilter.setDTDHandler(xmlFilter);
 107         assertNotNull(xmlFilter.getDTDHandler());
 108     }
 109 
 110     /**
 111      * No exception is expected when set DTD handler as null.
 112      */
 113     @Test
 114     public void dtdHandler02() {
 115         new XMLFilterImpl().setDTDHandler(null);
 116     }
 117 
 118     /**
 119      * No exception expected when set a correct error handler.
 120      */
 121     @Test
 122     public void errorHandler01() {
 123         XMLFilterImpl xmlFilter = new XMLFilterImpl();
 124         xmlFilter.setErrorHandler(xmlFilter);
 125         assertNotNull(xmlFilter.getErrorHandler());
 126     }
 127 
 128     /**
 129      * No exception is expected when set error handler as null.
 130      */
 131     @Test
 132     public void errorHandler02() {
 133         new XMLFilterImpl().setErrorHandler(null);
 134     }
 135 
 136     /**
 137      * By default true is expected get namespaces feature.
 138      *
 139      * @throws Exception If any errors occur.
 140      */
 141     @Test
 142     public void getFeature01() throws Exception {
 143         SAXParserFactory spf = SAXParserFactory.newInstance();
 144         spf.setNamespaceAware(true);
 145         XMLReader xmlReader = spf.newSAXParser().getXMLReader();
 146 
 147         XMLFilterImpl xmlFilter = new XMLFilterImpl();
 148         xmlFilter.setParent(xmlReader);
 149         assertTrue(xmlFilter.getFeature(NAMESPACES));
 150     }
 151 
 152     /**
 153      * By default false is expected get namespaces-prefix feature.
 154      *
 155      * @throws Exception If any errors occur.
 156      */
 157     @Test
 158     public void getFeature02() throws Exception {
 159         SAXParserFactory spf = SAXParserFactory.newInstance();
 160         spf.setNamespaceAware(true);
 161         XMLFilterImpl xmlFilter = new XMLFilterImpl();
 162         xmlFilter.setParent(spf.newSAXParser().getXMLReader());
 163         assertFalse(xmlFilter.getFeature(NAMESPACE_PREFIXES));
 164     }
 165 
 166     /**
 167      * SAXNotRecognizedException is expected when get a feature by an invalid
 168      * feature name.
 169      *
 170      * @throws Exception If any errors occur.
 171      */
 172     @Test(expectedExceptions = SAXNotRecognizedException.class)
 173     public void getFeature03() throws Exception {
 174         new XMLFilterImpl().getFeature("no-meaning-feature");
 175     }
 176 
 177     /**
 178      * Set namespaces feature to a value to XMLFilter. it's expected same when
 179      * obtain it again.
 180      *
 181      * @throws Exception If any errors occur.
 182      */
 183     @Test
 184     public void setFeature01() throws Exception {
 185         SAXParserFactory spf = SAXParserFactory.newInstance();
 186         spf.setNamespaceAware(true);
 187 
 188         XMLFilterImpl xmlFilter = new XMLFilterImpl();
 189         xmlFilter.setParent(spf.newSAXParser().getXMLReader());
 190         xmlFilter.setFeature(NAMESPACES, false);
 191         assertFalse(xmlFilter.getFeature(NAMESPACES));
 192         xmlFilter.setFeature(NAMESPACES, true);
 193         assertTrue(xmlFilter.getFeature(NAMESPACES));
 194     }
 195 
 196     /**
 197      * Set namespaces-prefix feature to a value to XMLFilter. it's expected same
 198      * when obtain it again.
 199      *
 200      * @throws Exception If any errors occur.
 201      */
 202     @Test
 203     public void setFeature02() throws Exception {
 204         SAXParserFactory spf = SAXParserFactory.newInstance();
 205         spf.setNamespaceAware(true);
 206 
 207         XMLFilterImpl xmlFilter = new XMLFilterImpl();
 208         xmlFilter.setParent(spf.newSAXParser().getXMLReader());
 209         xmlFilter.setFeature(NAMESPACE_PREFIXES, false);
 210         assertFalse(xmlFilter.getFeature(NAMESPACE_PREFIXES));
 211         xmlFilter.setFeature(NAMESPACE_PREFIXES, true);
 212         assertTrue(xmlFilter.getFeature(NAMESPACE_PREFIXES));
 213     }
 214 
 215     /**
 216      * NullPointerException is expected when parse a null InputSource.
 217      *
 218      * @throws Exception If any errors occur.
 219      */
 220     @Test(expectedExceptions = NullPointerException.class)
 221     public void parse01() throws Exception {
 222         new XMLFilterImpl().parse((InputSource)null);
 223     }
 224 
 225     /**
 226      * SAXException is expected when parsing a invalid formatted XML file.
 227      *
 228      * @throws Exception If any errors occur.
 229      */
 230     @Test(expectedExceptions = NullPointerException.class)
 231     public void parse02() throws Exception {
 232         try(FileInputStream fis = new FileInputStream(XML_DIR + "invalid.xml")) {
 233             new XMLFilterImpl().parse(new InputSource(fis));
 234         }
 235     }
 236 
 237     /**
 238      * No exception when parse a normal XML file.
 239      *
 240      * @throws Exception If any errors occur.
 241      */
 242     @Test(expectedExceptions = NullPointerException.class)
 243     public void parse03() throws Exception {
 244         try(FileInputStream fis = new FileInputStream(XML_DIR + "correct2.xml")) {
 245             new XMLFilterImpl().parse(new InputSource(fis));
 246         }
 247     }
 248 }