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