1 5 package com.tctest; 6 7 import org.apache.commons.collections.FastHashMap; 8 9 import com.tc.exception.TCNonPortableObjectError; 10 import com.tc.object.config.ConfigVisitor; 11 import com.tc.object.config.DSOClientConfigHelper; 12 import com.tc.object.tx.ReadOnlyException; 13 import com.tc.object.tx.UnlockedSharedObjectException; 14 import com.tc.simulator.app.ApplicationConfig; 15 import com.tc.simulator.app.ErrorContext; 16 import com.tc.simulator.listener.ListenerProvider; 17 import com.tc.util.Assert; 18 19 import gnu.trove.THashMap; 20 import gnu.trove.TObjectFunction; 21 import gnu.trove.TObjectHash; 22 23 import java.io.ByteArrayInputStream ; 24 import java.io.ByteArrayOutputStream ; 25 import java.io.IOException ; 26 import java.lang.reflect.Field ; 27 import java.lang.reflect.InvocationTargetException ; 28 import java.lang.reflect.Method ; 29 import java.util.ArrayList ; 30 import java.util.Collection ; 31 import java.util.Collections ; 32 import java.util.Comparator ; 33 import java.util.HashMap ; 34 import java.util.HashSet ; 35 import java.util.Hashtable ; 36 import java.util.Iterator ; 37 import java.util.LinkedHashMap ; 38 import java.util.List ; 39 import java.util.Map ; 40 import java.util.Properties ; 41 import java.util.Set ; 42 import java.util.TreeMap ; 43 import java.util.Map.Entry; 44 45 public class GenericMapTestApp extends GenericTestApp { 46 47 private final Map nonSharedArrayMap = new HashMap(); 48 49 public GenericMapTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 50 super(appId, cfg, listenerProvider, Map.class); 51 } 52 53 protected Object getTestObject(String test) { 54 List maps = (List ) sharedMap.get("maps"); 55 56 Assert.assertEquals(24, maps.size()); 59 60 return maps.iterator(); 61 } 62 63 protected void setupTestObject(String test) { 64 List maps = new ArrayList (); 65 66 maps.add(new HashMap()); 67 maps.add(new Hashtable ()); 68 maps.add(new TreeMap (new NullTolerantComparator())); 69 maps.add(new LinkedHashMap ()); 70 maps.add(new THashMap()); 71 maps.add(new FastHashMap()); 72 FastHashMap fm = new FastHashMap(); 73 fm.setFast(true); 74 maps.add(fm); 75 maps.add(new Properties ()); 76 maps.add(new MyHashMap(11)); 77 maps.add(new MyHashMap(new HashMap())); 78 maps.add(new MyHashMap2()); 79 maps.add(new MyHashMap3(0)); 80 maps.add(new MyTreeMap(new NullTolerantComparator())); 81 maps.add(new MyTreeMap2(new NullTolerantComparator())); 82 maps.add(new MyHashtable()); 83 maps.add(new MyHashtable2()); 84 maps.add(new MyLinkedHashMap()); 85 maps.add(new MyLinkedHashMap2()); 86 maps.add(new MyLinkedHashMap3(true)); 87 maps.add(new MyTHashMap()); 88 maps.add(new MyFastHashMap()); 89 maps.add(new MyProperties()); 90 maps.add(new MyProperties2()); 91 maps.add(new MyProperties3()); 92 93 96 109 110 sharedMap.put("maps", maps); 111 nonSharedArrayMap.put("arrayforHashMap", new Object [4]); 112 nonSharedArrayMap.put("arrayforHashtable", new Object [4]); 113 sharedMap.put("arrayforTreeMap", new Object [4]); 114 sharedMap.put("arrayforTreeMap2", new Object [4]); 115 sharedMap.put("arrayforTHashMap", new Object [4]); 116 nonSharedArrayMap.put("arrayforLinkedHashMap", new Object [4]); 117 nonSharedArrayMap.put("arrayforFastHashMap", new Object [4]); 118 nonSharedArrayMap.put("arrayforProperties", new Object [4]); 119 nonSharedArrayMap.put("arrayforFastHashMapWithFast", new Object [4]); 120 nonSharedArrayMap.put("arrayforMyHashMap", new Object [4]); 121 nonSharedArrayMap.put("arrayforMyHashMap2", new Object [4]); 122 sharedMap.put("arrayforMyTreeMap", new Object [4]); 123 nonSharedArrayMap.put("arrayforMyHashtable", new Object [4]); 124 nonSharedArrayMap.put("arrayforMyHashtable2", new Object [4]); 125 nonSharedArrayMap.put("arrayforMyLinkedHashMap", new Object [4]); 126 nonSharedArrayMap.put("arrayforMyLinkedHashMap2", new Object [4]); 127 nonSharedArrayMap.put("arrayforMyLinkedHashMap3", new Object [4]); 128 sharedMap.put("arrayforMyTHashMap", new Object [4]); 129 nonSharedArrayMap.put("arrayforMyFastHashMap", new Object [4]); 130 nonSharedArrayMap.put("arrayforMyProperties", new Object [4]); 131 nonSharedArrayMap.put("arrayforMyProperties2", new Object [4]); 132 nonSharedArrayMap.put("arrayforMyProperties3", new Object [4]); 133 } 134 135 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 136 String testClass = GenericMapTestApp.class.getName(); 137 config.addNewModule("clustered-commons-collections-3.1", "1.0.0"); 138 config.getOrCreateSpec(testClass); 139 String methodExpression = "* " + testClass + "*.*(..)"; 140 config.addWriteAutolock(methodExpression); 141 String readOnlyMethodExpression = "* " + testClass + "*.*ReadOnly*(..)"; 142 config.addReadAutolock(readOnlyMethodExpression); 143 config.addIncludePattern(testClass + "$*"); 144 config.addIncludePattern(Key.class.getName()); 145 config.addIncludePattern(NullTolerantComparator.class.getName()); 146 config.addIncludePattern(SimpleEntry.class.getName()); 147 config.addExcludePattern(MyNonPortableObject.class.getName()); 148 } 149 150 void testBasicUnSynchronizedPut(Map map, boolean validate) { 151 if (map instanceof Hashtable ) { return; } 152 if (map instanceof FastHashMap) { return; } 153 154 if (validate) { 155 assertEmptyMap(map); 156 } else { 157 try { 158 map.put("January", "Jan"); 159 throw new AssertionError ("Should have thrown a UnlockedSharedObjectException"); 160 } catch (UnlockedSharedObjectException use) { 161 } 163 } 164 } 165 166 void testBasicPut(Map map, boolean validate) { 167 if (validate) { 168 assertSingleMapping(map, "timmy", "teck"); 169 if (map instanceof MyHashMap) { 170 Assert.assertEquals("timmy", ((MyHashMap) map).getKey()); 171 Assert.assertEquals("teck", ((MyHashMap) map).getValue()); 172 } else if (map instanceof MyTreeMap2) { 173 Assert.assertEquals("timmy", ((MyTreeMap2) map).getKey()); 174 Assert.assertEquals("teck", ((MyTreeMap2) map).getValue()); 175 } else if (map instanceof MyLinkedHashMap2) { 176 Assert.assertEquals("timmy", ((MyLinkedHashMap2) map).getKey()); 177 Assert.assertEquals("teck", ((MyLinkedHashMap2) map).getValue()); 178 } else if (map instanceof MyProperties) { 179 Assert.assertEquals("timmy", ((MyProperties) map).getKey()); 180 Assert.assertEquals("teck", ((MyProperties) map).getValue()); 181 } 182 } else { 183 synchronized (map) { 184 Object prev = map.put("timmy", "teck"); 185 Assert.assertNull(prev); 186 } 187 } 188 } 189 190 void testBasicPutAll(Map map, boolean validate) { 191 Map toAdd = getOrderSensitiveMappings(); 192 193 if (validate) { 194 assertMappings(toAdd, map); 195 } else { 196 synchronized (map) { 197 map.putAll(toAdd); 198 } 199 } 200 } 201 202 void testPutNullKey(Map map, boolean validate) { 203 if (!allowsNull(map)) { return; } 204 if (validate) { 205 assertSingleMapping(map, null, "value"); 206 } else { 207 synchronized (map) { 208 Object prev = map.put(null, "value"); 209 Assert.assertNull(prev); 210 } 211 } 212 } 213 214 void testReplaceNullKey(Map map, boolean validate) { 215 if (!allowsNull(map)) { return; } 216 if (validate) { 217 assertSingleMapping(map, null, "value2"); 218 } else { 219 synchronized (map) { 220 Object prev = map.put(null, "value"); 221 Assert.assertNull(prev); 222 } 223 224 synchronized (map) { 225 Object prev = map.put(null, "value2"); 226 Assert.assertEquals("value", prev); 227 } 228 } 229 } 230 231 void testBasicReplace(Map map, boolean validate) { 232 if (validate) { 233 assertSingleMapping(map, "key", "value2"); 234 } else { 235 synchronized (map) { 236 Object prev = map.put("key", "value"); 237 Assert.assertNull(prev); 238 } 239 240 synchronized (map) { 241 Object prev = map.put("key", "value2"); 242 Assert.assertEquals("value", prev); 243 } 244 } 245 } 246 247 void testPutNullValue(Map map, boolean validate) { 248 if (!allowsNull(map)) { return; } 249 if (validate) { 250 assertSingleMapping(map, "key", null); 251 } else { 252 synchronized (map) { 253 Object prev = map.put("key", null); 254 Assert.assertNull(prev); 255 } 256 } 257 } 258 259 void testReplaceKeyRetention(Map map, boolean validate) { 260 if (validate) { 261 assertSingleMapping(map, new Key("** doesn't matter **", "key"), "value2"); 262 263 Assert.assertEquals(1, map.keySet().size()); 264 Key retainedKey = (Key) map.keySet().iterator().next(); 265 266 String expect = (map instanceof THashMap) ? "id2" : "id1"; 267 Assert.assertEquals(expect, retainedKey.id); 268 } else { 269 synchronized (map) { 270 Object prev = map.put(new Key("id1", "key"), "value"); 271 Assert.assertNull(prev); 272 } 273 synchronized (map) { 274 Object prev = map.put(new Key("id2", "key"), "value2"); 275 Assert.assertEquals("value", prev); 276 } 277 } 278 } 279 280 void testBasicSetProperty(Map map, boolean validate) { 281 if (!(map instanceof Properties )) { return; } 282 283 if (validate) { 284 assertSingleMapping(map, "timmy", "teck"); 285 } else { 286 synchronized (map) { 287 Object previous = ((Properties ) map).setProperty("timmy", "teck"); 288 Assert.assertNull(previous); 289 } 290 } 291 } 292 293 void testBasicGetProperty(Map map, boolean validate) { 294 if (!(map instanceof Properties )) { return; } 295 296 if (validate) { 297 Assert.assertEquals("value", ((Properties ) map).getProperty("key")); 298 Assert.assertEquals("defaultValue", ((Properties ) map).getProperty("nonsense", "defaultValue")); 299 Assert.assertEquals("value", ((Properties ) map).getProperty("key", "defaultValue")); 300 } else { 301 synchronized (map) { 302 ((Properties ) map).setProperty("key", "value"); 303 } 304 } 305 } 306 307 void testBasicLoad(Map map, boolean validate) { 308 if (!(map instanceof Properties )) { return; } 309 310 if (validate) { 311 Map expectedMap = new Properties (); 312 expectedMap.put("key1", "val1"); 313 expectedMap.put("key2", "val2"); 314 expectedMap.put("key3", "val3"); 315 assertMappings(expectedMap, map); 316 } else { 317 synchronized (map) { 318 Properties data = new Properties (); 319 data.setProperty("key1", "val1"); 320 data.setProperty("key2", "val2"); 321 data.setProperty("key3", "val3"); 322 ByteArrayOutputStream outputStream = new ByteArrayOutputStream (); 323 try { 324 data.store(outputStream, null); 325 } catch (IOException ioe) { 326 Assert.fail(); 327 } 328 ByteArrayInputStream inputStream = new ByteArrayInputStream (outputStream.toByteArray()); 329 try { 330 ((Properties ) map).load(inputStream); 331 } catch (IOException ioe) { 332 Assert.fail(); 333 } 334 } 335 } 336 } 337 338 348 349 void testKeySetClear(Map map, boolean validate) { 350 if (validate) { 351 assertEmptyMap(map); 352 } else { 353 addMappings(map, 42); 354 synchronized (map) { 355 map.keySet().clear(); 356 } 357 } 358 } 359 360 void testKeySetIteratorRemove(Map map, boolean validate) { 361 if (validate) { 362 Map expect = getOrderSensitiveMappings(); 363 expect.remove("February"); 364 expect.remove("March"); 365 assertMappings(expect, map); 366 } else { 367 synchronized (map) { 368 map.putAll(getOrderSensitiveMappings()); 369 } 370 371 synchronized (map) { 372 for (Iterator i = map.keySet().iterator(); i.hasNext();) { 373 Object key = i.next(); 374 if ("February".equals(key) || "March".equals(key)) { 375 i.remove(); 376 } 377 } 378 } 379 } 380 } 381 382 void testKeySetIteratorRemoveNull(Map map, boolean validate) { 383 if (!allowsNull(map)) { return; } 384 385 if (validate) { 386 assertSingleMapping(map, "key1", "value1"); 387 } else { 388 synchronized (map) { 389 map.put("key1", "value1"); 390 map.put(null, "value for null key"); 391 } 392 393 synchronized (map) { 394 for (Iterator i = map.keySet().iterator(); i.hasNext();) { 395 Object key = i.next(); 396 if (!"key1".equals(key)) { 397 i.remove(); 398 } 399 } 400 } 401 } 402 } 403 404 void testKeySetRemove(Map map, boolean validate) { 405 if (validate) { 406 Map expect = getOrderSensitiveMappings(); 407 expect.remove("February"); 408 expect.remove("March"); 409 assertMappings(expect, map); 410 } else { 411 synchronized (map) { 412 map.putAll(getOrderSensitiveMappings()); 413 } 414 415 synchronized (map) { 416 Set keys = map.keySet(); 417 boolean removed; 418 removed = keys.remove("February"); 419 Assert.assertTrue(removed); 420 removed = keys.remove("March"); 421 Assert.assertTrue(removed); 422 removed = keys.remove("key4"); 423 Assert.assertFalse(removed); 424 } 425 } 426 } 427 428 void testBasicGet(Map map, boolean validate) { 429 if (validate) { 430 try { 431 if (isAccessOrderedLinkedHashMap(map)) { 432 synchronized (map) { 433 Assert.assertEquals("value", map.get("key")); 434 } 435 } else { 436 Assert.assertEquals("value", map.get("key")); 437 } 438 if (map instanceof MyHashMap) { 439 Assert.assertEquals("value", ((MyHashMap) map).getObject("key")); 440 } 441 } catch (Throwable t) { 442 System.err.println("*******" + Thread.currentThread().getName() + ", map: " + map + "********"); 443 throw new RuntimeException (t); 444 } 445 } else { 446 synchronized (map) { 447 map.put("key", "value"); 448 } 449 } 450 } 451 452 void testBasicRemove(Map map, boolean validate) { 453 if (validate) { 454 assertEmptyMap(map); 455 } else { 456 synchronized (map) { 457 Object prev = map.put("key", "value"); 458 Assert.assertNull(prev); 459 } 460 synchronized (map) { 461 Object prev = map.remove("key"); 462 Assert.assertEquals("value", prev); 463 } 464 synchronized (map) { 465 Object prev = map.remove("key"); 466 Assert.assertNull(prev); 467 } 468 } 469 } 470 471 void testRemoveNullKey(Map map, boolean validate) { 472 if (!allowsNull(map)) { return; } 473 if (validate) { 474 assertEmptyMap(map); 475 } else { 476 synchronized (map) { 477 map.put(null, "value"); 478 } 479 synchronized (map) { 480 Object removed = map.remove(null); 481 Assert.assertEquals("value", removed); 482 } 483 } 484 } 485 486 void testClearNonEmpty(Map map, boolean validate) { 487 if (validate) { 488 assertEmptyMap(map); 489 } else { 490 addMappings(map, 1); 491 492 Assert.assertFalse(map.isEmpty()); 493 494 synchronized (map) { 495 map.clear(); 496 } 497 } 498 } 499 500 void testClearEmpty(Map map, boolean validate) { 501 if (validate) { 502 assertEmptyMap(map); 503 } else { 504 Assert.assertTrue(map.isEmpty()); 505 synchronized (map) { 506 map.clear(); 507 } 508 } 509 } 510 511 void testValuesClear(Map map, boolean validate) { 512 if (validate) { 513 assertEmptyMap(map); 514 } else { 515 addMappings(map, 23); 516 517 synchronized (map) { 518 map.values().clear(); 519 } 520 } 521 } 522 523 void testValuesRemove(Map map, boolean validate) { 524 if (validate) { 525 Map expect = getOrderSensitiveMappings(); 526 expect.remove("March"); 527 assertMappings(expect, map); 528 } else { 529 synchronized (map) { 530 map.putAll(getOrderSensitiveMappings()); 531 } 532 533 synchronized (map) { 534 map.values().remove("Mar"); 535 } 536 } 537 } 538 539 void testValuesDuplicateRemove(Map map, boolean validate) { 540 if (validate) { 541 if (map instanceof THashMap) { 542 assertEmptyMap(map); 544 } else { 545 Object expect = sharedMap.get("expect" + map.getClass().getName()); 546 assertSingleMapping(map, expect, "value"); 547 } 548 } else { 549 synchronized (map) { 550 map.put("key1", "value"); 551 map.put("key2", "value"); 552 } 553 554 synchronized (map) { 555 boolean removed = map.values().remove("value"); 556 Assert.assertTrue(removed); 557 String expectedKey = map.containsKey("key1") ? "key1" : "key2"; 558 sharedMap.put("expect" + map.getClass().getName(), expectedKey); 559 } 560 } 561 } 562 563 void testValuesRemoveNull(Map map, boolean validate) { 564 if (!allowsNull(map)) { return; } 565 if (validate) { 566 assertSingleMapping(map, "key1", "value1"); 567 } else { 568 synchronized (map) { 569 map.put("key1", "value1"); 570 map.put("key for null value", null); 571 map.put(null, "value for null key"); 572 } 573 574 synchronized (map) { 575 map.values().remove(null); 576 map.values().remove("value for null key"); 577 } 578 } 579 } 580 581 void testValuesRemoveAll(Map map, boolean validate) { 582 if (validate) { 583 Map expect = getOrderSensitiveMappings(); 584 expect.remove("February"); 585 expect.remove("March"); 586 assertMappings(expect, map); 587 } else { 588 synchronized (map) { 589 map.putAll(getOrderSensitiveMappings()); 590 } 591 592 Set toRemove = new HashSet (); 593 toRemove.add("Feb"); 594 toRemove.add("Mar"); 595 596 synchronized (map) { 597 map.values().removeAll(toRemove); 598 } 599 } 600 } 601 602 void testValuesRetainAll(Map map, boolean validate) { 603 if (validate) { 604 assertSingleMapping(map, "March", "Mar"); 605 } else { 606 synchronized (map) { 607 map.putAll(getOrderSensitiveMappings()); 608 } 609 610 Set toRetain = new HashSet (); 611 toRetain.add("Mar"); 612 613 synchronized (map) { 614 map.values().retainAll(toRetain); 615 } 616 } 617 } 618 619 void testValuesIteratorRemove(Map map, boolean validate) { 620 if (validate) { 621 Map expect = getOrderSensitiveMappings(); 622 expect.remove("March"); 623 assertMappings(expect, map); 624 } else { 625 synchronized (map) { 626 map.putAll(getOrderSensitiveMappings()); 627 } 628 629 synchronized (map) { 630 for (Iterator i = map.values().iterator(); i.hasNext();) { 631 Object value = i.next(); 632 if ("Mar".equals(value)) { 633 i.remove(); 634 } 635 } 636 } 637 } 638 } 639 640 void testValuesIteratorRemoveNull(Map map, boolean validate) { 641 if (!allowsNull(map)) { return; } 642 if (validate) { 643 assertSingleMapping(map, "key1", "value1"); 644 } else { 645 synchronized (map) { 646 map.put("key1", "value1"); 647 map.put(null, "value for null key"); 648 map.put("key for null value", null); 649 } 650 651 synchronized (map) { 652 for (Iterator i = map.values().iterator(); i.hasNext();) { 653 Object value = i.next(); 654 if (!"value1".equals(value)) { 655 i.remove(); 656 } 657 } 658 } 659 } 660 } 661 662 void testValuesIterator(Map map, boolean validate) { 663 if (validate) { 664 int count = 0; 665 for (Iterator i = map.values().iterator(); i.hasNext();) { 666 count++; 667 Assert.assertEquals("teck", i.next()); 668 } 669 Assert.assertEquals(1, count); 670 } else { 671 synchronized (map) { 672 map.put("timmy", "teck"); 673 } 674 } 675 } 676 677 void testKeySetIterator(Map map, boolean validate) { 678 if (validate) { 679 int count = 0; 680 for (Iterator i = map.keySet().iterator(); i.hasNext();) { 681 count++; 682 Assert.assertEquals("timmy", i.next()); 683 } 684 Assert.assertEquals(1, count); 685 } else { 686 synchronized (map) { 687 map.put("timmy", "teck"); 688 } 689 } 690 } 691 692 void testEntrySetIterator(Map map, boolean validate) { 693 if (validate) { 694 int count = 0; 695 for (Iterator i = map.entrySet().iterator(); i.hasNext();) { 696 count++; 697 Map.Entry entry = (Entry) i.next(); 698 Assert.assertEquals("timmy", entry.getKey()); 699 Assert.assertEquals("teck", entry.getValue()); 700 } 701 Assert.assertEquals(1, count); 702 } else { 703 synchronized (map) { 704 map.put("timmy", "teck"); 705 } 706 } 707 } 708 709 void testEntrySetAdd(Map map, boolean validate) { 710 } 713 714 void testEntrySetValue(Map map, boolean validate) { 715 if (validate) { 716 assertSingleMapping(map, "key", "value2"); 717 } else { 718 synchronized (map) { 719 map.put("key", "value1"); 720 } 721 722 synchronized (map) { 723 Set set = map.entrySet(); 724 for (Iterator i = set.iterator(); i.hasNext();) { 725 Entry entry = (Entry) i.next(); 726 Object prev = entry.setValue("value2"); 727 Assert.assertEquals("value1", prev); 728 } 729 } 730 } 731 } 732 733 void testEntrySetValueNull(Map map, boolean validate) { 734 if (!allowsNull(map)) return; 735 736 if (validate) { 737 assertSingleMapping(map, "key", null); 738 } else { 739 synchronized (map) { 740 map.put("key", "value1"); 741 } 742 743 synchronized (map) { 744 Set set = map.entrySet(); 745 for (Iterator i = set.iterator(); i.hasNext();) { 746 Entry entry = (Entry) i.next(); 747 Object prev = entry.setValue(null); 748 Assert.assertEquals("value1", prev); 749 } 750 } 751 } 752 } 753 754 void testEntrySetRemoveNull(Map map, boolean validate) { 755 if (!allowsNull(map)) return; 756 757 if (validate) { 758 assertEmptyMap(map); 759 } else { 760 synchronized (map) { 761 map.put("key", null); 762 map.put(null, "value"); 763 } 764 765 addMappings(map, 3); 766 767 synchronized (map) { 768 Set set = map.entrySet(); 769 for (Iterator i = set.iterator(); i.hasNext();) { 770 i.next(); 771 i.remove(); 772 } 773 } 774 } 775 } 776 777 void testEntrySetRemove(Map map, boolean validate) { 778 if (validate) { 779 Map expect = getOrderSensitiveMappings(); 780 expect.remove("March"); 781 assertMappings(expect, map); 782 } else { 783 synchronized (map) { 784 map.putAll(getOrderSensitiveMappings()); 785 } 786 787 synchronized (map) { 788 map.entrySet().remove(new SimpleEntry("March", "Mar")); 789 } 790 } 791 } 792 793 void testEntrySetRemoveAll(Map map, boolean validate) { 794 if (validate) { 795 Map expect = new HashMap(); 796 expect.put("die, die", "die, my darling"); 797 expect.put("die, die, die", "no die"); 798 assertMappings(expect, map); 799 } else { 800 synchronized (map) { 801 map.put("die", "another day"); 802 map.put("die, die", "die, my darling"); 803 map.put("die, die, die", "no die"); 804 map.put("on the last day of your life, don't forget to", "die"); 805 } 806 807 Set removeSet = new HashSet (2); 808 removeSet.add(new SimpleEntry("die", "another day")); 809 removeSet.add(new SimpleEntry("on the last day of your life, don't forget to", "die")); 810 811 synchronized (map) { 812 map.entrySet().removeAll(removeSet); 813 } 814 } 815 } 816 817 void testKeySetRemoveAll(Map map, boolean validate) { 818 if (validate) { 819 Map expect = getOrderSensitiveMappings(); 820 expect.remove("February"); 821 expect.remove("March"); 822 assertMappings(expect, map); 823 } else { 824 synchronized (map) { 825 map.putAll(getOrderSensitiveMappings()); 826 } 827 828 Set toRemove = new HashSet (2); 829 toRemove.add("February"); 830 toRemove.add("March"); 831 832 synchronized (map) { 833 map.keySet().removeAll(toRemove); 834 } 835 } 836 } 837 838 void testKeySetRetainAll(Map map, boolean validate) { 839 if (validate) { 840 Map expect = getOrderSensitiveMappings(); 841 expect.remove("January"); 842 expect.remove("April"); 843 assertMappings(expect, map); 844 } else { 845 synchronized (map) { 846 map.putAll(getOrderSensitiveMappings()); 847 } 848 849 Set toRetain = new HashSet (2); 850 toRetain.add("March"); 851 toRetain.add("February"); 852 853 synchronized (map) { 854 map.keySet().retainAll(toRetain); 855 } 856 } 857 } 858 859 void testKeySetRemoveNull(Map map, boolean validate) { 860 if (!allowsNull(map)) { return; } 861 862 if (validate) { 863 assertSingleMapping(map, "key1", "value1"); 864 } else { 865 synchronized (map) { 866 map.put("key1", "value1"); 867 map.put(null, "value for null key"); 868 map.put("key for null value", null); 869 } 870 871 synchronized (map) { 872 map.keySet().remove(null); 873 map.keySet().remove("key for null value"); 874 } 875 } 876 877 } 878 879 void testEntrySetRetainAll(Map map, boolean validate) { 880 if (validate) { 881 assertSingleMapping(map, "key1", "value1"); 884 } else { 885 synchronized (map) { 886 map.put("key1", "value1"); 887 map.put("key2", "value2"); 888 map.put("key3", "value3"); 889 } 890 891 Set retainSet = new HashSet (); 892 retainSet.add(new SimpleEntry("key1", "value1")); 893 894 synchronized (map) { 895 map.entrySet().retainAll(retainSet); 896 } 897 } 898 } 899 900 void testEntrySetRetainAll2(Map map, boolean validate) { 901 if (validate) { 902 Map expect = getOrderSensitiveMappings(); 903 expect.remove("January"); 904 expect.remove("April"); 905 assertMappings(expect, map); 906 } else { 907 synchronized (map) { 908 map.putAll(getOrderSensitiveMappings()); 909 } 910 911 Set retainSet = new HashSet (); 912 retainSet.add(new SimpleEntry("February", "Feb")); 913 retainSet.add(new SimpleEntry("March", "Mar")); 914 915 synchronized (map) { 916 map.entrySet().retainAll(retainSet); 917 } 918 } 919 } 920 921 void testEntrySetClear(Map map, boolean validate) { 922 if (validate) { 923 assertEmptyMap(map); 924 } else { 925 addMappings(map, 3); 926 927 synchronized (map) { 928 map.entrySet().clear(); 929 } 930 } 931 } 932 933 void testEntrySetIteratorRemove(Map map, boolean validate) { 934 if (validate) { 935 Map expect = getOrderSensitiveMappings(); 936 expect.remove("February"); 937 expect.remove("March"); 938 assertMappings(expect, map); 939 } else { 940 synchronized (map) { 941 map.putAll(getOrderSensitiveMappings()); 942 } 943 944 synchronized (map) { 945 Set set = map.entrySet(); 946 for (Iterator i = set.iterator(); i.hasNext();) { 947 Entry entry = (Entry) i.next(); 948 Object key = entry.getKey(); 949 if ("February".equals(key) || "March".equals(key)) { 950 i.remove(); 951 } 952 } 953 } 954 } 955 } 956 957 void testEntrySetIteratorRemoveNull(Map map, boolean validate) { 958 if (!allowsNull(map)) { return; } 959 if (validate) { 960 assertSingleMapping(map, "key1", "value1"); 961 } else { 962 synchronized (map) { 963 map.put("key1", "value1"); 964 map.put(null, "value for null key"); 965 map.put("key for null value", null); 966 } 967 968 synchronized (map) { 969 Set set = map.entrySet(); 970 for (Iterator i = set.iterator(); i.hasNext();) { 971 Entry entry = (Entry) i.next(); 972 Object key = entry.getKey(); 973 if (!"key1".equals(key)) { 974 i.remove(); 975 } 976 } 977 } 978 } 979 } 980 981 void testEntrySetToArray(Map map, boolean validate) { 982 Object [] array = getArray(map); 983 984 if (validate) { 985 if (canTestSharedArray(map)) { 986 assertMappingsEqual(array, map); 987 } 988 } else { 989 synchronized (map) { 990 map.putAll(getOrderSensitiveMappings()); 991 } 992 synchronized (array) { 993 Object [] returnArray = map.entrySet().toArray(array); 994 Assert.assertTrue(returnArray == array); 995 } 996 assertMappingsEqual(array, map); 997 } 998 } 999 1000 void testKeySetToArray(Map map, boolean validate) { 1001 Object [] array = getArray(map); 1002 1003 if (validate) { 1004 if (canTestSharedArray(map)) { 1005 assertMappingsKeysEqual(array, map.keySet()); 1006 } 1007 } else { 1008 synchronized (map) { 1009 map.putAll(getOrderSensitiveMappings()); 1010 } 1011 synchronized (array) { 1012 Object [] returnArray = map.keySet().toArray(array); 1013 Assert.assertTrue(returnArray == array); 1014 } 1015 assertMappingsKeysEqual(array, map.keySet()); 1016 } 1017 } 1018 1019 void testValuesToArray(Map map, boolean validate) { 1020 Object [] array = getArray(map); 1021 1022 if (validate) { 1023 if (canTestSharedArray(map)) { 1024 assertMappingsKeysEqual(array, map.values()); 1025 } 1026 } else { 1027 synchronized (map) { 1028 map.putAll(getOrderSensitiveMappings()); 1029 } 1030 synchronized (array) { 1031 Object [] returnArray = map.values().toArray(array); 1032 Assert.assertTrue(returnArray == array); 1033 } 1034 assertMappingsKeysEqual(array, map.values()); 1035 } 1036 } 1037 1038 void testFastHashMapSetFast(Map map, boolean validate) { 1039 if (!(map instanceof FastHashMap)) { return; } 1040 1041 FastHashMap fastHashMap = (FastHashMap) map; 1042 if (validate) { 1043 Assert.assertTrue(fastHashMap.getFast()); 1044 } else { 1045 synchronized (fastHashMap) { 1046 fastHashMap.setFast(true); 1047 } 1048 } 1049 } 1050 1051 void testReadOnlyPut(Map map, boolean validate) { 1053 if (map instanceof Hashtable ) { return; } 1054 if (map instanceof FastHashMap) { return; } 1055 1056 if (validate) { 1057 assertEmptyMap(map); 1058 } else { 1059 synchronized (map) { 1060 try { 1061 Object prev = map.put("timmy", "teck"); 1062 Assert.assertNull(prev); 1063 throw new AssertionError ("Should have thrown a ReadOnlyException"); 1064 } catch (ReadOnlyException re) { 1065 } 1067 } 1068 } 1069 } 1070 1071 void testReadOnlyPutAll(Map map, boolean validate) { 1072 if (map instanceof Hashtable ) { return; } 1073 if (map instanceof FastHashMap) { return; } 1074 1075 if (validate) { 1076 assertEmptyMap(map); 1077 } else { 1078 synchronized (map) { 1079 try { 1080 Map toAdd = new HashMap(); 1081 addMappings(toAdd, 3); 1082 map.putAll(toAdd); 1083 throw new AssertionError ("Should have thrown a ReadOnlyException"); 1084 } catch (ReadOnlyException t) { 1085 } 1087 } 1088 } 1089 } 1090 1091 void testSetUpRemove(Map map, boolean validate) { 1093 if (map instanceof Hashtable ) { return; } 1094 if (map instanceof FastHashMap) { return; } 1095 1096 Map toAdd = new HashMap(); 1097 addMappings(toAdd, 3); 1098 1099 if (validate) { 1100 assertMappings(toAdd, map); 1101 } else { 1102 synchronized (map) { 1103 map.putAll(toAdd); 1104 } 1105 tryReadOnlyRemove(map); 1106 } 1107 1108 } 1109 1110 private void tryReadOnlyRemove(Map map) { 1112 synchronized (map) { 1113 try { 1114 map.remove("key2"); 1115 throw new AssertionError ("Should have thrown a ReadOnlyException"); 1116 } catch (ReadOnlyException t) { 1117 } 1119 } 1120 } 1121 1122 void testSetUpClear(Map map, boolean validate) { 1124 if (map instanceof Hashtable ) { return; } 1125 if (map instanceof FastHashMap) { return; } 1126 1127 Map toAdd = new HashMap(); 1128 addMappings(toAdd, 3); 1129 1130 if (validate) { 1131 assertMappings(toAdd, map); 1132 } else { 1133 synchronized (map) { 1134 map.putAll(toAdd); 1135 } 1136 tryReadOnlyClear(map); 1137 } 1138 1139 } 1140 1141 private void tryReadOnlyClear(Map map) { 1143 synchronized (map) { 1144 try { 1145 map.clear(); 1146 throw new AssertionError ("Should have thrown a ReadOnlyException"); 1147 } catch (ReadOnlyException t) { 1148 } 1150 } 1151 } 1152 1153 void testSetUpEntrySetClear(Map map, boolean validate) { 1155 if (map instanceof Hashtable ) { return; } 1156 if (map instanceof FastHashMap) { return; } 1157 1158 Map toAdd = new HashMap(); 1159 addMappings(toAdd, 3); 1160 1161 if (validate) { 1162 assertMappings(toAdd, map); 1163 } else { 1164 synchronized (map) { 1165 map.putAll(toAdd); 1166 } 1167 tryReadOnlyEntrySetClear(map); 1168 } 1169 1170 } 1171 1172 private void tryReadOnlyEntrySetClear(Map map) { 1174 synchronized (map) { 1175 Set entrySet = map.entrySet(); 1176 try { 1177 entrySet.clear(); 1178 throw new AssertionError ("Should have thrown a ReadOnlyException"); 1179 } catch (ReadOnlyException t) { 1180 } 1182 } 1183 } 1184 1185 void testSetUpEntrySetRemove(Map map, boolean validate) { 1187 if (map instanceof Hashtable ) { return; } 1188 if (map instanceof FastHashMap) { return; } 1189 1190 Map toAdd = new HashMap(); 1191 addMappings(toAdd, 3); 1192 1193 if (validate) { 1194 assertMappings(toAdd, map); 1195 } else { 1196 synchronized (map) { 1197 map.putAll(toAdd); 1198 } 1199 tryReadOnlyEntrySetRemove(map); 1200 } 1201 1202 } 1203 1204 private void tryReadOnlyEntrySetRemove(Map map) { 1206 synchronized (map) { 1207 Set entrySet = map.entrySet(); 1208 Iterator iterator = entrySet.iterator(); 1209 Object o = iterator.next(); 1210 try { 1211 entrySet.remove(o); 1212 throw new AssertionError ("Should have thrown a ReadOnlyException"); 1213 } catch (ReadOnlyException t) { 1214 } 1216 } 1217 } 1218 1219 void testSetUpEntrySetRetainAll(Map map, boolean validate) { 1221 if (map instanceof Hashtable ) { return; } 1222 if (map instanceof FastHashMap) { return; } 1223 1224 Map toAdd = new HashMap(); 1225 addMappings(toAdd, 3); 1226 1227 if (validate) { 1228 assertMappings(toAdd, map); 1229 } else { 1230 synchronized (map) { 1231 map.putAll(toAdd); 1232 } 1233 tryReadOnlyEntrySetRetainAll(map); 1234 } 1235 1236 } 1237 1238 private void tryReadOnlyEntrySetRetainAll(Map map) { 1240 synchronized (map) { 1241 Set entrySet = map.entrySet(); 1242 Object o = entrySet.iterator().next(); 1243 Set retainAll = new HashSet (); 1244 retainAll.add(o); 1245 try { 1246 entrySet.retainAll(retainAll); 1247 throw new AssertionError ("Should have thrown a ReadOnlyException"); 1248 } catch (ReadOnlyException t) { 1249 } 1251 } 1252 } 1253 1254 void testSetUpEntrySetRemoveAll(Map map, boolean validate) { 1256 if (map instanceof Hashtable ) { return; } 1257 if (map instanceof FastHashMap) { return; } 1258 1259 Map toAdd = new HashMap(); 1260 addMappings(toAdd, 3); 1261 1262 if (validate) { 1263 assertMappings(toAdd, map); 1264 } else { 1265 synchronized (map) { 1266 map.putAll(toAdd); 1267 } 1268 tryReadOnlyEntrySetRemoveAll(map); 1269 } 1270 1271 } 1272 1273 private void tryReadOnlyEntrySetRemoveAll(Map map) { 1275 synchronized (map) { 1276 Set entrySet = map.entrySet(); 1277 Object o = entrySet.iterator().next(); 1278 Set removeAll = new HashSet (); 1279 removeAll.add(o); 1280 try { 1281 entrySet.removeAll(removeAll); 1282 throw new AssertionError ("Should have thrown a ReadOnlyException"); 1283 } catch (ReadOnlyException t) { 1284 } 1286 } 1287 } 1288 1289 void testSetUpEntrySetIteratorRemove(Map map, boolean validate) { 1291 if (map instanceof Hashtable ) { return; } 1292 if (map instanceof FastHashMap) { return; } 1293 1294 Map toAdd = new HashMap(); 1295 addMappings(toAdd, 3); 1296 1297 if (validate) { 1298 assertMappings(toAdd, map); 1299 } else { 1300 synchronized (map) { 1301 map.putAll(toAdd); 1302 } 1303 tryReadOnlyEntrySetIteratorRemove(map); 1304 } 1305 1306 } 1307 1308 private void tryReadOnlyEntrySetIteratorRemove(Map map) { 1310 synchronized (map) { 1311 Iterator iterator = map.entrySet().iterator(); 1312 try { 1313 iterator.next(); 1314 iterator.remove(); 1315 throw new AssertionError ("Should have thrown a ReadOnlyException"); 1316 } catch (ReadOnlyException t) { 1317 } 1319 } 1320 } 1321 1322 void testSetUpEntrySet(Map map, boolean validate) { 1324 if (map instanceof Hashtable ) { return; } 1325 if (map instanceof FastHashMap) { return; } 1326 1327 Map toAdd = new HashMap(); 1328 addMappings(toAdd, 3); 1329 1330 if (validate) { 1331 assertMappings(toAdd, map); 1332 } else { 1333 synchronized (map) { 1334 map.putAll(toAdd); 1335 } 1336 tryReadOnlyEntrySet(map); 1337 } 1338 1339 } 1340 1341 private void tryReadOnlyEntrySet(Map map) { 1343 synchronized (map) { 1344 Iterator iterator = map.entrySet().iterator(); 1345 try { 1346 Entry entry = (Entry) iterator.next(); 1347 entry.setValue("modified value"); 1348 throw new AssertionError ("Should have thrown a ReadOnlyException"); 1349 } catch (ReadOnlyException t) { 1350 } 1352 } 1353 } 1354 1355 void testSetUpKeySetClear(Map map, boolean validate) { 1357 if (map instanceof Hashtable ) { return; } 1358 if (map instanceof FastHashMap) { return; } 1359 1360 Map toAdd = new HashMap(); 1361 addMappings(toAdd, 3); 1362 1363 if (validate) { 1364 assertMappings(toAdd, map); 1365 } else { 1366 synchronized (map) { 1367 map.putAll(toAdd); 1368 } 1369 tryReadOnlyKeySetClear(map); 1370 } 1371 1372 } 1373 1374 private void tryReadOnlyKeySetClear(Map map) { 1376 synchronized (map) { 1377 Set keySet = map.keySet(); 1378 try { 1379 keySet.clear(); 1380 throw new AssertionError ("Should have thrown a ReadOnlyException"); 1381 } catch (ReadOnlyException t) { 1382 } 1384 } 1385 } 1386 1387 void testSetUpKeySetRetainAll(Map map, boolean validate) { 1389 if (map instanceof Hashtable ) { return; } 1390 if (map instanceof FastHashMap) { return; } 1391 1392 Map toAdd = new HashMap(); 1393 addMappings(toAdd, 3); 1394 1395 if (validate) { 1396 assertMappings(toAdd, map); 1397 } else { 1398 synchronized (map) { 1399 map.putAll(toAdd); 1400 } 1401 tryReadOnlyKeySetRetainAll(map); 1402 } 1403 1404 } 1405 1406 private void tryReadOnlyKeySetRetainAll(Map map) { 1408 synchronized (map) { 1409 Set keySet = map.keySet(); 1410 Object o = keySet.iterator().next(); 1411 Set retainAll = new HashSet (); 1412 retainAll.add(o); 1413 try { 1414 keySet.retainAll(retainAll); 1415 throw new AssertionError ("Should have thrown a ReadOnlyException"); 1416 } catch (ReadOnlyException t) { 1417 } 1419 } 1420 } 1421 1422 void testSetUpKeySetRemoveAll(Map map, boolean validate) { 1424 if (map instanceof Hashtable ) { return; } 1425 if (map instanceof FastHashMap) { return; } 1426 1427 Map toAdd = new HashMap(); 1428 addMappings(toAdd, 3); 1429 1430 if (validate) { 1431 assertMappings(toAdd, map); 1432 } else { 1433 synchronized (map) { 1434 map.putAll(toAdd); 1435 } 1436 tryReadOnlyKeySetRemoveAll(map); 1437 } 1438 1439 } 1440 1441 private void tryReadOnlyKeySetRemoveAll(Map map) { 1443 synchronized (map) { 1444 Set keySet = map.keySet(); 1445 Object o = keySet.iterator().next(); 1446 Set removeAll = new HashSet (); 1447 removeAll.add(o); 1448 try { 1449 keySet.removeAll(removeAll); 1450 throw new AssertionError ("Should have thrown a ReadOnlyException"); 1451 } catch (ReadOnlyException t) { 1452 } 1454 } 1455 } 1456 1457 void testSetUpKeySetIteratorRemove(Map map, boolean validate) { 1459 if (map instanceof Hashtable ) { return; } 1460 if (map instanceof FastHashMap) { return; } 1461 1462 Map toAdd = new HashMap(); 1463 addMappings(toAdd, 3); 1464 1465 if (validate) { 1466 assertMappings(toAdd, map); 1467 } else { 1468 synchronized (map) { 1469 map.putAll(toAdd); 1470 } 1471 tryReadOnlyKeySetIteratorRemove(map); 1472 } 1473 } 1474 1475 private void tryReadOnlyKeySetIteratorRemove(Map map) { 1477 synchronized (map) { 1478 Iterator iterator = map.keySet().iterator(); 1479 try { 1480 iterator.next(); 1481 iterator.remove(); 1482 throw new AssertionError ("Should have thrown a ReadOnlyException"); 1483 } catch (ReadOnlyException t) { 1484 } 1486 } 1487 } 1488 1489 void testSetUpValuesClear(Map map, boolean validate) { 1491 if (map instanceof Hashtable ) { return; } 1492 if (map instanceof FastHashMap) { return; } 1493 1494 Map toAdd = new HashMap(); 1495 addMappings(toAdd, 3); 1496 1497 if (validate) { 1498 assertMappings(toAdd, map); 1499 } else { 1500 synchronized (map) { 1501 map.putAll(toAdd); 1502 } 1503 tryReadOnlyValuesClear(map); 1504 } 1505 1506 } 1507 1508 private void tryReadOnlyValuesClear(Map map) { 1510 synchronized (map) { 1511 Collection values = map.values(); 1512 try { 1513 values.clear(); 1514 throw new AssertionError ("Should have thrown a ReadOnlyException"); 1515 } catch (ReadOnlyException t) { 1516 } 1518 } 1519 } 1520 1521 void testSetUpValuesRetainAll(Map map, boolean validate) { 1523 if (map instanceof Hashtable ) { return; } 1524 if (map instanceof FastHashMap) { return; } 1525 1526 Map toAdd = new HashMap(); 1527 addMappings(toAdd, 3); 1528 1529 if (validate) { 1530 assertMappings(toAdd, map); 1531 } else { 1532 synchronized (map) { 1533 map.putAll(toAdd); 1534 } 1535 tryReadOnlyValuesRetainAll(map); 1536 } 1537 1538 } 1539 1540 private void tryReadOnlyValuesRetainAll(Map map) { 1542 synchronized (map) { 1543 Collection values = map.values(); 1544 Object o = values.iterator().next(); 1545 Set retainAll = new HashSet (); 1546 retainAll.add(o); 1547 try { 1548 values.retainAll(retainAll); 1549 throw new AssertionError ("Should have thrown a ReadOnlyException"); 1550 } catch (ReadOnlyException t) { 1551 } 1553 } 1554 } 1555 1556 void testSetUpValuesRemoveAll(Map map, boolean validate) { 1558 if (map instanceof Hashtable ) { return; } 1559 if (map instanceof FastHashMap) { return; } 1560 1561 Map toAdd = new HashMap(); 1562 addMappings(toAdd, 3); 1563 1564 if (validate) { 1565 assertMappings(toAdd, map); 1566 } else { 1567 synchronized (map) { 1568 map.putAll(toAdd); 1569 } 1570 tryReadOnlyValuesRemoveAll(map); 1571 } 1572 1573 } 1574 1575 private void tryReadOnlyValuesRemoveAll(Map map) { 1577 synchronized (map) { 1578 Collection values = map.values(); 1579 Object o = values.iterator().next(); 1580 Set removeAll = new HashSet (); 1581 removeAll.add(o); 1582 try { 1583 values.removeAll(removeAll); 1584 throw new AssertionError ("Should have thrown a ReadOnlyException"); 1585 } catch (ReadOnlyException t) { 1586 } 1588 } 1589 } 1590 1591 void testSetUpValuesIteratorRemove(Map map, boolean validate) { 1593 if (map instanceof Hashtable ) { return; } 1594 if (map instanceof FastHashMap) { return; } 1595 1596 Map toAdd = new HashMap(); 1597 addMappings(toAdd, 3); 1598 1599 if (validate) { 1600 assertMappings(toAdd, map); 1601 } else { 1602 synchronized (map) { 1603 map.putAll(toAdd); 1604 } 1605 tryReadOnlyValuesIteratorRemove(map); 1606 } 1607 1608 } 1609 1610 private void tryReadOnlyValuesIteratorRemove(Map map) { 1612 synchronized (map) { 1613 Iterator iterator = map.values().iterator(); 1614 try { 1615 iterator.next(); 1616 iterator.remove(); 1617 throw new AssertionError ("Should have thrown a ReadOnlyException"); 1618 } catch (ReadOnlyException t) { 1619 } 1621 } 1622 } 1623 1624 void testTHashMapRemoveAt(Map map, boolean validate) { 1626 if (!(map instanceof THashMap)) { return; } 1627 1628 Map toAdd = new HashMap(); 1629 addMappings(toAdd, 2); 1630 if (validate) { 1631 assertMappings(toAdd, map); 1632 } else { 1633 synchronized (map) { 1634 map.putAll(toAdd); 1635 map.put("key2", "value2"); 1636 } 1637 Class mapClass = TObjectHash.class; 1638 Class [] parameterType = new Class [1]; 1639 1640 try { 1641 parameterType[0] = Object .class; 1642 Method m = mapClass.getDeclaredMethod("index", parameterType); 1643 m.setAccessible(true); Object indexObj = m.invoke(map, new Object [] { "key2" }); 1646 1647 synchronized (map) { 1648 parameterType[0] = Integer.TYPE; 1649 m = mapClass.getDeclaredMethod("removeAt", parameterType); 1650 m.setAccessible(true); 1653 m.invoke(map, new Object [] { indexObj }); 1654 } 1655 } catch (NoSuchMethodException e) { 1656 } catch (IllegalArgumentException e) { 1658 } catch (IllegalAccessException e) { 1660 } catch (InvocationTargetException e) { 1662 } 1664 } 1665 } 1666 1667 void testTHashMapTransformValues(Map map, boolean validate) { 1668 if (!(map instanceof THashMap)) { return; } 1669 THashMap tMap = (THashMap) map; 1670 if (validate) { 1671 for (int i = 0; i < 10; i++) { 1672 Assert.assertEquals(new Integer (i + 1), tMap.get(String.valueOf(i))); 1673 } 1674 } else { 1675 synchronized (tMap) { 1676 for (int i = 0; i < 10; i++) { 1677 tMap.put(String.valueOf(i), new Integer (i)); 1678 } 1679 } 1680 synchronized (tMap) { 1681 tMap.transformValues(new MyObjectFunction()); 1682 } 1683 } 1684 } 1685 1686 void testPutNonPortableObject(Map map, boolean validate) { 1687 if (!canTestNonPortableObject(map)) { return; } 1688 1689 Map toBeAdded = new HashMap(); 1690 toBeAdded.put("First", "First Value"); 1691 toBeAdded.put("Second", "Second Value"); 1692 if (validate) { 1693 assertMappings(toBeAdded, map); 1694 } else { 1695 synchronized (map) { 1696 map.putAll(toBeAdded); 1697 } 1698 synchronized (map) { 1699 try { 1700 map.put("Non-portable", new MyNonPortableObject()); 1701 throw new AssertionError ("Should have thrown a TCNonPortableObjectError."); 1702 } catch (TCNonPortableObjectError e) { 1703 } 1705 } 1706 } 1707 } 1708 1709 void testLinkedHashMapAccessOrderGet(Map map, boolean validate) { 1710 if (!isAccessOrderedLinkedHashMap(map)) { return; } 1711 1712 if (validate) { 1713 Map toBeExpected = new LinkedHashMap (); 1714 toBeExpected.put("First", "First Value"); 1715 toBeExpected.put("Third", "Third Value"); 1716 toBeExpected.put("Second", "Second Value"); assertMappings(toBeExpected, map); 1718 } else { 1719 Map toBeAdded = new LinkedHashMap (); 1720 toBeAdded.put("First", "First Value"); 1721 toBeAdded.put("Second", "Second Value"); 1722 toBeAdded.put("Third", "Third Value"); 1723 synchronized (map) { 1724 map.putAll(toBeAdded); 1725 } 1726 synchronized (map) { 1727 Assert.assertEquals("Second Value", map.get("Second")); 1728 } 1729 } 1730 } 1731 1732 void testLinkedHashMapInsertionOrderPut(Map map, boolean validate) { 1733 if (!(map instanceof LinkedHashMap )) { return; } 1734 1735 if (isAccessOrderedLinkedHashMap(map)) { return; } 1737 1738 if (validate) { 1739 Map toBeExpected = new LinkedHashMap (); 1740 toBeExpected.put("First", "First Value"); 1741 toBeExpected.put("Second", "New Second Value"); 1742 toBeExpected.put("Third", "Third Value"); 1743 assertMappings(toBeExpected, map); 1744 } else { 1745 Map toBeAdded = new LinkedHashMap (); 1746 toBeAdded.put("First", "First Value"); 1747 toBeAdded.put("Second", "Second Value"); 1748 toBeAdded.put("Third", "Third Value"); 1749 synchronized (map) { 1750 map.putAll(toBeAdded); 1751 } 1752 synchronized (map) { 1753 map.put("Second", "New Second Value"); 1755 } 1756 } 1757 } 1758 1759 void testLinkedHashMapInsertionOrderRemovePut(Map map, boolean validate) { 1760 if (!(map instanceof LinkedHashMap )) { return; } 1761 1762 if (isAccessOrderedLinkedHashMap(map)) { return; } 1764 1765 if (validate) { 1766 Map toBeExpected = new LinkedHashMap (); 1767 toBeExpected.put("First", "First Value"); 1768 toBeExpected.put("Third", "Third Value"); 1769 toBeExpected.put("Second", "New Second Value"); 1770 assertMappings(toBeExpected, map); 1771 } else { 1772 Map toBeAdded = new LinkedHashMap (); 1773 toBeAdded.put("First", "First Value"); 1774 toBeAdded.put("Second", "Second Value"); 1775 toBeAdded.put("Third", "Third Value"); 1776 synchronized (map) { 1777 map.putAll(toBeAdded); 1778 } 1779 synchronized (map) { 1780 map.remove("Second"); 1781 map.put("Second", "New Second Value"); 1782 } 1783 } 1784 } 1785 1786 void testLinkedHashMapAccessOrderPut(Map map, boolean validate) { 1787 if (!isAccessOrderedLinkedHashMap(map)) { return; } 1788 1789 if (validate) { 1790 Map toBeExpected = new LinkedHashMap (); 1791 toBeExpected.put("First", "First Value"); 1792 toBeExpected.put("Third", "Third Value"); 1793 toBeExpected.put("Second", "New Second Value"); assertMappings(toBeExpected, map); 1795 } else { 1796 Map toBeAdded = new LinkedHashMap (); 1797 toBeAdded.put("First", "First Value"); 1798 toBeAdded.put("Second", "Second Value"); 1799 toBeAdded.put("Third", "Third Value"); 1800 synchronized (map) { 1801 map.putAll(toBeAdded); 1802 } 1803 synchronized (map) { 1804 map.put("Second", "New Second Value"); 1806 } 1807 } 1808 } 1809 1810 private boolean canTestSharedArray(Map map) { 1811 return (!(map instanceof HashMap) && !(map instanceof LinkedHashMap ) && !(map instanceof Hashtable )); 1812 } 1813 1814 private boolean canTestNonPortableObject(Map map) { 1815 return ((map instanceof HashMap) || (map instanceof LinkedHashMap ) || (map instanceof Hashtable )); 1816 } 1817 1818 1822 private Object getMySubclassArray(Map map) { 1823 if (map instanceof MyLinkedHashMap3) { return nonSharedArrayMap.get("arrayforMyLinkedHashMap3"); } 1824 if (map instanceof MyLinkedHashMap2) { return nonSharedArrayMap.get("arrayforMyLinkedHashMap2"); } 1825 if (map instanceof MyLinkedHashMap) { return nonSharedArrayMap.get("arrayforMyLinkedHashMap"); } 1826 if (map instanceof MyHashMap2) { return nonSharedArrayMap.get("arrayforMyHashMap2"); } 1827 if (map instanceof MyHashMap) { return nonSharedArrayMap.get("arrayforMyHashMap"); } 1828 if (map instanceof MyHashMap3) { return sharedMap.get("arrayforMyHashMap3"); } 1829 if (map instanceof MyTreeMap) { return sharedMap.get("arrayforMyTreeMap"); } 1830 if (map instanceof MyTreeMap2) { return sharedMap.get("arrayforMyTreeMap"); } 1831 if (map instanceof MyHashtable2) { return nonSharedArrayMap.get("arrayforMyHashtable2"); } 1832 if (map instanceof MyHashtable) { return nonSharedArrayMap.get("arrayforMyHashtable"); } 1833 if (map instanceof MyTHashMap) { return sharedMap.get("arrayforMyTHashMap"); } 1834 if (map instanceof MyFastHashMap) { return nonSharedArrayMap.get("arrayforMyFastHashMap"); } 1835 if (map instanceof MyProperties2) { return nonSharedArrayMap.get("arrayforMyProperties2"); } 1836 if (map instanceof MyProperties3) { return nonSharedArrayMap.get("arrayforMyProperties3"); } 1837 if (map instanceof MyProperties) { return nonSharedArrayMap.get("arrayforMyProperties"); } 1838 return null; 1839 } 1840 1841 1845 private Object [] getArray(Map map) { 1846 Object o = getMySubclassArray(map); 1847 if (o != null) { return (Object []) o; } 1848 1849 if (map instanceof Properties ) { return (Object []) nonSharedArrayMap.get("arrayforProperties"); } 1850 if (map instanceof FastHashMap) { 1851 if (((FastHashMap) map).getFast()) { 1852 return (Object []) nonSharedArrayMap.get("arrayforFastHashMapWithFast"); 1853 } else { 1854 return (Object []) nonSharedArrayMap.get("arrayforFastHashMap"); 1855 } 1856 } else if (map instanceof LinkedHashMap ) { 1857 return (Object []) nonSharedArrayMap.get("arrayforLinkedHashMap"); 1858 } else if (map instanceof HashMap) { 1859 return (Object []) nonSharedArrayMap.get("arrayforHashMap"); 1860 } else if (map instanceof Hashtable ) { 1861 return (Object []) nonSharedArrayMap.get("arrayforHashtable"); 1862 } else if (map instanceof TreeMap ) { 1863 return (Object []) sharedMap.get("arrayforTreeMap"); 1864 } else if (map instanceof THashMap) { return (Object []) sharedMap.get("arrayforTHashMap"); } 1865 return null; 1866 } 1867 1868 void assertMappingsKeysEqual(Object [] expect, Collection collection) { 1869 Assert.assertEquals(expect.length, collection.size()); 1870 for (int i = 0; i < expect.length; i++) { 1871 Assert.assertTrue(collection.contains(expect[i])); 1872 } 1873 } 1874 1875 void assertMappings(Map expect, Map actual) { 1876 Assert.assertEquals(expect.size(), actual.size()); 1877 1878 Set expectEntries = expect.entrySet(); 1879 Set actualEntries = actual.entrySet(); 1880 if (actual instanceof LinkedHashMap ) { 1881 for (Iterator iExpect = expectEntries.iterator(), iActual = actualEntries.iterator(); iExpect.hasNext();) { 1882 Assert.assertEquals(iExpect.next(), iActual.next()); 1883 } 1884 } 1885 1886 for (Iterator i = actualEntries.iterator(); i.hasNext();) { 1887 Entry entry = (Entry) i.next(); 1888 Assert.assertEquals(entry.getValue(), expect.get(entry.getKey())); 1889 } 1890 } 1891 1892 void assertMappingsEqual(Object [] expect, Map map) { 1893 Assert.assertEquals(expect.length, map.size()); 1894 1895 if (map instanceof LinkedHashMap ) { 1896 Set entries = map.entrySet(); 1897 int i = 0; 1898 for (Iterator iActual = entries.iterator(); iActual.hasNext();) { 1899 Assert.assertEquals(expect[i++], iActual.next()); 1900 } 1901 } 1902 1903 for (int i = 0; i < expect.length; i++) { 1904 Entry entry = (Entry) expect[i]; 1905 if (isAccessOrderedLinkedHashMap(map)) { 1906 synchronized (map) { 1907 Assert.assertEquals(entry.getValue(), map.get(entry.getKey())); 1908 } 1909 } else { 1910 Assert.assertEquals(entry.getValue(), map.get(entry.getKey())); 1911 } 1912 } 1913 } 1914 1915 void assertSingleMapping(Map map, final Object key, final Object value) { 1916 try { 1917 Assert.assertFalse(map.isEmpty()); 1918 Assert.assertEquals(1, map.size()); 1919 Assert.assertEquals(1, map.entrySet().size()); 1920 Assert.assertEquals(1, map.values().size()); 1921 Assert.assertEquals(1, map.keySet().size()); 1922 1923 if (isAccessOrderedLinkedHashMap(map)) { 1924 synchronized (map) { 1927 Assert.assertEquals(value, map.get(key)); 1928 } 1929 } else { 1930 Assert.assertEquals(value, map.get(key)); 1931 } 1932 Assert.assertTrue(map.containsKey(key)); 1933 Assert.assertTrue(map.containsValue(value)); 1934 1935 Set entries = map.entrySet(); 1936 for (Iterator i = entries.iterator(); i.hasNext();) { 1937 Entry entry = (Entry) i.next(); 1938 Assert.assertEquals(key, entry.getKey()); 1939 Assert.assertEquals(value, entry.getValue()); 1940 } 1941 1942 for (Iterator i = map.values().iterator(); i.hasNext();) { 1943 Object o = i.next(); 1944 Assert.assertEquals(value, o); 1945 } 1946 1947 for (Iterator i = map.keySet().iterator(); i.hasNext();) { 1948 Object o = i.next(); 1949 Assert.assertEquals(key, o); 1950 } 1951 } catch (Throwable t) { 1952 notifyError(new ErrorContext(map.getClass().getName(), t)); 1953 } 1954 } 1955 1956 void assertEmptyMap(Map map) { 1957 try { 1958 Assert.assertTrue(map.isEmpty()); 1959 Assert.assertEquals(0, map.size()); 1960 Assert.assertEquals(0, map.entrySet().size()); 1961 Assert.assertEquals(0, map.values().size()); 1962 Assert.assertEquals(0, map.keySet().size()); 1963 Assert.assertEquals(Collections.EMPTY_MAP, map); 1964 } catch (Throwable t) { 1965 notifyError(new ErrorContext(map.getClass().getName(), t)); 1966 } 1967 } 1968 1969 private void addMappings(Map map, int count) { 1970 synchronized (map) { 1971 for (int i = 0; i < count; i++) { 1972 map.put("key" + i, "value" + i); 1973 } 1974 } 1975 } 1976 1977 private Map getOrderSensitiveMappings() { 1978 Map map = null; 1979 map = new HashMap(); 1980 map.put("January", "Jan"); 1981 map.put("February", "Feb"); 1982 map.put("March", "Mar"); 1983 map.put("April", "Apr"); 1984 return map; 1985 } 1986 1987 private boolean isAccessOrderedLinkedHashMap(Map map) { 1988 if (map instanceof LinkedHashMap ) { 1989 try { 1990 Field f = LinkedHashMap .class.getDeclaredField("accessOrder"); 1991 f.setAccessible(true); 1992 return ((Boolean ) f.get(map)).booleanValue(); 1993 } catch (Throwable t) { 1994 throw new RuntimeException (t); 1995 } 1996 } 1997 1998 return false; 1999 } 2000 2001 private static boolean allowsNull(Map map) { 2002 return !(map instanceof Hashtable ); 2003 } 2004 2005 private static class Key implements Comparable { 2007 private final String id; 2008 private final String equals; 2009 2010 Key(String id, String equals) { 2011 this.id = id; 2012 this.equals = equals; 2013 } 2014 2015 public int hashCode() { 2016 return equals == null ? 0 : equals.hashCode(); 2017 } 2018 2019 public boolean equals(Object o) { 2020 if (!(o instanceof Key)) { return false; } 2021 Key other = (Key) o; 2022 if (this.equals == null && other.equals == null) { return true; } 2023 if (this.equals == null && other.equals != null) { return false; } 2024 if (other.equals == null && this.equals != null) { return false; } 2025 return this.equals.equals(other.equals); 2026 } 2027 2028 public int compareTo(Object o) { 2029 Key cmp = (Key) o; 2030 return equals.compareTo(cmp.equals); 2031 } 2032 2033 } 2034 2035 private static class NullTolerantComparator implements Comparator { 2036 2037 public int compare(Object o1, Object o2) { 2038 if (o1 == null && o2 == null) { return 0; } 2039 if (o1 == null && o2 != null) { return -1; } 2040 if (o1 != null && o2 == null) { return 1; } 2041 return ((Comparable ) o1).compareTo(o2); 2042 } 2043 } 2044 2045 private static class SimpleEntry implements Map.Entry { 2046 2048 private final Object key; 2049 private Object value; 2050 2051 public SimpleEntry(Object key, Object value) { 2052 this.key = key; 2053 this.value = value; 2054 } 2055 2056 public SimpleEntry(Map.Entry e) { 2057 this.key = e.getKey(); 2058 this.value = e.getValue(); 2059 } 2060 2061 public Object getKey() { 2062 return key; 2063 } 2064 2065 public Object getValue() { 2066 return value; 2067 } 2068 2069 public Object setValue(Object value) { 2070 Object oldValue = this.value; 2071 this.value = value; 2072 return oldValue; 2073 } 2074 2075 public boolean equals(Object o) { 2076 if (!(o instanceof Map.Entry)) return false; 2077 Map.Entry e = (Map.Entry) o; 2078 return eq(key, e.getKey()) && eq(value, e.getValue()); 2079 } 2080 2081 public int hashCode() { 2082 return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode()); 2083 } 2084 2085 public String toString() { 2086 return key + "=" + value; 2087 } 2088 2089 private static boolean eq(Object o1, Object o2) { 2090 return (o1 == null ? o2 == null : o1.equals(o2)); 2091 } 2092 } 2093 2094 private static class MyHashMap extends HashMap { 2095 protected Object key; 2096 protected Object value; 2097 2098 public MyHashMap(int k) { 2099 super(11); 2100 } 2101 2102 public MyHashMap(Map map) { 2103 putAll(map); 2104 } 2105 2106 public Object put(Object arg0, Object arg1) { 2107 this.key = arg0; 2108 this.value = arg1; 2109 return super.put(arg0, arg1); 2110 } 2111 2112 public Object getKey() { 2113 return key; 2114 } 2115 2116 public Object getValue() { 2117 return value; 2118 } 2119 2120 public Object getObject(Object arg) { 2121 return super.get(arg); 2122 } 2123 2124 protected void testProtected() { 2125 } 2127 2128 private void readObject(java.io.ObjectInputStream s) throws IOException , ClassNotFoundException { 2129 s.defaultReadObject(); 2130 } 2131 2132 private void writeObject(java.io.ObjectOutputStream s) throws IOException { 2133 s.defaultWriteObject(); 2134 } 2135 } 2136 2137 private static class MyHashMap2 extends MyHashMap { 2138 public MyHashMap2() { 2139 super(12); 2140 } 2141 } 2142 2143 private static class MyTreeMap extends TreeMap { 2144 public MyTreeMap() { 2145 super(); 2146 } 2147 2148 public MyTreeMap(Comparator comparator) { 2149 super(comparator); 2150 } 2151 } 2152 2153 private static class MyHashMap3 extends HashMap { 2154 private int i; 2155 2156 public MyHashMap3(int i) { 2157 super(); 2158 this.i = i; 2159 } 2160 2161 public int getI() { 2162 return i; 2164 } 2165 } 2166 2167 private static class MyTreeMap2 extends MyTreeMap { 2168 private Object key; 2169 private Object value; 2170 2171 public MyTreeMap2(Comparator comparator) { 2172 super(comparator); 2173 } 2174 2175 public Object put(Object arg0, Object arg1) { 2176 this.key = arg0; 2177 this.value = arg1; 2178 return super.put(arg0, arg1); 2179 } 2180 2181 public Object getKey() { 2182 return key; 2183 } 2184 2185 public Object getValue() { 2186 return value; 2187 } 2188 } 2189 2190 private static class MyHashtable extends Hashtable { 2191 public MyHashtable() { 2192 super(); 2193 } 2194 2195 } 2196 2197 private static class MyHashtable2 extends MyHashtable { 2198 public MyHashtable2() { 2199 super(); 2200 } 2201 } 2202 2203 private static class MyLinkedHashMap extends LinkedHashMap { 2204 public MyLinkedHashMap() { 2205 super(); 2206 } 2207 } 2208 2209 private static class MyLinkedHashMap2 extends MyLinkedHashMap { 2210 private Object key; 2211 private Object value; 2212 2213 public MyLinkedHashMap2() { 2214 super(); 2215 } 2216 2217 public Object put(Object arg0, Object arg1) { 2218 this.key = arg0; 2219 this.value = arg1; 2220 return super.put(arg0, arg1); 2221 } 2222 2223 public Object getKey() { 2224 return key; 2225 } 2226 2227 public Object getValue() { 2228 return value; 2229 } 2230 } 2231 2232 private static class MyLinkedHashMap3 extends LinkedHashMap { 2233 public MyLinkedHashMap3(boolean accessOrder) { 2234 super(10, 0.75f, accessOrder); 2235 } 2236 } 2237 2238 private static class MyTHashMap extends THashMap { 2239 public MyTHashMap() { 2240 super(); 2241 } 2242 } 2243 2244 private static class MyFastHashMap extends FastHashMap { 2245 public MyFastHashMap() { 2246 super(); 2247 } 2248 } 2249 2250 private static class MyProperties extends Properties { 2251 private Object key; 2252 private Object value; 2253 2254 public MyProperties() { 2255 super(); 2256 } 2257 2258 public Object put(Object arg0, Object arg1) { 2259 this.key = arg0; 2260 this.value = arg1; 2261 return super.put(arg0, arg1); 2262 } 2263 2264 public Object getKey() { 2265 return key; 2266 } 2267 2268 public Object getValue() { 2269 return value; 2270 } 2271 } 2272 2273 private static class MyProperties2 extends MyProperties { 2274 private Object lastGetKey; 2275 2276 public MyProperties2() { 2277 super(); 2278 } 2279 2280 public synchronized Object get(Object key) { 2281 this.lastGetKey = key; 2282 return super.get(key); 2283 } 2284 2285 public Object getLastGetKey() { 2286 return lastGetKey; 2287 } 2288 } 2289 2290 private static class MyProperties3 extends Properties { 2291 public MyProperties3() { 2292 super(); 2293 } 2294 2295 public void setDefault(Properties newDefaults) { 2296 defaults = newDefaults; 2297 } 2298 2299 public Object put(Object arg0, Object arg1) { 2300 return super.put(arg0, arg1); 2301 } 2302 } 2303 2304 private static class MyObjectFunction implements TObjectFunction { 2305 2306 public Object execute(Object value) { 2307 return new Integer (((Integer ) value).intValue() + 1); 2308 } 2309 2310 } 2311 2312 private static class MyNonPortableObject { 2313 public MyNonPortableObject() { 2314 super(); 2315 } 2316 } 2317 2318} 2319 | Popular Tags |