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