test/jdk/java/net/URLConnection/SetIfModifiedSince.java
changeset 55645 3203e857fa71
parent 47216 71c04702a3d5
child 57760 ec948d19180a
equal deleted inserted replaced
55644:556313991cac 55645:3203e857fa71
     1 /*
     1 /*
     2  * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /* @test
    24 /* @test
    25  * @bug 4397096
    25  * @bug 4397096
       
    26  * @library /test/lib
    26  * @run main/othervm SetIfModifiedSince
    27  * @run main/othervm SetIfModifiedSince
    27  * @summary setIfModifiedSince() of HttpURLConnection sets invalid date of default locale
    28  * @summary setIfModifiedSince() of HttpURLConnection sets invalid date of default locale
    28  */
    29  */
    29 
    30 
    30 import java.net.*;
    31 import java.net.*;
    31 import java.io.*;
    32 import java.io.*;
    32 import java.util.*;
    33 import java.util.*;
    33 
    34 
       
    35 import jdk.test.lib.net.URIBuilder;
       
    36 
    34 public class SetIfModifiedSince {
    37 public class SetIfModifiedSince {
       
    38     static volatile boolean successfulHeaderCheck = false;
    35 
    39 
    36     static class XServer extends Thread {
    40     static class XServer extends Thread {
    37         ServerSocket srv;
    41         ServerSocket srv;
    38         Socket s;
    42         Socket s;
    39         InputStream is;
    43         InputStream is;
    64                                     ("Invalid HTTP date specification");
    68                                     ("Invalid HTTP date specification");
    65                         }
    69                         }
    66                         break;
    70                         break;
    67                     }
    71                     }
    68                 }
    72                 }
       
    73                 successfulHeaderCheck = true;
    69                 s.close ();
    74                 s.close ();
    70                 srv.close (); // or else the HTTPURLConnection will retry
    75                 srv.close (); // or else the HTTPURLConnection will retry
    71             } catch (IOException e) {}
    76             } catch (IOException e) {}
    72         }
    77         }
    73     }
    78     }
    74 
    79 
    75     public static void main (String[] args) {
    80     public static void main(String[] args) throws Exception {
    76         Locale reservedLocale = Locale.getDefault();
    81         Locale reservedLocale = Locale.getDefault();
    77         try {
    82         try {
    78             Locale.setDefault(Locale.JAPAN);
    83             Locale.setDefault(Locale.JAPAN);
    79             ServerSocket serversocket = new ServerSocket (0);
    84             ServerSocket serversocket = new ServerSocket();
       
    85             serversocket.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
    80             int port = serversocket.getLocalPort ();
    86             int port = serversocket.getLocalPort ();
    81             XServer server = new XServer (serversocket);
    87             XServer server = new XServer (serversocket);
    82             server.start ();
    88             server.start ();
    83             Thread.sleep (2000);
    89             Thread.sleep (2000);
    84             URL url = new URL ("http://localhost:"+port+"/index.html");
    90             URL url = URIBuilder.newBuilder()
    85             URLConnection urlc = url.openConnection ();
    91                     .scheme("http")
       
    92                     .loopback()
       
    93                     .port(port)
       
    94                     .path("/index.html")
       
    95                     .toURLUnchecked();
       
    96             URLConnection urlc = url.openConnection(Proxy.NO_PROXY);
    86             urlc.setIfModifiedSince (10000000);
    97             urlc.setIfModifiedSince (10000000);
    87             InputStream is = urlc.getInputStream ();
    98             InputStream is = urlc.getInputStream ();
    88             int i=0, c;
    99             int i = 0, c;
    89             Thread.sleep (5000);
   100             Thread.sleep(5000);
    90         } catch (Exception e) {
   101             if (!successfulHeaderCheck) {
       
   102                 throw new RuntimeException("Header check was unsuccessful");
       
   103             }
       
   104         } catch (SocketException ce) {
       
   105             if (!successfulHeaderCheck) {
       
   106                 throw ce;
       
   107             }
       
   108             System.out.println("ConnectionException expected on successful check of If-modified-since header");
    91         } finally {
   109         } finally {
    92             // restore the reserved locale
   110             // restore the reserved locale
    93             Locale.setDefault(reservedLocale);
   111             Locale.setDefault(reservedLocale);
    94         }
   112         }
    95 
   113