test/jdk/sun/net/www/http/HttpClient/IsAvailable.java
changeset 57760 ec948d19180a
parent 47216 71c04702a3d5
equal deleted inserted replaced
57759:22fa46d5dc2e 57760:ec948d19180a
     1 /*
     1 /*
     2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2013, 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.
    26  * @bug 8009650
    26  * @bug 8009650
    27  * @summary HttpClient available() check throws SocketException when connection
    27  * @summary HttpClient available() check throws SocketException when connection
    28  * has been closed
    28  * has been closed
    29  * @modules java.base/sun.net
    29  * @modules java.base/sun.net
    30  *          java.base/sun.net.www.http:+open
    30  *          java.base/sun.net.www.http:+open
       
    31  * @library /test/lib
    31  */
    32  */
    32 
    33 
       
    34 import java.net.InetAddress;
       
    35 import java.net.InetSocketAddress;
    33 import java.net.URL;
    36 import java.net.URL;
    34 import java.net.ServerSocket;
    37 import java.net.ServerSocket;
    35 import sun.net.www.http.HttpClient;
    38 import sun.net.www.http.HttpClient;
    36 import java.security.*;
    39 import java.security.*;
    37 import java.lang.reflect.Method;
    40 import java.lang.reflect.Method;
       
    41 import jdk.test.lib.net.URIBuilder;
    38 
    42 
    39 public class IsAvailable {
    43 public class IsAvailable {
    40 
    44 
    41     public static void main(String[] args) throws Exception {
    45     public static void main(String[] args) throws Exception {
    42         int readTimeout = 20;
    46         int readTimeout = 20;
    43         ServerSocket ss = new ServerSocket(0);
    47         ServerSocket ss = new ServerSocket();
       
    48         InetAddress loopback = InetAddress.getLoopbackAddress();
       
    49         ss.bind(new InetSocketAddress(loopback, 0));
    44 
    50 
    45         URL url1 = new URL("http://localhost:" + ss.getLocalPort());
    51         try (ServerSocket toclose = ss) {
    46         HttpClient c1 = HttpClient.New(url1);
       
    47 
    52 
    48         Method available = HttpClient.class.
    53             URL url1 = URIBuilder.newBuilder()
    49                 getDeclaredMethod("available", null);
    54                 .scheme("http")
    50         available.setAccessible(true);
    55                 .loopback()
       
    56                 .port(ss.getLocalPort())
       
    57                 .toURL();
    51 
    58 
    52         c1.setReadTimeout(readTimeout);
    59             HttpClient c1 = HttpClient.New(url1);
    53         boolean a = (boolean) available.invoke(c1);
    60 
    54         if (!a) {
    61             Method available = HttpClient.class.
    55             throw new RuntimeException("connection should be available");
    62                     getDeclaredMethod("available", null);
       
    63             available.setAccessible(true);
       
    64 
       
    65             c1.setReadTimeout(readTimeout);
       
    66             boolean a = (boolean) available.invoke(c1);
       
    67             if (!a) {
       
    68                 throw new RuntimeException("connection should be available");
       
    69             }
       
    70             if (c1.getReadTimeout() != readTimeout) {
       
    71                 throw new RuntimeException("read timeout has been altered");
       
    72             }
       
    73 
       
    74             c1.closeServer();
       
    75 
       
    76             a = (boolean) available.invoke(c1);
       
    77             if (a) {
       
    78                 throw new RuntimeException("connection shouldn't be available");
       
    79             }
    56         }
    80         }
    57         if (c1.getReadTimeout() != readTimeout) {
       
    58             throw new RuntimeException("read timeout has been altered");
       
    59         }
       
    60 
       
    61         c1.closeServer();
       
    62 
       
    63         a = (boolean) available.invoke(c1);
       
    64         if (a) {
       
    65             throw new RuntimeException("connection shouldn't be available");
       
    66         }
       
    67 
       
    68         ss.close();
       
    69     }
    81     }
    70 }
    82 }