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