ResponseCacheTest.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
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 org.apache.harmony.luni.tests.java.net; 18 19import java.io.IOException; 20import java.net.CacheRequest; 21import java.net.CacheResponse; 22import java.net.NetPermission; 23import java.net.ResponseCache; 24import java.net.URI; 25import java.net.URLConnection; 26import java.security.Permission; 27import java.util.Map; 28 29import junit.framework.TestCase; 30 31public class ResponseCacheTest extends TestCase { 32 33 /** 34 * @tests java.net.ResponseCache#getDefault() 35 */ 36 public void test_GetDefault() throws Exception { 37 assertNull(ResponseCache.getDefault()); 38 } 39 40 /** 41 * @tests java.net.ResponseCache#setDefault(ResponseCache) 42 */ 43 public void test_SetDefaultLjava_net_ResponseCache_Normal() 44 throws Exception { 45 ResponseCache rc1 = new MockResponseCache(); 46 ResponseCache rc2 = new MockResponseCache(); 47 ResponseCache.setDefault(rc1); 48 assertSame(ResponseCache.getDefault(), rc1); 49 ResponseCache.setDefault(rc2); 50 assertSame(ResponseCache.getDefault(), rc2); 51 ResponseCache.setDefault(null); 52 assertNull(ResponseCache.getDefault()); 53 } 54 55 /** 56 * @tests java.net.ResponseCache#getDefault() 57 */ 58 public void test_GetDefault_Security() { 59 SecurityManager old = System.getSecurityManager(); 60 try { 61 System.setSecurityManager(new MockSM()); 62 } catch (SecurityException e) { 63 System.err.println("No setSecurityManager permission."); 64 System.err.println("test_setDefaultLjava_net_ResponseCache_NoPermission is not tested"); 65 return; 66 } 67 try { 68 ResponseCache.getDefault(); 69 fail("should throw SecurityException"); 70 } catch (SecurityException e) { 71 // expected 72 } finally { 73 System.setSecurityManager(old); 74 } 75 } 76 77 /** 78 * @tests java.net.ResponseCache#setDefault(ResponseCache) 79 */ 80 public void test_setDefaultLjava_net_ResponseCache_NoPermission() { 81 ResponseCache rc = new MockResponseCache(); 82 SecurityManager old = System.getSecurityManager(); 83 try { 84 System.setSecurityManager(new MockSM()); 85 } catch (SecurityException e) { 86 System.err.println("No setSecurityManager permission."); 87 System.err.println("test_setDefaultLjava_net_ResponseCache_NoPermission is not tested"); 88 return; 89 } 90 try { 91 ResponseCache.setDefault(rc); 92 fail("should throw SecurityException"); 93 } catch (SecurityException e) { 94 // expected 95 } finally { 96 System.setSecurityManager(old); 97 } 98 } 99 100 /* 101 * MockResponseCache for testSetDefault(ResponseCache) 102 */ 103 class MockResponseCache extends ResponseCache { 104 105 public CacheResponse get(URI arg0, String arg1, Map arg2) 106 throws IOException { 107 return null; 108 } 109 110 public CacheRequest put(URI arg0, URLConnection arg1) 111 throws IOException { 112 return null; 113 } 114 } 115 116 /* 117 * MockSecurityMaanger. It denies NetPermission("getResponseCache") and 118 * NetPermission("setResponseCache"). 119 */ 120 class MockSM extends SecurityManager { 121 public void checkPermission(Permission permission) { 122 if (permission instanceof NetPermission) { 123 if ("setResponseCache".equals(permission.getName())) { 124 throw new SecurityException(); 125 } 126 } 127 128 if (permission instanceof NetPermission) { 129 if ("getResponseCache".equals(permission.getName())) { 130 throw new SecurityException(); 131 } 132 } 133 134 if (permission instanceof RuntimePermission) { 135 if ("setSecurityManager".equals(permission.getName())) { 136 return; 137 } 138 } 139 } 140 } 141} 142