8002297: sun/net/www/protocol/http/StackTraceTest.java fails intermittently
authorchegar
Tue, 06 Nov 2012 21:01:43 +0000
changeset 14410 4d59642be50d
parent 14409 d879c92507ec
child 14411 65913e68c0a6
8002297: sun/net/www/protocol/http/StackTraceTest.java fails intermittently Reviewed-by: alanb, dsamersoff
jdk/test/sun/net/www/protocol/http/StackTraceTest.java
--- a/jdk/test/sun/net/www/protocol/http/StackTraceTest.java	Tue Nov 06 14:59:22 2012 -0500
+++ b/jdk/test/sun/net/www/protocol/http/StackTraceTest.java	Tue Nov 06 21:01:43 2012 +0000
@@ -32,26 +32,28 @@
 import java.io.IOException;
 
 public class StackTraceTest {
-    public static void main(String[] args) {
-        try {
-            URL url = new URL("http://localhost:8080/");
-            URLConnection uc = url.openConnection();
-            System.out.println("key = "+uc.getHeaderFieldKey(20));
-            uc.getInputStream();
-        } catch (IOException ioe) {
-            ioe.printStackTrace();
+    public static void main(String[] args) throws Exception {
+        URL url;
+        try (ServerSocket ss = new ServerSocket(0)) {  // refusing socket
+            url = new URL("http://localhost:" + ss.getLocalPort() + "/");
+        }
+        URLConnection uc = url.openConnection();
+
+        // Trigger implicit connection by trying to retrieve bogus
+        // response header, and force remembered exception
+        uc.getHeaderFieldKey(20);
 
-            if (!(ioe instanceof ConnectException)) {
-                throw new RuntimeException("Expect ConnectException, got "+ioe);
-            }
-            if (ioe.getMessage() == null) {
+        try {
+            uc.getInputStream();  // expect to throw
+            throw new RuntimeException("Expected getInputStream to throw");
+        } catch (IOException ioe) {
+            if (!(ioe instanceof ConnectException))
+                throw new RuntimeException("Expect ConnectException, got " + ioe);
+            if (ioe.getMessage() == null)
                 throw new RuntimeException("Exception message is null");
-            }
-
-            // this exception should be a chained exception
-            if (ioe.getCause() == null) {
-                throw new RuntimeException("Excepting a chained exception, but got: ", ioe);
-            }
+            if (ioe.getCause() == null)
+                throw new RuntimeException("Excepting a chained exception, but got: ",
+                                           ioe);
         }
     }
 }