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.util; 18 19import dalvik.annotation.TestTargetNew; 20import dalvik.annotation.TestTargets; 21import dalvik.annotation.TestLevel; 22import dalvik.annotation.TestTargetClass; 23import dalvik.annotation.AndroidOnly; 24 25import java.util.ArrayList; 26import java.util.Collection; 27import java.util.EnumMap; 28import java.util.HashMap; 29import java.util.Iterator; 30import java.util.Map; 31import java.util.NoSuchElementException; 32import java.util.Set; 33 34import org.apache.harmony.testframework.serialization.SerializationTest; 35 36import junit.framework.TestCase; 37 38@TestTargetClass(EnumMap.class) 39public class EnumMapTest extends TestCase { 40 enum Size { 41 Small, Middle, Big {}; 42 } 43 44 enum Color { 45 Red, Green, Blue {}; 46 } 47 48 enum Empty { 49 //Empty 50 } 51 52 private static class MockEntry<K, V> implements Map.Entry<K, V> { 53 private K key; 54 55 private V value; 56 57 public MockEntry(K key, V value) { 58 this.key = key; 59 this.value = value; 60 } 61 62 @Override 63 public int hashCode() { 64 return (key == null ? 0 : key.hashCode()) 65 ^ (value == null ? 0 : value.hashCode()); 66 } 67 68 public K getKey() { 69 return key; 70 } 71 72 public V getValue() { 73 return value; 74 } 75 76 public V setValue(V object) { 77 V oldValue = value; 78 value = object; 79 return oldValue; 80 } 81 } 82 83 /** 84 * @tests java.util.EnumMap#EnumMap(Class) 85 */ 86 @TestTargetNew( 87 level = TestLevel.COMPLETE, 88 notes = "", 89 method = "EnumMap", 90 args = {java.lang.Class.class} 91 ) 92 @SuppressWarnings({ "unchecked", "boxing" }) 93 public void test_ConstructorLjava_lang_Class() { 94 try { 95 new EnumMap((Class) null); 96 fail("Expected NullPointerException"); //$NON-NLS-1$ 97 } catch (NullPointerException e) { 98 // Expected 99 } 100 101 102 try { 103 new EnumMap(Size.Big.getClass()); 104 fail("Expected NullPointerException"); //$NON-NLS-1$ 105 } catch (NullPointerException e) { 106 // Expected 107 } 108 109 try { 110 new EnumMap(Integer.class); 111 fail("Expected NullPointerException"); //$NON-NLS-1$ 112 } catch (NullPointerException e) { 113 // Expected 114 } 115 116 EnumMap enumColorMap = new EnumMap<Color, Double>(Color.class); 117 assertNull("Return non-null for non mapped key", enumColorMap.put( //$NON-NLS-1$ 118 Color.Green, 2)); 119 assertEquals("Get returned incorrect value for given key", 2, //$NON-NLS-1$ 120 enumColorMap.get(Color.Green)); 121 122 EnumMap enumEmptyMap = new EnumMap<Empty, Double>(Empty.class); 123 try { 124 enumEmptyMap.put(Color.Red, 2); 125 fail("Expected ClassCastException"); //$NON-NLS-1$ 126 } catch (ClassCastException e) { 127 // Expected 128 } 129 130 EnumMap enumSizeMap = new EnumMap(Size.class); 131 assertNull("Return non-null for non mapped key", enumSizeMap.put( //$NON-NLS-1$ 132 Size.Big, 2)); 133 assertEquals("Get returned incorrect value for given key", 2, //$NON-NLS-1$ 134 enumSizeMap.get(Size.Big)); 135 try { 136 enumSizeMap.put(Color.Red, 2); 137 fail("Expected ClassCastException"); //$NON-NLS-1$ 138 } catch (ClassCastException e) { 139 // Expected 140 } 141 142 enumSizeMap = new EnumMap(Size.Middle.getClass()); 143 assertNull("Return non-null for non mapped key", enumSizeMap.put( //$NON-NLS-1$ 144 Size.Small, 1)); 145 assertEquals("Get returned incorrect value for given key", 1, //$NON-NLS-1$ 146 enumSizeMap.get(Size.Small)); 147 try { 148 enumSizeMap.put(Color.Red, 2); 149 fail("Expected ClassCastException"); //$NON-NLS-1$ 150 } catch (ClassCastException e) { 151 // Expected 152 } 153 } 154 155 /** 156 * @tests java.util.EnumMap#EnumMap(EnumMap) 157 */ 158 @TestTargetNew( 159 level = TestLevel.COMPLETE, 160 notes = "", 161 method = "EnumMap", 162 args = {java.util.EnumMap.class} 163 ) 164 @SuppressWarnings({ "unchecked", "boxing" }) 165 public void test_ConstructorLjava_util_EnumMap() { 166 EnumMap enumMap; 167 EnumMap enumColorMap = null; 168 try { 169 enumMap = new EnumMap(enumColorMap); 170 fail("Expected NullPointerException"); //$NON-NLS-1$ 171 } catch (NullPointerException e) { 172 // Expected 173 } 174 175 enumColorMap = new EnumMap<Color, Double>(Color.class); 176 Double double1 = new Double(1); 177 enumColorMap.put(Color.Green, 2); 178 enumColorMap.put(Color.Blue, double1); 179 180 enumMap = new EnumMap(enumColorMap); 181 assertEquals("Constructor fails", 2, enumMap.get(Color.Green)); //$NON-NLS-1$ 182 assertSame("Constructor fails", double1, enumMap.get(Color.Blue)); //$NON-NLS-1$ 183 assertNull("Constructor fails", enumMap.get(Color.Red)); //$NON-NLS-1$ 184 enumMap.put(Color.Red, 1); 185 assertEquals("Wrong value", 1, enumMap.get(Color.Red)); //$NON-NLS-1$ 186 187 try { 188 enumMap.put(Size.Middle, 2); 189 fail("Expected ClassCastException"); //$NON-NLS-1$ 190 } catch (ClassCastException e) { 191 // Expected 192 } 193 } 194 195 /** 196 * @tests java.util.EnumMap#EnumMap(Map) 197 */ 198 @TestTargetNew( 199 level = TestLevel.COMPLETE, 200 notes = "", 201 method = "EnumMap", 202 args = {java.util.Map.class} 203 ) 204 @SuppressWarnings({ "unchecked", "boxing" }) 205 public void test_ConstructorLjava_util_Map() { 206 EnumMap enumMap; 207 Map enumColorMap = null; 208 try { 209 enumMap = new EnumMap(enumColorMap); 210 fail("Expected NullPointerException"); //$NON-NLS-1$ 211 } catch (NullPointerException e) { 212 // Expected 213 } 214 enumColorMap = new EnumMap<Color, Double>(Color.class); 215 enumMap = new EnumMap(enumColorMap); 216 enumColorMap.put(Color.Blue, 3); 217 enumMap = new EnumMap(enumColorMap); 218 219 HashMap hashColorMap = null; 220 try { 221 enumMap = new EnumMap(hashColorMap); 222 fail("Expected NullPointerException");//$NON-NLS-1$ 223 } catch (NullPointerException e) { 224 // Expected 225 } 226 227 hashColorMap = new HashMap(); 228 try { 229 enumMap = new EnumMap(hashColorMap); 230 fail("Expected IllegalArgumentException"); //$NON-NLS-1$ 231 } catch (IllegalArgumentException e) { 232 // Expected 233 } 234 235 hashColorMap.put(Color.Green, 2); 236 enumMap = new EnumMap(hashColorMap); 237 assertEquals("Constructor fails", 2, enumMap.get(Color.Green)); //$NON-NLS-1$ 238 assertNull("Constructor fails", enumMap.get(Color.Red)); //$NON-NLS-1$ 239 enumMap.put(Color.Red, 1); 240 assertEquals("Wrong value", 1, enumMap.get(Color.Red)); //$NON-NLS-1$ 241 hashColorMap.put(Size.Big, 3); 242 try { 243 enumMap = new EnumMap(hashColorMap); 244 fail("Expected ClassCastException"); //$NON-NLS-1$ 245 } catch (ClassCastException e) { 246 // Expected 247 } 248 249 hashColorMap = new HashMap(); 250 hashColorMap.put(new Integer(1), 1); 251 try { 252 enumMap = new EnumMap(hashColorMap); 253 fail("Expected ClassCastException"); //$NON-NLS-1$ 254 } catch (ClassCastException e) { 255 // Expected 256 } 257 } 258 259 /** 260 * @tests java.util.EnumMap#clear() 261 */ 262 @TestTargetNew( 263 level = TestLevel.COMPLETE, 264 notes = "", 265 method = "clear", 266 args = {} 267 ) 268 @SuppressWarnings({ "unchecked", "boxing" }) 269 public void test_clear() { 270 EnumMap enumSizeMap = new EnumMap(Size.class); 271 enumSizeMap.put(Size.Small, 1); 272 enumSizeMap.clear(); 273 assertNull("Failed to clear all elements", enumSizeMap.get(Size.Small)); //$NON-NLS-1$ 274 } 275 276 /** 277 * @tests java.util.EnumMap#containsKey(Object) 278 */ 279 @TestTargetNew( 280 level = TestLevel.COMPLETE, 281 notes = "", 282 method = "containsKey", 283 args = {java.lang.Object.class} 284 ) 285 @SuppressWarnings( { "unchecked", "boxing" }) 286 public void test_containsKeyLjava_lang_Object() { 287 EnumMap enumSizeMap = new EnumMap(Size.class); 288 assertFalse("Returned true for uncontained key", enumSizeMap //$NON-NLS-1$ 289 .containsKey(Size.Small)); 290 enumSizeMap.put(Size.Small, 1); 291 assertTrue("Returned false for contained key", enumSizeMap //$NON-NLS-1$ 292 .containsKey(Size.Small)); 293 294 enumSizeMap.put(Size.Big, null); 295 assertTrue("Returned false for contained key", enumSizeMap //$NON-NLS-1$ 296 .containsKey(Size.Big)); 297 298 assertFalse("Returned true for uncontained key", enumSizeMap //$NON-NLS-1$ 299 .containsKey(Color.Red)); 300 assertFalse("Returned true for uncontained key", enumSizeMap //$NON-NLS-1$ 301 .containsKey(new Integer("3"))); //$NON-NLS-1$ 302 assertFalse("Returned true for uncontained key", enumSizeMap //$NON-NLS-1$ 303 .containsKey(null)); 304 } 305 306 /** 307 * @tests java.util.EnumMap#clone() 308 */ 309 @TestTargetNew( 310 level = TestLevel.COMPLETE, 311 notes = "", 312 method = "clone", 313 args = {} 314 ) 315 @SuppressWarnings( { "unchecked", "boxing" }) 316 public void test_clone() { 317 EnumMap enumSizeMap = new EnumMap(Size.class); 318 Integer integer = new Integer("3"); //$NON-NLS-1$ 319 enumSizeMap.put(Size.Small, integer); 320 EnumMap enumSizeMapClone = enumSizeMap.clone(); 321 assertNotSame("Should not be same", enumSizeMap, enumSizeMapClone); //$NON-NLS-1$ 322 assertEquals("Clone answered unequal EnumMap", enumSizeMap, //$NON-NLS-1$ 323 enumSizeMapClone); 324 325 assertSame("Should be same", enumSizeMap.get(Size.Small), //$NON-NLS-1$ 326 enumSizeMapClone.get(Size.Small)); 327 assertSame("Clone is not shallow clone", integer, enumSizeMapClone //$NON-NLS-1$ 328 .get(Size.Small)); 329 enumSizeMap.remove(Size.Small); 330 assertSame("Clone is not shallow clone", integer, enumSizeMapClone //$NON-NLS-1$ 331 .get(Size.Small)); 332 } 333 334 /** 335 * @tests java.util.EnumMap#containsValue(Object) 336 */ 337 @TestTargetNew( 338 level = TestLevel.COMPLETE, 339 notes = "", 340 method = "containsValue", 341 args = {java.lang.Object.class} 342 ) 343 @SuppressWarnings( { "unchecked", "boxing" }) 344 public void test_containsValueLjava_lang_Object() { 345 EnumMap enumSizeMap = new EnumMap(Size.class); 346 Double double1 = new Double(3); 347 Double double2 = new Double(3); 348 349 assertFalse("Returned true for uncontained value", enumSizeMap //$NON-NLS-1$ 350 .containsValue(double1)); 351 enumSizeMap.put(Size.Middle, 2); 352 enumSizeMap.put(Size.Small, double1); 353 assertTrue("Returned false for contained value", enumSizeMap //$NON-NLS-1$ 354 .containsValue(double1)); 355 assertTrue("Returned false for contained value", enumSizeMap //$NON-NLS-1$ 356 .containsValue(double2)); 357 assertTrue("Returned false for contained value", enumSizeMap //$NON-NLS-1$ 358 .containsValue(2)); 359 assertFalse("Returned true for uncontained value", enumSizeMap //$NON-NLS-1$ 360 .containsValue(1)); 361 362 assertFalse("Returned true for uncontained value", enumSizeMap //$NON-NLS-1$ 363 .containsValue(null)); 364 enumSizeMap.put(Size.Big, null); 365 assertTrue("Returned false for contained value", enumSizeMap //$NON-NLS-1$ 366 .containsValue(null)); 367 } 368 369 /** 370 * @tests java.util.EnumMap#entrySet() 371 */ 372 @TestTargetNew( 373 level = TestLevel.COMPLETE, 374 notes = "", 375 method = "entrySet", 376 args = {} 377 ) 378 @AndroidOnly("Map.Entry is indirectly modified on RI when Iterator.next() is invoked") 379 @SuppressWarnings({ "unchecked", "boxing" }) 380 public void test_entrySet() { 381 EnumMap enumSizeMap = new EnumMap(Size.class); 382 enumSizeMap.put(Size.Middle, 1); 383 enumSizeMap.put(Size.Big, null); 384 MockEntry mockEntry = new MockEntry(Size.Middle, 1); 385 Set set = enumSizeMap.entrySet(); 386 387 Set set1 = enumSizeMap.entrySet(); 388 assertSame("Should be same", set1, set); //$NON-NLS-1$ 389 try { 390 set.add(mockEntry); 391 fail("Should throw UnsupportedOperationException"); //$NON-NLS-1$ 392 } catch (UnsupportedOperationException e) { 393 // Expected 394 } 395 396 assertTrue("Returned false for contained object", set//$NON-NLS-1$ 397 .contains(mockEntry)); 398 mockEntry = new MockEntry(Size.Middle, null); 399 assertFalse("Returned true for uncontained object", set //$NON-NLS-1$ 400 .contains(mockEntry)); 401 assertFalse("Returned true for uncontained object", set //$NON-NLS-1$ 402 .contains(Size.Small)); 403 mockEntry = new MockEntry(new Integer(1), 1); 404 assertFalse("Returned true for uncontained object", set //$NON-NLS-1$ 405 .contains(mockEntry)); 406 assertFalse("Returned true for uncontained object", set //$NON-NLS-1$ 407 .contains(new Integer(1))); 408 409 mockEntry = new MockEntry(Size.Big, null); 410 assertTrue("Returned false for contained object", set//$NON-NLS-1$ 411 .contains(mockEntry)); 412 assertTrue("Returned false when the object can be removed", set //$NON-NLS-1$ 413 .remove(mockEntry)); 414 assertFalse("Returned true for uncontained object", set //$NON-NLS-1$ 415 .contains(mockEntry)); 416 assertFalse("Returned true when the object can not be removed", set //$NON-NLS-1$ 417 .remove(mockEntry)); 418 mockEntry = new MockEntry(new Integer(1), 1); 419 assertFalse("Returned true when the object can not be removed", set //$NON-NLS-1$ 420 .remove(mockEntry)); 421 assertFalse("Returned true when the object can not be removed", set //$NON-NLS-1$ 422 .remove(new Integer(1))); 423 424 // The set is backed by the map so changes to one are reflected by the 425 // other. 426 enumSizeMap.put(Size.Big, 3); 427 mockEntry = new MockEntry(Size.Big, 3); 428 assertTrue("Returned false for contained object", set//$NON-NLS-1$ 429 .contains(mockEntry)); 430 enumSizeMap.remove(Size.Big); 431 assertFalse("Returned true for uncontained object", set //$NON-NLS-1$ 432 .contains(mockEntry)); 433 434 assertEquals("Wrong size", 1, set.size()); //$NON-NLS-1$ 435 set.clear(); 436 assertEquals("Wrong size", 0, set.size()); //$NON-NLS-1$ 437 438 enumSizeMap = new EnumMap(Size.class); 439 enumSizeMap.put(Size.Middle, 1); 440 enumSizeMap.put(Size.Big, null); 441 set = enumSizeMap.entrySet(); 442 Collection c = new ArrayList(); 443 c.add(new MockEntry(Size.Middle, 1)); 444 assertTrue("Return wrong value", set.containsAll(c)); //$NON-NLS-1$ 445 assertTrue("Remove does not success", set.removeAll(c)); //$NON-NLS-1$ 446 447 enumSizeMap.put(Size.Middle, 1); 448 c.add(new MockEntry(Size.Big, 3)); 449 assertTrue("Remove does not success", set.removeAll(c)); //$NON-NLS-1$ 450 assertFalse("Should return false", set.removeAll(c)); //$NON-NLS-1$ 451 assertEquals("Wrong size", 1, set.size()); //$NON-NLS-1$ 452 453 enumSizeMap = new EnumMap(Size.class); 454 enumSizeMap.put(Size.Middle, 1); 455 enumSizeMap.put(Size.Big, null); 456 set = enumSizeMap.entrySet(); 457 c = new ArrayList(); 458 c.add(new MockEntry(Size.Middle, 1)); 459 c.add(new MockEntry(Size.Big, 3)); 460 461 assertTrue("Retain does not success", set.retainAll(c)); //$NON-NLS-1$ 462 assertEquals("Wrong size", 1, set.size()); //$NON-NLS-1$ 463 assertFalse("Should return false", set.retainAll(c)); //$NON-NLS-1$ 464 465 enumSizeMap = new EnumMap(Size.class); 466 enumSizeMap.put(Size.Middle, 1); 467 enumSizeMap.put(Size.Big, null); 468 469 set = enumSizeMap.entrySet(); 470 Object[] array = set.toArray(); 471 assertEquals("Wrong length", 2, array.length); //$NON-NLS-1$ 472 Map.Entry entry = (Map.Entry) array[0]; 473 assertEquals("Wrong key", Size.Middle, entry.getKey()); //$NON-NLS-1$ 474 assertEquals("Wrong value", 1, entry.getValue()); //$NON-NLS-1$ 475 476 Object[] array1 = new Object[10]; 477 array1 = set.toArray(); 478 assertEquals("Wrong length", 2, array1.length); //$NON-NLS-1$ 479 entry = (Map.Entry) array[0]; 480 assertEquals("Wrong key", Size.Middle, entry.getKey()); //$NON-NLS-1$ 481 assertEquals("Wrong value", 1, entry.getValue()); //$NON-NLS-1$ 482 483 array1 = new Object[10]; 484 array1 = set.toArray(array1); 485 assertEquals("Wrong length", 10, array1.length); //$NON-NLS-1$ 486 entry = (Map.Entry) array[1]; 487 assertEquals("Wrong key", Size.Big, entry.getKey()); //$NON-NLS-1$ 488 assertNull("Should be null", array1[2]); //$NON-NLS-1$ 489 490 set = enumSizeMap.entrySet(); 491 Integer integer = new Integer("1"); //$NON-NLS-1$ 492 assertFalse("Returned true when the object can not be removed", set //$NON-NLS-1$ 493 .remove(integer)); 494 assertTrue("Returned false when the object can be removed", set //$NON-NLS-1$ 495 .remove(entry)); 496 497 enumSizeMap = new EnumMap(Size.class); 498 enumSizeMap.put(Size.Middle, 1); 499 enumSizeMap.put(Size.Big, null); 500 set = enumSizeMap.entrySet(); 501 Iterator iter = set.iterator(); 502 entry = (Map.Entry) iter.next(); 503 assertTrue("Returned false for contained object", set.contains(entry)); //$NON-NLS-1$ 504 mockEntry = new MockEntry(Size.Middle, 2); 505 assertFalse("Returned true for uncontained object", set //$NON-NLS-1$ 506 .contains(mockEntry)); 507 mockEntry = new MockEntry(new Integer(2), 2); 508 assertFalse("Returned true for uncontained object", set //$NON-NLS-1$ 509 .contains(mockEntry)); 510 entry = (Map.Entry) iter.next(); 511 assertTrue("Returned false for contained object", set.contains(entry)); //$NON-NLS-1$ 512 513 enumSizeMap.put(Size.Middle, 1); 514 enumSizeMap.remove(Size.Big); 515 mockEntry = new MockEntry(Size.Big, null); 516 assertEquals("Wrong size", 1, set.size()); //$NON-NLS-1$ 517 assertFalse("Returned true for uncontained object", set.contains(mockEntry)); //$NON-NLS-1$ 518 enumSizeMap.put(Size.Big, 2); 519 mockEntry = new MockEntry(Size.Big, 2); 520 assertTrue("Returned false for contained object", set //$NON-NLS-1$ 521 .contains(mockEntry)); 522 523 iter.remove(); 524 try { 525 iter.remove(); 526 fail("Should throw IllegalStateException"); //$NON-NLS-1$ 527 } catch (IllegalStateException e) { 528 // Expected 529 } 530 try { 531 entry.setValue(2); 532 fail("Should throw IllegalStateException"); //$NON-NLS-1$ 533 } catch (IllegalStateException e) { 534 // Expected 535 } 536 try { 537 set.contains(entry); 538 fail("Should throw IllegalStateException"); //$NON-NLS-1$ 539 } catch (IllegalStateException e) { 540 // Expected 541 } 542 543 enumSizeMap = new EnumMap(Size.class); 544 enumSizeMap.put(Size.Middle, 1); 545 enumSizeMap.put(Size.Big, null); 546 set = enumSizeMap.entrySet(); 547 iter = set.iterator(); 548 entry = (Map.Entry) iter.next(); 549 assertEquals("Wrong key", Size.Middle, entry.getKey()); //$NON-NLS-1$ 550 551 assertTrue("Returned false for contained object", set.contains(entry)); //$NON-NLS-1$ 552 enumSizeMap.put(Size.Middle, 3); 553 assertTrue("Returned false for contained object", set.contains(entry)); //$NON-NLS-1$ 554 entry.setValue(2); 555 assertTrue("Returned false for contained object", set.contains(entry)); //$NON-NLS-1$ 556 assertFalse("Returned true for uncontained object", set //$NON-NLS-1$ 557 .remove(new Integer(1))); 558 559 iter.next(); 560 assertEquals("Wrong key", Size.Middle, entry.getKey()); //$NON-NLS-1$ 561 set.clear(); 562 assertEquals("Wrong size", 0, set.size()); //$NON-NLS-1$ 563 564 enumSizeMap = new EnumMap(Size.class); 565 enumSizeMap.put(Size.Middle, 1); 566 enumSizeMap.put(Size.Big, null); 567 set = enumSizeMap.entrySet(); 568 iter = set.iterator(); 569 mockEntry = new MockEntry(Size.Middle, 1); 570 571 assertFalse("Wrong result", entry.equals(mockEntry)); //$NON-NLS-1$ 572 try { 573 iter.remove(); 574 fail("Should throw IllegalStateException"); //$NON-NLS-1$ 575 } catch (IllegalStateException e) { 576 // Expected 577 } 578 entry = (Map.Entry) iter.next(); 579 assertEquals("Wrong key", Size.Middle, entry.getKey()); //$NON-NLS-1$ 580 assertTrue("Should return true", entry.equals(mockEntry)); //$NON-NLS-1$ 581 assertEquals("Should be equal", mockEntry.hashCode(), entry.hashCode()); //$NON-NLS-1$ 582 mockEntry = new MockEntry(Size.Big, 1); 583 assertFalse("Wrong result", entry.equals(mockEntry)); //$NON-NLS-1$ 584 585 entry = (Map.Entry) iter.next(); 586 assertFalse("Wrong result", entry.equals(mockEntry)); //$NON-NLS-1$ 587 assertEquals("Wrong key", Size.Big, entry.getKey()); //$NON-NLS-1$ 588 iter.remove(); 589 assertFalse("Wrong result", entry.equals(mockEntry)); //$NON-NLS-1$ 590 assertEquals("Wrong size", 1, set.size()); //$NON-NLS-1$ 591 try { 592 iter.remove(); 593 fail("Should throw IllegalStateException"); //$NON-NLS-1$ 594 } catch (IllegalStateException e) { 595 // Expected 596 } 597 try { 598 iter.next(); 599 fail("Should throw NoSuchElementException"); //$NON-NLS-1$ 600 } catch (NoSuchElementException e) { 601 // Expected 602 } 603 } 604 605 /** 606 * @tests java.util.EnumMap#equals(Object) 607 */ 608 @TestTargetNew( 609 level = TestLevel.COMPLETE, 610 notes = "", 611 method = "equals", 612 args = {java.lang.Object.class} 613 ) 614 @SuppressWarnings( { "unchecked", "boxing" }) 615 public void test_equalsLjava_lang_Object() { 616 EnumMap enumMap = new EnumMap(Size.class); 617 enumMap.put(Size.Small, 1); 618 619 EnumMap enumSizeMap = new EnumMap(Size.class); 620 assertFalse("Returned true for unequal EnumMap", enumSizeMap //$NON-NLS-1$ 621 .equals(enumMap)); 622 enumSizeMap.put(Size.Small, 1); 623 assertTrue("Returned false for equal EnumMap", enumSizeMap //$NON-NLS-1$ 624 .equals(enumMap)); 625 enumSizeMap.put(Size.Big, null); 626 assertFalse("Returned true for unequal EnumMap", enumSizeMap //$NON-NLS-1$ 627 .equals(enumMap)); 628 629 enumMap.put(Size.Middle, null); 630 assertFalse("Returned true for unequal EnumMap", enumSizeMap //$NON-NLS-1$ 631 .equals(enumMap)); 632 enumMap.remove(Size.Middle); 633 enumMap.put(Size.Big, 3); 634 assertFalse("Returned true for unequal EnumMap", enumSizeMap //$NON-NLS-1$ 635 .equals(enumMap)); 636 enumMap.put(Size.Big, null); 637 assertTrue("Returned false for equal EnumMap", enumSizeMap //$NON-NLS-1$ 638 .equals(enumMap)); 639 640 HashMap hashMap = new HashMap(); 641 hashMap.put(Size.Small, 1); 642 assertFalse("Returned true for unequal EnumMap", hashMap //$NON-NLS-1$ 643 .equals(enumMap)); 644 hashMap.put(Size.Big, null); 645 assertTrue("Returned false for equal EnumMap", enumMap.equals(hashMap)); //$NON-NLS-1$ 646 647 assertFalse("Should return false", enumSizeMap //$NON-NLS-1$ 648 .equals(new Integer(1))); 649 } 650 651 /** 652 * @tests java.util.EnumMap#keySet() 653 */ 654 @TestTargetNew( 655 level = TestLevel.COMPLETE, 656 notes = "", 657 method = "keySet", 658 args = {} 659 ) 660 @SuppressWarnings( { "unchecked", "boxing" }) 661 public void test_keySet() { 662 EnumMap enumSizeMap = new EnumMap(Size.class); 663 enumSizeMap.put(Size.Middle, 2); 664 enumSizeMap.put(Size.Big, null); 665 Set set = enumSizeMap.keySet(); 666 667 Set set1 = enumSizeMap.keySet(); 668 assertSame("Should be same", set1, set); //$NON-NLS-1$ 669 try { 670 set.add(Size.Big); 671 fail("Should throw UnsupportedOperationException"); //$NON-NLS-1$ 672 } catch (UnsupportedOperationException e) { 673 // Expected 674 } 675 676 assertTrue("Returned false for contained object", set//$NON-NLS-1$ 677 .contains(Size.Middle)); 678 assertTrue("Returned false for contained object", set//$NON-NLS-1$ 679 .contains(Size.Big)); 680 assertFalse("Returned true for uncontained object", set //$NON-NLS-1$ 681 .contains(Size.Small)); 682 assertFalse("Returned true for uncontained object", set //$NON-NLS-1$ 683 .contains(new Integer(1))); 684 assertTrue("Returned false when the object can be removed", set //$NON-NLS-1$ 685 .remove(Size.Big)); 686 assertFalse("Returned true for uncontained object", set //$NON-NLS-1$ 687 .contains(Size.Big)); 688 assertFalse("Returned true when the object can not be removed", set //$NON-NLS-1$ 689 .remove(Size.Big)); 690 assertFalse("Returned true when the object can not be removed", set //$NON-NLS-1$ 691 .remove(new Integer(1))); 692 693 // The set is backed by the map so changes to one are reflected by the 694 // other. 695 enumSizeMap.put(Size.Big, 3); 696 assertTrue("Returned false for contained object", set//$NON-NLS-1$ 697 .contains(Size.Big)); 698 enumSizeMap.remove(Size.Big); 699 assertFalse("Returned true for uncontained object", set //$NON-NLS-1$ 700 .contains(Size.Big)); 701 702 assertEquals("Wrong size", 1, set.size()); //$NON-NLS-1$ 703 set.clear(); 704 assertEquals("Wrong size", 0, set.size()); //$NON-NLS-1$ 705 706 enumSizeMap = new EnumMap(Size.class); 707 enumSizeMap.put(Size.Middle, 1); 708 enumSizeMap.put(Size.Big, null); 709 set = enumSizeMap.keySet(); 710 Collection c = new ArrayList(); 711 c.add(Size.Big); 712 assertTrue("Should return true", set.containsAll(c)); //$NON-NLS-1$ 713 c.add(Size.Small); 714 assertFalse("Should return false", set.containsAll(c)); //$NON-NLS-1$ 715 assertTrue("Should return true", set.removeAll(c)); //$NON-NLS-1$ 716 assertEquals("Wrong size", 1, set.size()); //$NON-NLS-1$ 717 assertFalse("Should return false", set.removeAll(c)); //$NON-NLS-1$ 718 assertEquals("Wrong size", 1, set.size()); //$NON-NLS-1$ 719 try { 720 set.addAll(c); 721 fail("Should throw UnsupportedOperationException"); //$NON-NLS-1$ 722 } catch (UnsupportedOperationException e) { 723 // Expected 724 } 725 726 enumSizeMap.put(Size.Big, null); 727 assertEquals("Wrong size", 2, set.size()); //$NON-NLS-1$ 728 assertTrue("Should return true", set.retainAll(c)); //$NON-NLS-1$ 729 assertEquals("Wrong size", 1, set.size()); //$NON-NLS-1$ 730 assertFalse("Should return false", set.retainAll(c)); //$NON-NLS-1$ 731 assertEquals(1, set.size()); 732 Object[] array = set.toArray(); 733 assertEquals("Wrong length", 1, array.length); //$NON-NLS-1$ 734 assertEquals("Wrong key", Size.Big, array[0]); //$NON-NLS-1$ 735 736 enumSizeMap = new EnumMap(Size.class); 737 enumSizeMap.put(Size.Middle, 1); 738 enumSizeMap.put(Size.Big, null); 739 set = enumSizeMap.keySet(); 740 c = new ArrayList(); 741 c.add(Color.Blue); 742 assertFalse("Should return false", set.remove(c)); //$NON-NLS-1$ 743 assertEquals("Wrong size", 2, set.size()); //$NON-NLS-1$ 744 assertTrue("Should return true", set.retainAll(c)); //$NON-NLS-1$ 745 assertEquals("Wrong size", 0, set.size()); //$NON-NLS-1$ 746 747 enumSizeMap = new EnumMap(Size.class); 748 enumSizeMap.put(Size.Middle, 1); 749 enumSizeMap.put(Size.Big, null); 750 set = enumSizeMap.keySet(); 751 752 Iterator iter = set.iterator(); 753 Enum enumKey = (Enum) iter.next(); 754 assertTrue("Returned false for contained object", set.contains(enumKey)); //$NON-NLS-1$ 755 enumKey = (Enum) iter.next(); 756 assertTrue("Returned false for contained object", set.contains(enumKey)); //$NON-NLS-1$ 757 758 enumSizeMap.remove(Size.Big); 759 assertFalse("Returned true for uncontained object", set //$NON-NLS-1$ 760 .contains(enumKey)); 761 iter.remove(); 762 try { 763 iter.remove(); 764 fail("Should throw IllegalStateException"); //$NON-NLS-1$ 765 } catch (IllegalStateException e) { 766 // Expected 767 } 768 assertFalse("Returned true for uncontained object", set //$NON-NLS-1$ 769 .contains(enumKey)); 770 771 iter = set.iterator(); 772 enumKey = (Enum) iter.next(); 773 assertTrue("Returned false for contained object", set.contains(enumKey)); //$NON-NLS-1$ 774 enumSizeMap.put(Size.Middle, 3); 775 assertTrue("Returned false for contained object", set.contains(enumKey)); //$NON-NLS-1$ 776 777 enumSizeMap = new EnumMap(Size.class); 778 enumSizeMap.put(Size.Middle, 1); 779 enumSizeMap.put(Size.Big, null); 780 set = enumSizeMap.keySet(); 781 iter = set.iterator(); 782 try { 783 iter.remove(); 784 fail("Should throw IllegalStateException"); //$NON-NLS-1$ 785 } catch (IllegalStateException e) { 786 // Expected 787 } 788 enumKey = (Enum) iter.next(); 789 assertEquals("Wrong key", Size.Middle, enumKey); //$NON-NLS-1$ 790 assertSame("Wrong key", Size.Middle, enumKey); //$NON-NLS-1$ 791 assertFalse("Returned true for unequal object", iter.equals(enumKey)); //$NON-NLS-1$ 792 iter.remove(); 793 assertFalse("Returned true for uncontained object", set //$NON-NLS-1$ 794 .contains(enumKey)); 795 try { 796 iter.remove(); 797 fail("Should throw IllegalStateException"); //$NON-NLS-1$ 798 } catch (IllegalStateException e) { 799 // Expected 800 } 801 802 assertEquals("Wrong size", 1, set.size()); //$NON-NLS-1$ 803 enumKey = (Enum) iter.next(); 804 assertEquals("Wrong key", Size.Big, enumKey); //$NON-NLS-1$ 805 iter.remove(); 806 try { 807 iter.next(); 808 fail("Should throw NoSuchElementException"); //$NON-NLS-1$ 809 } catch (NoSuchElementException e) { 810 // Expected 811 } 812 } 813 814 /** 815 * @tests java.util.EnumMap#get(Object) 816 */ 817 @TestTargetNew( 818 level = TestLevel.COMPLETE, 819 notes = "", 820 method = "get", 821 args = {java.lang.Object.class} 822 ) 823 @SuppressWarnings({ "unchecked", "boxing" }) 824 public void test_getLjava_lang_Object() { 825 EnumMap enumSizeMap = new EnumMap(Size.class); 826 assertNull("Get returned non-null for non mapped key", enumSizeMap //$NON-NLS-1$ 827 .get(Size.Big)); 828 enumSizeMap.put(Size.Big, 1); 829 assertEquals("Get returned incorrect value for given key", 1, //$NON-NLS-1$ 830 enumSizeMap.get(Size.Big)); 831 832 assertNull("Get returned non-null for non mapped key", enumSizeMap //$NON-NLS-1$ 833 .get(Size.Small)); 834 assertNull("Get returned non-null for non existent key", enumSizeMap //$NON-NLS-1$ 835 .get(Color.Red)); 836 assertNull("Get returned non-null for non existent key", enumSizeMap //$NON-NLS-1$ 837 .get(new Integer(1))); 838 assertNull("Get returned non-null for non existent key", enumSizeMap //$NON-NLS-1$ 839 .get(null)); 840 841 EnumMap enumColorMap = new EnumMap<Color, Double>(Color.class); 842 assertNull("Get returned non-null for non mapped key", enumColorMap //$NON-NLS-1$ 843 .get(Color.Green)); 844 enumColorMap.put(Color.Green, 2); 845 assertEquals("Get returned incorrect value for given key", 2, //$NON-NLS-1$ 846 enumColorMap.get(Color.Green)); 847 assertNull("Get returned non-null for non mapped key", enumColorMap //$NON-NLS-1$ 848 .get(Color.Blue)); 849 850 enumColorMap.put(Color.Green, new Double(4)); 851 assertEquals("Get returned incorrect value for given key", //$NON-NLS-1$ 852 new Double(4), enumColorMap.get(Color.Green)); 853 enumColorMap.put(Color.Green, new Integer("3"));//$NON-NLS-1$ 854 assertEquals("Get returned incorrect value for given key", new Integer( //$NON-NLS-1$ 855 "3"), enumColorMap.get(Color.Green));//$NON-NLS-1$ 856 enumColorMap.put(Color.Green, null); 857 assertNull("Can not handle null value", enumColorMap.get(Color.Green)); //$NON-NLS-1$ 858 Float f = new Float("3.4");//$NON-NLS-1$ 859 enumColorMap.put(Color.Green, f); 860 assertSame("Get returned incorrect value for given key", f, //$NON-NLS-1$ 861 enumColorMap.get(Color.Green)); 862 } 863 864 /** 865 * @tests java.util.EnumMap#put(Object,Object) 866 */ 867 @TestTargetNew( 868 level = TestLevel.COMPLETE, 869 notes = "", 870 method = "put", 871 args = {Enum.class, java.lang.Object.class} 872 ) 873 public void test_putLjava_lang_ObjectLjava_lang_Object() { 874 EnumMap enumSizeMap = new EnumMap(Size.class); 875 try { 876 enumSizeMap.put(Color.Red, 2); 877 fail("Expected ClassCastException"); //$NON-NLS-1$ 878 } catch (ClassCastException e) { 879 // Expected 880 } 881 assertNull("Return non-null for non mapped key", enumSizeMap.put( //$NON-NLS-1$ 882 Size.Small, 1)); 883 884 EnumMap enumColorMap = new EnumMap<Color, Double>(Color.class); 885 try { 886 enumColorMap.put(Size.Big, 2); 887 fail("Expected ClassCastException"); //$NON-NLS-1$ 888 } catch (ClassCastException e) { 889 // Expected 890 } 891 try { 892 enumColorMap.put(null, 2); 893 fail("Expected NullPointerException"); //$NON-NLS-1$ 894 } catch (NullPointerException e) { 895 // Expected 896 } 897 assertNull("Return non-null for non mapped key", enumColorMap.put( //$NON-NLS-1$ 898 Color.Green, 2)); 899 assertEquals("Return wrong value", 2, enumColorMap.put(Color.Green, //$NON-NLS-1$ 900 new Double(4))); 901 assertEquals("Return wrong value", new Double(4), enumColorMap.put( //$NON-NLS-1$ 902 Color.Green, new Integer("3")));//$NON-NLS-1$ 903 assertEquals("Return wrong value", new Integer("3"), enumColorMap.put( //$NON-NLS-1$//$NON-NLS-2$ 904 Color.Green, null)); 905 Float f = new Float("3.4");//$NON-NLS-1$ 906 assertNull("Return non-null for non mapped key", enumColorMap.put( //$NON-NLS-1$ 907 Color.Green, f)); 908 assertNull("Return non-null for non mapped key", enumColorMap.put( //$NON-NLS-1$ 909 Color.Blue, 2)); 910 assertEquals("Return wrong value", 2, enumColorMap.put(Color.Blue, //$NON-NLS-1$ 911 new Double(4))); 912 } 913 914 /** 915 * @tests java.util.EnumMap#putAll(Map) 916 */ 917 @TestTargetNew( 918 level = TestLevel.COMPLETE, 919 notes = "", 920 method = "putAll", 921 args = {java.util.Map.class} 922 ) 923 @SuppressWarnings({ "unchecked", "boxing" }) 924 public void test_putAllLjava_util_Map() { 925 EnumMap enumColorMap = new EnumMap<Color, Double>(Color.class); 926 enumColorMap.put(Color.Green, 2); 927 928 EnumMap enumSizeMap = new EnumMap(Size.class); 929 enumColorMap.putAll(enumSizeMap); 930 931 enumSizeMap.put(Size.Big, 1); 932 try { 933 enumColorMap.putAll(enumSizeMap); 934 fail("Expected ClassCastException"); //$NON-NLS-1$ 935 } catch (ClassCastException e) { 936 // Expected 937 } 938 939 EnumMap enumColorMap1 = new EnumMap<Color, Double>(Color.class); 940 enumColorMap1.put(Color.Blue, 3); 941 enumColorMap.putAll(enumColorMap1); 942 assertEquals("Get returned incorrect value for given key", 3, //$NON-NLS-1$ 943 enumColorMap.get(Color.Blue)); 944 assertEquals("Wrong Size", 2, enumColorMap.size()); //$NON-NLS-1$ 945 946 enumColorMap = new EnumMap<Color, Double>(Color.class); 947 948 HashMap hashColorMap = null; 949 try { 950 enumColorMap.putAll(hashColorMap); 951 fail("Expected NullPointerException"); //$NON-NLS-1$ 952 } catch (NullPointerException e) { 953 // Expected 954 } 955 956 hashColorMap = new HashMap(); 957 enumColorMap.putAll(hashColorMap); 958 959 hashColorMap.put(Color.Green, 2); 960 enumColorMap.putAll(hashColorMap); 961 assertEquals("Get returned incorrect value for given key", 2, //$NON-NLS-1$ 962 enumColorMap.get(Color.Green)); 963 assertNull("Get returned non-null for non mapped key", enumColorMap //$NON-NLS-1$ 964 .get(Color.Red)); 965 hashColorMap.put(Color.Red, new Integer(1)); 966 enumColorMap.putAll(hashColorMap); 967 assertEquals("Get returned incorrect value for given key", new Integer(//$NON-NLS-1$ 968 2), enumColorMap.get(Color.Green)); 969 hashColorMap.put(Size.Big, 3); 970 try { 971 enumColorMap.putAll(hashColorMap); 972 fail("Expected ClassCastException"); //$NON-NLS-1$ 973 } catch (ClassCastException e) { 974 // Expected 975 } 976 977 hashColorMap = new HashMap(); 978 hashColorMap.put(new Integer(1), 1); 979 try { 980 enumColorMap.putAll(hashColorMap); 981 fail("Expected ClassCastException"); //$NON-NLS-1$ 982 } catch (ClassCastException e) { 983 // Expected 984 } 985 } 986 987 /** 988 * @tests java.util.EnumMap#remove(Object) 989 */ 990 @TestTargetNew( 991 level = TestLevel.COMPLETE, 992 notes = "", 993 method = "remove", 994 args = {java.lang.Object.class} 995 ) 996 @SuppressWarnings({ "unchecked", "boxing" }) 997 public void test_removeLjava_lang_Object() { 998 EnumMap enumSizeMap = new EnumMap(Size.class); 999 assertNull("Remove of non-mapped key returned non-null", enumSizeMap //$NON-NLS-1$ 1000 .remove(Size.Big)); 1001 enumSizeMap.put(Size.Big, 3); 1002 enumSizeMap.put(Size.Middle, 2); 1003 1004 assertNull("Get returned non-null for non mapped key", enumSizeMap //$NON-NLS-1$ 1005 .get(Size.Small)); 1006 assertEquals("Remove returned incorrect value", 3, enumSizeMap //$NON-NLS-1$ 1007 .remove(Size.Big)); 1008 assertNull("Get returned non-null for non mapped key", enumSizeMap //$NON-NLS-1$ 1009 .get(Size.Big)); 1010 assertNull("Remove of non-mapped key returned non-null", enumSizeMap //$NON-NLS-1$ 1011 .remove(Size.Big)); 1012 assertNull("Remove of non-existent key returned non-null", enumSizeMap //$NON-NLS-1$ 1013 .remove(Color.Red)); 1014 assertNull("Remove of non-existent key returned non-null", enumSizeMap //$NON-NLS-1$ 1015 .remove(new Double(4))); 1016 assertNull("Remove of non-existent key returned non-null", enumSizeMap //$NON-NLS-1$ 1017 .remove(null)); 1018 1019 EnumMap enumColorMap = new EnumMap<Color, Double>(Color.class); 1020 assertNull("Get returned non-null for non mapped key", enumColorMap //$NON-NLS-1$ 1021 .get(Color.Green)); 1022 enumColorMap.put(Color.Green, new Double(4)); 1023 assertEquals("Remove returned incorrect value", new Double(4), //$NON-NLS-1$ 1024 enumColorMap.remove(Color.Green)); 1025 assertNull("Get returned non-null for non mapped key", enumColorMap //$NON-NLS-1$ 1026 .get(Color.Green)); 1027 enumColorMap.put(Color.Green, null); 1028 assertNull("Can not handle null value", enumColorMap //$NON-NLS-1$ 1029 .remove(Color.Green)); 1030 assertNull("Get returned non-null for non mapped key", enumColorMap //$NON-NLS-1$ 1031 .get(Color.Green)); 1032 } 1033 1034 /** 1035 * @tests java.util.EnumMap#size() 1036 */ 1037 @TestTargetNew( 1038 level = TestLevel.COMPLETE, 1039 notes = "", 1040 method = "size", 1041 args = {} 1042 ) 1043 @SuppressWarnings({ "unchecked", "boxing" }) 1044 public void test_size() { 1045 EnumMap enumSizeMap = new EnumMap(Size.class); 1046 assertEquals("Wrong size", 0, enumSizeMap.size()); //$NON-NLS-1$ 1047 enumSizeMap.put(Size.Small, 1); 1048 assertEquals("Wrong size", 1, enumSizeMap.size()); //$NON-NLS-1$ 1049 enumSizeMap.put(Size.Small, 0); 1050 assertEquals("Wrong size", 1, enumSizeMap.size()); //$NON-NLS-1$ 1051 try { 1052 enumSizeMap.put(Color.Red, 2); 1053 fail("Expected ClassCastException"); //$NON-NLS-1$ 1054 } catch (ClassCastException e) { 1055 // Expected 1056 } 1057 assertEquals("Wrong size", 1, enumSizeMap.size()); //$NON-NLS-1$ 1058 1059 enumSizeMap.put(Size.Middle, null); 1060 assertEquals("Wrong size", 2, enumSizeMap.size()); //$NON-NLS-1$ 1061 enumSizeMap.remove(Size.Big); 1062 assertEquals("Wrong size", 2, enumSizeMap.size()); //$NON-NLS-1$ 1063 enumSizeMap.remove(Size.Middle); 1064 assertEquals("Wrong size", 1, enumSizeMap.size()); //$NON-NLS-1$ 1065 enumSizeMap.remove(Color.Green); 1066 assertEquals("Wrong size", 1, enumSizeMap.size()); //$NON-NLS-1$ 1067 1068 EnumMap enumColorMap = new EnumMap<Color, Double>(Color.class); 1069 enumColorMap.put(Color.Green, 2); 1070 assertEquals("Wrong size", 1, enumColorMap.size()); //$NON-NLS-1$ 1071 enumColorMap.remove(Color.Green); 1072 assertEquals("Wrong size", 0, enumColorMap.size()); //$NON-NLS-1$ 1073 1074 EnumMap enumEmptyMap = new EnumMap<Empty, Double>(Empty.class); 1075 assertEquals("Wrong size", 0, enumEmptyMap.size()); //$NON-NLS-1$ 1076 } 1077 1078 /** 1079 * @tests java.util.EnumMap#values() 1080 */ 1081 @TestTargetNew( 1082 level = TestLevel.COMPLETE, 1083 notes = "", 1084 method = "values", 1085 args = {} 1086 ) 1087 @SuppressWarnings( { "unchecked", "boxing" }) 1088 public void test_values() { 1089 EnumMap enumColorMap = new EnumMap<Color, Double>(Color.class); 1090 enumColorMap.put(Color.Red, 1); 1091 enumColorMap.put(Color.Blue, null); 1092 Collection collection = enumColorMap.values(); 1093 1094 Collection collection1 = enumColorMap.values(); 1095 assertSame("Should be same", collection1, collection); //$NON-NLS-1$ 1096 try { 1097 collection.add(new Integer(1)); 1098 fail("Should throw UnsupportedOperationException"); //$NON-NLS-1$ 1099 } catch (UnsupportedOperationException e) { 1100 // Expected 1101 } 1102 1103 assertTrue("Returned false for contained object", collection//$NON-NLS-1$ 1104 .contains(1)); 1105 assertTrue("Returned false for contained object", collection//$NON-NLS-1$ 1106 .contains(null)); 1107 assertFalse("Returned true for uncontained object", collection //$NON-NLS-1$ 1108 .contains(2)); 1109 1110 assertTrue("Returned false when the object can be removed", collection //$NON-NLS-1$ 1111 .remove(null)); 1112 assertFalse("Returned true for uncontained object", collection //$NON-NLS-1$ 1113 .contains(null)); 1114 assertFalse("Returned true when the object can not be removed", //$NON-NLS-1$ 1115 collection.remove(null)); 1116 1117 // The set is backed by the map so changes to one are reflected by the 1118 // other. 1119 enumColorMap.put(Color.Blue, 3); 1120 assertTrue("Returned false for contained object", collection//$NON-NLS-1$ 1121 .contains(3)); 1122 enumColorMap.remove(Color.Blue); 1123 assertFalse("Returned true for uncontained object", collection//$NON-NLS-1$ 1124 .contains(3)); 1125 1126 assertEquals("Wrong size", 1, collection.size()); //$NON-NLS-1$ 1127 collection.clear(); 1128 assertEquals("Wrong size", 0, collection.size()); //$NON-NLS-1$ 1129 1130 enumColorMap = new EnumMap<Color, Double>(Color.class); 1131 enumColorMap.put(Color.Red, 1); 1132 enumColorMap.put(Color.Blue, null); 1133 collection = enumColorMap.values(); 1134 Collection c = new ArrayList(); 1135 c.add(new Integer(1)); 1136 assertTrue("Should return true", collection.containsAll(c)); //$NON-NLS-1$ 1137 c.add(new Double(3.4)); 1138 assertFalse("Should return false", collection.containsAll(c)); //$NON-NLS-1$ 1139 assertTrue("Should return true", collection.removeAll(c)); //$NON-NLS-1$ 1140 assertEquals("Wrong size", 1, collection.size()); //$NON-NLS-1$ 1141 assertFalse("Should return false", collection.removeAll(c)); //$NON-NLS-1$ 1142 assertEquals("Wrong size", 1, collection.size()); //$NON-NLS-1$ 1143 try { 1144 collection.addAll(c); 1145 fail("Should throw UnsupportedOperationException"); //$NON-NLS-1$ 1146 } catch (UnsupportedOperationException e) { 1147 // Expected 1148 } 1149 1150 enumColorMap.put(Color.Red, 1); 1151 assertEquals("Wrong size", 2, collection.size()); //$NON-NLS-1$ 1152 assertTrue("Should return true", collection.retainAll(c)); //$NON-NLS-1$ 1153 assertEquals("Wrong size", 1, collection.size()); //$NON-NLS-1$ 1154 assertFalse("Should return false", collection.retainAll(c)); //$NON-NLS-1$ 1155 assertEquals(1, collection.size()); 1156 Object[] array = collection.toArray(); 1157 assertEquals("Wrong length", 1, array.length); //$NON-NLS-1$ 1158 assertEquals("Wrong key", 1, array[0]); //$NON-NLS-1$ 1159 1160 enumColorMap = new EnumMap<Color, Double>(Color.class); 1161 enumColorMap.put(Color.Red, 1); 1162 enumColorMap.put(Color.Blue, null); 1163 collection = enumColorMap.values(); 1164 1165 assertEquals("Wrong size", 2, collection.size()); //$NON-NLS-1$ 1166 assertFalse("Returned true when the object can not be removed", //$NON-NLS-1$ 1167 collection.remove(new Integer("10"))); //$NON-NLS-1$ 1168 1169 Iterator iter = enumColorMap.values().iterator(); 1170 Object value = iter.next(); 1171 assertTrue("Returned false for contained object", collection //$NON-NLS-1$ 1172 .contains(value)); 1173 value = iter.next(); 1174 assertTrue("Returned false for contained object", collection //$NON-NLS-1$ 1175 .contains(value)); 1176 1177 enumColorMap.put(Color.Green, 1); 1178 enumColorMap.remove(Color.Blue); 1179 assertFalse("Returned true for uncontained object", collection //$NON-NLS-1$ 1180 .contains(value)); 1181 iter.remove(); 1182 try { 1183 iter.remove(); 1184 fail("Should throw IllegalStateException"); //$NON-NLS-1$ 1185 } catch (IllegalStateException e) { 1186 // Expected 1187 } 1188 assertFalse("Returned true for uncontained object", collection //$NON-NLS-1$ 1189 .contains(value)); 1190 1191 iter = enumColorMap.values().iterator(); 1192 value = iter.next(); 1193 assertTrue("Returned false for contained object", collection //$NON-NLS-1$ 1194 .contains(value)); 1195 enumColorMap.put(Color.Green, 3); 1196 assertTrue("Returned false for contained object", collection //$NON-NLS-1$ 1197 .contains(value)); 1198 assertTrue("Returned false for contained object", collection //$NON-NLS-1$ 1199 .remove(new Integer("1"))); //$NON-NLS-1$ 1200 assertEquals("Wrong size", 1, collection.size()); //$NON-NLS-1$ 1201 collection.clear(); 1202 assertEquals("Wrong size", 0, collection.size()); //$NON-NLS-1$ 1203 1204 enumColorMap = new EnumMap<Color, Double>(Color.class); 1205 Integer integer1 = new Integer(1); 1206 enumColorMap.put(Color.Green, integer1); 1207 enumColorMap.put(Color.Blue, null); 1208 collection = enumColorMap.values(); 1209 iter = enumColorMap.values().iterator(); 1210 try { 1211 iter.remove(); 1212 fail("Should throw IllegalStateException"); //$NON-NLS-1$ 1213 } catch (IllegalStateException e) { 1214 // Expected 1215 } 1216 value = iter.next(); 1217 assertEquals("Wrong value", integer1, value); //$NON-NLS-1$ 1218 assertSame("Wrong value", integer1, value); //$NON-NLS-1$ 1219 assertFalse("Returned true for unequal object", iter.equals(value)); //$NON-NLS-1$ 1220 iter.remove(); 1221 assertFalse("Returned true for unequal object", iter.equals(value)); //$NON-NLS-1$ 1222 try { 1223 iter.remove(); 1224 fail("Should throw IllegalStateException"); //$NON-NLS-1$ 1225 } catch (IllegalStateException e) { 1226 // Expected 1227 } 1228 assertEquals("Wrong size", 1, collection.size()); //$NON-NLS-1$ 1229 value = iter.next(); 1230 assertFalse("Returned true for unequal object", iter.equals(value)); //$NON-NLS-1$ 1231 iter.remove(); 1232 try { 1233 iter.next(); 1234 fail("Should throw NoSuchElementException"); //$NON-NLS-1$ 1235 } catch (NoSuchElementException e) { 1236 // Expected 1237 } 1238 } 1239 1240 /** 1241 * @tests serialization/deserialization. 1242 */ 1243 @TestTargetNew( 1244 level = TestLevel.COMPLETE, 1245 notes = "Verifies serialization/deserialization compatibility.", 1246 method = "!SerializationSelf", 1247 args = {} 1248 ) 1249 @SuppressWarnings({ "unchecked", "boxing" }) 1250 public void testSerializationSelf() throws Exception { 1251 EnumMap enumColorMap = new EnumMap<Color, Double>(Color.class); 1252 enumColorMap.put(Color.Blue, 3); 1253 SerializationTest.verifySelf(enumColorMap); 1254 } 1255 1256 /** 1257 * @tests serialization/deserialization compatibility with RI. 1258 */ 1259 @TestTargetNew( 1260 level = TestLevel.COMPLETE, 1261 notes = "Verifies serialization/deserialization compatibility.", 1262 method = "!SerializationGolden", 1263 args = {} 1264 ) 1265 @SuppressWarnings({ "unchecked", "boxing" }) 1266 public void testSerializationCompatibility() throws Exception { 1267 EnumMap enumColorMap = new EnumMap<Color, Double>(Color.class); 1268 enumColorMap.put(Color.Red, 1); 1269 enumColorMap.put(Color.Blue, 3); 1270 SerializationTest.verifyGolden(this, enumColorMap); 1271 } 1272 1273 /** 1274 * Sets up the fixture. 1275 */ 1276 @Override 1277 protected void setUp() throws Exception { 1278 super.setUp(); 1279 } 1280 1281 /** 1282 * Tears down the fixture. 1283 */ 1284 @Override 1285 protected void tearDown() throws Exception{ 1286 super.tearDown(); 1287 } 1288} 1289