ResponseCacheTest.java revision 743fc438ecba5ee39e44e4e8b36dfbe9381340bd
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.KnownFailure; 20import dalvik.annotation.TestTargetClass; 21import dalvik.annotation.TestTargets; 22import dalvik.annotation.TestLevel; 23import dalvik.annotation.TestTargetNew; 24 25import java.io.File; 26import java.io.FileOutputStream; 27import java.io.IOException; 28import java.io.InputStream; 29import java.io.OutputStream; 30import java.net.CacheRequest; 31import java.net.CacheResponse; 32import java.net.HttpURLConnection; 33import java.net.NetPermission; 34import java.net.ResponseCache; 35import java.net.URI; 36import java.net.URISyntaxException; 37import java.net.URL; 38import java.net.URLConnection; 39import java.security.Permission; 40import java.util.Arrays; 41import java.util.List; 42import java.util.Map; 43 44import junit.framework.TestCase; 45 46import tests.support.Support_PortManager; 47import tests.support.Support_TestWebData; 48import tests.support.Support_TestWebServer; 49 50@TestTargetClass(value = ResponseCache.class) 51public class ResponseCacheTest extends TestCase { 52 53 54 55 /** 56 * @tests java.net.ResponseCache#getDefault() 57 */ 58 @TestTargetNew( 59 level = TestLevel.PARTIAL_COMPLETE, 60 notes = "This is a complete subset of tests for getDefault method.", 61 method = "getDefault", 62 args = {} 63 ) 64 public void test_GetDefault() throws Exception { 65 assertNull(ResponseCache.getDefault()); 66 } 67 68 /** 69 * @tests java.net.ResponseCache#setDefault(ResponseCache) 70 */ 71 @TestTargets({ 72 @TestTargetNew( 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} 77 ), 78 @TestTargetNew( 79 level = TestLevel.COMPLETE, 80 notes = "This is a complete subset of tests for setDefault method.", 81 method = "ResponseCache", 82 args = {} 83 ) 84 }) 85 public void test_SetDefaultLjava_net_ResponseCache_Normal() 86 throws Exception { 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()); 95 } 96 97 /** 98 * @tests java.net.ResponseCache#getDefault() 99 */ 100 @TestTargetNew( 101 level = TestLevel.PARTIAL_COMPLETE, 102 notes = "This is a complete subset of tests for getDefault method.", 103 method = "getDefault", 104 args = {} 105 ) 106 public void test_GetDefault_Security() { 107 SecurityManager old = System.getSecurityManager(); 108 try { 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"); 113 return; 114 } 115 try { 116 ResponseCache.getDefault(); 117 fail("should throw SecurityException"); 118 } catch (SecurityException e) { 119 // expected 120 } finally { 121 System.setSecurityManager(old); 122 } 123 } 124 125 /** 126 * @tests java.net.ResponseCache#setDefault(ResponseCache) 127 */ 128 @TestTargetNew( 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} 133 ) 134 public void test_setDefaultLjava_net_ResponseCache_NoPermission() { 135 ResponseCache rc = new MockResponseCache(); 136 SecurityManager old = System.getSecurityManager(); 137 try { 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"); 142 return; 143 } 144 try { 145 ResponseCache.setDefault(rc); 146 fail("should throw SecurityException"); 147 } catch (SecurityException e) { 148 // expected 149 } finally { 150 System.setSecurityManager(old); 151 } 152 } 153 154 @TestTargetNew( 155 level = TestLevel.COMPLETE, 156 notes = "", 157 method = "get", 158 args = {URI.class, String.class, Map.class} 159 ) 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); 167 httpCon.connect(); 168 try { 169 Thread.sleep(5000); 170 } catch(Exception e) {} 171 172 InputStream is = httpCon.getInputStream(); 173 byte[] array = new byte [10]; 174 is.read(array); 175 assertEquals(url.toURI(), cache.getWasCalled); 176 assertEquals("Cache test", new String(array)); 177 is.close(); 178 httpCon.disconnect(); 179 180 } 181 182 @TestTargetNew( 183 level = TestLevel.COMPLETE, 184 notes = "", 185 method = "put", 186 args = {URI.class, URLConnection.class} 187 ) 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); 196 197 // Start Server 198 int port = Support_PortManager.getNextPort(); 199 Support_TestWebServer s = new Support_TestWebServer(); 200 try { 201 s.initServer(port, 10000, false); 202 Thread.currentThread().sleep(2500); 203 204 // Create connection to server 205 URL url = new URL("http://localhost:" + port + "/test1"); 206 HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); 207 httpCon.setUseCaches(true); 208 httpCon.connect(); 209 Thread.currentThread().sleep(2500); 210 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); 218 219 // get the 220 InputStream is = httpCon.getInputStream(); 221 222 byte[] array = new byte[Support_TestWebData.test1.length]; 223 is.read(array); 224 assertTrue(Arrays.equals(Support_TestWebData.tests[0], array)); 225 is.close(); 226 httpCon.disconnect(); 227 } finally { 228 s.close(); 229 } 230 } 231 232 /* 233 * MockResponseCache for testSetDefault(ResponseCache) 234 */ 235 class MockResponseCache extends ResponseCache { 236 237 public CacheResponse get(URI arg0, String arg1, Map arg2) 238 throws IOException { 239 return null; 240 } 241 242 public CacheRequest put(URI arg0, URLConnection arg1) 243 throws IOException { 244 return null; 245 } 246 } 247 248 /* 249 * MockSecurityMaanger. It denies NetPermission("getResponseCache") and 250 * NetPermission("setResponseCache"). 251 */ 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(); 257 } 258 } 259 260 if (permission instanceof NetPermission) { 261 if ("getResponseCache".equals(permission.getName())) { 262 263 throw new SecurityException(); 264 } 265 } 266 267 if (permission instanceof RuntimePermission) { 268 if ("setSecurityManager".equals(permission.getName())) { 269 return; 270 } 271 } 272 } 273 } 274 275 class TestCacheResponse extends CacheResponse { 276 InputStream is = null; 277 Map<String, List<String>> headers = null; 278 279 public TestCacheResponse(String filename) { 280 String path = getClass().getPackage().getName().replace(".", "/"); 281 is = getClass().getResourceAsStream("/" + path + "/" + filename); 282 } 283 284 @Override 285 public InputStream getBody() { 286 return is; 287 } 288 289 @Override 290 public Map getHeaders() { 291 return null; 292 } 293 } 294 295 class TestCacheRequest extends CacheRequest { 296 297 @Override 298 public OutputStream getBody() { 299 return null; 300 } 301 302 @Override 303 public void abort() { 304 } 305 } 306 307 class TestResponseCache extends ResponseCache { 308 309 URI uri1 = null; 310 boolean testGet = false; 311 312 public URI getWasCalled = null; 313 public URI putWasCalled = null; 314 315 TestResponseCache(String uri, boolean testGet) { 316 try { 317 uri1 = new URI(uri); 318 } catch (URISyntaxException e) { 319 } 320 this.testGet = testGet; 321 } 322 323 @Override 324 public CacheResponse get(URI uri, String rqstMethod, Map rqstHeaders) { 325 getWasCalled = uri; 326 if (testGet && uri.equals(uri1)) { 327 return new TestCacheResponse("file1.cache"); 328 } 329 return null; 330 } 331 332 @Override 333 public CacheRequest put(URI uri, URLConnection conn) { 334 putWasCalled = uri; 335 if (!testGet && uri.equals(uri1)) { 336 return new TestCacheRequest(); 337 } 338 return null; 339 } 340 } 341}