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.assertEquals; 29 import static org.testng.Assert.assertFalse; 30 import static org.testng.Assert.assertNotNull; 31 import static org.testng.Assert.assertNull; 32 import static org.testng.Assert.assertTrue; 33 import org.testng.annotations.Test; 34 import org.xml.sax.InputSource; 35 import org.xml.sax.SAXException; 36 import org.xml.sax.SAXNotRecognizedException; 37 import org.xml.sax.SAXNotSupportedException; 38 import org.xml.sax.XMLReader; 39 import org.xml.sax.ext.DeclHandler; 40 import org.xml.sax.ext.LexicalHandler; 41 import org.xml.sax.helpers.XMLFilterImpl; 42 import static org.xml.sax.ptests.SAXTestConst.XML_DIR; 43 44 /** 45 * Class containing the test cases for SAXParser API 46 */ 47 public class XMLReaderTest extends JAXPFileReadOnlyBaseTest { 48 49 /** 50 * XML namespaces. 51 */ 52 private static final String NAMESPACES 53 = "http://xml.org/sax/features/namespaces"; 54 55 /** 56 * XML namespaces prefixes. 57 */ 58 private static final String NAMESPACE_PREFIXES 59 = "http://xml.org/sax/features/namespace-prefixes"; 60 61 /** 62 * A string intern name. 63 */ 64 private static final String STRING_INTERNING 65 = "http://xml.org/sax/features/string-interning"; 66 67 /** 417 xmlReader.setErrorHandler(null); 418 } 419 420 /** 421 * Parse a null input source throw NPE. 422 * 423 * @throws Exception If any errors occur. 424 */ 425 @Test(expectedExceptions = NullPointerException.class) 426 public void parse01() throws Exception { 427 SAXParserFactory spf = SAXParserFactory.newInstance(); 428 spf.setNamespaceAware(true); 429 spf.newSAXParser().getXMLReader().parse((InputSource) null); 430 } 431 432 /** 433 * Unit test for parse a error-formatted file. SAXException is expected. 434 * 435 * @throws Exception If any errors occur. 436 */ 437 @Test(groups = {"readLocalFiles"}, expectedExceptions = SAXException.class) 438 public void parse02() throws Exception { 439 try (FileInputStream fis = new FileInputStream(XML_DIR + "invalid.xml")) { 440 SAXParserFactory spf = SAXParserFactory.newInstance(); 441 spf.setNamespaceAware(true); 442 spf.newSAXParser().getXMLReader().parse(new InputSource(fis)); 443 } 444 } 445 446 /** 447 * Unit test for parse a well-formatted file. No exception is expected. 448 * 449 * @throws Exception If any errors occur. 450 */ 451 @Test(groups = {"readLocalFiles"}) 452 public void parse03() throws Exception { 453 try (FileInputStream fis = new FileInputStream(XML_DIR + "correct2.xml")) { 454 SAXParserFactory spf = SAXParserFactory.newInstance(); 455 spf.setNamespaceAware(true); 456 spf.newSAXParser().getXMLReader().parse(new InputSource(fis)); 457 } 458 } 459 460 /** 461 * Modified by IBM Xerces does not support this feature and it is not 462 * mandatory. 463 * 464 * @throws Exception If any errors occur. 465 */ 466 @Test(expectedExceptions = SAXNotSupportedException.class) 467 public void xrProperty01() throws Exception { 468 SAXParserFactory spf = SAXParserFactory.newInstance(); 469 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 470 xmlReader.getProperty(XML_STRING); 471 } | 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.assertEquals; 26 import static org.testng.Assert.assertFalse; 27 import static org.testng.Assert.assertNotNull; 28 import static org.testng.Assert.assertNull; 29 import static org.testng.Assert.assertTrue; 30 import static org.xml.sax.ptests.SAXTestConst.XML_DIR; 31 32 import java.io.FileInputStream; 33 34 import javax.xml.parsers.SAXParserFactory; 35 36 import org.testng.annotations.Listeners; 37 import org.testng.annotations.Test; 38 import org.xml.sax.InputSource; 39 import org.xml.sax.SAXException; 40 import org.xml.sax.SAXNotRecognizedException; 41 import org.xml.sax.SAXNotSupportedException; 42 import org.xml.sax.XMLReader; 43 import org.xml.sax.ext.DeclHandler; 44 import org.xml.sax.ext.LexicalHandler; 45 import org.xml.sax.helpers.XMLFilterImpl; 46 47 /** 48 * Class containing the test cases for SAXParser API 49 */ 50 @Listeners({jaxp.library.FilePolicy.class}) 51 public class XMLReaderTest { 52 53 /** 54 * XML namespaces. 55 */ 56 private static final String NAMESPACES 57 = "http://xml.org/sax/features/namespaces"; 58 59 /** 60 * XML namespaces prefixes. 61 */ 62 private static final String NAMESPACE_PREFIXES 63 = "http://xml.org/sax/features/namespace-prefixes"; 64 65 /** 66 * A string intern name. 67 */ 68 private static final String STRING_INTERNING 69 = "http://xml.org/sax/features/string-interning"; 70 71 /** 421 xmlReader.setErrorHandler(null); 422 } 423 424 /** 425 * Parse a null input source throw NPE. 426 * 427 * @throws Exception If any errors occur. 428 */ 429 @Test(expectedExceptions = NullPointerException.class) 430 public void parse01() throws Exception { 431 SAXParserFactory spf = SAXParserFactory.newInstance(); 432 spf.setNamespaceAware(true); 433 spf.newSAXParser().getXMLReader().parse((InputSource) null); 434 } 435 436 /** 437 * Unit test for parse a error-formatted file. SAXException is expected. 438 * 439 * @throws Exception If any errors occur. 440 */ 441 @Test(expectedExceptions = SAXException.class) 442 public void parse02() throws Exception { 443 try (FileInputStream fis = new FileInputStream(XML_DIR + "invalid.xml")) { 444 SAXParserFactory spf = SAXParserFactory.newInstance(); 445 spf.setNamespaceAware(true); 446 spf.newSAXParser().getXMLReader().parse(new InputSource(fis)); 447 } 448 } 449 450 /** 451 * Unit test for parse a well-formatted file. No exception is expected. 452 * 453 * @throws Exception If any errors occur. 454 */ 455 @Test 456 public void parse03() throws Exception { 457 try (FileInputStream fis = new FileInputStream(XML_DIR + "correct2.xml")) { 458 SAXParserFactory spf = SAXParserFactory.newInstance(); 459 spf.setNamespaceAware(true); 460 spf.newSAXParser().getXMLReader().parse(new InputSource(fis)); 461 } 462 } 463 464 /** 465 * Modified by IBM Xerces does not support this feature and it is not 466 * mandatory. 467 * 468 * @throws Exception If any errors occur. 469 */ 470 @Test(expectedExceptions = SAXNotSupportedException.class) 471 public void xrProperty01() throws Exception { 472 SAXParserFactory spf = SAXParserFactory.newInstance(); 473 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 474 xmlReader.getProperty(XML_STRING); 475 } |