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 dom.ls;
25
26import javax.xml.parsers.DocumentBuilderFactory;
27import javax.xml.parsers.ParserConfigurationException;
28
29import org.testng.Assert;
30import org.testng.annotations.Listeners;
31import org.testng.annotations.Test;
32import org.w3c.dom.Document;
33import org.w3c.dom.Element;
34import org.w3c.dom.ls.DOMImplementationLS;
35import org.w3c.dom.ls.LSException;
36
37/*
38 * @test
39 * @bug 6710741
40 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
41 * @run testng/othervm -DrunSecMngr=true dom.ls.Bug6710741Test
42 * @run testng/othervm dom.ls.Bug6710741Test
43 * @summary Test there should be stack trace information if LSSerializer().writeToString reports an exception.
44 */
45@Listeners({jaxp.library.BasePolicy.class})
46public class Bug6710741Test {
47
48    @Test
49    public final void test() {
50        try {
51            Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
52            Element el = doc.createElement("x");
53            DOMImplementationLS ls = (DOMImplementationLS) doc.getImplementation().getFeature("LS", "3.0");
54            System.out.println(ls.createLSSerializer().writeToString(el));
55        } catch (ParserConfigurationException ex) {
56            ex.printStackTrace();
57            Assert.fail(ex.getMessage());
58        } catch (LSException ex) {
59            ex.printStackTrace();
60            System.out.println("cause: " + ex.getCause());
61            if (ex.getCause() == null) {
62                Assert.fail("should set cause.");
63            }
64        }
65    }
66
67    @Test
68    public void testWorkaround() {
69        Document doc;
70        try {
71            doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
72            Element el = doc.createElement("x");
73            doc.appendChild(el);
74            DOMImplementationLS ls = (DOMImplementationLS) doc.getImplementation().getFeature("LS", "3.0");
75            System.out.println(ls.createLSSerializer().writeToString(doc));
76        } catch (ParserConfigurationException ex) {
77            ex.printStackTrace();
78            Assert.fail(ex.getMessage());
79        }
80    }
81
82}
83