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