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.ContentHandler; 33 import org.xml.sax.InputSource; 34 import org.xml.sax.SAXException; 35 import org.xml.sax.SAXNotRecognizedException; 36 import org.xml.sax.XMLReader; 37 import org.xml.sax.helpers.ParserAdapter; 38 import org.xml.sax.helpers.XMLFilterImpl; 39 import org.xml.sax.helpers.XMLReaderAdapter; 40 import static org.xml.sax.ptests.SAXTestConst.XML_DIR; 41 42 43 /** 44 * Unit test cases for ParserAdapter API. By default the only features recognized 45 * are namespaces and namespace-prefixes. 46 */ 47 public class ParserAdapterTest extends JAXPFileReadOnlyBaseTest { 48 /** 49 * namespaces feature name. 50 */ 51 private static final String NAMESPACES = 52 "http://xml.org/sax/features/namespaces"; 53 54 /** 55 * namespaces-prefiexs feature name. 56 */ 57 private static final String NAMESPACE_PREFIXES = 58 "http://xml.org/sax/features/namespace-prefixes"; 59 60 /** 61 * ParserAdapter instance to share by all tests. 62 */ 63 private final ParserAdapter parserAdapter; 64 65 /** 66 * Initiate ParserAdapter. 67 * @throws Exception If any errors occur. 217 public void setFeature04() throws Exception { 218 parserAdapter.setFeature(NAMESPACE_PREFIXES, true); 219 assertTrue(parserAdapter.getFeature(NAMESPACE_PREFIXES)); 220 } 221 222 /** 223 * NPE expected when parsing a null object by ParserAdapter. 224 * 225 * @throws Exception If any errors occur. 226 */ 227 @Test(expectedExceptions = NullPointerException.class) 228 public void parse01() throws Exception { 229 parserAdapter.parse((InputSource)null); 230 } 231 232 /** 233 * SAXException expected when parsing a wrong-formatter XML with ParserAdapter. 234 * 235 * @throws Exception If any errors occur. 236 */ 237 @Test(groups = {"readLocalFiles"}, expectedExceptions = SAXException.class) 238 public void parse02() throws Exception { 239 try(FileInputStream fis = new FileInputStream(XML_DIR + "invalid.xml")) { 240 InputSource is = new InputSource(fis); 241 parserAdapter.parse(is); 242 } 243 } 244 245 /** 246 * Parse a well-formatter XML with ParserAdapter. 247 * 248 * @throws Exception If any errors occur. 249 */ 250 @Test(groups = {"readLocalFiles"}) 251 public void parse03() throws Exception { 252 try(FileInputStream fis = new FileInputStream(XML_DIR + "correct.xml")) { 253 InputSource is = new InputSource(fis); 254 parserAdapter.parse(is); 255 } 256 } 257 } | 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. 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 } |