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