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