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 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
|
/*
* Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved.
* 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.
*/
/*
* @test
* @bug 8010122 8004518 8024331 8024688
* @summary Test Map default methods
* @author Mike Duigou
* @run testng Defaults
*/
import org.testng.Assert.ThrowingRunnable;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
import static java.util.Objects.requireNonNull;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertSame;
import static org.testng.Assert.assertThrows;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
public class Defaults {
@Test(dataProvider = "Map<IntegerEnum,String> rw=all keys=withNull values=withNull")
public void testGetOrDefaultNulls(String description, Map<IntegerEnum, String> map) {
assertTrue(map.containsKey(null), description + ": null key absent");
assertNull(map.get(null), description + ": value not null");
assertSame(map.get(null), map.getOrDefault(null, EXTRA_VALUE), description + ": values should match");
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=all keys=all values=all")
public void testGetOrDefault(String description, Map<IntegerEnum, String> map) {
assertTrue(map.containsKey(KEYS[1]), "expected key missing");
assertSame(map.get(KEYS[1]), map.getOrDefault(KEYS[1], EXTRA_VALUE), "values should match");
assertFalse(map.containsKey(EXTRA_KEY), "expected absent key");
assertSame(map.getOrDefault(EXTRA_KEY, EXTRA_VALUE), EXTRA_VALUE, "value not returned as default");
assertNull(map.getOrDefault(EXTRA_KEY, null), "null not returned as default");
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=withNull values=withNull")
public void testPutIfAbsentNulls(String description, Map<IntegerEnum, String> map) {
// null -> null
assertTrue(map.containsKey(null), "null key absent");
assertNull(map.get(null), "value not null");
assertNull(map.putIfAbsent(null, EXTRA_VALUE), "previous not null");
// null -> EXTRA_VALUE
assertTrue(map.containsKey(null), "null key absent");
assertSame(map.get(null), EXTRA_VALUE, "unexpected value");
assertSame(map.putIfAbsent(null, null), EXTRA_VALUE, "previous not expected value");
assertTrue(map.containsKey(null), "null key absent");
assertSame(map.get(null), EXTRA_VALUE, "unexpected value");
assertSame(map.remove(null), EXTRA_VALUE, "removed unexpected value");
// null -> <absent>
assertFalse(map.containsKey(null), description + ": key present after remove");
assertNull(map.putIfAbsent(null, null), "previous not null");
// null -> null
assertTrue(map.containsKey(null), "null key absent");
assertNull(map.get(null), "value not null");
assertNull(map.putIfAbsent(null, EXTRA_VALUE), "previous not null");
assertSame(map.get(null), EXTRA_VALUE, "value not expected");
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all")
public void testPutIfAbsent(String description, Map<IntegerEnum, String> map) {
// 1 -> 1
assertTrue(map.containsKey(KEYS[1]));
Object expected = map.get(KEYS[1]);
assertTrue(null == expected || expected == VALUES[1]);
assertSame(map.putIfAbsent(KEYS[1], EXTRA_VALUE), expected);
assertSame(map.get(KEYS[1]), expected);
// EXTRA_KEY -> <absent>
assertFalse(map.containsKey(EXTRA_KEY));
assertSame(map.putIfAbsent(EXTRA_KEY, EXTRA_VALUE), null);
assertSame(map.get(EXTRA_KEY), EXTRA_VALUE);
assertSame(map.putIfAbsent(EXTRA_KEY, VALUES[2]), EXTRA_VALUE);
assertSame(map.get(EXTRA_KEY), EXTRA_VALUE);
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=all keys=all values=all")
public void testForEach(String description, Map<IntegerEnum, String> map) {
IntegerEnum[] EACH_KEY = new IntegerEnum[map.size()];
map.forEach((k, v) -> {
int idx = (null == k) ? 0 : k.ordinal(); // substitute for index.
assertNull(EACH_KEY[idx]);
EACH_KEY[idx] = (idx == 0) ? KEYS[0] : k; // substitute for comparison.
assertSame(v, map.get(k));
});
assertEquals(KEYS, EACH_KEY, description);
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all")
public static void testReplaceAll(String description, Map<IntegerEnum, String> map) {
IntegerEnum[] EACH_KEY = new IntegerEnum[map.size()];
Set<String> EACH_REPLACE = new HashSet<>(map.size());
map.replaceAll((k,v) -> {
int idx = (null == k) ? 0 : k.ordinal(); // substitute for index.
assertNull(EACH_KEY[idx]);
EACH_KEY[idx] = (idx == 0) ? KEYS[0] : k; // substitute for comparison.
assertSame(v, map.get(k));
String replacement = v + " replaced";
EACH_REPLACE.add(replacement);
return replacement;
});
assertEquals(KEYS, EACH_KEY, description);
assertEquals(map.values().size(), EACH_REPLACE.size(), description + EACH_REPLACE);
assertTrue(EACH_REPLACE.containsAll(map.values()), description + " : " + EACH_REPLACE + " != " + map.values());
assertTrue(map.values().containsAll(EACH_REPLACE), description + " : " + EACH_REPLACE + " != " + map.values());
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=nonNull values=nonNull")
public static void testReplaceAllNoNullReplacement(String description, Map<IntegerEnum, String> map) {
assertThrowsNPE(() -> map.replaceAll(null));
assertThrowsNPE(() -> map.replaceAll((k,v) -> null)); //should not allow replacement with null value
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=withNull values=withNull")
public static void testRemoveNulls(String description, Map<IntegerEnum, String> map) {
assertTrue(map.containsKey(null), "null key absent");
assertNull(map.get(null), "value not null");
assertFalse(map.remove(null, EXTRA_VALUE), description);
assertTrue(map.containsKey(null));
assertNull(map.get(null));
assertTrue(map.remove(null, null));
assertFalse(map.containsKey(null));
assertNull(map.get(null));
assertFalse(map.remove(null, null));
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all")
public static void testRemove(String description, Map<IntegerEnum, String> map) {
assertTrue(map.containsKey(KEYS[1]));
Object expected = map.get(KEYS[1]);
assertTrue(null == expected || expected == VALUES[1]);
assertFalse(map.remove(KEYS[1], EXTRA_VALUE), description);
assertSame(map.get(KEYS[1]), expected);
assertTrue(map.remove(KEYS[1], expected));
assertNull(map.get(KEYS[1]));
assertFalse(map.remove(KEYS[1], expected));
assertFalse(map.containsKey(EXTRA_KEY));
assertFalse(map.remove(EXTRA_KEY, EXTRA_VALUE));
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=withNull values=withNull")
public void testReplaceKVNulls(String description, Map<IntegerEnum, String> map) {
assertTrue(map.containsKey(null), "null key absent");
assertNull(map.get(null), "value not null");
assertSame(map.replace(null, EXTRA_VALUE), null);
assertSame(map.get(null), EXTRA_VALUE);
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=nonNull values=nonNull")
public void testReplaceKVNoNulls(String description, Map<IntegerEnum, String> map) {
assertTrue(map.containsKey(FIRST_KEY), "expected key missing");
assertSame(map.get(FIRST_KEY), FIRST_VALUE, "found wrong value");
assertThrowsNPE(() -> map.replace(FIRST_KEY, null));
assertSame(map.replace(FIRST_KEY, EXTRA_VALUE), FIRST_VALUE, description + ": replaced wrong value");
assertSame(map.get(FIRST_KEY), EXTRA_VALUE, "found wrong value");
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all")
public void testReplaceKV(String description, Map<IntegerEnum, String> map) {
assertTrue(map.containsKey(KEYS[1]));
Object expected = map.get(KEYS[1]);
assertTrue(null == expected || expected == VALUES[1]);
assertSame(map.replace(KEYS[1], EXTRA_VALUE), expected);
assertSame(map.get(KEYS[1]), EXTRA_VALUE);
assertFalse(map.containsKey(EXTRA_KEY));
assertNull(map.replace(EXTRA_KEY, EXTRA_VALUE));
assertFalse(map.containsKey(EXTRA_KEY));
assertNull(map.get(EXTRA_KEY));
assertNull(map.put(EXTRA_KEY, EXTRA_VALUE));
assertSame(map.get(EXTRA_KEY), EXTRA_VALUE);
assertSame(map.replace(EXTRA_KEY, (String)expected), EXTRA_VALUE);
assertSame(map.get(EXTRA_KEY), expected);
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=withNull values=withNull")
public void testReplaceKVVNulls(String description, Map<IntegerEnum, String> map) {
assertTrue(map.containsKey(null), "null key absent");
assertNull(map.get(null), "value not null");
assertFalse(map.replace(null, EXTRA_VALUE, EXTRA_VALUE));
assertNull(map.get(null));
assertTrue(map.replace(null, null, EXTRA_VALUE));
assertSame(map.get(null), EXTRA_VALUE);
assertTrue(map.replace(null, EXTRA_VALUE, EXTRA_VALUE));
assertSame(map.get(null), EXTRA_VALUE);
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=nonNull values=nonNull")
public void testReplaceKVVNoNulls(String description, Map<IntegerEnum, String> map) {
assertTrue(map.containsKey(FIRST_KEY), "expected key missing");
assertSame(map.get(FIRST_KEY), FIRST_VALUE, "found wrong value");
assertThrowsNPE(() -> map.replace(FIRST_KEY, FIRST_VALUE, null));
assertThrowsNPE(
() -> {
if (!map.replace(FIRST_KEY, null, EXTRA_VALUE)) {
throw new NullPointerException("default returns false rather than throwing");
}
});
assertTrue(map.replace(FIRST_KEY, FIRST_VALUE, EXTRA_VALUE), description + ": replaced wrong value");
assertSame(map.get(FIRST_KEY), EXTRA_VALUE, "found wrong value");
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all")
public void testReplaceKVV(String description, Map<IntegerEnum, String> map) {
assertTrue(map.containsKey(KEYS[1]));
Object expected = map.get(KEYS[1]);
assertTrue(null == expected || expected == VALUES[1]);
assertFalse(map.replace(KEYS[1], EXTRA_VALUE, EXTRA_VALUE));
assertSame(map.get(KEYS[1]), expected);
assertTrue(map.replace(KEYS[1], (String)expected, EXTRA_VALUE));
assertSame(map.get(KEYS[1]), EXTRA_VALUE);
assertTrue(map.replace(KEYS[1], EXTRA_VALUE, EXTRA_VALUE));
assertSame(map.get(KEYS[1]), EXTRA_VALUE);
assertFalse(map.containsKey(EXTRA_KEY));
assertFalse(map.replace(EXTRA_KEY, EXTRA_VALUE, EXTRA_VALUE));
assertFalse(map.containsKey(EXTRA_KEY));
assertNull(map.get(EXTRA_KEY));
assertNull(map.put(EXTRA_KEY, EXTRA_VALUE));
assertTrue(map.containsKey(EXTRA_KEY));
assertSame(map.get(EXTRA_KEY), EXTRA_VALUE);
assertTrue(map.replace(EXTRA_KEY, EXTRA_VALUE, EXTRA_VALUE));
assertSame(map.get(EXTRA_KEY), EXTRA_VALUE);
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=withNull values=withNull")
public void testComputeIfAbsentNulls(String description, Map<IntegerEnum, String> map) {
// null -> null
assertTrue(map.containsKey(null), "null key absent");
assertNull(map.get(null), "value not null");
assertSame(map.computeIfAbsent(null, (k) -> null), null, "not expected result");
assertTrue(map.containsKey(null), "null key absent");
assertNull(map.get(null), "value not null");
assertSame(map.computeIfAbsent(null, (k) -> EXTRA_VALUE), EXTRA_VALUE, "not mapped to result");
// null -> EXTRA_VALUE
assertTrue(map.containsKey(null), "null key absent");
assertSame(map.get(null), EXTRA_VALUE, "not expected value");
assertSame(map.remove(null), EXTRA_VALUE, "removed unexpected value");
// null -> <absent>
assertFalse(map.containsKey(null), "null key present");
assertSame(map.computeIfAbsent(null, (k) -> EXTRA_VALUE), EXTRA_VALUE, "not mapped to result");
// null -> EXTRA_VALUE
assertTrue(map.containsKey(null), "null key absent");
assertSame(map.get(null), EXTRA_VALUE, "not expected value");
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all")
public void testComputeIfAbsent(String description, Map<IntegerEnum, String> map) {
// 1 -> 1
assertTrue(map.containsKey(KEYS[1]));
Object expected = map.get(KEYS[1]);
assertTrue(null == expected || expected == VALUES[1], description + String.valueOf(expected));
expected = (null == expected) ? EXTRA_VALUE : expected;
assertSame(map.computeIfAbsent(KEYS[1], (k) -> EXTRA_VALUE), expected, description);
assertSame(map.get(KEYS[1]), expected, description);
// EXTRA_KEY -> <absent>
assertFalse(map.containsKey(EXTRA_KEY));
assertNull(map.computeIfAbsent(EXTRA_KEY, (k) -> null));
assertFalse(map.containsKey(EXTRA_KEY));
assertSame(map.computeIfAbsent(EXTRA_KEY, (k) -> EXTRA_VALUE), EXTRA_VALUE);
// EXTRA_KEY -> EXTRA_VALUE
assertSame(map.get(EXTRA_KEY), EXTRA_VALUE);
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all")
public void testComputeIfAbsentNullFunction(String description, Map<IntegerEnum, String> map) {
assertThrowsNPE(() -> map.computeIfAbsent(KEYS[1], null));
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=withNull values=withNull")
public void testComputeIfPresentNulls(String description, Map<IntegerEnum, String> map) {
assertTrue(map.containsKey(null), description + ": null key absent");
assertNull(map.get(null), description + ": value not null");
assertSame(map.computeIfPresent(null, (k, v) -> {
fail(description + ": null value is not deemed present");
return EXTRA_VALUE;
}), null, description);
assertTrue(map.containsKey(null));
assertNull(map.get(null), description);
assertNull(map.remove(EXTRA_KEY), description + ": unexpected mapping");
assertNull(map.put(EXTRA_KEY, null), description + ": unexpected value");
assertSame(map.computeIfPresent(EXTRA_KEY, (k, v) -> {
fail(description + ": null value is not deemed present");
return EXTRA_VALUE;
}), null, description);
assertNull(map.get(EXTRA_KEY), description + ": null mapping gone");
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all")
public void testComputeIfPresent(String description, Map<IntegerEnum, String> map) {
assertTrue(map.containsKey(KEYS[1]));
Object value = map.get(KEYS[1]);
assertTrue(null == value || value == VALUES[1], description + String.valueOf(value));
Object expected = (null == value) ? null : EXTRA_VALUE;
assertSame(map.computeIfPresent(KEYS[1], (k, v) -> {
assertSame(v, value);
return EXTRA_VALUE;
}), expected, description);
assertSame(map.get(KEYS[1]), expected, description);
assertFalse(map.containsKey(EXTRA_KEY));
assertSame(map.computeIfPresent(EXTRA_KEY, (k, v) -> {
fail();
return EXTRA_VALUE;
}), null);
assertFalse(map.containsKey(EXTRA_KEY));
assertSame(map.get(EXTRA_KEY), null);
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all")
public void testComputeIfPresentNullFunction(String description, Map<IntegerEnum, String> map) {
assertThrowsNPE(() -> map.computeIfPresent(KEYS[1], null));
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=withNull values=withNull")
public void testComputeNulls(String description, Map<IntegerEnum, String> map) {
assertTrue(map.containsKey(null), "null key absent");
assertNull(map.get(null), "value not null");
assertSame(map.compute(null, (k, v) -> {
assertNull(k);
assertNull(v);
return null;
}), null, description);
assertFalse(map.containsKey(null), description + ": null key present.");
assertSame(map.compute(null, (k, v) -> {
assertSame(k, null);
assertNull(v);
return EXTRA_VALUE;
}), EXTRA_VALUE, description);
assertTrue(map.containsKey(null));
assertSame(map.get(null), EXTRA_VALUE, description);
assertSame(map.remove(null), EXTRA_VALUE, description + ": removed value not expected");
// no mapping before and after
assertFalse(map.containsKey(null), description + ": null key present");
assertSame(map.compute(null, (k, v) -> {
assertNull(k);
assertNull(v);
return null;
}), null, description + ": expected null result" );
assertFalse(map.containsKey(null), description + ": null key present");
// compute with map not containing value
assertNull(map.remove(EXTRA_KEY), description + ": unexpected mapping");
assertFalse(map.containsKey(EXTRA_KEY), description + ": key present");
assertSame(map.compute(EXTRA_KEY, (k, v) -> {
assertSame(k, EXTRA_KEY);
assertNull(v);
return null;
}), null, description);
assertFalse(map.containsKey(EXTRA_KEY), description + ": null key present");
// ensure removal.
assertNull(map.put(EXTRA_KEY, EXTRA_VALUE));
assertSame(map.compute(EXTRA_KEY, (k, v) -> {
assertSame(k, EXTRA_KEY);
assertSame(v, EXTRA_VALUE);
return null;
}), null, description + ": null resulted expected");
assertFalse(map.containsKey(EXTRA_KEY), description + ": null key present");
// compute with map containing null value
assertNull(map.put(EXTRA_KEY, null), description + ": unexpected value");
assertSame(map.compute(EXTRA_KEY, (k, v) -> {
assertSame(k, EXTRA_KEY);
assertNull(v);
return null;
}), null, description);
assertFalse(map.containsKey(EXTRA_KEY), description + ": null key present");
assertNull(map.put(EXTRA_KEY, null), description + ": unexpected value");
assertSame(map.compute(EXTRA_KEY, (k, v) -> {
assertSame(k, EXTRA_KEY);
assertNull(v);
return EXTRA_VALUE;
}), EXTRA_VALUE, description);
assertTrue(map.containsKey(EXTRA_KEY), "null key present");
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all")
public void testCompute(String description, Map<IntegerEnum, String> map) {
assertTrue(map.containsKey(KEYS[1]));
Object value = map.get(KEYS[1]);
assertTrue(null == value || value == VALUES[1], description + String.valueOf(value));
assertSame(map.compute(KEYS[1], (k, v) -> {
assertSame(k, KEYS[1]);
assertSame(v, value);
return EXTRA_VALUE;
}), EXTRA_VALUE, description);
assertSame(map.get(KEYS[1]), EXTRA_VALUE, description);
assertNull(map.compute(KEYS[1], (k, v) -> {
assertSame(v, EXTRA_VALUE);
return null;
}), description);
assertFalse(map.containsKey(KEYS[1]));
assertFalse(map.containsKey(EXTRA_KEY));
assertSame(map.compute(EXTRA_KEY, (k, v) -> {
assertNull(v);
return EXTRA_VALUE;
}), EXTRA_VALUE);
assertTrue(map.containsKey(EXTRA_KEY));
assertSame(map.get(EXTRA_KEY), EXTRA_VALUE);
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all")
public void testComputeNullFunction(String description, Map<IntegerEnum, String> map) {
assertThrowsNPE(() -> map.compute(KEYS[1], null));
}
@Test(dataProvider = "MergeCases")
private void testMerge(String description, Map<IntegerEnum, String> map, Merging.Value oldValue, Merging.Value newValue, Merging.Merger merger, Merging.Value put, Merging.Value result) {
// add and check initial conditions.
switch (oldValue) {
case ABSENT :
map.remove(EXTRA_KEY);
assertFalse(map.containsKey(EXTRA_KEY), "key not absent");
break;
case NULL :
map.put(EXTRA_KEY, null);
assertTrue(map.containsKey(EXTRA_KEY), "key absent");
assertNull(map.get(EXTRA_KEY), "wrong value");
break;
case OLDVALUE :
map.put(EXTRA_KEY, VALUES[1]);
assertTrue(map.containsKey(EXTRA_KEY), "key absent");
assertSame(map.get(EXTRA_KEY), VALUES[1], "wrong value");
break;
default:
fail("unexpected old value");
}
String returned = map.merge(EXTRA_KEY,
newValue == Merging.Value.NULL ? (String) null : VALUES[2],
merger
);
// check result
switch (result) {
case NULL :
assertNull(returned, "wrong value");
break;
case NEWVALUE :
assertSame(returned, VALUES[2], "wrong value");
break;
case RESULT :
assertSame(returned, VALUES[3], "wrong value");
break;
default:
fail("unexpected new value");
}
// check map
switch (put) {
case ABSENT :
assertFalse(map.containsKey(EXTRA_KEY), "key not absent");
break;
case NULL :
assertTrue(map.containsKey(EXTRA_KEY), "key absent");
assertNull(map.get(EXTRA_KEY), "wrong value");
break;
case NEWVALUE :
assertTrue(map.containsKey(EXTRA_KEY), "key absent");
assertSame(map.get(EXTRA_KEY), VALUES[2], "wrong value");
break;
case RESULT :
assertTrue(map.containsKey(EXTRA_KEY), "key absent");
assertSame(map.get(EXTRA_KEY), VALUES[3], "wrong value");
break;
default:
fail("unexpected new value");
}
}
@Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all values=all")
public void testMergeNullMerger(String description, Map<IntegerEnum, String> map) {
assertThrowsNPE(() -> map.merge(KEYS[1], VALUES[1], null));
}
/** A function that flipflops between running two other functions. */
static <T,U,V> BiFunction<T,U,V> twoStep(AtomicBoolean b,
BiFunction<T,U,V> first,
BiFunction<T,U,V> second) {
return (t, u) -> {
boolean bb = b.get();
try {
return (b.get() ? first : second).apply(t, u);
} finally {
b.set(!bb);
}};
}
/**
* Simulates races by modifying the map within the mapping function.
*/
@Test
public void testConcurrentMap_computeIfAbsent_racy() {
final ConcurrentMap<Long,Long> map = new ImplementsConcurrentMap<>();
final Long two = 2L;
Function<Long,Long> f, g;
// race not detected if function returns null
f = (k) -> { map.put(two, 42L); return null; };
assertNull(map.computeIfAbsent(two, f));
assertEquals(42L, (long)map.get(two));
map.clear();
f = (k) -> { map.put(two, 42L); return 86L; };
assertEquals(42L, (long)map.computeIfAbsent(two, f));
assertEquals(42L, (long)map.get(two));
// mapping function ignored if value already exists
map.put(two, 99L);
assertEquals(99L, (long)map.computeIfAbsent(two, f));
assertEquals(99L, (long)map.get(two));
}
/**
* Simulates races by modifying the map within the remapping function.
*/
@Test
public void testConcurrentMap_computeIfPresent_racy() {
final AtomicBoolean b = new AtomicBoolean(true);
final ConcurrentMap<Long,Long> map = new ImplementsConcurrentMap<>();
final Long two = 2L;
BiFunction<Long,Long,Long> f, g;
for (Long val : new Long[] { null, 86L }) {
map.clear();
// Function not invoked if no mapping exists
f = (k, v) -> { map.put(two, 42L); return val; };
assertNull(map.computeIfPresent(two, f));
assertNull(map.get(two));
map.put(two, 42L);
f = (k, v) -> { map.put(two, 86L); return val; };
g = (k, v) -> {
assertSame(two, k);
assertEquals(86L, (long)v);
return null;
};
assertNull(map.computeIfPresent(two, twoStep(b, f, g)));
assertFalse(map.containsKey(two));
assertTrue(b.get());
map.put(two, 42L);
f = (k, v) -> { map.put(two, 86L); return val; };
g = (k, v) -> {
assertSame(two, k);
assertEquals(86L, (long)v);
return 99L;
};
assertEquals(99L, (long)map.computeIfPresent(two, twoStep(b, f, g)));
assertTrue(map.containsKey(two));
assertTrue(b.get());
}
}
@Test
public void testConcurrentMap_compute_simple() {
final ConcurrentMap<Long,Long> map = new ImplementsConcurrentMap<>();
BiFunction<Long,Long,Long> fun = (k, v) -> ((v == null) ? 0L : k + v);
assertEquals(Long.valueOf(0L), map.compute(3L, fun));
assertEquals(Long.valueOf(3L), map.compute(3L, fun));
assertEquals(Long.valueOf(6L), map.compute(3L, fun));
assertNull(map.compute(3L, (k, v) -> null));
assertTrue(map.isEmpty());
assertEquals(Long.valueOf(0L), map.compute(new Long(3L), fun));
assertEquals(Long.valueOf(3L), map.compute(new Long(3L), fun));
assertEquals(Long.valueOf(6L), map.compute(new Long(3L), fun));
assertNull(map.compute(3L, (k, v) -> null));
assertTrue(map.isEmpty());
}
/**
* Simulates races by modifying the map within the remapping function.
*/
@Test
public void testConcurrentMap_compute_racy() {
final AtomicBoolean b = new AtomicBoolean(true);
final ConcurrentMap<Long,Long> map = new ImplementsConcurrentMap<>();
final Long two = 2L;
BiFunction<Long,Long,Long> f, g;
// null -> null is a no-op; race not detected
f = (k, v) -> { map.put(two, 42L); return null; };
assertNull(map.compute(two, f));
assertEquals(42L, (long)map.get(two));
for (Long val : new Long[] { null, 86L }) {
map.clear();
f = (k, v) -> { map.put(two, 42L); return 86L; };
g = (k, v) -> {
assertSame(two, k);
assertEquals(42L, (long)v);
return k + v;
};
assertEquals(44L, (long)map.compute(two, twoStep(b, f, g)));
assertEquals(44L, (long)map.get(two));
assertTrue(b.get());
f = (k, v) -> { map.remove(two); return val; };
g = (k, v) -> {
assertSame(two, k);
assertNull(v);
return 44L;
};
assertEquals(44L, (long)map.compute(two, twoStep(b, f, g)));
assertEquals(44L, (long)map.get(two));
assertTrue(map.containsKey(two));
assertTrue(b.get());
f = (k, v) -> { map.remove(two); return val; };
g = (k, v) -> {
assertSame(two, k);
assertNull(v);
return null;
};
assertNull(map.compute(two, twoStep(b, f, g)));
assertNull(map.get(two));
assertFalse(map.containsKey(two));
assertTrue(b.get());
}
}
/**
* Simulates races by modifying the map within the remapping function.
*/
@Test
public void testConcurrentMap_merge_racy() {
final AtomicBoolean b = new AtomicBoolean(true);
final ConcurrentMap<Long,Long> map = new ImplementsConcurrentMap<>();
final Long two = 2L;
BiFunction<Long,Long,Long> f, g;
for (Long val : new Long[] { null, 86L }) {
map.clear();
f = (v, w) -> { throw new AssertionError(); };
assertEquals(99L, (long)map.merge(two, 99L, f));
assertEquals(99L, (long)map.get(two));
f = (v, w) -> { map.put(two, 42L); return val; };
g = (v, w) -> {
assertEquals(42L, (long)v);
assertEquals(3L, (long)w);
return v + w;
};
assertEquals(45L, (long)map.merge(two, 3L, twoStep(b, f, g)));
assertEquals(45L, (long)map.get(two));
assertTrue(b.get());
f = (v, w) -> { map.remove(two); return val; };
g = (k, v) -> { throw new AssertionError(); };
assertEquals(55L, (long)map.merge(two, 55L, twoStep(b, f, g)));
assertEquals(55L, (long)map.get(two));
assertTrue(map.containsKey(two));
assertFalse(b.get()); b.set(true);
}
}
public enum IntegerEnum {
e0, e1, e2, e3, e4, e5, e6, e7, e8, e9,
e10, e11, e12, e13, e14, e15, e16, e17, e18, e19,
e20, e21, e22, e23, e24, e25, e26, e27, e28, e29,
e30, e31, e32, e33, e34, e35, e36, e37, e38, e39,
e40, e41, e42, e43, e44, e45, e46, e47, e48, e49,
e50, e51, e52, e53, e54, e55, e56, e57, e58, e59,
e60, e61, e62, e63, e64, e65, e66, e67, e68, e69,
e70, e71, e72, e73, e74, e75, e76, e77, e78, e79,
e80, e81, e82, e83, e84, e85, e86, e87, e88, e89,
e90, e91, e92, e93, e94, e95, e96, e97, e98, e99,
EXTRA_KEY;
public static final int SIZE = values().length;
}
private static final int TEST_SIZE = IntegerEnum.SIZE - 1;
/**
* Realized keys ensure that there is always a hard ref to all test objects.
*/
private static final IntegerEnum[] KEYS = new IntegerEnum[TEST_SIZE];
/**
* Realized values ensure that there is always a hard ref to all test
* objects.
*/
private static final String[] VALUES = new String[TEST_SIZE];
static {
IntegerEnum[] keys = IntegerEnum.values();
for (int each = 0; each < TEST_SIZE; each++) {
KEYS[each] = keys[each];
VALUES[each] = String.valueOf(each);
}
}
private static final IntegerEnum FIRST_KEY = KEYS[0];
private static final String FIRST_VALUE = VALUES[0];
private static final IntegerEnum EXTRA_KEY = IntegerEnum.EXTRA_KEY;
private static final String EXTRA_VALUE = String.valueOf(TEST_SIZE);
@DataProvider(name = "Map<IntegerEnum,String> rw=all keys=all values=all", parallel = true)
public static Iterator<Object[]> allMapProvider() {
return makeAllMaps().iterator();
}
@DataProvider(name = "Map<IntegerEnum,String> rw=all keys=withNull values=withNull", parallel = true)
public static Iterator<Object[]> allMapWithNullsProvider() {
return makeAllMapsWithNulls().iterator();
}
@DataProvider(name = "Map<IntegerEnum,String> rw=true keys=nonNull values=nonNull", parallel = true)
public static Iterator<Object[]> rwNonNullMapProvider() {
return makeRWNoNullsMaps().iterator();
}
@DataProvider(name = "Map<IntegerEnum,String> rw=true keys=nonNull values=all", parallel = true)
public static Iterator<Object[]> rwNonNullKeysMapProvider() {
return makeRWMapsNoNulls().iterator();
}
@DataProvider(name = "Map<IntegerEnum,String> rw=true keys=all values=all", parallel = true)
public static Iterator<Object[]> rwMapProvider() {
return makeAllRWMaps().iterator();
}
@DataProvider(name = "Map<IntegerEnum,String> rw=true keys=withNull values=withNull", parallel = true)
public static Iterator<Object[]> rwNullsMapProvider() {
return makeAllRWMapsWithNulls().iterator();
}
private static Collection<Object[]> makeAllRWMapsWithNulls() {
Collection<Object[]> all = new ArrayList<>();
all.addAll(makeRWMaps(true, true));
return all;
}
private static Collection<Object[]> makeRWMapsNoNulls() {
Collection<Object[]> all = new ArrayList<>();
all.addAll(makeRWNoNullKeysMaps(false));
all.addAll(makeRWNoNullsMaps());
return all;
}
private static Collection<Object[]> makeAllROMaps() {
Collection<Object[]> all = new ArrayList<>();
all.addAll(makeROMaps(false));
all.addAll(makeROMaps(true));
return all;
}
private static Collection<Object[]> makeAllRWMaps() {
Collection<Object[]> all = new ArrayList<>();
all.addAll(makeRWNoNullsMaps());
all.addAll(makeRWMaps(false,true));
all.addAll(makeRWMaps(true,true));
all.addAll(makeRWNoNullKeysMaps(true));
return all;
}
private static Collection<Object[]> makeAllMaps() {
Collection<Object[]> all = new ArrayList<>();
all.addAll(makeAllROMaps());
all.addAll(makeAllRWMaps());
return all;
}
private static Collection<Object[]> makeAllMapsWithNulls() {
Collection<Object[]> all = new ArrayList<>();
all.addAll(makeROMaps(true));
all.addAll(makeRWMaps(true,true));
return all;
}
/**
* @param nullKeys include null keys
* @param nullValues include null values
* @return
*/
private static Collection<Object[]> makeRWMaps(boolean nullKeys, boolean nullValues) {
return Arrays.asList(
new Object[]{"HashMap", makeMap(HashMap::new, nullKeys, nullValues)},
new Object[]{"IdentityHashMap", makeMap(IdentityHashMap::new, nullKeys, nullValues)},
new Object[]{"LinkedHashMap", makeMap(LinkedHashMap::new, nullKeys, nullValues)},
new Object[]{"WeakHashMap", makeMap(WeakHashMap::new, nullKeys, nullValues)},
new Object[]{"Collections.checkedMap(HashMap)", Collections.checkedMap(makeMap(HashMap::new, nullKeys, nullValues), IntegerEnum.class, String.class)},
new Object[]{"Collections.synchronizedMap(HashMap)", Collections.synchronizedMap(makeMap(HashMap::new, nullKeys, nullValues))},
new Object[]{"ExtendsAbstractMap", makeMap(ExtendsAbstractMap::new, nullKeys, nullValues)});
}
/**
* @param nulls include null values
* @return
*/
private static Collection<Object[]> makeRWNoNullKeysMaps(boolean nulls) {
return Arrays.asList(
// null key hostile
new Object[]{"EnumMap", makeMap(() -> new EnumMap(IntegerEnum.class), false, nulls)},
new Object[]{"TreeMap", makeMap(TreeMap::new, false, nulls)},
new Object[]{"ExtendsAbstractMap(TreeMap)", makeMap(() -> {return new ExtendsAbstractMap(new TreeMap());}, false, nulls)},
new Object[]{"Collections.synchronizedMap(EnumMap)", Collections.synchronizedMap(makeMap(() -> new EnumMap(IntegerEnum.class), false, nulls))}
);
}
private static Collection<Object[]> makeRWNoNullsMaps() {
return Arrays.asList(
// null key and value hostile
new Object[]{"Hashtable", makeMap(Hashtable::new, false, false)},
new Object[]{"ConcurrentHashMap", makeMap(ConcurrentHashMap::new, false, false)},
new Object[]{"ConcurrentSkipListMap", makeMap(ConcurrentSkipListMap::new, false, false)},
new Object[]{"Collections.synchronizedMap(ConcurrentHashMap)", Collections.synchronizedMap(makeMap(ConcurrentHashMap::new, false, false))},
new Object[]{"Collections.checkedMap(ConcurrentHashMap)", Collections.checkedMap(makeMap(ConcurrentHashMap::new, false, false), IntegerEnum.class, String.class)},
new Object[]{"ExtendsAbstractMap(ConcurrentHashMap)", makeMap(() -> {return new ExtendsAbstractMap(new ConcurrentHashMap());}, false, false)},
new Object[]{"ImplementsConcurrentMap", makeMap(ImplementsConcurrentMap::new, false, false)}
);
}
/**
* @param nulls include nulls
* @return
*/
private static Collection<Object[]> makeROMaps(boolean nulls) {
return Arrays.asList(new Object[][]{
new Object[]{"Collections.unmodifiableMap(HashMap)", Collections.unmodifiableMap(makeMap(HashMap::new, nulls, nulls))}
});
}
/**
* @param supplier a supplier of mutable map instances.
*
* @param nullKeys include null keys
* @param nullValues include null values
* @return
*/
private static Map<IntegerEnum, String> makeMap(Supplier<Map<IntegerEnum, String>> supplier, boolean nullKeys, boolean nullValues) {
Map<IntegerEnum, String> result = supplier.get();
for (int each = 0; each < TEST_SIZE; each++) {
IntegerEnum key = nullKeys ? (each == 0) ? null : KEYS[each] : KEYS[each];
String value = nullValues ? (each == 0) ? null : VALUES[each] : VALUES[each];
result.put(key, value);
}
return result;
}
static class Merging {
public enum Value {
ABSENT,
NULL,
OLDVALUE,
NEWVALUE,
RESULT
}
public enum Merger implements BiFunction<String,String,String> {
UNUSED {
public String apply(String oldValue, String newValue) {
fail("should not be called");
return null;
}
},
NULL {
public String apply(String oldValue, String newValue) {
return null;
}
},
RESULT {
public String apply(String oldValue, String newValue) {
return VALUES[3];
}
},
}
}
@DataProvider(name = "MergeCases", parallel = true)
public Iterator<Object[]> mergeCasesProvider() {
Collection<Object[]> cases = new ArrayList<>();
cases.addAll(makeMergeTestCases());
return cases.iterator();
}
static Collection<Object[]> makeMergeTestCases() {
Collection<Object[]> cases = new ArrayList<>();
for (Object[] mapParams : makeAllRWMaps() ) {
cases.add(new Object[] { mapParams[0], mapParams[1], Merging.Value.ABSENT, Merging.Value.NEWVALUE, Merging.Merger.UNUSED, Merging.Value.NEWVALUE, Merging.Value.NEWVALUE });
}
for (Object[] mapParams : makeAllRWMaps() ) {
cases.add(new Object[] { mapParams[0], mapParams[1], Merging.Value.OLDVALUE, Merging.Value.NEWVALUE, Merging.Merger.NULL, Merging.Value.ABSENT, Merging.Value.NULL });
}
for (Object[] mapParams : makeAllRWMaps() ) {
cases.add(new Object[] { mapParams[0], mapParams[1], Merging.Value.OLDVALUE, Merging.Value.NEWVALUE, Merging.Merger.RESULT, Merging.Value.RESULT, Merging.Value.RESULT });
}
return cases;
}
public static void assertThrowsNPE(ThrowingRunnable r) {
assertThrows(NullPointerException.class, r);
}
/**
* A simple mutable map implementation that provides only default
* implementations of all methods. ie. none of the Map interface default
* methods have overridden implementations.
*
* @param <K> Type of keys
* @param <V> Type of values
*/
public static class ExtendsAbstractMap<M extends Map<K,V>, K, V> extends AbstractMap<K,V> {
protected final M map;
public ExtendsAbstractMap() { this( (M) new HashMap<K,V>()); }
protected ExtendsAbstractMap(M map) { this.map = map; }
@Override public Set<Map.Entry<K,V>> entrySet() {
return new AbstractSet<Map.Entry<K,V>>() {
@Override public int size() {
return map.size();
}
@Override public Iterator<Map.Entry<K,V>> iterator() {
final Iterator<Map.Entry<K,V>> source = map.entrySet().iterator();
return new Iterator<Map.Entry<K,V>>() {
public boolean hasNext() { return source.hasNext(); }
public Map.Entry<K,V> next() { return source.next(); }
public void remove() { source.remove(); }
};
}
@Override public boolean add(Map.Entry<K,V> e) {
return map.entrySet().add(e);
}
};
}
@Override public V put(K key, V value) {
return map.put(key, value);
}
}
/**
* A simple mutable concurrent map implementation that provides only default
* implementations of all methods, i.e. none of the ConcurrentMap interface
* default methods have overridden implementations.
*
* @param <K> Type of keys
* @param <V> Type of values
*/
public static class ImplementsConcurrentMap<K,V> extends ExtendsAbstractMap<ConcurrentMap<K,V>, K, V> implements ConcurrentMap<K,V> {
public ImplementsConcurrentMap() { super(new ConcurrentHashMap<K,V>()); }
// ConcurrentMap reabstracts these methods.
//
// Unlike ConcurrentHashMap, we have zero tolerance for null values.
@Override public V replace(K k, V v) {
return map.replace(requireNonNull(k), requireNonNull(v));
}
@Override public boolean replace(K k, V v, V vv) {
return map.replace(requireNonNull(k),
requireNonNull(v),
requireNonNull(vv));
}
@Override public boolean remove(Object k, Object v) {
return map.remove(requireNonNull(k), requireNonNull(v));
}
@Override public V putIfAbsent(K k, V v) {
return map.putIfAbsent(requireNonNull(k), requireNonNull(v));
}
}
}
|