1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
|
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* This file is available under and governed by the GNU General Public
* License version 2 only, as published by the Free Software Foundation.
* However, the following notice accompanied the original version of this
* file:
*
* Written by Doug Lea and Martin Buchholz with assistance from
* members of JCP JSR-166 Expert Group and released to the public
* domain, as explained at
* http://creativecommons.org/publicdomain/zero/1.0/
* Other contributors include Andrew Wright, Jeffrey Hayes,
* Pat Fisher, Mike Judd.
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import junit.framework.Test;
public class ConcurrentHashMapTest extends JSR166TestCase {
public static void main(String[] args) {
main(suite(), args);
}
public static Test suite() {
class Implementation implements MapImplementation {
public Class<?> klazz() { return ConcurrentHashMap.class; }
public Map emptyMap() { return new ConcurrentHashMap(); }
public Object makeKey(int i) { return i; }
public Object makeValue(int i) { return i; }
public boolean isConcurrent() { return true; }
public boolean permitsNullKeys() { return false; }
public boolean permitsNullValues() { return false; }
public boolean supportsSetValue() { return true; }
}
return newTestSuite(
ConcurrentHashMapTest.class,
MapTest.testSuite(new Implementation()));
}
/**
* Returns a new map from Integers 1-5 to Strings "A"-"E".
*/
private static ConcurrentHashMap<Integer, String> map5() {
ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>(5);
assertTrue(map.isEmpty());
map.put(one, "A");
map.put(two, "B");
map.put(three, "C");
map.put(four, "D");
map.put(five, "E");
assertFalse(map.isEmpty());
assertEquals(5, map.size());
return map;
}
// classes for testing Comparable fallbacks
static class BI implements Comparable<BI> {
private final int value;
BI(int value) { this.value = value; }
public int compareTo(BI other) {
return Integer.compare(value, other.value);
}
public boolean equals(Object x) {
return (x instanceof BI) && ((BI)x).value == value;
}
public int hashCode() { return 42; }
}
static class CI extends BI { CI(int value) { super(value); } }
static class DI extends BI { DI(int value) { super(value); } }
static class BS implements Comparable<BS> {
private final String value;
BS(String value) { this.value = value; }
public int compareTo(BS other) {
return value.compareTo(other.value);
}
public boolean equals(Object x) {
return (x instanceof BS) && value.equals(((BS)x).value);
}
public int hashCode() { return 42; }
}
static class LexicographicList<E extends Comparable<E>> extends ArrayList<E>
implements Comparable<LexicographicList<E>> {
LexicographicList(Collection<E> c) { super(c); }
LexicographicList(E e) { super(Collections.singleton(e)); }
public int compareTo(LexicographicList<E> other) {
int common = Math.min(size(), other.size());
int r = 0;
for (int i = 0; i < common; i++) {
if ((r = get(i).compareTo(other.get(i))) != 0)
break;
}
if (r == 0)
r = Integer.compare(size(), other.size());
return r;
}
private static final long serialVersionUID = 0;
}
static class CollidingObject {
final String value;
CollidingObject(final String value) { this.value = value; }
public int hashCode() { return this.value.hashCode() & 1; }
public boolean equals(final Object obj) {
return (obj instanceof CollidingObject) && ((CollidingObject)obj).value.equals(value);
}
}
static class ComparableCollidingObject extends CollidingObject implements Comparable<ComparableCollidingObject> {
ComparableCollidingObject(final String value) { super(value); }
public int compareTo(final ComparableCollidingObject o) {
return value.compareTo(o.value);
}
}
/**
* Inserted elements that are subclasses of the same Comparable
* class are found.
*/
public void testComparableFamily() {
int size = 500; // makes measured test run time -> 60ms
ConcurrentHashMap<BI, Boolean> m = new ConcurrentHashMap<>();
for (int i = 0; i < size; i++) {
assertNull(m.put(new CI(i), true));
}
for (int i = 0; i < size; i++) {
assertTrue(m.containsKey(new CI(i)));
assertTrue(m.containsKey(new DI(i)));
}
}
/**
* Elements of classes with erased generic type parameters based
* on Comparable can be inserted and found.
*/
public void testGenericComparable() {
int size = 120; // makes measured test run time -> 60ms
ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>();
for (int i = 0; i < size; i++) {
BI bi = new BI(i);
BS bs = new BS(String.valueOf(i));
LexicographicList<BI> bis = new LexicographicList<>(bi);
LexicographicList<BS> bss = new LexicographicList<>(bs);
assertNull(m.putIfAbsent(bis, true));
assertTrue(m.containsKey(bis));
if (m.putIfAbsent(bss, true) == null)
assertTrue(m.containsKey(bss));
assertTrue(m.containsKey(bis));
}
for (int i = 0; i < size; i++) {
assertTrue(m.containsKey(Collections.singletonList(new BI(i))));
}
}
/**
* Elements of non-comparable classes equal to those of classes
* with erased generic type parameters based on Comparable can be
* inserted and found.
*/
public void testGenericComparable2() {
int size = 500; // makes measured test run time -> 60ms
ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>();
for (int i = 0; i < size; i++) {
m.put(Collections.singletonList(new BI(i)), true);
}
for (int i = 0; i < size; i++) {
LexicographicList<BI> bis = new LexicographicList<>(new BI(i));
assertTrue(m.containsKey(bis));
}
}
/**
* Mixtures of instances of comparable and non-comparable classes
* can be inserted and found.
*/
public void testMixedComparable() {
int size = 1200; // makes measured test run time -> 35ms
ConcurrentHashMap<Object, Object> map = new ConcurrentHashMap<>();
Random rng = new Random();
for (int i = 0; i < size; i++) {
Object x;
switch (rng.nextInt(4)) {
case 0:
x = new Object();
break;
case 1:
x = new CollidingObject(Integer.toString(i));
break;
default:
x = new ComparableCollidingObject(Integer.toString(i));
}
assertNull(map.put(x, x));
}
int count = 0;
for (Object k : map.keySet()) {
assertEquals(map.get(k), k);
++count;
}
assertEquals(count, size);
assertEquals(map.size(), size);
for (Object k : map.keySet()) {
assertEquals(map.put(k, k), k);
}
}
/**
* clear removes all pairs
*/
public void testClear() {
ConcurrentHashMap map = map5();
map.clear();
assertEquals(0, map.size());
}
/**
* Maps with same contents are equal
*/
public void testEquals() {
ConcurrentHashMap map1 = map5();
ConcurrentHashMap map2 = map5();
assertEquals(map1, map2);
assertEquals(map2, map1);
map1.clear();
assertFalse(map1.equals(map2));
assertFalse(map2.equals(map1));
}
/**
* hashCode() equals sum of each key.hashCode ^ value.hashCode
*/
public void testHashCode() {
ConcurrentHashMap<Integer,String> map = map5();
int sum = 0;
for (Map.Entry<Integer,String> e : map.entrySet())
sum += e.getKey().hashCode() ^ e.getValue().hashCode();
assertEquals(sum, map.hashCode());
}
/**
* contains returns true for contained value
*/
public void testContains() {
ConcurrentHashMap map = map5();
assertTrue(map.contains("A"));
assertFalse(map.contains("Z"));
}
/**
* containsKey returns true for contained key
*/
public void testContainsKey() {
ConcurrentHashMap map = map5();
assertTrue(map.containsKey(one));
assertFalse(map.containsKey(zero));
}
/**
* containsValue returns true for held values
*/
public void testContainsValue() {
ConcurrentHashMap map = map5();
assertTrue(map.containsValue("A"));
assertFalse(map.containsValue("Z"));
}
/**
* enumeration returns an enumeration containing the correct
* elements
*/
public void testEnumeration() {
ConcurrentHashMap map = map5();
Enumeration e = map.elements();
int count = 0;
while (e.hasMoreElements()) {
count++;
e.nextElement();
}
assertEquals(5, count);
}
/**
* get returns the correct element at the given key,
* or null if not present
*/
public void testGet() {
ConcurrentHashMap map = map5();
assertEquals("A", (String)map.get(one));
ConcurrentHashMap empty = new ConcurrentHashMap();
assertNull(map.get("anything"));
assertNull(empty.get("anything"));
}
/**
* isEmpty is true of empty map and false for non-empty
*/
public void testIsEmpty() {
ConcurrentHashMap empty = new ConcurrentHashMap();
ConcurrentHashMap map = map5();
assertTrue(empty.isEmpty());
assertFalse(map.isEmpty());
}
/**
* keys returns an enumeration containing all the keys from the map
*/
public void testKeys() {
ConcurrentHashMap map = map5();
Enumeration e = map.keys();
int count = 0;
while (e.hasMoreElements()) {
count++;
e.nextElement();
}
assertEquals(5, count);
}
/**
* keySet returns a Set containing all the keys
*/
public void testKeySet() {
ConcurrentHashMap map = map5();
Set s = map.keySet();
assertEquals(5, s.size());
assertTrue(s.contains(one));
assertTrue(s.contains(two));
assertTrue(s.contains(three));
assertTrue(s.contains(four));
assertTrue(s.contains(five));
}
/**
* Test keySet().removeAll on empty map
*/
public void testKeySet_empty_removeAll() {
ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>();
Set<Integer> set = map.keySet();
set.removeAll(Collections.emptyList());
assertTrue(map.isEmpty());
assertTrue(set.isEmpty());
// following is test for JDK-8163353
set.removeAll(Collections.emptySet());
assertTrue(map.isEmpty());
assertTrue(set.isEmpty());
}
/**
* keySet.toArray returns contains all keys
*/
public void testKeySetToArray() {
ConcurrentHashMap map = map5();
Set s = map.keySet();
Object[] ar = s.toArray();
assertTrue(s.containsAll(Arrays.asList(ar)));
assertEquals(5, ar.length);
ar[0] = m10;
assertFalse(s.containsAll(Arrays.asList(ar)));
}
/**
* Values.toArray contains all values
*/
public void testValuesToArray() {
ConcurrentHashMap map = map5();
Collection v = map.values();
Object[] ar = v.toArray();
ArrayList s = new ArrayList(Arrays.asList(ar));
assertEquals(5, ar.length);
assertTrue(s.contains("A"));
assertTrue(s.contains("B"));
assertTrue(s.contains("C"));
assertTrue(s.contains("D"));
assertTrue(s.contains("E"));
}
/**
* entrySet.toArray contains all entries
*/
public void testEntrySetToArray() {
ConcurrentHashMap map = map5();
Set s = map.entrySet();
Object[] ar = s.toArray();
assertEquals(5, ar.length);
for (int i = 0; i < 5; ++i) {
assertTrue(map.containsKey(((Map.Entry)(ar[i])).getKey()));
assertTrue(map.containsValue(((Map.Entry)(ar[i])).getValue()));
}
}
/**
* values collection contains all values
*/
public void testValues() {
ConcurrentHashMap map = map5();
Collection s = map.values();
assertEquals(5, s.size());
assertTrue(s.contains("A"));
assertTrue(s.contains("B"));
assertTrue(s.contains("C"));
assertTrue(s.contains("D"));
assertTrue(s.contains("E"));
}
/**
* entrySet contains all pairs
*/
public void testEntrySet() {
ConcurrentHashMap map = map5();
Set s = map.entrySet();
assertEquals(5, s.size());
Iterator it = s.iterator();
while (it.hasNext()) {
Map.Entry e = (Map.Entry) it.next();
assertTrue(
(e.getKey().equals(one) && e.getValue().equals("A")) ||
(e.getKey().equals(two) && e.getValue().equals("B")) ||
(e.getKey().equals(three) && e.getValue().equals("C")) ||
(e.getKey().equals(four) && e.getValue().equals("D")) ||
(e.getKey().equals(five) && e.getValue().equals("E")));
}
}
/**
* putAll adds all key-value pairs from the given map
*/
public void testPutAll() {
ConcurrentHashMap empty = new ConcurrentHashMap();
ConcurrentHashMap map = map5();
empty.putAll(map);
assertEquals(5, empty.size());
assertTrue(empty.containsKey(one));
assertTrue(empty.containsKey(two));
assertTrue(empty.containsKey(three));
assertTrue(empty.containsKey(four));
assertTrue(empty.containsKey(five));
}
/**
* putIfAbsent works when the given key is not present
*/
public void testPutIfAbsent() {
ConcurrentHashMap map = map5();
map.putIfAbsent(six, "Z");
assertTrue(map.containsKey(six));
}
/**
* putIfAbsent does not add the pair if the key is already present
*/
public void testPutIfAbsent2() {
ConcurrentHashMap map = map5();
assertEquals("A", map.putIfAbsent(one, "Z"));
}
/**
* replace fails when the given key is not present
*/
public void testReplace() {
ConcurrentHashMap map = map5();
assertNull(map.replace(six, "Z"));
assertFalse(map.containsKey(six));
}
/**
* replace succeeds if the key is already present
*/
public void testReplace2() {
ConcurrentHashMap map = map5();
assertNotNull(map.replace(one, "Z"));
assertEquals("Z", map.get(one));
}
/**
* replace value fails when the given key not mapped to expected value
*/
public void testReplaceValue() {
ConcurrentHashMap map = map5();
assertEquals("A", map.get(one));
assertFalse(map.replace(one, "Z", "Z"));
assertEquals("A", map.get(one));
}
/**
* replace value succeeds when the given key mapped to expected value
*/
public void testReplaceValue2() {
ConcurrentHashMap map = map5();
assertEquals("A", map.get(one));
assertTrue(map.replace(one, "A", "Z"));
assertEquals("Z", map.get(one));
}
/**
* remove removes the correct key-value pair from the map
*/
public void testRemove() {
ConcurrentHashMap map = map5();
map.remove(five);
assertEquals(4, map.size());
assertFalse(map.containsKey(five));
}
/**
* remove(key,value) removes only if pair present
*/
public void testRemove2() {
ConcurrentHashMap map = map5();
map.remove(five, "E");
assertEquals(4, map.size());
assertFalse(map.containsKey(five));
map.remove(four, "A");
assertEquals(4, map.size());
assertTrue(map.containsKey(four));
}
/**
* size returns the correct values
*/
public void testSize() {
ConcurrentHashMap map = map5();
ConcurrentHashMap empty = new ConcurrentHashMap();
assertEquals(0, empty.size());
assertEquals(5, map.size());
}
/**
* toString contains toString of elements
*/
public void testToString() {
ConcurrentHashMap map = map5();
String s = map.toString();
for (int i = 1; i <= 5; ++i) {
assertTrue(s.contains(String.valueOf(i)));
}
}
// Exception tests
/**
* Cannot create with only negative capacity
*/
public void testConstructor1() {
try {
new ConcurrentHashMap(-1);
shouldThrow();
} catch (IllegalArgumentException success) {}
}
/**
* Constructor (initialCapacity, loadFactor) throws
* IllegalArgumentException if either argument is negative
*/
public void testConstructor2() {
try {
new ConcurrentHashMap(-1, .75f);
shouldThrow();
} catch (IllegalArgumentException success) {}
try {
new ConcurrentHashMap(16, -1);
shouldThrow();
} catch (IllegalArgumentException success) {}
}
/**
* Constructor (initialCapacity, loadFactor, concurrencyLevel)
* throws IllegalArgumentException if any argument is negative
*/
public void testConstructor3() {
try {
new ConcurrentHashMap(-1, .75f, 1);
shouldThrow();
} catch (IllegalArgumentException success) {}
try {
new ConcurrentHashMap(16, -1, 1);
shouldThrow();
} catch (IllegalArgumentException success) {}
try {
new ConcurrentHashMap(16, .75f, -1);
shouldThrow();
} catch (IllegalArgumentException success) {}
}
/**
* ConcurrentHashMap(map) throws NullPointerException if the given
* map is null
*/
public void testConstructor4() {
try {
new ConcurrentHashMap(null);
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* ConcurrentHashMap(map) creates a new map with the same mappings
* as the given map
*/
public void testConstructor5() {
ConcurrentHashMap map1 = map5();
ConcurrentHashMap map2 = new ConcurrentHashMap(map5());
assertTrue(map2.equals(map1));
map2.put(one, "F");
assertFalse(map2.equals(map1));
}
/**
* get(null) throws NPE
*/
public void testGet_NullPointerException() {
ConcurrentHashMap c = new ConcurrentHashMap(5);
try {
c.get(null);
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* containsKey(null) throws NPE
*/
public void testContainsKey_NullPointerException() {
ConcurrentHashMap c = new ConcurrentHashMap(5);
try {
c.containsKey(null);
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* containsValue(null) throws NPE
*/
public void testContainsValue_NullPointerException() {
ConcurrentHashMap c = new ConcurrentHashMap(5);
try {
c.containsValue(null);
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* contains(null) throws NPE
*/
public void testContains_NullPointerException() {
ConcurrentHashMap c = new ConcurrentHashMap(5);
try {
c.contains(null);
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* put(null,x) throws NPE
*/
public void testPut1_NullPointerException() {
ConcurrentHashMap c = new ConcurrentHashMap(5);
try {
c.put(null, "whatever");
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* put(x, null) throws NPE
*/
public void testPut2_NullPointerException() {
ConcurrentHashMap c = new ConcurrentHashMap(5);
try {
c.put("whatever", null);
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* putIfAbsent(null, x) throws NPE
*/
public void testPutIfAbsent1_NullPointerException() {
ConcurrentHashMap c = new ConcurrentHashMap(5);
try {
c.putIfAbsent(null, "whatever");
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* replace(null, x) throws NPE
*/
public void testReplace_NullPointerException() {
ConcurrentHashMap c = new ConcurrentHashMap(5);
try {
c.replace(null, "whatever");
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* replace(null, x, y) throws NPE
*/
public void testReplaceValue_NullPointerException() {
ConcurrentHashMap c = new ConcurrentHashMap(5);
try {
c.replace(null, one, "whatever");
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* putIfAbsent(x, null) throws NPE
*/
public void testPutIfAbsent2_NullPointerException() {
ConcurrentHashMap c = new ConcurrentHashMap(5);
try {
c.putIfAbsent("whatever", null);
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* replace(x, null) throws NPE
*/
public void testReplace2_NullPointerException() {
ConcurrentHashMap c = new ConcurrentHashMap(5);
try {
c.replace("whatever", null);
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* replace(x, null, y) throws NPE
*/
public void testReplaceValue2_NullPointerException() {
ConcurrentHashMap c = new ConcurrentHashMap(5);
try {
c.replace("whatever", null, "A");
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* replace(x, y, null) throws NPE
*/
public void testReplaceValue3_NullPointerException() {
ConcurrentHashMap c = new ConcurrentHashMap(5);
try {
c.replace("whatever", one, null);
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* remove(null) throws NPE
*/
public void testRemove1_NullPointerException() {
ConcurrentHashMap c = new ConcurrentHashMap(5);
c.put("sadsdf", "asdads");
try {
c.remove(null);
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* remove(null, x) throws NPE
*/
public void testRemove2_NullPointerException() {
ConcurrentHashMap c = new ConcurrentHashMap(5);
c.put("sadsdf", "asdads");
try {
c.remove(null, "whatever");
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* remove(x, null) returns false
*/
public void testRemove3() {
ConcurrentHashMap c = new ConcurrentHashMap(5);
c.put("sadsdf", "asdads");
assertFalse(c.remove("sadsdf", null));
}
/**
* A deserialized/reserialized map equals original
*/
public void testSerialization() throws Exception {
Map x = map5();
Map y = serialClone(x);
assertNotSame(x, y);
assertEquals(x.size(), y.size());
assertEquals(x, y);
assertEquals(y, x);
}
/**
* SetValue of an EntrySet entry sets value in the map.
*/
public void testSetValueWriteThrough() {
// Adapted from a bug report by Eric Zoerner
ConcurrentHashMap map = new ConcurrentHashMap(2, 5.0f, 1);
assertTrue(map.isEmpty());
for (int i = 0; i < 20; i++)
map.put(new Integer(i), new Integer(i));
assertFalse(map.isEmpty());
Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next();
// Unless it happens to be first (in which case remainder of
// test is skipped), remove a possibly-colliding key from map
// which, under some implementations, may cause entry1 to be
// cloned in map
if (!entry1.getKey().equals(new Integer(16))) {
map.remove(new Integer(16));
entry1.setValue("XYZ");
assertTrue(map.containsValue("XYZ")); // fails if write-through broken
}
}
/**
* Tests performance of removeAll when the other collection is much smaller.
* ant -Djsr166.tckTestClass=ConcurrentHashMapTest -Djsr166.methodFilter=testRemoveAll_performance -Djsr166.expensiveTests=true tck
*/
public void testRemoveAll_performance() {
final int mapSize = expensiveTests ? 1_000_000 : 100;
final int iterations = expensiveTests ? 500 : 2;
final ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>();
for (int i = 0; i < mapSize; i++)
map.put(i, i);
Set<Integer> keySet = map.keySet();
Collection<Integer> removeMe = Arrays.asList(new Integer[] { -99, -86 });
for (int i = 0; i < iterations; i++)
assertFalse(keySet.removeAll(removeMe));
assertEquals(mapSize, map.size());
}
}
|