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 libcore.java.net;
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;
32 import java.net.URISyntaxException;
34 import java.net.URLConnection;
35 import java.security.Permission;
36 import java.util.Arrays;
38 import junit.framework.TestCase;
39 import tests.support.Support_PortManager;
40 import tests.support.Support_TestWebData;
41 import tests.support.Support_TestWebServer;
43 @TestTargetClass(value = ResponseCache.class)
44 public class OldResponseCacheTest extends TestCase {
49 * @tests java.net.ResponseCache#getDefault()
52 level = TestLevel.PARTIAL_COMPLETE,
53 notes = "This is a complete subset of tests for getDefault method.",
54 method = "getDefault",
57 public void test_GetDefault() throws Exception {
58 assertNull(ResponseCache.getDefault());
62 * @tests java.net.ResponseCache#setDefault(ResponseCache)
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}
72 level = TestLevel.COMPLETE,
73 notes = "This is a complete subset of tests for setDefault method.",
74 method = "ResponseCache",
78 public void test_SetDefaultLjava_net_ResponseCache_Normal()
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());
91 level = TestLevel.COMPLETE,
94 args = {URI.class, String.class, Map.class}
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);
106 } catch(Exception e) {}
108 InputStream is = httpCon.getInputStream();
109 byte[] array = new byte [10];
111 assertEquals(url.toURI(), cache.getWasCalled);
112 assertEquals("Cache test", new String(array));
114 httpCon.disconnect();
119 level = TestLevel.COMPLETE,
122 args = {URI.class, URLConnection.class}
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);
131 int port = Support_PortManager.getNextPort();
132 Support_TestWebServer s = new Support_TestWebServer();
134 s.initServer(port, 10000, false);
135 Thread.currentThread().sleep(2500);
137 // Create connection to server
138 URL url = new URL("http://localhost:" + port + "/test1");
139 HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
140 httpCon.setUseCaches(true);
142 Thread.currentThread().sleep(2500);
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);
153 InputStream is = httpCon.getInputStream();
155 byte[] array = new byte[Support_TestWebData.test1.length];
157 assertTrue(Arrays.equals(Support_TestWebData.tests[0], array));
159 httpCon.disconnect();
166 * MockResponseCache for testSetDefault(ResponseCache)
168 class MockResponseCache extends ResponseCache {
170 public CacheResponse get(URI arg0, String arg1, Map arg2)
175 public CacheRequest put(URI arg0, URLConnection arg1)
182 * MockSecurityMaanger. It denies NetPermission("getResponseCache") and
183 * NetPermission("setResponseCache").
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();
193 if (permission instanceof NetPermission) {
194 if ("getResponseCache".equals(permission.getName())) {
196 throw new SecurityException();
200 if (permission instanceof RuntimePermission) {
201 if ("setSecurityManager".equals(permission.getName())) {
208 class TestCacheResponse extends CacheResponse {
209 InputStream is = null;
211 public TestCacheResponse(String filename) {
212 String path = getClass().getPackage().getName().replace(".", "/");
213 is = getClass().getResourceAsStream("/" + path + "/" + filename);
217 public InputStream getBody() {
222 public Map getHeaders() {
227 class TestCacheRequest extends CacheRequest {
230 public OutputStream getBody() {
235 public void abort() {
239 class TestResponseCache extends ResponseCache {
242 boolean testGet = false;
244 public URI getWasCalled = null;
245 public URI putWasCalled = null;
247 TestResponseCache(String uri, boolean testGet) {
250 } catch (URISyntaxException e) {
252 this.testGet = testGet;
256 public CacheResponse get(URI uri, String rqstMethod, Map rqstHeaders) {
258 if (testGet && uri.equals(uri1)) {
259 return new TestCacheResponse("file1.cache");
265 public CacheRequest put(URI uri, URLConnection conn) {
267 if (!testGet && uri.equals(uri1)) {
268 return new TestCacheRequest();