jdk/test/java/net/ResponseCache/file2.1
author amurillo
Wed, 15 Aug 2012 16:49:38 -0700
changeset 13465 d3fc5d192448
parent 80 5ea387b814e6
permissions -rw-r--r--
7191765: make jdk8 the default jprt release for hs24 Reviewed-by: jcoomes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
80
5ea387b814e6 6623830: SCCS cleanup has broken two regression tests.
wetmore
parents: 2
diff changeset
     1
/* @test @(#)file2.1	1.1 03/08/09
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * @summary Unit test for java.net.ResponseCacheHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * @bug 4837267
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * @author Yingxian Wang
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
import java.nio.channels.ReadableByteChannel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
import sun.net.www.ParseUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * Request should get serviced by the cache handler. Response get
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * saved through the cache handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
public class ResponseCacheHandlerTest implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
    ServerSocket ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
    static URL url1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
    static URL url2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
    static boolean flag = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
    /* 
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
     * Our "http" server to return a 404 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
	try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
	    Socket s = ss.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
	    // check request contains "Cookie"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
	    InputStream is = s.getInputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
	    BufferedReader r = new BufferedReader(new InputStreamReader(is));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
	    boolean flag = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
	    String x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
	    while ((x=r.readLine()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
		if (x.length() ==0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
		    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
		}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
	    PrintStream out = new PrintStream( 
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
				 new BufferedOutputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
				    s.getOutputStream() ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
	    
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
	    /* send file2.1 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
	    File file2 = new File("file2.1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
	    out.print("HTTP/1.1 200 OK\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
	    out.print("Content-Type: text/html; charset=iso-8859-1\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
	    out.print("Content-Length: "+file2.length()+"\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
	    out.print("Connection: close\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
	    out.print("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
	    FileInputStream fis = new FileInputStream(file2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
	    byte[] buf = new byte[(int)file2.length()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
	    int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
	    while ((len = fis.read(buf)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
		out.print(new String(buf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
	    out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
	    s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
	    ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
	} catch (Exception e) { 
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
	    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    ResponseCacheHandlerTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
	/* start the server */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
	ss = new ServerSocket(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
	(new Thread(this)).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
	/* establish http connection to server */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
	url1 = new URL("http://localhost/file1.cache");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
	HttpURLConnection http = (HttpURLConnection)url1.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
	InputStream is = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
	System.out.println("headers1:"+http.getHeaderFields());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
	try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
	    is = http.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
	} catch (IOException ioex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
	    throw new RuntimeException(ioex.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
	BufferedReader r = new BufferedReader(new InputStreamReader(is));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
	String x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
	File fileout = new File("file1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
	PrintStream out = new PrintStream( 
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
				 new BufferedOutputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
				    new FileOutputStream(fileout)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
	while ((x=r.readLine()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
	    out.print(x+"\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
	out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
	out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
	// if !(file1.out == file1.cache)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
	// throw new RuntimeException("testing ResponseCacheHandler.get() failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
	http.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
	// testing ResponseCacheHandler.put()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
	url2 = new URL("http://localhost:" + 
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
		       Integer.toString(ss.getLocalPort())+"/file2.1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
	http = (HttpURLConnection)url2.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
	System.out.println("headers2:"+http.getHeaderFields());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
	try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
	    is = http.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
	} catch (IOException ioex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
	    throw new RuntimeException(ioex.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
	DataInputStream dis = new DataInputStream(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
	fileout = new File("file2.2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
	byte[] buf = new byte[(int)new File("file2.1").length()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
	DataOutputStream dos =  new DataOutputStream(new FileOutputStream(fileout));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
	int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
	while ((len=dis.read(buf)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
	    dos.write(buf, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
	dos.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
	dos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
	// if !(file2.1 == file2.2 == file2.cache)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
	// throw new RuntimeException("testing ResponseCacheHandler.put() failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
	ResponseCacheHandler.setDefault(new MyResponseCacheHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
	new ResponseCacheHandlerTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    static class MyResponseCacheHandler extends ResponseCacheHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
	public Response 
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
	get(URI uri, Map requestHeaders)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
	    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
	    if (uri.equals(ParseUtil.toURI(url1))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
		return new MyResponse("file1.cache");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
	    } if (uri.equals(ParseUtil.toURI(url2)) && !flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
		flag = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
		return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
	    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
		return new MyResponse("file2.cache");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
	public boolean put(URI uri, Response response)  throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
	    // save cache to file2.cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
	    // 1. serialize headers into file2.cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
	    // 2. write data to file2.cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
	    File file = new File("file2.cache");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
	    FileOutputStream fos = new FileOutputStream(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
	    ObjectOutputStream oos = new ObjectOutputStream(fos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
	    oos.writeObject(response.getHeaders());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
	    ReadableByteChannel rbc = response.getBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
	    int len = (int)new File("file2.1").length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
	    ByteBuffer buf = ByteBuffer.allocate(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
	    byte[] b = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
	    while ((len = rbc.read(buf)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
		buf.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
		buf.get(b, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
		buf.compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
		oos.write(b, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
	    oos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
	    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    static class MyResponse extends Response {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
	FileInputStream fis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
	Map headers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
	public MyResponse(String filename) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
	    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
		fis = new FileInputStream(new File(filename));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
		headers = (Map)new ObjectInputStream(fis).readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
	    } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
		throw new RuntimeException(ex.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
	public ReadableByteChannel getBody() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
	    return fis.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
	public Map getHeaders() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
	    return headers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
}