1/*
2 * Copyright (c) 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 parsers;
25
26import java.io.StringReader;
27import javax.xml.parsers.SAXParser;
28import javax.xml.parsers.SAXParserFactory;
29import org.xml.sax.InputSource;
30import org.xml.sax.SAXException;
31import org.xml.sax.SAXParseException;
32import org.xml.sax.helpers.DefaultHandler;
33
34import org.testng.annotations.DataProvider;
35import org.testng.annotations.Listeners;
36import org.testng.annotations.Test;
37
38/*
39 * @test
40 * @bug 8157797
41 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
42 * @run testng/othervm -DrunSecMngr=true parsers.HandleError
43 * @run testng/othervm parsers.HandleError
44 * @summary Tests that the parser handles errors properly.
45 */
46@Listeners({jaxp.library.BasePolicy.class})
47public class HandleError {
48
49    /*
50     * Verifies that the parser returns with no unexpected "java.lang.InternalError"
51     * when continue-after-fatal-error is requested.
52    */
53    @Test
54    public void test() throws Exception {
55        String invalidXml = "<a>";
56        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
57        saxParserFactory.setFeature("http://apache.org/xml/features/continue-after-fatal-error", true);
58        SAXParser parser = saxParserFactory.newSAXParser();
59        parser.parse(new InputSource(new StringReader(invalidXml)), new DefaultHandler() {
60            @Override
61            public void fatalError(SAXParseException e) throws SAXException {
62                System.err.printf("%s%n", e.getMessage());
63            }
64        });
65    }
66
67
68    /*
69     * Verifies that the parser throws SAXParseException when parsing error is
70     * encountered when:
71     * continue-after-fatal-error is not set, the default it false
72     * continue-after-fatal-error is explicitly set to false
73    */
74    @Test(dataProvider = "setFeature", expectedExceptions = SAXParseException.class)
75    public void test1(boolean setFeature, boolean value) throws Exception {
76        String invalidXml = "<a>";
77        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
78        if (setFeature) {
79            saxParserFactory.setFeature("http://apache.org/xml/features/continue-after-fatal-error", value);
80        }
81        SAXParser parser = saxParserFactory.newSAXParser();
82        parser.parse(new InputSource(new StringReader(invalidXml)), new DefaultHandler());
83    }
84
85    /*
86       DataProvider: used to set feature "continue-after-fatal-error"
87        Data columns:
88        flag to indicate the feature is to be set, the value of the feature
89     */
90    @DataProvider(name = "setFeature")
91    public Object[][] getFeatureSetting() {
92
93        return new Object[][]{
94            {false, false},
95            {true, false},
96         };
97    }
98}
99