1/*
2 * Copyright (c) 2014, 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 sax;
25
26import java.io.IOException;
27import java.io.StringReader;
28
29import javax.xml.parsers.ParserConfigurationException;
30import javax.xml.parsers.SAXParserFactory;
31
32import org.testng.Assert;
33import org.testng.annotations.Listeners;
34import org.testng.annotations.Test;
35import org.xml.sax.InputSource;
36import org.xml.sax.SAXException;
37import org.xml.sax.helpers.DefaultHandler;
38
39/*
40 * @test
41 * @bug 6889654
42 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
43 * @run testng/othervm -DrunSecMngr=true sax.Bug6889654Test
44 * @run testng/othervm sax.Bug6889654Test
45 * @summary Test SAXException includes whole information.
46 */
47@Listeners({jaxp.library.BasePolicy.class})
48public class Bug6889654Test {
49
50    final String MSG = "Failed to parse XML";
51
52    @Test
53    public void testException() {
54        try {
55            parse();
56        } catch (SAXException e) {
57            // e.printStackTrace();
58            String msg = e.toString();
59            if (msg.indexOf("systemId") == -1) {
60                Assert.fail("CR6889654 -- details should be returned.");
61            }
62            if (msg.indexOf(MSG) == -1) {
63                Assert.fail("CR6889649 -- additional error message not returned.");
64            }
65            System.out.println("error message:\n" + msg);
66        }
67    }
68
69    void parse() throws SAXException {
70        String xml = "<data>\n<broken/>\u0000</data>";
71
72        try {
73            InputSource is = new InputSource(new StringReader(xml));
74            is.setSystemId("file:///path/to/some.xml");
75            // notice that exception thrown here doesn't include the line number
76            // information when reported by JVM -- CR6889654
77            SAXParserFactory.newInstance().newSAXParser().parse(is, new DefaultHandler());
78        } catch (SAXException e) {
79            // notice that this message isn't getting displayed -- CR6889649
80            throw new SAXException(MSG, e);
81        } catch (ParserConfigurationException pce) {
82
83        } catch (IOException ioe) {
84
85        }
86
87    }
88
89}
90