jdk/test/java/net/ResponseCache/ResponseCacheTest.java
author duke
Wed, 05 Jul 2017 16:45:59 +0200
changeset 1810 54dffad0bf06
parent 2 90ce3da70b43
child 5165 5693cc1e95c6
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2004 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @summary Unit test for java.net.ResponseCache
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4837267
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @author Yingxian Wang
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.net.www.ParseUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Request should get serviced by the cache handler. Response get
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * saved through the cache handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class ResponseCacheTest implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    ServerSocket ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static URL url1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static URL url2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static String FNPrefix, OutFNPrefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * Our "http" server to return a 404 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            Socket s = ss.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            InputStream is = s.getInputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            BufferedReader r = new BufferedReader(new InputStreamReader(is));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            String x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            while ((x=r.readLine()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                if (x.length() ==0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            PrintStream out = new PrintStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                                 new BufferedOutputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                                    s.getOutputStream() ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            /* send file2.1 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            File file2 = new File(FNPrefix+"file2.1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            out.print("HTTP/1.1 200 OK\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            out.print("Content-Type: text/html; charset=iso-8859-1\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            out.print("Content-Length: "+file2.length()+"\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            out.print("Connection: close\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            out.print("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            FileInputStream fis = new FileInputStream(file2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            byte[] buf = new byte[(int)file2.length()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            while ((len = fis.read(buf)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                out.print(new String(buf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
static class NameVerifier implements HostnameVerifier {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        public boolean verify(String hostname, SSLSession session) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    ResponseCacheTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        /* start the server */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        ss = new ServerSocket(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        (new Thread(this)).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        /* establish http connection to server */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        url1 = new URL("http://localhost/file1.cache");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        HttpURLConnection http = (HttpURLConnection)url1.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        InputStream is = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        System.out.println("request headers: "+http.getRequestProperties());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        System.out.println("responsecode is :"+http.getResponseCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        Map<String,List<String>> headers1 = http.getHeaderFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            is = http.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        } catch (IOException ioex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            throw new RuntimeException(ioex.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        BufferedReader r = new BufferedReader(new InputStreamReader(is));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        String x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        File fileout = new File(OutFNPrefix+"file1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        PrintStream out = new PrintStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                                 new BufferedOutputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                                    new FileOutputStream(fileout)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        while ((x=r.readLine()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            out.print(x+"\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        http.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        // testing ResponseCacheHandler.put()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        url2 = new URL("http://localhost:" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                       Integer.toString(ss.getLocalPort())+"/file2.1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        http = (HttpURLConnection)url2.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        System.out.println("responsecode2 is :"+http.getResponseCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        Map<String,List<String>> headers2 = http.getHeaderFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            is = http.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        } catch (IOException ioex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            throw new RuntimeException(ioex.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        r = new BufferedReader(new InputStreamReader(is));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        fileout = new File(OutFNPrefix+"file2.2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        out = new PrintStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                                 new BufferedOutputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                                    new FileOutputStream(fileout)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        while ((x=r.readLine()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            out.print(x+"\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        // assert (headers1 == headers2 && file1 == file2.2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        File file1 = new File(OutFNPrefix+"file1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        File file2 = new File(OutFNPrefix+"file2.2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        System.out.println("headers1"+headers1+"\nheaders2="+headers2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (!headers1.equals(headers2) || file1.length() != file2.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            throw new RuntimeException("test failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        ResponseCache.setDefault(new MyResponseCache());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        FNPrefix = System.getProperty("test.src", ".")+"/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        OutFNPrefix = System.getProperty("test.scratch", ".")+"/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        new ResponseCacheTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    static class MyResponseCache extends ResponseCache {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        public CacheResponse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        get(URI uri, String rqstMethod, Map<String,List<String>> rqstHeaders)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            if (uri.equals(ParseUtil.toURI(url1))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                return new MyCacheResponse(FNPrefix+"file1.cache");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        public CacheRequest put(URI uri, URLConnection conn)  throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            // save cache to file2.cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            // 1. serialize headers into file2.cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            // 2. write data to file2.cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            return new MyCacheRequest(OutFNPrefix+"file2.cache", conn.getHeaderFields());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    static class MyCacheResponse extends CacheResponse {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        FileInputStream fis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        Map<String,List<String>> headers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        public MyCacheResponse(String filename) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                fis = new FileInputStream(new File(filename));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                ObjectInputStream ois = new ObjectInputStream(fis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                headers = (Map<String,List<String>>)ois.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                // throw new RuntimeException(ex.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        public InputStream getBody() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            return fis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        public Map<String,List<String>> getHeaders() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            return headers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    static class MyCacheRequest extends CacheRequest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        FileOutputStream fos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        public MyCacheRequest(String filename, Map<String,List<String>> rspHeaders) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                File file = new File(filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                fos = new FileOutputStream(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                ObjectOutputStream oos = new ObjectOutputStream(fos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                oos.writeObject(rspHeaders);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                throw new RuntimeException(ex.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        public OutputStream getBody() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            return fos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        public void abort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            // no op
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
}