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.InputSource; 37 import org.xml.sax.SAXNotRecognizedException; 38 import org.xml.sax.XMLReader; 39 import org.xml.sax.helpers.XMLFilterImpl; 40 41 /** 42 * Unit test for XMLFilter. 43 */ 44 @Listeners({jaxp.library.FilePolicy.class}) 45 public class XMLFilterTest { 46 /** 47 * name spaces constant. 48 */ 49 private static final String NAMESPACES = 50 "http://xml.org/sax/features/namespaces"; 51 52 /** 53 * name spaces prefixes constant. 54 */ 55 private static final String NAMESPACE_PREFIXES = 56 "http://xml.org/sax/features/namespace-prefixes"; 57 58 /** 59 * No exception expected when set a correct content handler. 60 */ 61 @Test 62 public void contentHandler01() { 63 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 64 xmlFilter.setContentHandler(xmlFilter); 65 assertNotNull(xmlFilter.getContentHandler()); 66 } 67 68 /** 69 * No exception is expected when set content handler as null. 70 */ 71 @Test 72 public void contentHandler02() { 73 new XMLFilterImpl().setContentHandler(null); 74 } 75 76 /** 77 * No exception expected when set a correct entity solver. 78 */ 79 @Test 80 public void entity01() { 81 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 82 xmlFilter.setEntityResolver(xmlFilter); 83 assertNotNull(xmlFilter.getEntityResolver()); 84 } 85 86 /** 87 * No exception is expected when set entity resolver as null. 88 */ 89 @Test 90 public void entity02() { 91 new XMLFilterImpl().setEntityResolver(null); 92 } 93 94 /** 95 * No exception expected when set a correct DTD handler. 96 */ 97 @Test 98 public void dtdHandler01() { 99 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 100 xmlFilter.setDTDHandler(xmlFilter); 101 assertNotNull(xmlFilter.getDTDHandler()); 102 } 103 104 /** 105 * No exception is expected when set DTD handler as null. 106 */ 107 @Test 108 public void dtdHandler02() { 109 new XMLFilterImpl().setDTDHandler(null); 110 } 111 112 /** 113 * No exception expected when set a correct error handler. 114 */ 115 @Test 116 public void errorHandler01() { 117 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 118 xmlFilter.setErrorHandler(xmlFilter); 119 assertNotNull(xmlFilter.getErrorHandler()); 120 } 121 122 /** 123 * No exception is expected when set error handler as null. 124 */ 125 @Test 126 public void errorHandler02() { 127 new XMLFilterImpl().setErrorHandler(null); 128 } 129 130 /** 131 * By default true is expected get namespaces feature. 132 * 133 * @throws Exception If any errors occur. 134 */ 135 @Test 136 public void getFeature01() throws Exception { 137 SAXParserFactory spf = SAXParserFactory.newInstance(); 138 spf.setNamespaceAware(true); 139 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 140 141 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 142 xmlFilter.setParent(xmlReader); 143 assertTrue(xmlFilter.getFeature(NAMESPACES)); 144 } 145 146 /** 147 * By default false is expected get namespaces-prefix feature. 148 * 149 * @throws Exception If any errors occur. 150 */ 151 @Test 152 public void getFeature02() throws Exception { 153 SAXParserFactory spf = SAXParserFactory.newInstance(); 154 spf.setNamespaceAware(true); 155 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 156 xmlFilter.setParent(spf.newSAXParser().getXMLReader()); 157 assertFalse(xmlFilter.getFeature(NAMESPACE_PREFIXES)); 158 } 159 160 /** 161 * SAXNotRecognizedException is expected when get a feature by an invalid 162 * feature name. 163 * 164 * @throws Exception If any errors occur. 165 */ 166 @Test(expectedExceptions = SAXNotRecognizedException.class) 167 public void getFeature03() throws Exception { 168 new XMLFilterImpl().getFeature("no-meaning-feature"); 169 } 170 171 /** 172 * Set namespaces feature to a value to XMLFilter. it's expected same when 173 * obtain it again. 174 * 175 * @throws Exception If any errors occur. 176 */ 177 @Test 178 public void setFeature01() throws Exception { 179 SAXParserFactory spf = SAXParserFactory.newInstance(); 180 spf.setNamespaceAware(true); 181 182 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 183 xmlFilter.setParent(spf.newSAXParser().getXMLReader()); 184 xmlFilter.setFeature(NAMESPACES, false); 185 assertFalse(xmlFilter.getFeature(NAMESPACES)); 186 xmlFilter.setFeature(NAMESPACES, true); 187 assertTrue(xmlFilter.getFeature(NAMESPACES)); 188 } 189 190 /** 191 * Set namespaces-prefix feature to a value to XMLFilter. it's expected same 192 * when obtain it again. 193 * 194 * @throws Exception If any errors occur. 195 */ 196 @Test 197 public void setFeature02() throws Exception { 198 SAXParserFactory spf = SAXParserFactory.newInstance(); 199 spf.setNamespaceAware(true); 200 201 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 202 xmlFilter.setParent(spf.newSAXParser().getXMLReader()); 203 xmlFilter.setFeature(NAMESPACE_PREFIXES, false); 204 assertFalse(xmlFilter.getFeature(NAMESPACE_PREFIXES)); 205 xmlFilter.setFeature(NAMESPACE_PREFIXES, true); 206 assertTrue(xmlFilter.getFeature(NAMESPACE_PREFIXES)); 207 } 208 209 /** 210 * NullPointerException is expected when parse a null InputSource. 211 * 212 * @throws Exception If any errors occur. 213 */ 214 @Test(expectedExceptions = NullPointerException.class) 215 public void parse01() throws Exception { 216 new XMLFilterImpl().parse((InputSource)null); 217 } 218 219 /** 220 * SAXException is expected when parsing a invalid formatted XML file. 221 * 222 * @throws Exception If any errors occur. 223 */ 224 @Test(expectedExceptions = NullPointerException.class) 225 public void parse02() throws Exception { 226 try(FileInputStream fis = new FileInputStream(XML_DIR + "invalid.xml")) { 227 new XMLFilterImpl().parse(new InputSource(fis)); 228 } 229 } 230 231 /** 232 * No exception when parse a normal XML file. 233 * 234 * @throws Exception If any errors occur. 235 */ 236 @Test(expectedExceptions = NullPointerException.class) 237 public void parse03() throws Exception { 238 try(FileInputStream fis = new FileInputStream(XML_DIR + "correct2.xml")) { 239 new XMLFilterImpl().parse(new InputSource(fis)); 240 } 241 } 242 }