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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package tests.api.java.net;
19 import dalvik.annotation.KnownFailure;
20 import dalvik.annotation.TestTargetClass;
21 import dalvik.annotation.TestTargets;
22 import dalvik.annotation.TestLevel;
23 import dalvik.annotation.TestTargetNew;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.OutputStream;
30 import java.net.CacheRequest;
31 import java.net.CacheResponse;
32 import java.net.HttpURLConnection;
33 import java.net.NetPermission;
34 import java.net.ResponseCache;
36 import java.net.URISyntaxException;
38 import java.net.URLConnection;
39 import java.security.Permission;
40 import java.util.Arrays;
41 import java.util.List;
44 import junit.framework.TestCase;
46 import tests.support.Support_PortManager;
47 import tests.support.Support_TestWebData;
48 import tests.support.Support_TestWebServer;
50 @TestTargetClass(value = ResponseCache.class)
51 public class ResponseCacheTest extends TestCase {
56 * @tests java.net.ResponseCache#getDefault()
59 level = TestLevel.PARTIAL_COMPLETE,
60 notes = "This is a complete subset of tests for getDefault method.",
61 method = "getDefault",
64 public void test_GetDefault() throws Exception {
65 assertNull(ResponseCache.getDefault());
69 * @tests java.net.ResponseCache#setDefault(ResponseCache)
73 level = TestLevel.COMPLETE,
74 notes = "This is a complete subset of tests for setDefault method.",
75 method = "setDefault",
76 args = {java.net.ResponseCache.class}
79 level = TestLevel.COMPLETE,
80 notes = "This is a complete subset of tests for setDefault method.",
81 method = "ResponseCache",
85 public void test_SetDefaultLjava_net_ResponseCache_Normal()
87 ResponseCache rc1 = new MockResponseCache();
88 ResponseCache rc2 = new MockResponseCache();
89 ResponseCache.setDefault(rc1);
90 assertSame(ResponseCache.getDefault(), rc1);
91 ResponseCache.setDefault(rc2);
92 assertSame(ResponseCache.getDefault(), rc2);
93 ResponseCache.setDefault(null);
94 assertNull(ResponseCache.getDefault());
98 * @tests java.net.ResponseCache#getDefault()
101 level = TestLevel.PARTIAL_COMPLETE,
102 notes = "This is a complete subset of tests for getDefault method.",
103 method = "getDefault",
106 public void test_GetDefault_Security() {
107 SecurityManager old = System.getSecurityManager();
109 System.setSecurityManager(new MockSM());
110 } catch (SecurityException e) {
111 System.err.println("No setSecurityManager permission.");
112 System.err.println("test_setDefaultLjava_net_ResponseCache_NoPermission is not tested");
116 ResponseCache.getDefault();
117 fail("should throw SecurityException");
118 } catch (SecurityException e) {
121 System.setSecurityManager(old);
126 * @tests java.net.ResponseCache#setDefault(ResponseCache)
129 level = TestLevel.ADDITIONAL,
130 notes = "This is a complete subset of tests for setDefault method.",
131 method = "setDefault",
132 args = {java.net.ResponseCache.class}
134 public void test_setDefaultLjava_net_ResponseCache_NoPermission() {
135 ResponseCache rc = new MockResponseCache();
136 SecurityManager old = System.getSecurityManager();
138 System.setSecurityManager(new MockSM());
139 } catch (SecurityException e) {
140 System.err.println("No setSecurityManager permission.");
141 System.err.println("test_setDefaultLjava_net_ResponseCache_NoPermission is not tested");
145 ResponseCache.setDefault(rc);
146 fail("should throw SecurityException");
147 } catch (SecurityException e) {
150 System.setSecurityManager(old);
155 level = TestLevel.COMPLETE,
158 args = {URI.class, String.class, Map.class}
160 public void test_get() throws Exception {
161 String uri = "http://localhost/";
162 URL url = new URL(uri);
163 TestResponseCache cache = new TestResponseCache(uri, true);
164 ResponseCache.setDefault(cache);
165 HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
166 httpCon.setUseCaches(true);
170 } catch(Exception e) {}
172 InputStream is = httpCon.getInputStream();
173 byte[] array = new byte [10];
175 assertEquals(url.toURI(), cache.getWasCalled);
176 assertEquals("Cache test", new String(array));
178 httpCon.disconnect();
183 level = TestLevel.COMPLETE,
186 args = {URI.class, URLConnection.class}
188 @KnownFailure("the call to put is made with a wrong uri."
189 + " The RI calls with http://localhost:<port>/test1,"
190 + " but android only calls with http://localhost:<port>")
191 public void test_put() throws Exception {
192 // Create test ResponseCache
193 TestResponseCache cache = new TestResponseCache(
194 "http://localhost/not_cached", false);
195 ResponseCache.setDefault(cache);
198 int port = Support_PortManager.getNextPort();
199 Support_TestWebServer s = new Support_TestWebServer();
201 s.initServer(port, 10000, false);
202 Thread.currentThread().sleep(2500);
204 // Create connection to server
205 URL url = new URL("http://localhost:" + port + "/test1");
206 HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
207 httpCon.setUseCaches(true);
209 Thread.currentThread().sleep(2500);
211 // Check that a call to the cache was made.
212 assertEquals(url.toURI(), cache.getWasCalled);
213 // Make the HttpConnection get the content. It should try to
214 // put it into the cache.
215 httpCon.getContent();
216 // Check if put was called
217 assertEquals(url.toURI(), cache.putWasCalled);
220 InputStream is = httpCon.getInputStream();
222 byte[] array = new byte[Support_TestWebData.test1.length];
224 assertTrue(Arrays.equals(Support_TestWebData.tests[0], array));
226 httpCon.disconnect();
233 * MockResponseCache for testSetDefault(ResponseCache)
235 class MockResponseCache extends ResponseCache {
237 public CacheResponse get(URI arg0, String arg1, Map arg2)
242 public CacheRequest put(URI arg0, URLConnection arg1)
249 * MockSecurityMaanger. It denies NetPermission("getResponseCache") and
250 * NetPermission("setResponseCache").
252 class MockSM extends SecurityManager {
253 public void checkPermission(Permission permission) {
254 if (permission instanceof NetPermission) {
255 if ("setResponseCache".equals(permission.getName())) {
256 throw new SecurityException();
260 if (permission instanceof NetPermission) {
261 if ("getResponseCache".equals(permission.getName())) {
263 throw new SecurityException();
267 if (permission instanceof RuntimePermission) {
268 if ("setSecurityManager".equals(permission.getName())) {
275 class TestCacheResponse extends CacheResponse {
276 InputStream is = null;
277 Map<String, List<String>> headers = null;
279 public TestCacheResponse(String filename) {
280 String path = getClass().getPackage().getName().replace(".", "/");
281 is = getClass().getResourceAsStream("/" + path + "/" + filename);
285 public InputStream getBody() {
290 public Map getHeaders() {
295 class TestCacheRequest extends CacheRequest {
298 public OutputStream getBody() {
303 public void abort() {
307 class TestResponseCache extends ResponseCache {
310 boolean testGet = false;
312 public URI getWasCalled = null;
313 public URI putWasCalled = null;
315 TestResponseCache(String uri, boolean testGet) {
318 } catch (URISyntaxException e) {
320 this.testGet = testGet;
324 public CacheResponse get(URI uri, String rqstMethod, Map rqstHeaders) {
326 if (testGet && uri.equals(uri1)) {
327 return new TestCacheResponse("file1.cache");
333 public CacheRequest put(URI uri, URLConnection conn) {
335 if (!testGet && uri.equals(uri1)) {
336 return new TestCacheRequest();