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