1 /*
   2  * Copyright (c) 1999, 2015, 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 
  24 package javax.xml.parsers.ptests;
  25 
  26 import static org.testng.Assert.assertFalse;
  27 import static org.testng.Assert.assertNull;
  28 import static org.testng.Assert.assertTrue;
  29 import javax.xml.parsers.SAXParser;
  30 import javax.xml.parsers.SAXParserFactory;
  31 import jaxp.library.JAXPBaseTest;
  32 import static org.testng.Assert.assertNotNull;
  33 import org.testng.annotations.DataProvider;
  34 import org.testng.annotations.Test;
  35 import org.xml.sax.SAXException;
  36 import org.xml.sax.SAXNotSupportedException;
  37 import org.xml.sax.ext.DeclHandler;
  38 import org.xml.sax.ext.LexicalHandler;
  39 
  40 /**
  41  * Class contains the test cases for SAXParser API
  42  */
  43 public class SAXParserTest02 extends JAXPBaseTest {
  44     private static final String DOM_NODE = "http://xml.org/sax/properties/dom-node";
  45     private static final String XML_STRING = "http://xml.org/sax/properties/xml-string";
  46     private static final String DECL_HANDLER = "http://xml.org/sax/properties/declaration-handler";
  47     private static final String LEXICAL_HANDLER = "http://xml.org/sax/properties/lexical-handler";
  48 
  49     /**
  50      * Provide SAXParser.
  51      *
  52      * @return a data provider contains a SAXParser instance.
  53      * @throws Exception If any errors occur.
  54      */
  55     @DataProvider(name = "parser-provider")
  56     public Object[][] getParser() throws Exception {
  57         SAXParserFactory spf = SAXParserFactory.newInstance();
  58         SAXParser saxparser = spf.newSAXParser();
  59         return new Object[][] { { saxparser } };
  60     }
  61 
  62     /**
  63      * Test to test the default functionality (No validation) of the parser.
  64      *
  65      * @param saxparser a SAXParser instance.
  66      */
  67     @Test(dataProvider = "parser-provider")
  68     public void testValidate01(SAXParser saxparser) {
  69         assertFalse(saxparser.isValidating());
  70     }
  71 
  72     /**
  73      * Test to test the functionality of setValidating and isValidating
  74      * methods.
  75      *
  76      * @throws Exception If any errors occur.
  77      */
  78     @Test
  79     public void testValidate02() throws Exception {
  80         SAXParserFactory spf = SAXParserFactory.newInstance();
  81         spf.setValidating(true);
  82         spf.newSAXParser();
  83         assertTrue(spf.isValidating());
  84     }
  85 
  86     /**
  87      * Test isNamespaceAware() method. By default, namespaces are
  88      * not supported.
  89      *
  90      * @param saxparser a SAXParser instance.
  91      */
  92     @Test(dataProvider = "parser-provider")
  93     public void testNamespace01(SAXParser saxparser) {
  94         assertFalse(saxparser.isNamespaceAware());
  95     }
  96 
  97     /**
  98      * Test case to test setnamespaceAware() method.
  99      *
 100      * @throws Exception If any errors occur.
 101      */
 102     @Test
 103     public void testNamespace02() throws Exception {
 104         SAXParserFactory spf = SAXParserFactory.newInstance();
 105         spf.setNamespaceAware(true);
 106         SAXParser saxparser = spf.newSAXParser();
 107         assertTrue(saxparser.isNamespaceAware());
 108     }
 109 
 110     /**
 111      * Test case to test if the getParser() method returns instance of Parser.
 112      *
 113      * @param saxparser a SAXParser instance.
 114      * @throws SAXException If any parse errors occur.
 115      */
 116     @Test(dataProvider = "parser-provider")
 117     public void testParser01(SAXParser saxparser) throws SAXException {
 118         assertNotNull(saxparser.getParser());
 119     }
 120 
 121     /**
 122      * Test case to test if the getXMLReader() method returns instance of
 123      * XMLReader.
 124      *
 125      * @param saxparser a SAXParser instance.
 126      * @throws SAXException If any parse errors occur.
 127      */
 128     @Test(dataProvider = "parser-provider")
 129     public void testXmlReader01(SAXParser saxparser) throws SAXException {
 130         assertNotNull(saxparser.getXMLReader());
 131     }
 132 
 133     /**
 134      * Test whether the xml-string property is not supported.
 135      *
 136      * @param saxparser a SAXParser instance.
 137      * @throws SAXException If any parse errors occur.
 138      */
 139     @Test(expectedExceptions = SAXNotSupportedException.class,
 140             dataProvider = "parser-provider")
 141     public void testProperty01(SAXParser saxparser) throws SAXException {
 142         saxparser.getProperty(XML_STRING);
 143     }
 144 
 145     /**
 146      * Test whether the dom-node property is not supported.
 147      *
 148      * @param saxparser a SAXParser instance.
 149      * @throws SAXException If any parse errors occur.
 150      */
 151     @Test(expectedExceptions = SAXNotSupportedException.class,
 152             dataProvider = "parser-provider")
 153     public void testProperty02(SAXParser saxparser) throws SAXException {
 154         saxparser.getProperty(DOM_NODE);
 155     }
 156 
 157     /**
 158      * Test the default lexical-handler not exists.
 159      *
 160      * @param saxparser a SAXParser instance.
 161      * @throws SAXException If any parse errors occur.
 162      */
 163     @Test(dataProvider = "parser-provider")
 164     public void testProperty03(SAXParser saxparser) throws SAXException {
 165         assertNull(saxparser.getProperty(LEXICAL_HANDLER));
 166     }
 167 
 168     /**
 169      * Test the default declaration-handler not exists.
 170      *
 171      * @param saxparser a SAXParser instance.
 172      * @throws SAXException If any parse errors occur.
 173      */
 174     @Test(dataProvider = "parser-provider")
 175     public void testProperty04(SAXParser saxparser) throws SAXException {
 176         assertNull(saxparser.getProperty(DECL_HANDLER));
 177     }
 178 
 179     /**
 180      * Test to set and get the lexical-handler.
 181      *
 182      * @param saxparser a SAXParser instance.
 183      * @throws SAXException If any parse errors occur.
 184      */
 185     @Test(dataProvider = "parser-provider")
 186     public void testProperty05(SAXParser saxparser) throws SAXException {
 187         MyLexicalHandler myLexicalHandler = new MyLexicalHandler();
 188         saxparser.setProperty(LEXICAL_HANDLER, myLexicalHandler);
 189         assertTrue(saxparser.getProperty(LEXICAL_HANDLER) instanceof LexicalHandler);
 190     }
 191 
 192     /**
 193      * Test to set and get the declaration-handler.
 194      *
 195      * @param saxparser a SAXParser instance.
 196      * @throws SAXException If any parse errors occur.
 197      */
 198     @Test(dataProvider = "parser-provider")
 199     public void testProperty06(SAXParser saxparser) throws SAXException {
 200         MyDeclHandler myDeclHandler = new MyDeclHandler();
 201         saxparser.setProperty(DECL_HANDLER, myDeclHandler);
 202         assertTrue(saxparser.getProperty(DECL_HANDLER) instanceof DeclHandler);
 203     }
 204 
 205     /**
 206      * Customized LexicalHandler used for test. An empty implementation for
 207      * LexicalHandler.
 208      */
 209     private class MyLexicalHandler implements LexicalHandler {
 210 
 211         @Override
 212         public void comment(char[] ch, int start, int length) {
 213         }
 214 
 215         @Override
 216         public void endCDATA() {
 217         }
 218 
 219         @Override
 220         public void endDTD() {
 221         }
 222 
 223         @Override
 224         public void endEntity(String name) {
 225         }
 226 
 227         @Override
 228         public void startCDATA() {
 229         }
 230 
 231         @Override
 232         public void startDTD(String name, String publicId, String systemId) {
 233         }
 234 
 235         @Override
 236         public void startEntity(String name) {
 237         }
 238     }
 239 
 240     /**
 241      * Customized DeclHandler used for test. An empty implementation for
 242      * DeclHandler.
 243      */
 244     private class MyDeclHandler implements DeclHandler {
 245 
 246         @Override
 247         public void attributeDecl(String eName, String aName, String type, String valueDefault, String value) {
 248         }
 249 
 250         @Override
 251         public void elementDecl(String name, String model) {
 252         }
 253 
 254         @Override
 255         public void externalEntityDecl(String name, String publicId, String systemId) {
 256         }
 257 
 258         @Override
 259         public void internalEntityDecl(String name, String value) {
 260         }
 261     }
 262 }