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