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 java.io.FileInputStream;
  26 import java.io.FilePermission;
  27 import java.io.IOException;
  28 import javax.xml.parsers.ParserConfigurationException;
  29 import javax.xml.parsers.SAXParserFactory;
  30 import jaxp.library.JAXPBaseTest;
  31 import static org.testng.Assert.assertFalse;
  32 import static org.testng.Assert.assertNotNull;
  33 import static org.testng.Assert.assertTrue;
  34 import org.testng.annotations.AfterGroups;
  35 import org.testng.annotations.BeforeGroups;
  36 import org.testng.annotations.Test;
  37 import org.xml.sax.ContentHandler;
  38 import org.xml.sax.InputSource;
  39 import org.xml.sax.SAXException;
  40 import org.xml.sax.SAXNotRecognizedException;
  41 import org.xml.sax.SAXNotSupportedException;
  42 import org.xml.sax.XMLReader;
  43 import org.xml.sax.helpers.ParserAdapter;
  44 import org.xml.sax.helpers.XMLFilterImpl;
  45 import org.xml.sax.helpers.XMLReaderAdapter;
  46 import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
  47 
  48 
  49 /**
  50  * Unit test cases for ParserAdapter API. By default the only features recognized
  51  * are namespaces and namespace-prefixes.
  52  */
  53 public class ParserAdapterTest extends JAXPBaseTest {
  54     /**
  55      * namespaces feature name.
  56      */
  57     private static final String NAMESPACES =
  58                 "http://xml.org/sax/features/namespaces";
  59 
  60     /**
  61      * namespaces-prefiexs feature name.
  62      */
  63     private static final String NAMESPACE_PREFIXES =
  64                 "http://xml.org/sax/features/namespace-prefixes";
  65 
  66     /**
  67      * ParserAdapter instance to share by all tests.
  68      */
  69     private final ParserAdapter parserAdapter;
  70 
  71     /**
  72      * Initiate ParserAdapter.
  73      * @throws ParserConfigurationException
  74      * @throws SAXException
  75      */
  76     ParserAdapterTest() throws ParserConfigurationException, SAXException {
  77         SAXParserFactory spf = SAXParserFactory.newInstance();
  78         XMLReader xmlReader = spf.newSAXParser().getXMLReader();
  79         XMLReaderAdapter xmlReaderAdapter = new XMLReaderAdapter(xmlReader);
  80         parserAdapter = new ParserAdapter(xmlReaderAdapter);
  81     }
  82 
  83     /**
  84      * Verifies parserAdapter.getContentHandler()
  85      */
  86     @Test
  87     public void contentHandler01() {
  88         ContentHandler contentHandler = new XMLFilterImpl();
  89         parserAdapter.setContentHandler(contentHandler);
  90         assertNotNull(parserAdapter.getContentHandler());
  91     }
  92 
  93     /**
  94      * No exception is expected when set content handler as null.
  95      */
  96     @Test
  97     public void contentHandler02() {
  98         parserAdapter.setContentHandler(null);
  99     }
 100 
 101     /**
 102      * Verifies parserAdapter.getEntityResolver()
 103      */
 104     @Test
 105     public void entity01() {
 106         XMLFilterImpl xmlFilter = new XMLFilterImpl();
 107         parserAdapter.setEntityResolver(xmlFilter);
 108         assertNotNull(parserAdapter.getEntityResolver());
 109     }
 110 
 111     /**
 112      * No exception is expected when set entity resolver as null.
 113      */
 114     @Test
 115     public void entity02() {
 116         parserAdapter.setEntityResolver(null);
 117     }
 118 
 119     /**
 120      * Verifies parserAdapter.getDTDHandler()
 121      */
 122     @Test
 123     public void dtdHandler01() {
 124         XMLFilterImpl xmlFilter = new XMLFilterImpl();
 125         parserAdapter.setDTDHandler(xmlFilter);
 126         assertNotNull(parserAdapter.getDTDHandler());
 127     }
 128 
 129     /**
 130      * No exception is expected when set DTD handler as null.
 131      */
 132     @Test
 133     public void dtdHandler02() {
 134         parserAdapter.setDTDHandler(null);
 135     }
 136 
 137     /**
 138      * Verifies parserAdapter.getErrorHandler()
 139      */
 140     @Test
 141     public void errorHandler01() {
 142         XMLFilterImpl eHandler = new XMLFilterImpl();
 143         parserAdapter.setErrorHandler(eHandler);
 144         assertNotNull(parserAdapter.getErrorHandler());
 145     }
 146 
 147     /**
 148      * No exception is expected when set error handler as null.
 149      */
 150     @Test
 151     public void errorHandler02() {
 152         parserAdapter.setErrorHandler(null);
 153     }
 154 
 155     /**
 156      * parserAdapter.getFeature(NAMESPACES) returns true be default.
 157      * 
 158      * @exception SAXNotRecognizedException If the feature
 159      *            value can't be assigned or retrieved.
 160      * @exception SAXNotSupportedException If the
 161      *            feature is not currently readable.
 162      */
 163     @Test
 164     public void getFeature01() throws SAXNotRecognizedException, SAXNotSupportedException {
 165         assertTrue(parserAdapter.getFeature(NAMESPACES));
 166     }
 167 
 168     /**
 169      * parserAdapter.getFeature(NAMESPACE_PREFIXES) returns true be default.
 170      * 
 171      * @exception SAXNotRecognizedException If the feature
 172      *            value can't be assigned or retrieved.
 173      * @exception SAXNotSupportedException If the
 174      *            feature is not currently readable.
 175      */
 176     @Test
 177     public void getFeature02() throws SAXNotRecognizedException, SAXNotSupportedException {
 178         assertFalse(parserAdapter.getFeature(NAMESPACE_PREFIXES));
 179     }
 180 
 181     /**
 182      * SAXNotRecognizedException thrown when feature name is not known one.
 183      * 
 184      * @exception SAXNotRecognizedException If the feature
 185      *            value can't be assigned or retrieved.
 186      * @exception SAXNotSupportedException If the
 187      *            feature is not currently readable.
 188      */
 189     @Test(expectedExceptions = SAXNotRecognizedException.class)
 190     public void getFeature03() throws SAXNotRecognizedException, SAXNotSupportedException {
 191         parserAdapter.getFeature("no-meaning-feature");
 192     }
 193 
 194     /**
 195      * Obtain getFeature after it's set returns set value.
 196      * 
 197      * @exception SAXNotRecognizedException If the feature
 198      *            value can't be assigned or retrieved.
 199      * @exception SAXNotSupportedException If the
 200      *            feature is not currently readable.
 201      */
 202     @Test
 203     public void setFeature01() throws SAXNotRecognizedException, SAXNotSupportedException {
 204         parserAdapter.setFeature(NAMESPACES, false);
 205         assertFalse(parserAdapter.getFeature(NAMESPACES));
 206     }
 207 
 208     /**
 209      * Obtain getFeature after it's set returns set value.
 210      * 
 211      * @exception SAXNotRecognizedException If the feature
 212      *            value can't be assigned or retrieved.
 213      * @exception SAXNotSupportedException If the
 214      *            feature is not currently readable.
 215      */
 216     @Test
 217     public void setFeature02() throws SAXNotRecognizedException, SAXNotSupportedException {
 218         parserAdapter.setFeature(NAMESPACE_PREFIXES, false);
 219         assertFalse(parserAdapter.getFeature(NAMESPACE_PREFIXES));
 220     }
 221 
 222     /**
 223      * Obtain getFeature after it's set returns set value.
 224      * 
 225      * @exception SAXNotRecognizedException If the feature
 226      *            value can't be assigned or retrieved.
 227      * @exception SAXNotSupportedException If the
 228      *            feature is not currently readable.
 229      */
 230     @Test
 231     public void setFeature03() throws SAXNotRecognizedException, SAXNotSupportedException {
 232         parserAdapter.setFeature(NAMESPACES, true);
 233         assertTrue(parserAdapter.getFeature(NAMESPACES));
 234     }
 235 
 236     /**
 237      * Obtain getFeature after it's set returns set value.
 238      * 
 239      * @exception SAXNotRecognizedException If the feature
 240      *            value can't be assigned or retrieved.
 241      * @exception SAXNotSupportedException If the
 242      *            feature is not currently readable.
 243      */
 244     @Test
 245     public void setFeature04() throws SAXNotRecognizedException, 
 246             SAXNotSupportedException {
 247         parserAdapter.setFeature(NAMESPACE_PREFIXES, true);
 248         assertTrue(parserAdapter.getFeature(NAMESPACE_PREFIXES));
 249     }
 250 
 251     /**
 252      * NPE expected when parsing a null object by ParserAdapter. 
 253      * 
 254      * @throws SAXException If there is a problem processing the document.
 255      * @throws IOException if the file exists but is a directory rather than
 256      *         a regular file, does not exist but cannot be created, or cannot 
 257      *         be opened for any other reason.
 258      */
 259     @Test(expectedExceptions = NullPointerException.class)
 260     public void parse01() throws IOException, SAXException {
 261         parserAdapter.parse((InputSource)null);
 262     }
 263   
 264     /**
 265      * Save system property for restoring.
 266      */
 267     @BeforeGroups (groups = {"readLocalFiles"})
 268     public void setFilePermissions() {
 269         setPermissions(new FilePermission(XML_DIR + "/-", "read"));
 270     }
 271     
 272     /**
 273      * Restore the system property.
 274      */
 275     @AfterGroups (groups = {"readLocalFiles"})
 276     public void restoreFilePermissions() {
 277         setPermissions();
 278     }
 279 
 280     /**
 281      * SAXException expected when parsing a wrong-formatter XML with ParserAdapter.
 282      * 
 283      * @throws SAXException If there is a problem processing the document.
 284      * @throws IOException if the file exists but is a directory rather than
 285      *         a regular file, does not exist but cannot be created, or cannot 
 286      *         be opened for any other reason.
 287      */
 288     @Test(groups = {"readLocalFiles"}, expectedExceptions = SAXException.class)
 289     public void parse02() throws SAXException, IOException {
 290         try(FileInputStream fis = new FileInputStream(XML_DIR + "invalid.xml")) {
 291             InputSource is = new InputSource(fis);
 292             parserAdapter.parse(is);
 293         }
 294     }
 295 
 296     /**
 297      * Parse a well-formatter XML with ParserAdapter.
 298      * 
 299      * @throws SAXException If there is a problem processing the document.
 300      * @throws IOException if the file exists but is a directory rather than
 301      *         a regular file, does not exist but cannot be created, or cannot 
 302      *         be opened for any other reason.
 303      */
 304     @Test(groups = {"readLocalFiles"})
 305     public void parse03() throws SAXException, IOException {
 306         try(FileInputStream fis = new FileInputStream(XML_DIR + "correct.xml")) {
 307             InputSource is = new InputSource(fis);
 308             parserAdapter.parse(is);
 309         }
 310     }
 311 }