ResponseCacheTest.java revision f6c387128427e121477c1b32ad35cdcaa5101ba3
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
17package tests.api.java.net;
18
19import dalvik.annotation.BrokenTest;
20import dalvik.annotation.TestTargetClass;
21import dalvik.annotation.TestTargets;
22import dalvik.annotation.TestLevel;
23import dalvik.annotation.TestTargetNew;
24
25import java.io.File;
26import java.io.FileInputStream;
27import java.io.FileOutputStream;
28import java.io.IOException;
29import java.io.InputStream;
30import java.io.ObjectInputStream;
31import java.io.ObjectOutputStream;
32import java.io.OutputStream;
33import java.net.CacheRequest;
34import java.net.CacheResponse;
35import java.net.HttpURLConnection;
36import java.net.NetPermission;
37import java.net.ResponseCache;
38import java.net.URI;
39import java.net.URISyntaxException;
40import java.net.URL;
41import java.net.URLConnection;
42import java.security.Permission;
43import java.util.HashMap;
44import java.util.List;
45import java.util.Map;
46
47import junit.framework.TestCase;
48
49import tests.support.Support_Configuration;
50
51@TestTargetClass(
52    value = ResponseCache.class,
53    untestedMethods = {
54        @TestTargetNew(
55            level = TestLevel.NOT_FEASIBLE,
56            notes = "put method is not tested completely",
57            method = "put",
58            args = {java.net.URI.class, java.net.URLConnection.class}
59        )
60    }
61)
62public class ResponseCacheTest extends TestCase {
63
64
65
66    /**
67     * @tests java.net.ResponseCache#getDefault()
68     */
69    @TestTargetNew(
70        level = TestLevel.PARTIAL_COMPLETE,
71        notes = "This is a complete subset of tests for getDefault method.",
72        method = "getDefault",
73        args = {}
74    )
75    public void test_GetDefault() throws Exception {
76        assertNull(ResponseCache.getDefault());
77    }
78
79    /**
80     * @tests java.net.ResponseCache#setDefault(ResponseCache)
81     */
82    @TestTargets({
83        @TestTargetNew(
84            level = TestLevel.COMPLETE,
85            notes = "This is a complete subset of tests for setDefault method.",
86            method = "setDefault",
87            args = {java.net.ResponseCache.class}
88        ),
89        @TestTargetNew(
90            level = TestLevel.COMPLETE,
91            notes = "This is a complete subset of tests for setDefault method.",
92            method = "ResponseCache",
93            args = {}
94        )
95    })
96    public void test_SetDefaultLjava_net_ResponseCache_Normal()
97            throws Exception {
98        ResponseCache rc1 = new MockResponseCache();
99        ResponseCache rc2 = new MockResponseCache();
100        ResponseCache.setDefault(rc1);
101        assertSame(ResponseCache.getDefault(), rc1);
102        ResponseCache.setDefault(rc2);
103        assertSame(ResponseCache.getDefault(), rc2);
104        ResponseCache.setDefault(null);
105        assertNull(ResponseCache.getDefault());
106    }
107
108    /**
109     * @tests java.net.ResponseCache#getDefault()
110     */
111    @TestTargetNew(
112        level = TestLevel.PARTIAL_COMPLETE,
113        notes = "This is a complete subset of tests for getDefault method.",
114        method = "getDefault",
115        args = {}
116    )
117    public void test_GetDefault_Security() {
118        SecurityManager old = System.getSecurityManager();
119        try {
120            System.setSecurityManager(new MockSM());
121        } catch (SecurityException e) {
122            System.err.println("No setSecurityManager permission.");
123            System.err.println("test_setDefaultLjava_net_ResponseCache_NoPermission is not tested");
124            return;
125        }
126        try {
127            ResponseCache.getDefault();
128            fail("should throw SecurityException");
129        } catch (SecurityException e) {
130            // expected
131        } finally {
132            System.setSecurityManager(old);
133        }
134    }
135
136    /**
137     * @tests java.net.ResponseCache#setDefault(ResponseCache)
138     */
139    @TestTargetNew(
140        level = TestLevel.ADDITIONAL,
141        notes = "This is a complete subset of tests for setDefault method.",
142        method = "setDefault",
143        args = {java.net.ResponseCache.class}
144    )
145    public void test_setDefaultLjava_net_ResponseCache_NoPermission() {
146        ResponseCache rc = new MockResponseCache();
147        SecurityManager old = System.getSecurityManager();
148        try {
149            System.setSecurityManager(new MockSM());
150        } catch (SecurityException e) {
151            System.err.println("No setSecurityManager permission.");
152            System.err.println("test_setDefaultLjava_net_ResponseCache_NoPermission is not tested");
153            return;
154        }
155        try {
156            ResponseCache.setDefault(rc);
157            fail("should throw SecurityException");
158        } catch (SecurityException e) {
159            // expected
160        } finally {
161            System.setSecurityManager(old);
162        }
163    }
164
165    @TestTargetNew(
166        level = TestLevel.COMPLETE,
167        notes = "",
168        method = "get",
169        args = {java.net.URI.class, java.lang.String.class, java.util.Map.class}
170    )
171    @BrokenTest("cache seems not to be used")
172    public void test_get_put() throws Exception {
173
174        URL url  = new URL("http://" +
175                Support_Configuration.SpecialInetTestAddress);
176        ResponseCache.setDefault(new TestResponseCache());
177        HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
178        httpCon.setUseCaches(true);
179        httpCon.connect();
180        try {
181            Thread.sleep(5000);
182        } catch(Exception e) {}
183
184        InputStream is = httpCon.getInputStream();
185        byte [] array = new byte [10];
186        is.read(array);
187        assertEquals("Cache test", new String(array));
188
189        try {
190            Thread.sleep(5000);
191        } catch(Exception e) {}
192        is.close();
193        httpCon.disconnect();
194    }
195
196    /*
197     * MockResponseCache for testSetDefault(ResponseCache)
198     */
199    class MockResponseCache extends ResponseCache {
200
201        public CacheResponse get(URI arg0, String arg1, Map arg2)
202                throws IOException {
203            return null;
204        }
205
206        public CacheRequest put(URI arg0, URLConnection arg1)
207                throws IOException {
208            return null;
209        }
210    }
211
212    /*
213     * MockSecurityMaanger. It denies NetPermission("getResponseCache") and
214     * NetPermission("setResponseCache").
215     */
216    class MockSM extends SecurityManager {
217        public void checkPermission(Permission permission) {
218            if (permission instanceof NetPermission) {
219                if ("setResponseCache".equals(permission.getName())) {
220                    throw new SecurityException();
221                }
222            }
223
224            if (permission instanceof NetPermission) {
225                if ("getResponseCache".equals(permission.getName())) {
226
227                    throw new SecurityException();
228                }
229            }
230
231            if (permission instanceof RuntimePermission) {
232                if ("setSecurityManager".equals(permission.getName())) {
233                    return;
234                }
235            }
236        }
237    }
238
239    class TestCacheResponse extends CacheResponse {
240        InputStream is = null;
241        Map<String, List<String>> headers = null;
242
243        public TestCacheResponse(String filename) {
244            String path = getClass().getPackage().getName().replace(".", "/");
245            is = getClass().getResourceAsStream("/" + path + "/" + filename);
246        }
247
248        public InputStream getBody() throws IOException {
249           return is;
250        }
251
252         public Map getHeaders() throws IOException {
253           return null;
254         }
255    }
256
257    class TestCacheRequest extends CacheRequest {
258
259        public TestCacheRequest(String filename,
260                            Map<String, List<String>> rspHeaders) {
261        }
262        public OutputStream getBody() throws IOException {
263            return null;
264        }
265
266        public void abort() {
267        }
268    }
269
270    class TestResponseCache extends ResponseCache {
271
272        URI uri1 = null;
273
274        public CacheResponse get(URI uri, String rqstMethod, Map rqstHeaders)
275                throws IOException {
276          try {
277            uri1  = new URI("http://" +
278                    Support_Configuration.SpecialInetTestAddress);
279          } catch (URISyntaxException e) {
280          }
281          if (uri.equals(uri1)) {
282            return new TestCacheResponse("file1.cache");
283          }
284          return null;
285        }
286
287       public CacheRequest put(URI uri, URLConnection conn)
288              throws IOException {
289           try {
290               uri1  = new URI("http://www.google.com");
291             } catch (URISyntaxException e) {
292             }
293          if (uri.equals(uri1)) {
294              return new TestCacheRequest("file2.cache",
295                          conn.getHeaderFields());
296          }
297          return null;
298        }
299    }
300}