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.InputSource; 38 import org.xml.sax.SAXException; 39 import org.xml.sax.SAXNotRecognizedException; 40 import org.xml.sax.SAXNotSupportedException; 41 import org.xml.sax.XMLReader; 42 import org.xml.sax.helpers.XMLFilterImpl; 43 import static org.xml.sax.ptests.SAXTestConst.XML_DIR; 44 45 /** 46 * Unit test for XMLFilter. 47 */ 48 public class XMLFilterTest extends JAXPBaseTest { 49 /** 50 * name spaces constant. 51 */ 52 private static final String NAMESPACES = 53 "http://xml.org/sax/features/namespaces"; 54 55 /** 56 * name spaces prefixes constant. 57 */ 58 private static final String NAMESPACE_PREFIXES = 59 "http://xml.org/sax/features/namespace-prefixes"; 60 61 /** 62 * No exception expected when set a correct content handler. 63 */ 64 @Test 65 public void contentHandler01() { 66 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 67 xmlFilter.setContentHandler(xmlFilter); 68 assertNotNull(xmlFilter.getContentHandler()); 69 } 70 71 /** 72 * No exception is expected when set content handler as null. 73 */ 74 @Test 75 public void contentHandler02() { 76 new XMLFilterImpl().setContentHandler(null); 77 } 78 79 /** 80 * No exception expected when set a correct entity solver. 81 */ 82 @Test 83 public void entity01() { 84 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 85 xmlFilter.setEntityResolver(xmlFilter); 86 assertNotNull(xmlFilter.getEntityResolver()); 87 } 88 89 /** 90 * No exception is expected when set entity resolver as null. 91 */ 92 @Test 93 public void entity02() { 94 new XMLFilterImpl().setEntityResolver(null); 95 } 96 97 /** 98 * No exception expected when set a correct DTD handler. 99 */ 100 @Test 101 public void dtdHandler01() { 102 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 103 xmlFilter.setDTDHandler(xmlFilter); 104 assertNotNull(xmlFilter.getDTDHandler()); 105 } 106 107 /** 108 * No exception is expected when set DTD handler as null. 109 */ 110 @Test 111 public void dtdHandler02() { 112 new XMLFilterImpl().setDTDHandler(null); 113 } 114 115 /** 116 * No exception expected when set a correct error handler. 117 */ 118 @Test 119 public void errorHandler01() { 120 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 121 xmlFilter.setErrorHandler(xmlFilter); 122 assertNotNull(xmlFilter.getErrorHandler()); 123 } 124 125 /** 126 * No exception is expected when set error handler as null. 127 */ 128 @Test 129 public void errorHandler02() { 130 new XMLFilterImpl().setErrorHandler(null); 131 } 132 133 /** 134 * By default true is expected get namespaces feature. 135 * 136 * @throws SAXException If there is a problem processing the document. 137 * @throws ParserConfigurationException if a DocumentBuilder cannot be 138 * created which satisfies the configuration requested. 139 */ 140 @Test 141 public void getFeature01() throws SAXException, ParserConfigurationException { 142 SAXParserFactory spf = SAXParserFactory.newInstance(); 143 spf.setNamespaceAware(true); 144 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 145 146 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 147 xmlFilter.setParent(xmlReader); 148 assertTrue(xmlFilter.getFeature(NAMESPACES)); 149 } 150 151 /** 152 * By default false is expected get namespaces-prefix feature. 153 * 154 * @throws SAXException If there is a problem processing the document. 155 * @throws ParserConfigurationException if a DocumentBuilder cannot be 156 * created which satisfies the configuration requested. 157 */ 158 @Test 159 public void getFeature02() throws ParserConfigurationException, SAXException { 160 SAXParserFactory spf = SAXParserFactory.newInstance(); 161 spf.setNamespaceAware(true); 162 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 163 xmlFilter.setParent(spf.newSAXParser().getXMLReader()); 164 assertFalse(xmlFilter.getFeature(NAMESPACE_PREFIXES)); 165 } 166 167 /** 168 * SAXNotRecognizedException is expected when get a feature by an invalid 169 * feature name. 170 * @throws org.xml.sax.SAXNotRecognizedException If the feature 171 * value can't be assigned or retrieved from the parent. 172 * @throws org.xml.sax.SAXNotSupportedException When the 173 * parent recognizes the feature name but 174 * cannot determine its value at this time. 175 */ 176 @Test(expectedExceptions = SAXNotRecognizedException.class) 177 public void getFeature03() throws SAXNotRecognizedException, 178 SAXNotSupportedException { 179 new XMLFilterImpl().getFeature("no-meaning-feature"); 180 } 181 182 /** 183 * Set namespaces feature to a value to XMLFilter. it's expected same when 184 * obtain it again. 185 * 186 * @throws SAXException If there is a problem processing the document. 187 * @throws ParserConfigurationException if a DocumentBuilder cannot be 188 * created which satisfies the configuration requested. 189 */ 190 @Test 191 public void setFeature01() throws ParserConfigurationException, SAXException { 192 SAXParserFactory spf = SAXParserFactory.newInstance(); 193 spf.setNamespaceAware(true); 194 195 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 196 xmlFilter.setParent(spf.newSAXParser().getXMLReader()); 197 xmlFilter.setFeature(NAMESPACES, false); 198 assertFalse(xmlFilter.getFeature(NAMESPACES)); 199 xmlFilter.setFeature(NAMESPACES, true); 200 assertTrue(xmlFilter.getFeature(NAMESPACES)); 201 } 202 203 /** 204 * Set namespaces-prefix feature to a value to XMLFilter. it's expected same 205 * when obtain it again. 206 * 207 * @throws SAXException If there is a problem processing the document. 208 * @throws ParserConfigurationException if a DocumentBuilder cannot be 209 * created which satisfies the configuration requested. 210 */ 211 @Test 212 public void setFeature02() throws ParserConfigurationException, SAXException { 213 SAXParserFactory spf = SAXParserFactory.newInstance(); 214 spf.setNamespaceAware(true); 215 216 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 217 xmlFilter.setParent(spf.newSAXParser().getXMLReader()); 218 xmlFilter.setFeature(NAMESPACE_PREFIXES, false); 219 assertFalse(xmlFilter.getFeature(NAMESPACE_PREFIXES)); 220 xmlFilter.setFeature(NAMESPACE_PREFIXES, true); 221 assertTrue(xmlFilter.getFeature(NAMESPACE_PREFIXES)); 222 } 223 224 /** 225 * NullPointerException is expected when parse a null InputSource. 226 * 227 * @throws SAXException If there is a problem processing the document. 228 * @throws IOException if the file exists but is a directory rather than 229 * a regular file, does not exist but cannot be created, or cannot 230 * be opened for any other reason. 231 */ 232 @Test(expectedExceptions = NullPointerException.class) 233 public void parse01() throws SAXException, IOException { 234 new XMLFilterImpl().parse((InputSource)null); 235 } 236 237 /** 238 * Save system property for restoring. 239 */ 240 @BeforeGroups (groups = {"readLocalFiles"}) 241 public void setFilePermissions() { 242 setPermissions(new FilePermission(XML_DIR + "/-", "read")); 243 } 244 245 /** 246 * Restore the system property. 247 */ 248 @AfterGroups (groups = {"readLocalFiles"}) 249 public void restoreFilePermissions() { 250 setPermissions(); 251 } 252 253 /** 254 * SAXException is expected when parsing a invalid formatted XML file. 255 * 256 * @throws SAXException when parse a incorrect formatted XML file. 257 * @throws IOException if the file exists but is a directory rather than 258 * a regular file, does not exist but cannot be created, or cannot 259 * be opened for any other reason. 260 */ 261 @Test(groups = {"readLocalFiles"}, expectedExceptions = NullPointerException.class) 262 public void parse02() throws SAXException, IOException { 263 try(FileInputStream fis = new FileInputStream(XML_DIR + "invalid.xml")) { 264 new XMLFilterImpl().parse(new InputSource(fis)); 265 } 266 } 267 268 /** 269 * No exception when parse a normal XML file. 270 * 271 * @throws SAXException when parse a incorrect formatted XML file. 272 * @throws IOException if the file exists but is a directory rather than 273 * a regular file, does not exist but cannot be created, or cannot 274 * be opened for any other reason. 275 */ 276 @Test(groups = {"readLocalFiles"}, expectedExceptions = NullPointerException.class) 277 public void parse03() throws SAXException, IOException { 278 try(FileInputStream fis = new FileInputStream(XML_DIR + "correct2.xml")) { 279 new XMLFilterImpl().parse(new InputSource(fis)); 280 } 281 } 282 }