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.ContentHandler;
  37 import org.xml.sax.InputSource;
  38 import org.xml.sax.SAXException;
  39 import org.xml.sax.SAXNotRecognizedException;
  40 import org.xml.sax.XMLReader;
  41 import org.xml.sax.helpers.ParserAdapter;
  42 import org.xml.sax.helpers.XMLFilterImpl;
  43 import org.xml.sax.helpers.XMLReaderAdapter;
  44 
  45 
  46 /**
  47  * Unit test cases for ParserAdapter API. By default the only features recognized
  48  * are namespaces and namespace-prefixes.
  49  */
  50 /*
  51  * @test
  52  * @library /javax/xml/jaxp/libs
  53  * @run testng/othervm -DrunSecMngr=true org.xml.sax.ptests.ParserAdapterTest
  54  * @run testng/othervm org.xml.sax.ptests.ParserAdapterTest
  55  */
  56 @Listeners({jaxp.library.FilePolicy.class})
  57 public class ParserAdapterTest {
  58     /**
  59      * namespaces feature name.
  60      */
  61     private static final String NAMESPACES =
  62                 "http://xml.org/sax/features/namespaces";
  63 
  64     /**
  65      * namespaces-prefiexs feature name.
  66      */
  67     private static final String NAMESPACE_PREFIXES =
  68                 "http://xml.org/sax/features/namespace-prefixes";
  69 
  70     /**
  71      * ParserAdapter instance to share by all tests.
  72      */
  73     private final ParserAdapter parserAdapter;
  74 
  75     /**
  76      * Initiate ParserAdapter.
  77      * @throws Exception If any errors occur.
  78      */
  79     ParserAdapterTest() throws Exception {
  80         SAXParserFactory spf = SAXParserFactory.newInstance();
  81         XMLReader xmlReader = spf.newSAXParser().getXMLReader();
  82         XMLReaderAdapter xmlReaderAdapter = new XMLReaderAdapter(xmlReader);
  83         parserAdapter = new ParserAdapter(xmlReaderAdapter);
  84     }
  85 
  86     /**
  87      * Verifies parserAdapter.getContentHandler()
  88      */
  89     @Test
  90     public void contentHandler01() {
  91         ContentHandler contentHandler = new XMLFilterImpl();
  92         parserAdapter.setContentHandler(contentHandler);
  93         assertNotNull(parserAdapter.getContentHandler());
  94     }
  95 
  96     /**
  97      * No exception is expected when set content handler as null.
  98      */
  99     @Test
 100     public void contentHandler02() {
 101         parserAdapter.setContentHandler(null);
 102     }
 103 
 104     /**
 105      * Verifies parserAdapter.getEntityResolver()
 106      */
 107     @Test
 108     public void entity01() {
 109         XMLFilterImpl xmlFilter = new XMLFilterImpl();
 110         parserAdapter.setEntityResolver(xmlFilter);
 111         assertNotNull(parserAdapter.getEntityResolver());
 112     }
 113 
 114     /**
 115      * No exception is expected when set entity resolver as null.
 116      */
 117     @Test
 118     public void entity02() {
 119         parserAdapter.setEntityResolver(null);
 120     }
 121 
 122     /**
 123      * Verifies parserAdapter.getDTDHandler()
 124      */
 125     @Test
 126     public void dtdHandler01() {
 127         XMLFilterImpl xmlFilter = new XMLFilterImpl();
 128         parserAdapter.setDTDHandler(xmlFilter);
 129         assertNotNull(parserAdapter.getDTDHandler());
 130     }
 131 
 132     /**
 133      * No exception is expected when set DTD handler as null.
 134      */
 135     @Test
 136     public void dtdHandler02() {
 137         parserAdapter.setDTDHandler(null);
 138     }
 139 
 140     /**
 141      * Verifies parserAdapter.getErrorHandler()
 142      */
 143     @Test
 144     public void errorHandler01() {
 145         XMLFilterImpl eHandler = new XMLFilterImpl();
 146         parserAdapter.setErrorHandler(eHandler);
 147         assertNotNull(parserAdapter.getErrorHandler());
 148     }
 149 
 150     /**
 151      * No exception is expected when set error handler as null.
 152      */
 153     @Test
 154     public void errorHandler02() {
 155         parserAdapter.setErrorHandler(null);
 156     }
 157 
 158     /**
 159      * parserAdapter.getFeature(NAMESPACES) returns true be default.
 160      *
 161      * @exception Exception If any errors occur.
 162      */
 163     @Test
 164     public void getFeature01() throws Exception {
 165         assertTrue(parserAdapter.getFeature(NAMESPACES));
 166     }
 167 
 168     /**
 169      * parserAdapter.getFeature(NAMESPACE_PREFIXES) returns true be default.
 170      *
 171      * @exception Exception If any errors occur.
 172      */
 173     @Test
 174     public void getFeature02() throws Exception {
 175         assertFalse(parserAdapter.getFeature(NAMESPACE_PREFIXES));
 176     }
 177 
 178     /**
 179      * SAXNotRecognizedException thrown when feature name is not known one.
 180      *
 181      * @exception Exception If any errors occur.
 182      */
 183     @Test(expectedExceptions = SAXNotRecognizedException.class)
 184     public void getFeature03() throws Exception {
 185         parserAdapter.getFeature("no-meaning-feature");
 186     }
 187 
 188     /**
 189      * Obtain getFeature after it's set returns set value.
 190      *
 191      * @exception Exception If any errors occur.
 192      */
 193     @Test
 194     public void setFeature01() throws Exception {
 195         parserAdapter.setFeature(NAMESPACES, false);
 196         assertFalse(parserAdapter.getFeature(NAMESPACES));
 197     }
 198 
 199     /**
 200      * Obtain getFeature after it's set returns set value.
 201      *
 202      * @exception Exception If any errors occur.
 203      */
 204     @Test
 205     public void setFeature02() throws Exception {
 206         parserAdapter.setFeature(NAMESPACE_PREFIXES, false);
 207         assertFalse(parserAdapter.getFeature(NAMESPACE_PREFIXES));
 208     }
 209 
 210     /**
 211      * Obtain getFeature after it's set returns set value.
 212      *
 213      * @exception Exception If any errors occur.
 214      */
 215     @Test
 216     public void setFeature03() throws Exception {
 217         parserAdapter.setFeature(NAMESPACES, true);
 218         assertTrue(parserAdapter.getFeature(NAMESPACES));
 219     }
 220 
 221     /**
 222      * Obtain getFeature after it's set returns set value.
 223      *
 224      * @exception Exception If any errors occur.
 225      */
 226     @Test
 227     public void setFeature04() throws Exception {
 228         parserAdapter.setFeature(NAMESPACE_PREFIXES, true);
 229         assertTrue(parserAdapter.getFeature(NAMESPACE_PREFIXES));
 230     }
 231 
 232     /**
 233      * NPE expected when parsing a null object by ParserAdapter.
 234      *
 235      * @throws Exception If any errors occur.
 236      */
 237     @Test(expectedExceptions = NullPointerException.class)
 238     public void parse01() throws Exception {
 239         parserAdapter.parse((InputSource)null);
 240     }
 241 
 242     /**
 243      * SAXException expected when parsing a wrong-formatter XML with ParserAdapter.
 244      *
 245      * @throws Exception If any errors occur.
 246      */
 247     @Test(expectedExceptions = SAXException.class)
 248     public void parse02() throws Exception {
 249         try(FileInputStream fis = new FileInputStream(XML_DIR + "invalid.xml")) {
 250             InputSource is = new InputSource(fis);
 251             parserAdapter.parse(is);
 252         }
 253     }
 254 
 255     /**
 256      * Parse a well-formatter XML with ParserAdapter.
 257      *
 258      * @throws Exception If any errors occur.
 259      */
 260     @Test
 261     public void parse03() throws Exception {
 262         try(FileInputStream fis = new FileInputStream(XML_DIR + "correct.xml")) {
 263             InputSource is = new InputSource(fis);
 264             parserAdapter.parse(is);
 265         }
 266     }
 267 }