1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package tests.api.java.util; 19 20import java.util.AbstractMap; 21import java.util.Arrays; 22import java.util.Collection; 23import java.util.Iterator; 24import java.util.List; 25import java.util.Map; 26import java.util.Set; 27import java.util.WeakHashMap; 28import libcore.java.lang.ref.FinalizationTester; 29 30import tests.support.Support_MapTest2; 31 32public class WeakHashMapTest extends junit.framework.TestCase { 33 class MockMap extends AbstractMap { 34 public Set entrySet() { 35 return null; 36 } 37 public int size(){ 38 return 0; 39 } 40 } 41 42 Object[] keyArray = new Object[100]; 43 44 Object[] valueArray = new Object[100]; 45 46 WeakHashMap whm; 47 48 /** 49 * java.util.WeakHashMap#WeakHashMap() 50 */ 51 public void test_Constructor() { 52 // Test for method java.util.WeakHashMap() 53 new Support_MapTest2(new WeakHashMap()).runTest(); 54 55 whm = new WeakHashMap(); 56 for (int i = 0; i < 100; i++) 57 whm.put(keyArray[i], valueArray[i]); 58 for (int i = 0; i < 100; i++) 59 assertTrue("Incorrect value retrieved", 60 whm.get(keyArray[i]) == valueArray[i]); 61 62 } 63 64 /** 65 * java.util.WeakHashMap#WeakHashMap(int) 66 */ 67 public void test_ConstructorI() { 68 // Test for method java.util.WeakHashMap(int) 69 whm = new WeakHashMap(50); 70 for (int i = 0; i < 100; i++) 71 whm.put(keyArray[i], valueArray[i]); 72 for (int i = 0; i < 100; i++) 73 assertTrue("Incorrect value retrieved", 74 whm.get(keyArray[i]) == valueArray[i]); 75 76 WeakHashMap empty = new WeakHashMap(0); 77 assertNull("Empty weakhashmap access", empty.get("nothing")); 78 empty.put("something", "here"); 79 assertTrue("cannot get element", empty.get("something") == "here"); 80 81 try { 82 new WeakHashMap(-50); 83 fail("IllegalArgumentException expected"); 84 } catch (IllegalArgumentException e) { 85 //expected 86 } 87 } 88 89 /** 90 * java.util.WeakHashMap#WeakHashMap(int, float) 91 */ 92 public void test_ConstructorIF() { 93 // Test for method java.util.WeakHashMap(int, float) 94 whm = new WeakHashMap(50, 0.5f); 95 for (int i = 0; i < 100; i++) 96 whm.put(keyArray[i], valueArray[i]); 97 for (int i = 0; i < 100; i++) 98 assertTrue("Incorrect value retrieved", 99 whm.get(keyArray[i]) == valueArray[i]); 100 101 WeakHashMap empty = new WeakHashMap(0, 0.75f); 102 assertNull("Empty hashtable access", empty.get("nothing")); 103 empty.put("something", "here"); 104 assertTrue("cannot get element", empty.get("something") == "here"); 105 106 try { 107 new WeakHashMap(50, -0.5f); 108 fail("IllegalArgumentException expected"); 109 } catch (IllegalArgumentException e) { 110 //expected 111 } 112 113 try { 114 new WeakHashMap(-50, 0.5f); 115 fail("IllegalArgumentException expected"); 116 } catch (IllegalArgumentException e) { 117 //expected 118 } 119 } 120 121 /** 122 * java.util.WeakHashMap#WeakHashMap(java.util.Map) 123 */ 124 public void test_ConstructorLjava_util_Map() { 125 Map mockMap = new MockMap(); 126 WeakHashMap map = new WeakHashMap(mockMap); 127 assertEquals("Size should be 0", 0, map.size()); 128 129 try { 130 new WeakHashMap(null); 131 fail("NullPointerException expected"); 132 } catch (NullPointerException e) { 133 //expected 134 } 135 } 136 137 /** 138 * java.util.WeakHashMap#clear() 139 */ 140 public void test_clear() { 141 // Test for method boolean java.util.WeakHashMap.clear() 142 whm = new WeakHashMap(); 143 for (int i = 0; i < 100; i++) 144 whm.put(keyArray[i], valueArray[i]); 145 whm.clear(); 146 assertTrue("Cleared map should be empty", whm.isEmpty()); 147 for (int i = 0; i < 100; i++) 148 assertNull("Cleared map should only return null", whm 149 .get(keyArray[i])); 150 151 } 152 153 /** 154 * java.util.WeakHashMap#containsKey(java.lang.Object) 155 */ 156 public void test_containsKeyLjava_lang_Object() { 157 // Test for method boolean java.util.WeakHashMap.containsKey() 158 whm = new WeakHashMap(); 159 for (int i = 0; i < 100; i++) 160 whm.put(keyArray[i], valueArray[i]); 161 for (int i = 0; i < 100; i++) 162 assertTrue("Should contain referenced key", whm 163 .containsKey(keyArray[i])); 164 keyArray[25] = null; 165 keyArray[50] = null; 166 } 167 168 /** 169 * java.util.WeakHashMap#containsValue(java.lang.Object) 170 */ 171 public void test_containsValueLjava_lang_Object() { 172 // Test for method boolean java.util.WeakHashMap.containsValue() 173 whm = new WeakHashMap(); 174 for (int i = 0; i < 100; i++) 175 whm.put(keyArray[i], valueArray[i]); 176 for (int i = 0; i < 100; i++) 177 assertTrue("Should contain referenced value", whm 178 .containsValue(valueArray[i])); 179 keyArray[25] = null; 180 keyArray[50] = null; 181 } 182 183 /** 184 * java.util.WeakHashMap#entrySet() 185 */ 186 public void test_entrySet() { 187 // Test for method java.util.Set java.util.WeakHashMap.entrySet() 188 whm = new WeakHashMap(); 189 for (int i = 0; i < 100; i++) 190 whm.put(keyArray[i], valueArray[i]); 191 List keys = Arrays.asList(keyArray); 192 List values = Arrays.asList(valueArray); 193 Set entrySet = whm.entrySet(); 194 assertTrue("Incorrect number of entries returned--wanted 100, got: " 195 + entrySet.size(), entrySet.size() == 100); 196 Iterator it = entrySet.iterator(); 197 while (it.hasNext()) { 198 Map.Entry entry = (Map.Entry) it.next(); 199 assertTrue("Invalid map entry returned--bad key", keys 200 .contains(entry.getKey())); 201 assertTrue("Invalid map entry returned--bad key", values 202 .contains(entry.getValue())); 203 } 204 keys = null; 205 values = null; 206 keyArray[50] = null; 207 208 int count = 0; 209 do { 210 System.gc(); 211 System.gc(); 212 FinalizationTester.induceFinalization(); 213 count++; 214 } while (count <= 5 && entrySet.size() == 100); 215 216 assertTrue( 217 "Incorrect number of entries returned after gc--wanted 99, got: " 218 + entrySet.size(), entrySet.size() == 99); 219 } 220 221 /** 222 * java.util.WeakHashMap#isEmpty() 223 */ 224 public void test_isEmpty() { 225 // Test for method boolean java.util.WeakHashMap.isEmpty() 226 whm = new WeakHashMap(); 227 assertTrue("New map should be empty", whm.isEmpty()); 228 Object myObject = new Object(); 229 whm.put(myObject, myObject); 230 assertTrue("Map should not be empty", !whm.isEmpty()); 231 whm.remove(myObject); 232 assertTrue("Map with elements removed should be empty", whm.isEmpty()); 233 } 234 235 /** 236 * java.util.WeakHashMap#put(java.lang.Object, java.lang.Object) 237 */ 238 public void test_putLjava_lang_ObjectLjava_lang_Object() { 239 // Test for method java.lang.Object 240 // java.util.WeakHashMap.put(java.lang.Object, java.lang.Object) 241 WeakHashMap map = new WeakHashMap(); 242 map.put(null, "value"); // add null key 243 System.gc(); 244 System.gc(); 245 FinalizationTester.induceFinalization(); 246 map.remove("nothing"); // Cause objects in queue to be removed 247 assertEquals("null key was removed", 1, map.size()); 248 } 249 250 /** 251 * java.util.WeakHashMap#putAll(java.util.Map) 252 */ 253 public void test_putAllLjava_util_Map() { 254 Map mockMap=new MockMap(); 255 WeakHashMap map = new WeakHashMap(); 256 map.putAll(mockMap); 257 assertEquals("Size should be 0", 0, map.size()); 258 259 try { 260 map.putAll(null); 261 fail("NullPointerException exected"); 262 } catch (NullPointerException e) { 263 //expected 264 } 265 } 266 267 /** 268 * java.util.WeakHashMap#remove(java.lang.Object) 269 */ 270 public void test_removeLjava_lang_Object() { 271 // Test for method java.lang.Object 272 // java.util.WeakHashMap.remove(java.lang.Object) 273 whm = new WeakHashMap(); 274 for (int i = 0; i < 100; i++) 275 whm.put(keyArray[i], valueArray[i]); 276 277 assertTrue("Remove returned incorrect value", 278 whm.remove(keyArray[25]) == valueArray[25]); 279 assertNull("Remove returned incorrect value", 280 whm.remove(keyArray[25])); 281 assertEquals("Size should be 99 after remove", 99, whm.size()); 282 } 283 284 /** 285 * java.util.WeakHashMap#size() 286 */ 287 public void test_size() { 288 whm = new WeakHashMap(); 289 assertEquals(0, whm.size()); 290 } 291 292 /** 293 * java.util.WeakHashMap#keySet() 294 */ 295 public void test_keySet() { 296 // Test for method java.util.Set java.util.WeakHashMap.keySet() 297 whm = new WeakHashMap(); 298 for (int i = 0; i < 100; i++) 299 whm.put(keyArray[i], valueArray[i]); 300 301 List keys = Arrays.asList(keyArray); 302 List values = Arrays.asList(valueArray); 303 304 Set keySet = whm.keySet(); 305 assertEquals("Incorrect number of keys returned,", 100, keySet.size()); 306 Iterator it = keySet.iterator(); 307 while (it.hasNext()) { 308 Object key = it.next(); 309 assertTrue("Invalid map entry returned--bad key", keys 310 .contains(key)); 311 } 312 keys = null; 313 values = null; 314 keyArray[50] = null; 315 316 int count = 0; 317 do { 318 System.gc(); 319 System.gc(); 320 FinalizationTester.induceFinalization(); 321 count++; 322 } while (count <= 5 && keySet.size() == 100); 323 324 assertEquals("Incorrect number of keys returned after gc,", 99, keySet 325 .size()); 326 } 327 328 /** 329 * java.util.WeakHashMap#values() 330 */ 331 public void test_values() { 332 // Test for method java.util.Set java.util.WeakHashMap.values() 333 whm = new WeakHashMap(); 334 for (int i = 0; i < 100; i++) 335 whm.put(keyArray[i], valueArray[i]); 336 337 List keys = Arrays.asList(keyArray); 338 List values = Arrays.asList(valueArray); 339 340 Collection valuesCollection = whm.values(); 341 assertEquals("Incorrect number of keys returned,", 100, 342 valuesCollection.size()); 343 Iterator it = valuesCollection.iterator(); 344 while (it.hasNext()) { 345 Object value = it.next(); 346 assertTrue("Invalid map entry returned--bad value", values 347 .contains(value)); 348 } 349 keys = null; 350 values = null; 351 keyArray[50] = null; 352 353 int count = 0; 354 do { 355 System.gc(); 356 System.gc(); 357 FinalizationTester.induceFinalization(); 358 count++; 359 } while (count <= 5 && valuesCollection.size() == 100); 360 361 assertEquals("Incorrect number of keys returned after gc,", 99, 362 valuesCollection.size()); 363 } 364 365 /** 366 * Sets up the fixture, for example, open a network connection. This method 367 * is called before a test is executed. 368 */ 369 protected void setUp() { 370 for (int i = 0; i < 100; i++) { 371 keyArray[i] = new Object(); 372 valueArray[i] = new Object(); 373 } 374 375 } 376 377 /** 378 * Tears down the fixture, for example, close a network connection. This 379 * method is called after a test is executed. 380 */ 381 protected void tearDown() { 382 } 383} 384