1 /* 2 * Copyright (c) 2002, 2012, 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 /** 25 * @test 26 * @bug 4773417 5003746 27 * @summary HttpURLConnection.getInputStream() produces IOException with 28 * bad stack trace; HttpURLConnection.getInputStream loses 29 * exception message, exception class 30 */ 31 import java.net.*; 32 import java.io.IOException; 33 34 public class StackTraceTest { 35 public static void main(String[] args) throws Exception { 36 URL url; 37 try (ServerSocket ss = new ServerSocket(0)) { // refusing socket 38 url = new URL("http://localhost:" + ss.getLocalPort() + "/"); 39 } 40 URLConnection uc = url.openConnection(); 41 42 // Trigger implicit connection by trying to retrieve bogus 43 // response header, and force remembered exception 44 uc.getHeaderFieldKey(20); 45 46 try { 47 uc.getInputStream(); // expect to throw 48 throw new RuntimeException("Expected getInputStream to throw"); 49 } catch (IOException ioe) { 50 if (!(ioe instanceof ConnectException)) 51 throw new RuntimeException("Expect ConnectException, got " + ioe); 52 if (ioe.getMessage() == null) 53 throw new RuntimeException("Exception message is null"); 54 if (ioe.getCause() == null) 55 throw new RuntimeException("Excepting a chained exception, but got: ", 56 ioe); 57 } 58 } 59 } | 1 /* 2 * Copyright (c) 2002, 2019, 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 /* 25 * @test 26 * @bug 4773417 5003746 27 * @library /test/lib 28 * @build jdk.test.lib.Utils 29 * @run main StackTraceTest 30 * @summary HttpURLConnection.getInputStream() produces IOException with 31 * bad stack trace; HttpURLConnection.getInputStream loses 32 * exception message, exception class 33 */ 34 import java.net.*; 35 import java.io.IOException; 36 import jdk.test.lib.Utils; 37 38 public class StackTraceTest { 39 public static void main(String[] args) throws Exception { 40 InetSocketAddress refusing = Utils.refusingEndpoint(); 41 int port = refusing.getPort(); 42 String host = refusing.getAddress().getHostAddress(); 43 if (host.contains(":")) 44 host = "[" + host + "]"; 45 URL url = URI.create("http://" + host + ":" + port + "/").toURL(); 46 System.out.println("URL: " + url); 47 48 URLConnection uc = url.openConnection(); 49 50 // Trigger implicit connection by trying to retrieve bogus 51 // response header, and force remembered exception 52 uc.getHeaderFieldKey(20); 53 54 try { 55 uc.getInputStream(); // expect to throw 56 throw new RuntimeException("Expected getInputStream to throw"); 57 } catch (IOException ioe) { 58 if (!(ioe instanceof ConnectException)) 59 throw new RuntimeException("Expect ConnectException, got " + ioe); 60 if (ioe.getMessage() == null) 61 throw new RuntimeException("Exception message is null"); 62 if (ioe.getCause() == null) 63 throw new RuntimeException("Excepting a chained exception, but got: ", 64 ioe); 65 } 66 } 67 } |