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