OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / test / java / libcore / java / net / OldResponseCacheTest.java
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package libcore.java.net;
18
19 import dalvik.annotation.TestLevel;
20 import dalvik.annotation.TestTargetClass;
21 import dalvik.annotation.TestTargetNew;
22 import dalvik.annotation.TestTargets;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.net.CacheRequest;
27 import java.net.CacheResponse;
28 import java.net.HttpURLConnection;
29 import java.net.NetPermission;
30 import java.net.ResponseCache;
31 import java.net.URI;
32 import java.net.URISyntaxException;
33 import java.net.URL;
34 import java.net.URLConnection;
35 import java.security.Permission;
36 import java.util.Arrays;
37 import java.util.Map;
38 import junit.framework.TestCase;
39 import tests.support.Support_PortManager;
40 import tests.support.Support_TestWebData;
41 import tests.support.Support_TestWebServer;
42
43 @TestTargetClass(value = ResponseCache.class)
44 public class OldResponseCacheTest extends TestCase {
45
46
47
48     /**
49      * @tests java.net.ResponseCache#getDefault()
50      */
51     @TestTargetNew(
52         level = TestLevel.PARTIAL_COMPLETE,
53         notes = "This is a complete subset of tests for getDefault method.",
54         method = "getDefault",
55         args = {}
56     )
57     public void test_GetDefault() throws Exception {
58         assertNull(ResponseCache.getDefault());
59     }
60
61     /**
62      * @tests java.net.ResponseCache#setDefault(ResponseCache)
63      */
64     @TestTargets({
65         @TestTargetNew(
66             level = TestLevel.COMPLETE,
67             notes = "This is a complete subset of tests for setDefault method.",
68             method = "setDefault",
69             args = {java.net.ResponseCache.class}
70         ),
71         @TestTargetNew(
72             level = TestLevel.COMPLETE,
73             notes = "This is a complete subset of tests for setDefault method.",
74             method = "ResponseCache",
75             args = {}
76         )
77     })
78     public void test_SetDefaultLjava_net_ResponseCache_Normal()
79             throws Exception {
80         ResponseCache rc1 = new MockResponseCache();
81         ResponseCache rc2 = new MockResponseCache();
82         ResponseCache.setDefault(rc1);
83         assertSame(ResponseCache.getDefault(), rc1);
84         ResponseCache.setDefault(rc2);
85         assertSame(ResponseCache.getDefault(), rc2);
86         ResponseCache.setDefault(null);
87         assertNull(ResponseCache.getDefault());
88     }
89
90     @TestTargetNew(
91         level = TestLevel.COMPLETE,
92         notes = "",
93         method = "get",
94         args = {URI.class, String.class, Map.class}
95     )
96     public void test_get() throws Exception {
97         String uri = "http://localhost/";
98         URL url  = new URL(uri);
99         TestResponseCache cache = new TestResponseCache(uri, true);
100         ResponseCache.setDefault(cache);
101         HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
102         httpCon.setUseCaches(true);
103         httpCon.connect();
104         try {
105             Thread.sleep(5000);
106         } catch(Exception e) {}
107
108         InputStream is = httpCon.getInputStream();
109         byte[] array = new byte [10];
110         is.read(array);
111         assertEquals(url.toURI(), cache.getWasCalled);
112         assertEquals("Cache test", new String(array));
113         is.close();
114         httpCon.disconnect();
115
116     }
117
118     @TestTargetNew(
119         level = TestLevel.COMPLETE,
120         notes = "",
121         method = "put",
122         args = {URI.class, URLConnection.class}
123     )
124     public void test_put() throws Exception {
125         // Create test ResponseCache
126         TestResponseCache cache = new TestResponseCache(
127                 "http://localhost/not_cached", false);
128         ResponseCache.setDefault(cache);
129
130         // Start Server
131         int port = Support_PortManager.getNextPort();
132         Support_TestWebServer s = new Support_TestWebServer();
133         try {
134             s.initServer(port, 10000, false);
135             Thread.currentThread().sleep(2500);
136
137             // Create connection to server
138             URL url  = new URL("http://localhost:" + port + "/test1");
139             HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
140             httpCon.setUseCaches(true);
141             httpCon.connect();
142             Thread.currentThread().sleep(2500);
143
144             // Check that a call to the cache was made.
145             assertEquals(url.toURI(), cache.getWasCalled);
146             // Make the HttpConnection get the content. It should try to
147             // put it into the cache.
148             httpCon.getContent();
149             // Check if put was called
150             assertEquals(url.toURI(), cache.putWasCalled);
151
152             // get the
153             InputStream is = httpCon.getInputStream();
154
155             byte[] array = new byte[Support_TestWebData.test1.length];
156             is.read(array);
157             assertTrue(Arrays.equals(Support_TestWebData.tests[0], array));
158             is.close();
159             httpCon.disconnect();
160         } finally {
161             s.close();
162         }
163     }
164
165     /*
166      * MockResponseCache for testSetDefault(ResponseCache)
167      */
168     class MockResponseCache extends ResponseCache {
169
170         public CacheResponse get(URI arg0, String arg1, Map arg2)
171                 throws IOException {
172             return null;
173         }
174
175         public CacheRequest put(URI arg0, URLConnection arg1)
176                 throws IOException {
177             return null;
178         }
179     }
180
181     /*
182      * MockSecurityMaanger. It denies NetPermission("getResponseCache") and
183      * NetPermission("setResponseCache").
184      */
185     class MockSM extends SecurityManager {
186         public void checkPermission(Permission permission) {
187             if (permission instanceof NetPermission) {
188                 if ("setResponseCache".equals(permission.getName())) {
189                     throw new SecurityException();
190                 }
191             }
192
193             if (permission instanceof NetPermission) {
194                 if ("getResponseCache".equals(permission.getName())) {
195
196                     throw new SecurityException();
197                 }
198             }
199
200             if (permission instanceof RuntimePermission) {
201                 if ("setSecurityManager".equals(permission.getName())) {
202                     return;
203                 }
204             }
205         }
206     }
207
208     class TestCacheResponse extends CacheResponse {
209         InputStream is = null;
210
211         public TestCacheResponse(String filename) {
212             String path = getClass().getPackage().getName().replace(".", "/");
213             is = getClass().getResourceAsStream("/" + path + "/" + filename);
214         }
215
216         @Override
217         public InputStream getBody() {
218            return is;
219         }
220
221         @Override
222          public Map getHeaders() {
223            return null;
224          }
225     }
226
227     class TestCacheRequest extends CacheRequest {
228
229         @Override
230         public OutputStream getBody() {
231             return null;
232         }
233
234         @Override
235         public void abort() {
236         }
237     }
238
239     class TestResponseCache extends ResponseCache {
240
241         URI uri1 = null;
242         boolean testGet = false;
243
244         public URI getWasCalled = null;
245         public URI putWasCalled = null;
246
247         TestResponseCache(String uri, boolean testGet) {
248             try {
249                 uri1  = new URI(uri);
250             } catch (URISyntaxException e) {
251             }
252             this.testGet = testGet;
253         }
254
255         @Override
256         public CacheResponse get(URI uri, String rqstMethod, Map rqstHeaders) {
257             getWasCalled = uri;
258             if (testGet && uri.equals(uri1)) {
259                 return new TestCacheResponse("file1.cache");
260             }
261             return null;
262         }
263
264         @Override
265         public CacheRequest put(URI uri, URLConnection conn) {
266             putWasCalled = uri;
267             if (!testGet && uri.equals(uri1)) {
268                 return new TestCacheRequest();
269             }
270             return null;
271         }
272     }
273 }