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