6886108: Test case B4933582 binding to fixed port number
authormichaelm
Tue, 29 Sep 2009 10:00:57 +0100
changeset 3946 7ee79633ec36
parent 3944 e098e010b520
child 3947 49663e664250
6886108: Test case B4933582 binding to fixed port number Reviewed-by: chegar
jdk/test/java/net/Authenticator/B4933582.java
jdk/test/sun/net/www/httptest/HttpTransaction.java
jdk/test/sun/security/ssl/sun/net/www/httpstest/HttpTransaction.java
--- a/jdk/test/java/net/Authenticator/B4933582.java	Thu Sep 24 21:35:34 2009 +0800
+++ b/jdk/test/java/net/Authenticator/B4933582.java	Tue Sep 29 10:00:57 2009 +0100
@@ -125,9 +125,16 @@
         firstTime = args[0].equals ("first");
         MyAuthenticator auth = new MyAuthenticator ();
         Authenticator.setDefault (auth);
-        AuthCacheValue.setAuthCache (new CacheImpl());
+        CacheImpl cache;
         try {
-            server = new HttpServer (new B4933582(), 1, 10, 5009);
+            if (firstTime) {
+                server = new HttpServer (new B4933582(), 1, 10, 0);
+                cache = new CacheImpl (server.getLocalPort());
+            } else {
+                cache = new CacheImpl ();
+                server = new HttpServer(new B4933582(), 1, 10, cache.getPort());
+            }
+            AuthCacheValue.setAuthCache (cache);
             System.out.println ("Server: listening on port: " + server.getLocalPort());
             client ("http://localhost:"+server.getLocalPort()+"/d1/foo.html");
         } catch (Exception e) {
@@ -172,8 +179,15 @@
 
     static class CacheImpl extends AuthCacheImpl {
         HashMap map;
+        int port; // need to store the port number the server is using
+
         CacheImpl () throws IOException {
+            this (-1);
+        }
+
+        CacheImpl (int port) throws IOException {
             super();
+            this.port = port;
             File src = new File ("cache.ser");
             if (src.exists()) {
                 ObjectInputStream is = new ObjectInputStream (
@@ -181,6 +195,8 @@
                 );
                 try {
                     map = (HashMap)is.readObject ();
+                    this.port = (Integer)is.readObject ();
+                    System.out.println ("read port from file " + port);
                 } catch (ClassNotFoundException e) {
                     assert false;
                 }
@@ -192,6 +208,10 @@
             setMap (map);
         }
 
+        int getPort () {
+            return port;
+        }
+
         private void writeMap () {
             try {
                 File dst = new File ("cache.ser");
@@ -203,6 +223,8 @@
                         new FileOutputStream (dst)
                 );
                 os.writeObject(map);
+                os.writeObject(port);
+                System.out.println ("wrote port " + port);
                 os.close();
             } catch (IOException e) {}
         }
--- a/jdk/test/sun/net/www/httptest/HttpTransaction.java	Thu Sep 24 21:35:34 2009 +0800
+++ b/jdk/test/sun/net/www/httptest/HttpTransaction.java	Tue Sep 29 10:00:57 2009 +0100
@@ -102,7 +102,8 @@
         if (rspheaders != null) {
             buf.append (rspheaders.toString()).append("\r\n");
         }
-        buf.append ("Body: ").append (new String(rspbody)).append("\r\n");
+        String rbody = rspbody == null? "": new String (rspbody);
+        buf.append ("Body: ").append (rbody).append("\r\n");
         return new String (buf);
     }
 
--- a/jdk/test/sun/security/ssl/sun/net/www/httpstest/HttpTransaction.java	Thu Sep 24 21:35:34 2009 +0800
+++ b/jdk/test/sun/security/ssl/sun/net/www/httpstest/HttpTransaction.java	Tue Sep 29 10:00:57 2009 +0100
@@ -102,7 +102,8 @@
         if (rspheaders != null) {
             buf.append (rspheaders.toString()).append("\r\n");
         }
-        buf.append ("Body: ").append (new String(rspbody)).append("\r\n");
+        String rbody = rspbody == null? "": new String (rspbody);
+        buf.append ("Body: ").append (rbody).append("\r\n");
         return new String (buf);
     }