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.USER_DIR; 27 import static jaxp.library.JAXPTestUtilities.failUnexpected; 28 import static org.testng.Assert.assertFalse; 29 import static org.testng.Assert.assertTrue; 30 31 import java.io.File; 32 import java.io.IOException; 33 34 import javax.xml.parsers.ParserConfigurationException; 35 import javax.xml.parsers.SAXParser; 36 import javax.xml.parsers.SAXParserFactory; 37 38 import org.testng.annotations.BeforeTest; 39 import org.testng.annotations.Test; 40 import org.xml.sax.SAXException; 41 42 /** 43 * Class contains the test cases for SAXParser API 44 */ 45 public class SAXParserTest03 { 46 47 /** 48 * Copy necessary dtd files to user directory before any test run. 49 */ 50 @BeforeTest 51 protected void setup() throws IOException { 52 TestUtils.copyFiles(TestUtils.XML_DIR, USER_DIR, "firstdtd.dtd"); 53 } 54 55 /** 56 * parsertest.xml holds a valid document. This method tests the validating 57 * parser. 58 */ 59 @Test 60 public void testParseValidate01() { 61 try { 62 SAXParserFactory spf = SAXParserFactory.newInstance(); 63 spf.setValidating(true); 64 SAXParser saxparser = spf.newSAXParser(); 65 MyErrorHandler handler = MyErrorHandler.newInstance(); 66 saxparser.parse(new File(TestUtils.XML_DIR, "parsertest.xml"), handler); 67 assertFalse(handler.errorOccured); 68 } catch (ParserConfigurationException | SAXException | IOException e) { 69 failUnexpected(e); 70 } 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 @Test 78 public void testParseValidate02() { 79 try { 80 SAXParserFactory spf = SAXParserFactory.newInstance(); 81 spf.setValidating(true); 82 spf.setNamespaceAware(true); 83 SAXParser saxparser = spf.newSAXParser(); 84 MyErrorHandler handler = MyErrorHandler.newInstance(); 85 saxparser.parse(new File(TestUtils.XML_DIR, "validns.xml"), handler); 86 assertFalse(handler.errorOccured); 87 } catch (ParserConfigurationException | SAXException | IOException e) { 88 failUnexpected(e); 89 } 90 } 91 92 /** 93 * invalidns.xml holds an invalid document with XML namespaces in it. This 94 * method tests the validating parser with namespace processing on. It 95 * should throw validation error. 96 */ 97 @Test 98 public void testParseValidate03() { 99 MyErrorHandler handler = MyErrorHandler.newInstance(); 100 try { 101 SAXParserFactory spf = SAXParserFactory.newInstance(); 102 spf.setValidating(true); 103 spf.setNamespaceAware(true); 104 SAXParser saxparser = spf.newSAXParser(); 105 saxparser.parse(new File(TestUtils.XML_DIR, "invalidns.xml"), handler); 106 failUnexpected(new RuntimeException()); 107 } catch (ParserConfigurationException | SAXException | IOException e) { 108 if (e instanceof SAXException) { 109 assertTrue(handler.errorOccured); 110 } 111 } 112 } 113 114 }