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()); 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 } | 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()); 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 } |