< prev index next >

test/java/net/CookieHandler/CookieManagerTest.java

Print this page

        

@@ -23,23 +23,43 @@
 
 /*
  * @test
  * @summary Unit test for java.net.CookieManager
  * @bug 6244040 7150552 7051862
+ * @modules jdk.httpserver
  * @run main/othervm -ea CookieManagerTest
  * @author Edward Wang
  */
 
 import com.sun.net.httpserver.*;
 import java.io.IOException;
 import java.net.*;
+import static java.net.Proxy.NO_PROXY;
 
 public class CookieManagerTest {
 
     static CookieTransactionHandler httpTrans;
     static HttpServer server;
 
+    static final String hostAddress = getAddr();
+
+    /** Returns an IP literal suitable for use by the test. */
+    static String getAddr() {
+        try {
+            InetAddress lh = InetAddress.getLocalHost();
+            System.out.println("Trying: " + lh);
+            if (lh.isReachable(5_000)) {
+                System.out.println("Using: " + lh);
+                return lh.getHostAddress();
+            }
+        } catch (IOException x) {
+            System.out.println("Debug: caught:" + x);
+        }
+        System.out.println("Using: \"127.0.0.1\"");
+        return "127.0.0.1";
+    }
+
     public static void main(String[] args) throws Exception {
         startHttpServer();
         makeHttpCall();
 
         if (httpTrans.badRequest) {

@@ -76,12 +96,12 @@
             throw new RuntimeException("Return value is not false!");
     }
 
     public static void makeHttpCall() throws IOException {
         try {
-            System.out.println("http server listenining on: "
-                    + server.getAddress().getPort());
+            int port = server.getAddress().getPort();
+            System.out.println("http server listenining on: " + port);
 
             // install CookieManager to use
             CookieHandler.setDefault(new CookieManager());
 
             for (int i = 0; i < CookieTransactionHandler.testCount; i++) {

@@ -90,15 +110,16 @@
                 ((CookieManager)CookieHandler.getDefault())
                     .setCookiePolicy(CookieTransactionHandler.testPolicies[i]);
                 ((CookieManager)CookieHandler.getDefault())
                     .getCookieStore().removeAll();
                 URL url = new URL("http" ,
-                                  InetAddress.getLocalHost().getHostAddress(),
+                                  hostAddress,
                                   server.getAddress().getPort(),
                                   CookieTransactionHandler.testCases[i][0]
                                                           .serverPath);
-                HttpURLConnection uc = (HttpURLConnection)url.openConnection();
+                System.out.println("Requesting " + url);
+                HttpURLConnection uc = (HttpURLConnection)url.openConnection(NO_PROXY);
                 uc.getResponseCode();
                 uc.disconnect();
             }
         } finally {
             server.stop(0);

@@ -114,12 +135,10 @@
     public static boolean badRequest = false;
     // the main test control logic will also loop exactly this number
     // to send http request
     public static final int testCount = 6;
 
-    private String localHostAddr = "127.0.0.1";
-
     @Override
     public void handle(HttpExchange exchange) throws IOException {
         if (testDone < testCases[testcaseDone].length) {
             // still have other tests to run,
             // check the Cookie header and then redirect it

@@ -186,14 +205,12 @@
 
     CookieTransactionHandler() {
         testCases = new CookieTestCase[testCount][];
         testPolicies = new CookiePolicy[testCount];
 
-        try {
-            localHostAddr = InetAddress.getLocalHost().getHostAddress();
-        } catch (Exception ignored) {
-        };
+        String localHostAddr = CookieManagerTest.hostAddress;
+
         int count = 0;
 
         // an http session with Netscape cookies exchanged
         testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER;
         testCases[count++] = new CookieTestCase[]{
< prev index next >