CookieHandlerTest.java revision cc05ad238516f1303687aba4a978e24e57c0c07a
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.IOException; 26import java.io.InputStream; 27import java.net.CookieHandler; 28import java.net.MalformedURLException; 29import java.net.NetPermission; 30import java.net.URI; 31import java.net.URL; 32import java.net.URLConnection; 33import java.security.Permission; 34import java.util.Map; 35 36import junit.framework.TestCase; 37 38import tests.support.Support_Configuration; 39 40@TestTargetClass(CookieHandler.class) 41public class CookieHandlerTest extends TestCase { 42 43 URI getURI, putURI; 44 String link = "http://" + Support_Configuration.SpecialInetTestAddress + "/"; 45 boolean isGetCalled = false; 46 boolean isPutCalled = false; 47 48 /** 49 * @tests java.net.CookieHandler#getDefault() 50 */ 51 @TestTargetNew( 52 level = TestLevel.PARTIAL_COMPLETE, 53 notes = "This is a complete subset of tests for getDefault method.", 54 method = "getDefault", 55 args = {} 56 ) 57 public void test_GetDefault() { 58 assertNull(CookieHandler.getDefault()); 59 } 60 61 /** 62 * @tests java.net.CookieHandler#setDefault(CookieHandler) 63 */ 64 @TestTargetNew( 65 level = TestLevel.PARTIAL_COMPLETE, 66 notes = "This is a complete subset of tests for setDefault method.", 67 method = "setDefault", 68 args = {java.net.CookieHandler.class} 69 ) 70 public void test_SetDefault_java_net_cookieHandler() { 71 MockCookieHandler rc1 = new MockCookieHandler(); 72 MockCookieHandler rc2 = new MockCookieHandler(); 73 CookieHandler.setDefault(rc1); 74 assertSame(CookieHandler.getDefault(), rc1); 75 CookieHandler.setDefault(rc2); 76 assertSame(CookieHandler.getDefault(), rc2); 77 CookieHandler.setDefault(null); 78 assertNull(CookieHandler.getDefault()); 79 } 80 81 /** 82 * @tests java.net.CookieHandler#getDefault() 83 */ 84 @TestTargetNew( 85 level = TestLevel.PARTIAL_COMPLETE, 86 notes = "This is a complete subset of tests for getDefault method.", 87 method = "getDefault", 88 args = {} 89 ) 90 public void testGetDefault_Security() { 91 SecurityManager old = System.getSecurityManager(); 92 try { 93 System.setSecurityManager(new MockSM()); 94 } catch (SecurityException e) { 95 System.err.println("Unable to reset securityManager,test ignored"); 96 return; 97 } 98 try { 99 CookieHandler.getDefault(); 100 fail("should throw SecurityException"); 101 } catch (SecurityException e) { 102 // correct 103 } finally { 104 System.setSecurityManager(old); 105 } 106 } 107 108 /** 109 * @tests java.net.CookieHandler#setDefault(CookieHandler) 110 */ 111 @TestTargetNew( 112 level = TestLevel.PARTIAL_COMPLETE, 113 notes = "This is a complete subset of tests for setDefault method.", 114 method = "setDefault", 115 args = {java.net.CookieHandler.class} 116 ) public void testSetDefault_Security() { 117 CookieHandler rc = new MockCookieHandler(); 118 SecurityManager old = System.getSecurityManager(); 119 try { 120 System.setSecurityManager(new MockSM()); 121 } catch (SecurityException e) { 122 System.err.println("Unable to reset securityManager,test ignored"); 123 return; 124 } 125 126 try { 127 CookieHandler.setDefault(rc); 128 fail("should throw SecurityException"); 129 } catch (SecurityException e) { 130 // correct 131 } finally { 132 System.setSecurityManager(old); 133 } 134 } 135 136 @TestTargetNew( 137 level = TestLevel.COMPLETE, 138 notes = "", 139 method = "CookieHandler", 140 args = {} 141 ) 142 public void test_CookieHandler() { 143 MockCookieHandler mch = new MockCookieHandler(); 144 assertNull(mch.getDefault()); 145 } 146 147 @TestTargets({ 148 @TestTargetNew( 149 level = TestLevel.COMPLETE, 150 notes = "", 151 method = "get", 152 args = {java.net.URI.class, java.util.Map.class} 153 ), 154 @TestTargetNew( 155 level = TestLevel.COMPLETE, 156 notes = "", 157 method = "put", 158 args = {java.net.URI.class, java.util.Map.class} 159 ) 160 }) 161 @KnownFailure("Cache is not used") 162 public void test_get_put() { 163 MockCookieHandler mch = new MockCookieHandler(); 164 CookieHandler defaultHandler = CookieHandler.getDefault(); 165 CookieHandler.setDefault(mch); 166 167 class TestThread extends Thread { 168 public void run() { 169 try { 170 URL url = new URL(link); 171 URLConnection conn = url.openConnection(); 172 Object obj = conn.getContent(); 173 url = new URL(link); 174 conn = url.openConnection(); 175 obj = conn.getContent(); 176 } catch (MalformedURLException e) { 177 fail("MalformedURLException was thrown: " + e.toString()); 178 } catch (IOException e) { 179 fail("IOException was thrown."); 180 } 181 } 182 }; 183 try { 184 TestThread thread = new TestThread(); 185 186 thread.start(); 187 try { 188 thread.join(); 189 } catch (InterruptedException e) { 190 fail("InterruptedException was thrown."); 191 } 192 193 assertTrue(isGetCalled); 194 assertTrue(isPutCalled); 195 } finally { 196 CookieHandler.setDefault(defaultHandler); 197 } 198 } 199 200 class MockCookieHandler extends CookieHandler { 201 202 public Map get(URI uri, Map requestHeaders) throws IOException { 203 getURI = uri; 204 isGetCalled = true; 205 return requestHeaders; 206 } 207 208 public void put(URI uri, Map responseHeaders) throws IOException { 209 putURI = uri; 210 isPutCalled = true; 211 } 212 213 } 214 215 class MockSM extends SecurityManager { 216 public void checkPermission(Permission permission) { 217 if (permission instanceof NetPermission) { 218 if ("setCookieHandler".equals(permission.getName())) { 219 throw new SecurityException(); 220 } 221 } 222 223 if (permission instanceof NetPermission) { 224 if ("getCookieHandler".equals(permission.getName())) { 225 throw new SecurityException(); 226 } 227 } 228 229 if (permission instanceof RuntimePermission) { 230 if ("setSecurityManager".equals(permission.getName())) { 231 return; 232 } 233 } 234 } 235 } 236} 237