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