1 /* 2 * Copyright (c) 2014, 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 24 package javax.xml.parsers.ptests; 25 26 import static jaxp.library.JAXPTestUtilities.failUnexpected; 27 import static org.testng.Assert.assertFalse; 28 import static org.testng.Assert.assertTrue; 29 30 import java.io.File; 31 import java.io.IOException; 32 33 import javax.xml.parsers.ParserConfigurationException; 34 import javax.xml.parsers.SAXParser; 35 import javax.xml.parsers.SAXParserFactory; 36 37 import org.testng.annotations.DataProvider; 38 import org.testng.annotations.Test; 39 import org.xml.sax.SAXException; 40 41 /** 42 * Class contains the test cases for SAXParser API 43 */ 44 public class SAXParserTest03 { 45 46 /** 47 * Provide SAXParserFactory. 48 * 49 * @throws Exception 50 */ 51 @DataProvider(name = "input-provider") 52 public Object[][] getFactory() { 53 SAXParserFactory spf = SAXParserFactory.newInstance(); 54 spf.setValidating(true); 55 MyErrorHandler handler = MyErrorHandler.newInstance(); 56 return new Object[][] { { spf, handler } }; 57 } 58 59 /** 60 * parsertest.xml holds a valid document. This method tests the validating 61 * parser. 62 */ 63 @Test(dataProvider = "input-provider") 64 public void testParseValidate01(SAXParserFactory spf, MyErrorHandler handler) { 65 try { 66 SAXParser saxparser = spf.newSAXParser(); 67 saxparser.parse(new File(TestUtils.XML_DIR, "parsertest.xml"), handler); 68 assertFalse(handler.errorOccured); 69 } catch (ParserConfigurationException | SAXException | IOException e) { 70 failUnexpected(e); 71 } 72 } 73 74 /** 75 * validns.xml holds a valid document with XML namespaces in it. This method 76 * tests the Validating parser with namespace processing on. 77 */ 78 @Test(dataProvider = "input-provider") 79 public void testParseValidate02(SAXParserFactory spf, MyErrorHandler handler) { 80 try { 81 spf.setNamespaceAware(true); 82 SAXParser saxparser = spf.newSAXParser(); 83 saxparser.parse(new File(TestUtils.XML_DIR, "validns.xml"), handler); 84 assertFalse(handler.errorOccured); 85 } catch (ParserConfigurationException | SAXException | IOException e) { 86 failUnexpected(e); 87 } 88 } 89 90 /** 91 * invalidns.xml holds an invalid document with XML namespaces in it. This 92 * method tests the validating parser with namespace processing on. It 93 * should throw validation error. 94 */ 95 @Test(dataProvider = "input-provider") 96 public void testParseValidate03(SAXParserFactory spf, MyErrorHandler handler) { 97 try { 98 spf.setNamespaceAware(true); 99 SAXParser saxparser = spf.newSAXParser(); 100 saxparser.parse(new File(TestUtils.XML_DIR, "invalidns.xml"), handler); 101 failUnexpected(new RuntimeException()); 102 } catch (ParserConfigurationException | SAXException | IOException e) { 103 if (e instanceof SAXException) { 104 assertTrue(handler.errorOccured); 105 } 106 } 107 } 108 109 } | 1 /* 2 * Copyright (c) 1999, 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 24 package javax.xml.parsers.ptests; 25 26 import static org.testng.Assert.assertFalse; 27 import static org.testng.Assert.assertTrue; 28 import java.io.File; 29 import java.io.FilePermission; 30 import javax.xml.parsers.SAXParser; 31 import javax.xml.parsers.SAXParserFactory; 32 import static javax.xml.parsers.ptests.ParserTestConst.XML_DIR; 33 import jaxp.library.JAXPFileReadOnlyBaseTest; 34 import static org.testng.Assert.fail; 35 import org.testng.annotations.AfterGroups; 36 import org.testng.annotations.BeforeGroups; 37 import org.testng.annotations.DataProvider; 38 import org.testng.annotations.Test; 39 import org.xml.sax.SAXException; 40 41 /** 42 * Class contains the test cases for SAXParser API 43 */ 44 public class SAXParserTest03 extends JAXPFileReadOnlyBaseTest { 45 46 /** 47 * Provide SAXParserFactory. 48 * 49 * @return a dimensional contains. 50 */ 51 @DataProvider(name = "input-provider") 52 public Object[][] getFactory() { 53 SAXParserFactory spf = SAXParserFactory.newInstance(); 54 spf.setValidating(true); 55 return new Object[][] { { spf, MyErrorHandler.newInstance() } }; 56 } 57 58 /** 59 * parsertest.xml holds a valid document. This method tests the validating 60 * parser. 61 * 62 * @param spf a Parser factory. 63 * @param handler an error handler for capturing events. 64 * @throws Exception If any errors occur. 65 */ 66 @Test(groups = {"readLocalFiles"}, dataProvider = "input-provider") 67 public void testParseValidate01(SAXParserFactory spf, MyErrorHandler handler) 68 throws Exception { 69 spf.newSAXParser().parse(new File(XML_DIR, "parsertest.xml"), handler); 70 assertFalse(handler.isErrorOccured()); 71 } 72 73 /** 74 * validns.xml holds a valid document with XML namespaces in it. This method 75 * tests the Validating parser with namespace processing on. 76 * 77 * @param spf a Parser factory. 78 * @param handler an error handler for capturing events. 79 * @throws Exception If any errors occur. 80 */ 81 @Test(groups = {"readLocalFiles"}, dataProvider = "input-provider") 82 public void testParseValidate02(SAXParserFactory spf, MyErrorHandler handler) 83 throws Exception { 84 spf.setNamespaceAware(true); 85 spf.newSAXParser().parse(new File(XML_DIR, "validns.xml"), handler); 86 assertFalse(handler.isErrorOccured()); 87 } 88 89 /** 90 * invalidns.xml holds an invalid document with XML namespaces in it. This 91 * method tests the validating parser with namespace processing on. It 92 * should throw validation error. 93 * 94 * @param spf a Parser factory. 95 * @param handler an error handler for capturing events. 96 * @throws Exception If any errors occur. 97 */ 98 @Test(groups = {"readLocalFiles"}, dataProvider = "input-provider") 99 public void testParseValidate03(SAXParserFactory spf, MyErrorHandler handler) 100 throws Exception { 101 try { 102 spf.setNamespaceAware(true); 103 SAXParser saxparser = spf.newSAXParser(); 104 saxparser.parse(new File(XML_DIR, "invalidns.xml"), handler); 105 fail("Expecting SAXException here"); 106 } catch (SAXException e) { 107 assertTrue(handler.isErrorOccured()); 108 } 109 } 110 111 } |