1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain, as explained at |
4 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
* Pat Fisher, Mike Judd. |
7 |
*/ |
8 |
|
9 |
import junit.framework.*; |
10 |
import java.util.*; |
11 |
import java.util.concurrent.ConcurrentHashMap; |
12 |
|
13 |
public class ConcurrentHashMap8Test extends JSR166TestCase { |
14 |
public static void main(String[] args) { |
15 |
junit.textui.TestRunner.run(suite()); |
16 |
} |
17 |
public static Test suite() { |
18 |
return new TestSuite(ConcurrentHashMap8Test.class); |
19 |
} |
20 |
|
21 |
/** |
22 |
* Returns a new map from Integers 1-5 to Strings "A"-"E". |
23 |
*/ |
24 |
private static ConcurrentHashMap map5() { |
25 |
ConcurrentHashMap map = new ConcurrentHashMap(5); |
26 |
assertTrue(map.isEmpty()); |
27 |
map.put(one, "A"); |
28 |
map.put(two, "B"); |
29 |
map.put(three, "C"); |
30 |
map.put(four, "D"); |
31 |
map.put(five, "E"); |
32 |
assertFalse(map.isEmpty()); |
33 |
assertEquals(5, map.size()); |
34 |
return map; |
35 |
} |
36 |
|
37 |
/** |
38 |
* computeIfAbsent adds when the given key is not present |
39 |
*/ |
40 |
public void testComputeIfAbsent() { |
41 |
ConcurrentHashMap map = map5(); |
42 |
map.computeIfAbsent(six, (x) -> "Z"); |
43 |
assertTrue(map.containsKey(six)); |
44 |
} |
45 |
|
46 |
/** |
47 |
* computeIfAbsent does not replace if the key is already present |
48 |
*/ |
49 |
public void testComputeIfAbsent2() { |
50 |
ConcurrentHashMap map = map5(); |
51 |
assertEquals("A", map.computeIfAbsent(one, (x) -> "Z")); |
52 |
} |
53 |
|
54 |
/** |
55 |
* computeIfAbsent does not add if function returns null |
56 |
*/ |
57 |
public void testComputeIfAbsent3() { |
58 |
ConcurrentHashMap map = map5(); |
59 |
map.computeIfAbsent(six, (x) -> null); |
60 |
assertFalse(map.containsKey(six)); |
61 |
} |
62 |
|
63 |
/** |
64 |
* computeIfPresent does not replace if the key is already present |
65 |
*/ |
66 |
public void testComputeIfPresent() { |
67 |
ConcurrentHashMap map = map5(); |
68 |
map.computeIfPresent(six, (x, y) -> "Z"); |
69 |
assertFalse(map.containsKey(six)); |
70 |
} |
71 |
|
72 |
/** |
73 |
* computeIfPresent adds when the given key is not present |
74 |
*/ |
75 |
public void testComputeIfPresent2() { |
76 |
ConcurrentHashMap map = map5(); |
77 |
assertEquals("Z", map.computeIfPresent(one, (x, y) -> "Z")); |
78 |
} |
79 |
|
80 |
/** |
81 |
* compute does not replace if the function returns null |
82 |
*/ |
83 |
public void testCompute() { |
84 |
ConcurrentHashMap map = map5(); |
85 |
map.compute(six, (x, y) -> null); |
86 |
assertFalse(map.containsKey(six)); |
87 |
} |
88 |
|
89 |
/** |
90 |
* compute adds when the given key is not present |
91 |
*/ |
92 |
public void testCompute2() { |
93 |
ConcurrentHashMap map = map5(); |
94 |
assertEquals("Z", map.compute(six, (x, y) -> "Z")); |
95 |
} |
96 |
|
97 |
/** |
98 |
* compute replaces when the given key is present |
99 |
*/ |
100 |
public void testCompute3() { |
101 |
ConcurrentHashMap map = map5(); |
102 |
assertEquals("Z", map.compute(one, (x, y) -> "Z")); |
103 |
} |
104 |
|
105 |
/** |
106 |
* compute removes when the given key is present and function returns null |
107 |
*/ |
108 |
public void testCompute4() { |
109 |
ConcurrentHashMap map = map5(); |
110 |
map.compute(one, (x, y) -> null); |
111 |
assertFalse(map.containsKey(one)); |
112 |
} |
113 |
|
114 |
/** |
115 |
* merge adds when the given key is not present |
116 |
*/ |
117 |
public void testMerge1() { |
118 |
ConcurrentHashMap map = map5(); |
119 |
assertEquals("Y", map.merge(six, "Y", (x, y) -> "Z")); |
120 |
} |
121 |
|
122 |
/** |
123 |
* merge replaces when the given key is present |
124 |
*/ |
125 |
public void testMerge2() { |
126 |
ConcurrentHashMap map = map5(); |
127 |
assertEquals("Z", map.merge(one, "Y", (x, y) -> "Z")); |
128 |
} |
129 |
|
130 |
/** |
131 |
* merge removes when the given key is present and function returns null |
132 |
*/ |
133 |
public void testMerge3() { |
134 |
ConcurrentHashMap map = map5(); |
135 |
map.merge(one, "Y", (x, y) -> null); |
136 |
assertFalse(map.containsKey(one)); |
137 |
} |
138 |
|
139 |
|
140 |
|
141 |
} |