1 |
dl |
1.1 |
/* |
2 |
dl |
1.9 |
* 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 |
jsr166 |
1.23 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
jsr166 |
1.14 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
|
|
import junit.framework.*; |
10 |
|
|
import java.util.*; |
11 |
jsr166 |
1.25 |
import java.util.concurrent.ConcurrentHashMap; |
12 |
dl |
1.1 |
|
13 |
jsr166 |
1.16 |
public class ConcurrentHashMapTest extends JSR166TestCase { |
14 |
dl |
1.1 |
public static void main(String[] args) { |
15 |
jsr166 |
1.21 |
junit.textui.TestRunner.run(suite()); |
16 |
dl |
1.1 |
} |
17 |
|
|
public static Test suite() { |
18 |
jsr166 |
1.17 |
return new TestSuite(ConcurrentHashMapTest.class); |
19 |
dl |
1.1 |
} |
20 |
|
|
|
21 |
dl |
1.5 |
/** |
22 |
jsr166 |
1.28 |
* Returns a new map from Integers 1-5 to Strings "A"-"E". |
23 |
dl |
1.5 |
*/ |
24 |
jsr166 |
1.14 |
private static ConcurrentHashMap map5() { |
25 |
jsr166 |
1.17 |
ConcurrentHashMap map = new ConcurrentHashMap(5); |
26 |
dl |
1.1 |
assertTrue(map.isEmpty()); |
27 |
jsr166 |
1.17 |
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 |
dl |
1.1 |
assertFalse(map.isEmpty()); |
33 |
|
|
assertEquals(5, map.size()); |
34 |
jsr166 |
1.17 |
return map; |
35 |
dl |
1.1 |
} |
36 |
|
|
|
37 |
jsr166 |
1.30 |
// classes for testing Comparable fallbacks |
38 |
|
|
static class BI implements Comparable<BI> { |
39 |
|
|
private final int value; |
40 |
|
|
BI(int value) { this.value = value; } |
41 |
|
|
public int compareTo(BI other) { |
42 |
|
|
return Integer.compare(value, other.value); |
43 |
|
|
} |
44 |
|
|
public boolean equals(Object x) { |
45 |
|
|
return (x instanceof BI) && ((BI)x).value == value; |
46 |
|
|
} |
47 |
|
|
public int hashCode() { return 42; } |
48 |
|
|
} |
49 |
|
|
static class CI extends BI { CI(int value) { super(value); } } |
50 |
|
|
static class DI extends BI { DI(int value) { super(value); } } |
51 |
|
|
|
52 |
|
|
static class BS implements Comparable<BS> { |
53 |
|
|
private final String value; |
54 |
|
|
BS(String value) { this.value = value; } |
55 |
|
|
public int compareTo(BS other) { |
56 |
|
|
return value.compareTo(other.value); |
57 |
|
|
} |
58 |
|
|
public boolean equals(Object x) { |
59 |
|
|
return (x instanceof BS) && value.equals(((BS)x).value); |
60 |
|
|
} |
61 |
|
|
public int hashCode() { return 42; } |
62 |
|
|
} |
63 |
|
|
|
64 |
|
|
static class LexicographicList<E extends Comparable<E>> extends ArrayList<E> |
65 |
|
|
implements Comparable<LexicographicList<E>> { |
66 |
|
|
LexicographicList(Collection<E> c) { super(c); } |
67 |
|
|
LexicographicList(E e) { super(Collections.singleton(e)); } |
68 |
|
|
public int compareTo(LexicographicList<E> other) { |
69 |
|
|
int common = Math.min(size(), other.size()); |
70 |
|
|
int r = 0; |
71 |
|
|
for (int i = 0; i < common; i++) { |
72 |
|
|
if ((r = get(i).compareTo(other.get(i))) != 0) |
73 |
|
|
break; |
74 |
|
|
} |
75 |
|
|
if (r == 0) |
76 |
|
|
r = Integer.compare(size(), other.size()); |
77 |
|
|
return r; |
78 |
|
|
} |
79 |
|
|
private static final long serialVersionUID = 0; |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
/** |
83 |
|
|
* Inserted elements that are subclasses of the same Comparable |
84 |
|
|
* class are found. |
85 |
|
|
*/ |
86 |
|
|
public void testComparableFamily() { |
87 |
|
|
ConcurrentHashMap<BI, Boolean> m = new ConcurrentHashMap<>(); |
88 |
|
|
for (int i = 0; i < 1000; i++) { |
89 |
|
|
assertTrue(m.put(new CI(i), true) == null); |
90 |
|
|
} |
91 |
|
|
for (int i = 0; i < 1000; i++) { |
92 |
|
|
assertTrue(m.containsKey(new CI(i))); |
93 |
|
|
assertTrue(m.containsKey(new DI(i))); |
94 |
|
|
} |
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
/** |
98 |
|
|
* Elements of classes with erased generic type parameters based |
99 |
|
|
* on Comparable can be inserted and found. |
100 |
|
|
*/ |
101 |
|
|
public void testGenericComparable() { |
102 |
|
|
ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>(); |
103 |
|
|
for (int i = 0; i < 1000; i++) { |
104 |
|
|
BI bi = new BI(i); |
105 |
|
|
BS bs = new BS(String.valueOf(i)); |
106 |
|
|
LexicographicList<BI> bis = new LexicographicList<BI>(bi); |
107 |
|
|
LexicographicList<BS> bss = new LexicographicList<BS>(bs); |
108 |
|
|
assertTrue(m.putIfAbsent(bis, true) == null); |
109 |
|
|
assertTrue(m.containsKey(bis)); |
110 |
|
|
if (m.putIfAbsent(bss, true) == null) |
111 |
|
|
assertTrue(m.containsKey(bss)); |
112 |
|
|
assertTrue(m.containsKey(bis)); |
113 |
|
|
} |
114 |
|
|
for (int i = 0; i < 1000; i++) { |
115 |
|
|
assertTrue(m.containsKey(new ArrayList(Collections.singleton(new BI(i))))); |
116 |
|
|
} |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
/** |
120 |
|
|
* Elements of non-comparable classes equal to those of classes |
121 |
|
|
* with erased generic type parameters based on Comparable can be |
122 |
|
|
* inserted and found. |
123 |
|
|
*/ |
124 |
|
|
public void testGenericComparable2() { |
125 |
|
|
ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>(); |
126 |
|
|
for (int i = 0; i < 1000; i++) { |
127 |
|
|
m.put(new ArrayList(Collections.singleton(new BI(i))), true); |
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
for (int i = 0; i < 1000; i++) { |
131 |
|
|
LexicographicList<BI> bis = new LexicographicList<BI>(new BI(i)); |
132 |
|
|
assertTrue(m.containsKey(bis)); |
133 |
|
|
} |
134 |
|
|
} |
135 |
|
|
|
136 |
dl |
1.1 |
/** |
137 |
jsr166 |
1.22 |
* clear removes all pairs |
138 |
dl |
1.1 |
*/ |
139 |
dl |
1.5 |
public void testClear() { |
140 |
dl |
1.1 |
ConcurrentHashMap map = map5(); |
141 |
jsr166 |
1.17 |
map.clear(); |
142 |
jsr166 |
1.26 |
assertEquals(0, map.size()); |
143 |
dl |
1.1 |
} |
144 |
|
|
|
145 |
|
|
/** |
146 |
jsr166 |
1.22 |
* Maps with same contents are equal |
147 |
dl |
1.5 |
*/ |
148 |
|
|
public void testEquals() { |
149 |
|
|
ConcurrentHashMap map1 = map5(); |
150 |
|
|
ConcurrentHashMap map2 = map5(); |
151 |
|
|
assertEquals(map1, map2); |
152 |
|
|
assertEquals(map2, map1); |
153 |
jsr166 |
1.17 |
map1.clear(); |
154 |
dl |
1.5 |
assertFalse(map1.equals(map2)); |
155 |
|
|
assertFalse(map2.equals(map1)); |
156 |
|
|
} |
157 |
|
|
|
158 |
|
|
/** |
159 |
jsr166 |
1.22 |
* contains returns true for contained value |
160 |
dl |
1.1 |
*/ |
161 |
dl |
1.5 |
public void testContains() { |
162 |
dl |
1.1 |
ConcurrentHashMap map = map5(); |
163 |
jsr166 |
1.17 |
assertTrue(map.contains("A")); |
164 |
dl |
1.1 |
assertFalse(map.contains("Z")); |
165 |
|
|
} |
166 |
jsr166 |
1.14 |
|
167 |
dl |
1.1 |
/** |
168 |
jsr166 |
1.22 |
* containsKey returns true for contained key |
169 |
dl |
1.1 |
*/ |
170 |
dl |
1.5 |
public void testContainsKey() { |
171 |
dl |
1.1 |
ConcurrentHashMap map = map5(); |
172 |
jsr166 |
1.17 |
assertTrue(map.containsKey(one)); |
173 |
dl |
1.5 |
assertFalse(map.containsKey(zero)); |
174 |
dl |
1.1 |
} |
175 |
|
|
|
176 |
|
|
/** |
177 |
jsr166 |
1.22 |
* containsValue returns true for held values |
178 |
dl |
1.1 |
*/ |
179 |
dl |
1.5 |
public void testContainsValue() { |
180 |
dl |
1.1 |
ConcurrentHashMap map = map5(); |
181 |
jsr166 |
1.17 |
assertTrue(map.containsValue("A")); |
182 |
dl |
1.11 |
assertFalse(map.containsValue("Z")); |
183 |
dl |
1.1 |
} |
184 |
|
|
|
185 |
|
|
/** |
186 |
jsr166 |
1.22 |
* enumeration returns an enumeration containing the correct |
187 |
|
|
* elements |
188 |
dl |
1.1 |
*/ |
189 |
dl |
1.5 |
public void testEnumeration() { |
190 |
dl |
1.1 |
ConcurrentHashMap map = map5(); |
191 |
jsr166 |
1.17 |
Enumeration e = map.elements(); |
192 |
|
|
int count = 0; |
193 |
|
|
while (e.hasMoreElements()) { |
194 |
|
|
count++; |
195 |
|
|
e.nextElement(); |
196 |
|
|
} |
197 |
|
|
assertEquals(5, count); |
198 |
dl |
1.4 |
} |
199 |
|
|
|
200 |
|
|
/** |
201 |
jsr166 |
1.22 |
* get returns the correct element at the given key, |
202 |
|
|
* or null if not present |
203 |
dl |
1.1 |
*/ |
204 |
dl |
1.5 |
public void testGet() { |
205 |
dl |
1.1 |
ConcurrentHashMap map = map5(); |
206 |
jsr166 |
1.17 |
assertEquals("A", (String)map.get(one)); |
207 |
dl |
1.1 |
ConcurrentHashMap empty = new ConcurrentHashMap(); |
208 |
dl |
1.5 |
assertNull(map.get("anything")); |
209 |
dl |
1.1 |
} |
210 |
|
|
|
211 |
|
|
/** |
212 |
jsr166 |
1.22 |
* isEmpty is true of empty map and false for non-empty |
213 |
dl |
1.1 |
*/ |
214 |
dl |
1.5 |
public void testIsEmpty() { |
215 |
dl |
1.1 |
ConcurrentHashMap empty = new ConcurrentHashMap(); |
216 |
|
|
ConcurrentHashMap map = map5(); |
217 |
jsr166 |
1.17 |
assertTrue(empty.isEmpty()); |
218 |
dl |
1.1 |
assertFalse(map.isEmpty()); |
219 |
|
|
} |
220 |
|
|
|
221 |
|
|
/** |
222 |
jsr166 |
1.22 |
* keys returns an enumeration containing all the keys from the map |
223 |
dl |
1.1 |
*/ |
224 |
dl |
1.5 |
public void testKeys() { |
225 |
dl |
1.1 |
ConcurrentHashMap map = map5(); |
226 |
jsr166 |
1.17 |
Enumeration e = map.keys(); |
227 |
|
|
int count = 0; |
228 |
|
|
while (e.hasMoreElements()) { |
229 |
|
|
count++; |
230 |
|
|
e.nextElement(); |
231 |
|
|
} |
232 |
|
|
assertEquals(5, count); |
233 |
dl |
1.1 |
} |
234 |
|
|
|
235 |
|
|
/** |
236 |
jsr166 |
1.22 |
* keySet returns a Set containing all the keys |
237 |
dl |
1.1 |
*/ |
238 |
dl |
1.5 |
public void testKeySet() { |
239 |
dl |
1.1 |
ConcurrentHashMap map = map5(); |
240 |
jsr166 |
1.17 |
Set s = map.keySet(); |
241 |
|
|
assertEquals(5, s.size()); |
242 |
|
|
assertTrue(s.contains(one)); |
243 |
|
|
assertTrue(s.contains(two)); |
244 |
|
|
assertTrue(s.contains(three)); |
245 |
|
|
assertTrue(s.contains(four)); |
246 |
|
|
assertTrue(s.contains(five)); |
247 |
dl |
1.1 |
} |
248 |
|
|
|
249 |
dl |
1.5 |
/** |
250 |
jsr166 |
1.22 |
* keySet.toArray returns contains all keys |
251 |
dl |
1.13 |
*/ |
252 |
|
|
public void testKeySetToArray() { |
253 |
|
|
ConcurrentHashMap map = map5(); |
254 |
jsr166 |
1.17 |
Set s = map.keySet(); |
255 |
dl |
1.13 |
Object[] ar = s.toArray(); |
256 |
|
|
assertTrue(s.containsAll(Arrays.asList(ar))); |
257 |
jsr166 |
1.17 |
assertEquals(5, ar.length); |
258 |
dl |
1.13 |
ar[0] = m10; |
259 |
|
|
assertFalse(s.containsAll(Arrays.asList(ar))); |
260 |
|
|
} |
261 |
|
|
|
262 |
|
|
/** |
263 |
jsr166 |
1.22 |
* Values.toArray contains all values |
264 |
dl |
1.13 |
*/ |
265 |
|
|
public void testValuesToArray() { |
266 |
|
|
ConcurrentHashMap map = map5(); |
267 |
jsr166 |
1.17 |
Collection v = map.values(); |
268 |
dl |
1.13 |
Object[] ar = v.toArray(); |
269 |
|
|
ArrayList s = new ArrayList(Arrays.asList(ar)); |
270 |
jsr166 |
1.17 |
assertEquals(5, ar.length); |
271 |
|
|
assertTrue(s.contains("A")); |
272 |
|
|
assertTrue(s.contains("B")); |
273 |
|
|
assertTrue(s.contains("C")); |
274 |
|
|
assertTrue(s.contains("D")); |
275 |
|
|
assertTrue(s.contains("E")); |
276 |
dl |
1.13 |
} |
277 |
|
|
|
278 |
|
|
/** |
279 |
jsr166 |
1.22 |
* entrySet.toArray contains all entries |
280 |
dl |
1.13 |
*/ |
281 |
|
|
public void testEntrySetToArray() { |
282 |
|
|
ConcurrentHashMap map = map5(); |
283 |
jsr166 |
1.17 |
Set s = map.entrySet(); |
284 |
dl |
1.13 |
Object[] ar = s.toArray(); |
285 |
|
|
assertEquals(5, ar.length); |
286 |
|
|
for (int i = 0; i < 5; ++i) { |
287 |
|
|
assertTrue(map.containsKey(((Map.Entry)(ar[i])).getKey())); |
288 |
|
|
assertTrue(map.containsValue(((Map.Entry)(ar[i])).getValue())); |
289 |
|
|
} |
290 |
|
|
} |
291 |
|
|
|
292 |
|
|
/** |
293 |
dl |
1.5 |
* values collection contains all values |
294 |
|
|
*/ |
295 |
|
|
public void testValues() { |
296 |
dl |
1.1 |
ConcurrentHashMap map = map5(); |
297 |
jsr166 |
1.17 |
Collection s = map.values(); |
298 |
|
|
assertEquals(5, s.size()); |
299 |
|
|
assertTrue(s.contains("A")); |
300 |
|
|
assertTrue(s.contains("B")); |
301 |
|
|
assertTrue(s.contains("C")); |
302 |
|
|
assertTrue(s.contains("D")); |
303 |
|
|
assertTrue(s.contains("E")); |
304 |
dl |
1.1 |
} |
305 |
|
|
|
306 |
dl |
1.5 |
/** |
307 |
|
|
* entrySet contains all pairs |
308 |
|
|
*/ |
309 |
|
|
public void testEntrySet() { |
310 |
dl |
1.1 |
ConcurrentHashMap map = map5(); |
311 |
jsr166 |
1.17 |
Set s = map.entrySet(); |
312 |
|
|
assertEquals(5, s.size()); |
313 |
dl |
1.1 |
Iterator it = s.iterator(); |
314 |
|
|
while (it.hasNext()) { |
315 |
|
|
Map.Entry e = (Map.Entry) it.next(); |
316 |
jsr166 |
1.14 |
assertTrue( |
317 |
dl |
1.1 |
(e.getKey().equals(one) && e.getValue().equals("A")) || |
318 |
|
|
(e.getKey().equals(two) && e.getValue().equals("B")) || |
319 |
|
|
(e.getKey().equals(three) && e.getValue().equals("C")) || |
320 |
|
|
(e.getKey().equals(four) && e.getValue().equals("D")) || |
321 |
|
|
(e.getKey().equals(five) && e.getValue().equals("E"))); |
322 |
|
|
} |
323 |
|
|
} |
324 |
|
|
|
325 |
|
|
/** |
326 |
jsr166 |
1.22 |
* putAll adds all key-value pairs from the given map |
327 |
dl |
1.1 |
*/ |
328 |
dl |
1.5 |
public void testPutAll() { |
329 |
dl |
1.1 |
ConcurrentHashMap empty = new ConcurrentHashMap(); |
330 |
|
|
ConcurrentHashMap map = map5(); |
331 |
jsr166 |
1.17 |
empty.putAll(map); |
332 |
|
|
assertEquals(5, empty.size()); |
333 |
|
|
assertTrue(empty.containsKey(one)); |
334 |
|
|
assertTrue(empty.containsKey(two)); |
335 |
|
|
assertTrue(empty.containsKey(three)); |
336 |
|
|
assertTrue(empty.containsKey(four)); |
337 |
|
|
assertTrue(empty.containsKey(five)); |
338 |
dl |
1.1 |
} |
339 |
|
|
|
340 |
|
|
/** |
341 |
jsr166 |
1.22 |
* putIfAbsent works when the given key is not present |
342 |
dl |
1.1 |
*/ |
343 |
dl |
1.5 |
public void testPutIfAbsent() { |
344 |
dl |
1.1 |
ConcurrentHashMap map = map5(); |
345 |
jsr166 |
1.17 |
map.putIfAbsent(six, "Z"); |
346 |
dl |
1.7 |
assertTrue(map.containsKey(six)); |
347 |
dl |
1.1 |
} |
348 |
|
|
|
349 |
|
|
/** |
350 |
jsr166 |
1.22 |
* putIfAbsent does not add the pair if the key is already present |
351 |
dl |
1.1 |
*/ |
352 |
dl |
1.5 |
public void testPutIfAbsent2() { |
353 |
dl |
1.1 |
ConcurrentHashMap map = map5(); |
354 |
|
|
assertEquals("A", map.putIfAbsent(one, "Z")); |
355 |
|
|
} |
356 |
|
|
|
357 |
|
|
/** |
358 |
jsr166 |
1.22 |
* replace fails when the given key is not present |
359 |
dl |
1.7 |
*/ |
360 |
|
|
public void testReplace() { |
361 |
|
|
ConcurrentHashMap map = map5(); |
362 |
jsr166 |
1.17 |
assertNull(map.replace(six, "Z")); |
363 |
dl |
1.7 |
assertFalse(map.containsKey(six)); |
364 |
|
|
} |
365 |
|
|
|
366 |
|
|
/** |
367 |
jsr166 |
1.22 |
* replace succeeds if the key is already present |
368 |
dl |
1.7 |
*/ |
369 |
|
|
public void testReplace2() { |
370 |
|
|
ConcurrentHashMap map = map5(); |
371 |
dl |
1.8 |
assertNotNull(map.replace(one, "Z")); |
372 |
dl |
1.7 |
assertEquals("Z", map.get(one)); |
373 |
|
|
} |
374 |
|
|
|
375 |
|
|
/** |
376 |
|
|
* replace value fails when the given key not mapped to expected value |
377 |
|
|
*/ |
378 |
|
|
public void testReplaceValue() { |
379 |
|
|
ConcurrentHashMap map = map5(); |
380 |
|
|
assertEquals("A", map.get(one)); |
381 |
jsr166 |
1.17 |
assertFalse(map.replace(one, "Z", "Z")); |
382 |
dl |
1.7 |
assertEquals("A", map.get(one)); |
383 |
|
|
} |
384 |
|
|
|
385 |
|
|
/** |
386 |
|
|
* replace value succeeds when the given key mapped to expected value |
387 |
|
|
*/ |
388 |
|
|
public void testReplaceValue2() { |
389 |
|
|
ConcurrentHashMap map = map5(); |
390 |
|
|
assertEquals("A", map.get(one)); |
391 |
jsr166 |
1.17 |
assertTrue(map.replace(one, "A", "Z")); |
392 |
dl |
1.7 |
assertEquals("Z", map.get(one)); |
393 |
|
|
} |
394 |
|
|
|
395 |
|
|
/** |
396 |
jsr166 |
1.22 |
* remove removes the correct key-value pair from the map |
397 |
dl |
1.1 |
*/ |
398 |
dl |
1.5 |
public void testRemove() { |
399 |
dl |
1.1 |
ConcurrentHashMap map = map5(); |
400 |
jsr166 |
1.17 |
map.remove(five); |
401 |
|
|
assertEquals(4, map.size()); |
402 |
|
|
assertFalse(map.containsKey(five)); |
403 |
dl |
1.1 |
} |
404 |
|
|
|
405 |
dl |
1.5 |
/** |
406 |
|
|
* remove(key,value) removes only if pair present |
407 |
|
|
*/ |
408 |
|
|
public void testRemove2() { |
409 |
dl |
1.1 |
ConcurrentHashMap map = map5(); |
410 |
jsr166 |
1.17 |
map.remove(five, "E"); |
411 |
|
|
assertEquals(4, map.size()); |
412 |
|
|
assertFalse(map.containsKey(five)); |
413 |
|
|
map.remove(four, "A"); |
414 |
|
|
assertEquals(4, map.size()); |
415 |
|
|
assertTrue(map.containsKey(four)); |
416 |
dl |
1.1 |
} |
417 |
|
|
|
418 |
|
|
/** |
419 |
jsr166 |
1.22 |
* size returns the correct values |
420 |
dl |
1.1 |
*/ |
421 |
dl |
1.5 |
public void testSize() { |
422 |
dl |
1.1 |
ConcurrentHashMap map = map5(); |
423 |
|
|
ConcurrentHashMap empty = new ConcurrentHashMap(); |
424 |
jsr166 |
1.17 |
assertEquals(0, empty.size()); |
425 |
|
|
assertEquals(5, map.size()); |
426 |
dl |
1.1 |
} |
427 |
|
|
|
428 |
dl |
1.5 |
/** |
429 |
|
|
* toString contains toString of elements |
430 |
|
|
*/ |
431 |
|
|
public void testToString() { |
432 |
dl |
1.1 |
ConcurrentHashMap map = map5(); |
433 |
|
|
String s = map.toString(); |
434 |
|
|
for (int i = 1; i <= 5; ++i) { |
435 |
jsr166 |
1.24 |
assertTrue(s.contains(String.valueOf(i))); |
436 |
dl |
1.1 |
} |
437 |
jsr166 |
1.14 |
} |
438 |
dl |
1.1 |
|
439 |
|
|
// Exception tests |
440 |
jsr166 |
1.14 |
|
441 |
dl |
1.5 |
/** |
442 |
jsr166 |
1.14 |
* Cannot create with negative capacity |
443 |
dl |
1.5 |
*/ |
444 |
|
|
public void testConstructor1() { |
445 |
|
|
try { |
446 |
dl |
1.1 |
new ConcurrentHashMap(-1,0,1); |
447 |
dl |
1.5 |
shouldThrow(); |
448 |
jsr166 |
1.19 |
} catch (IllegalArgumentException success) {} |
449 |
dl |
1.1 |
} |
450 |
|
|
|
451 |
dl |
1.5 |
/** |
452 |
|
|
* Cannot create with negative concurrency level |
453 |
|
|
*/ |
454 |
|
|
public void testConstructor2() { |
455 |
|
|
try { |
456 |
dl |
1.1 |
new ConcurrentHashMap(1,0,-1); |
457 |
dl |
1.5 |
shouldThrow(); |
458 |
jsr166 |
1.19 |
} catch (IllegalArgumentException success) {} |
459 |
dl |
1.1 |
} |
460 |
|
|
|
461 |
dl |
1.5 |
/** |
462 |
|
|
* Cannot create with only negative capacity |
463 |
|
|
*/ |
464 |
|
|
public void testConstructor3() { |
465 |
|
|
try { |
466 |
dl |
1.1 |
new ConcurrentHashMap(-1); |
467 |
dl |
1.5 |
shouldThrow(); |
468 |
jsr166 |
1.19 |
} catch (IllegalArgumentException success) {} |
469 |
dl |
1.1 |
} |
470 |
|
|
|
471 |
dl |
1.5 |
/** |
472 |
|
|
* get(null) throws NPE |
473 |
|
|
*/ |
474 |
|
|
public void testGet_NullPointerException() { |
475 |
|
|
try { |
476 |
dl |
1.1 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
477 |
|
|
c.get(null); |
478 |
dl |
1.5 |
shouldThrow(); |
479 |
jsr166 |
1.19 |
} catch (NullPointerException success) {} |
480 |
dl |
1.1 |
} |
481 |
|
|
|
482 |
dl |
1.5 |
/** |
483 |
|
|
* containsKey(null) throws NPE |
484 |
|
|
*/ |
485 |
|
|
public void testContainsKey_NullPointerException() { |
486 |
|
|
try { |
487 |
dl |
1.1 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
488 |
|
|
c.containsKey(null); |
489 |
dl |
1.5 |
shouldThrow(); |
490 |
jsr166 |
1.19 |
} catch (NullPointerException success) {} |
491 |
dl |
1.1 |
} |
492 |
|
|
|
493 |
dl |
1.5 |
/** |
494 |
|
|
* containsValue(null) throws NPE |
495 |
|
|
*/ |
496 |
|
|
public void testContainsValue_NullPointerException() { |
497 |
|
|
try { |
498 |
dl |
1.1 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
499 |
|
|
c.containsValue(null); |
500 |
dl |
1.5 |
shouldThrow(); |
501 |
jsr166 |
1.19 |
} catch (NullPointerException success) {} |
502 |
dl |
1.1 |
} |
503 |
|
|
|
504 |
dl |
1.5 |
/** |
505 |
|
|
* contains(null) throws NPE |
506 |
|
|
*/ |
507 |
|
|
public void testContains_NullPointerException() { |
508 |
|
|
try { |
509 |
dl |
1.1 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
510 |
|
|
c.contains(null); |
511 |
dl |
1.5 |
shouldThrow(); |
512 |
jsr166 |
1.19 |
} catch (NullPointerException success) {} |
513 |
dl |
1.1 |
} |
514 |
|
|
|
515 |
dl |
1.5 |
/** |
516 |
|
|
* put(null,x) throws NPE |
517 |
|
|
*/ |
518 |
|
|
public void testPut1_NullPointerException() { |
519 |
|
|
try { |
520 |
dl |
1.1 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
521 |
|
|
c.put(null, "whatever"); |
522 |
dl |
1.5 |
shouldThrow(); |
523 |
jsr166 |
1.19 |
} catch (NullPointerException success) {} |
524 |
dl |
1.1 |
} |
525 |
|
|
|
526 |
dl |
1.5 |
/** |
527 |
|
|
* put(x, null) throws NPE |
528 |
|
|
*/ |
529 |
|
|
public void testPut2_NullPointerException() { |
530 |
|
|
try { |
531 |
dl |
1.1 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
532 |
|
|
c.put("whatever", null); |
533 |
dl |
1.5 |
shouldThrow(); |
534 |
jsr166 |
1.19 |
} catch (NullPointerException success) {} |
535 |
dl |
1.1 |
} |
536 |
|
|
|
537 |
dl |
1.5 |
/** |
538 |
|
|
* putIfAbsent(null, x) throws NPE |
539 |
|
|
*/ |
540 |
|
|
public void testPutIfAbsent1_NullPointerException() { |
541 |
|
|
try { |
542 |
dl |
1.1 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
543 |
|
|
c.putIfAbsent(null, "whatever"); |
544 |
dl |
1.5 |
shouldThrow(); |
545 |
jsr166 |
1.19 |
} catch (NullPointerException success) {} |
546 |
dl |
1.1 |
} |
547 |
|
|
|
548 |
dl |
1.5 |
/** |
549 |
dl |
1.7 |
* replace(null, x) throws NPE |
550 |
|
|
*/ |
551 |
|
|
public void testReplace_NullPointerException() { |
552 |
|
|
try { |
553 |
|
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
554 |
|
|
c.replace(null, "whatever"); |
555 |
|
|
shouldThrow(); |
556 |
jsr166 |
1.19 |
} catch (NullPointerException success) {} |
557 |
dl |
1.7 |
} |
558 |
|
|
|
559 |
|
|
/** |
560 |
|
|
* replace(null, x, y) throws NPE |
561 |
|
|
*/ |
562 |
|
|
public void testReplaceValue_NullPointerException() { |
563 |
|
|
try { |
564 |
|
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
565 |
|
|
c.replace(null, one, "whatever"); |
566 |
|
|
shouldThrow(); |
567 |
jsr166 |
1.19 |
} catch (NullPointerException success) {} |
568 |
dl |
1.7 |
} |
569 |
|
|
|
570 |
|
|
/** |
571 |
dl |
1.5 |
* putIfAbsent(x, null) throws NPE |
572 |
|
|
*/ |
573 |
|
|
public void testPutIfAbsent2_NullPointerException() { |
574 |
|
|
try { |
575 |
dl |
1.1 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
576 |
|
|
c.putIfAbsent("whatever", null); |
577 |
dl |
1.7 |
shouldThrow(); |
578 |
jsr166 |
1.19 |
} catch (NullPointerException success) {} |
579 |
dl |
1.7 |
} |
580 |
|
|
|
581 |
|
|
/** |
582 |
|
|
* replace(x, null) throws NPE |
583 |
|
|
*/ |
584 |
|
|
public void testReplace2_NullPointerException() { |
585 |
|
|
try { |
586 |
|
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
587 |
|
|
c.replace("whatever", null); |
588 |
|
|
shouldThrow(); |
589 |
jsr166 |
1.19 |
} catch (NullPointerException success) {} |
590 |
dl |
1.7 |
} |
591 |
|
|
|
592 |
|
|
/** |
593 |
|
|
* replace(x, null, y) throws NPE |
594 |
|
|
*/ |
595 |
|
|
public void testReplaceValue2_NullPointerException() { |
596 |
|
|
try { |
597 |
|
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
598 |
|
|
c.replace("whatever", null, "A"); |
599 |
|
|
shouldThrow(); |
600 |
jsr166 |
1.19 |
} catch (NullPointerException success) {} |
601 |
dl |
1.7 |
} |
602 |
|
|
|
603 |
|
|
/** |
604 |
|
|
* replace(x, y, null) throws NPE |
605 |
|
|
*/ |
606 |
|
|
public void testReplaceValue3_NullPointerException() { |
607 |
|
|
try { |
608 |
|
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
609 |
|
|
c.replace("whatever", one, null); |
610 |
dl |
1.5 |
shouldThrow(); |
611 |
jsr166 |
1.19 |
} catch (NullPointerException success) {} |
612 |
dl |
1.1 |
} |
613 |
|
|
|
614 |
dl |
1.5 |
/** |
615 |
|
|
* remove(null) throws NPE |
616 |
|
|
*/ |
617 |
|
|
public void testRemove1_NullPointerException() { |
618 |
|
|
try { |
619 |
dl |
1.1 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
620 |
|
|
c.put("sadsdf", "asdads"); |
621 |
|
|
c.remove(null); |
622 |
dl |
1.5 |
shouldThrow(); |
623 |
jsr166 |
1.19 |
} catch (NullPointerException success) {} |
624 |
dl |
1.1 |
} |
625 |
|
|
|
626 |
dl |
1.5 |
/** |
627 |
|
|
* remove(null, x) throws NPE |
628 |
|
|
*/ |
629 |
|
|
public void testRemove2_NullPointerException() { |
630 |
|
|
try { |
631 |
dl |
1.1 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
632 |
|
|
c.put("sadsdf", "asdads"); |
633 |
|
|
c.remove(null, "whatever"); |
634 |
dl |
1.5 |
shouldThrow(); |
635 |
jsr166 |
1.19 |
} catch (NullPointerException success) {} |
636 |
dl |
1.1 |
} |
637 |
dl |
1.2 |
|
638 |
dl |
1.5 |
/** |
639 |
dl |
1.12 |
* remove(x, null) returns false |
640 |
|
|
*/ |
641 |
|
|
public void testRemove3() { |
642 |
jsr166 |
1.19 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
643 |
|
|
c.put("sadsdf", "asdads"); |
644 |
|
|
assertFalse(c.remove("sadsdf", null)); |
645 |
dl |
1.12 |
} |
646 |
|
|
|
647 |
|
|
/** |
648 |
dl |
1.5 |
* A deserialized map equals original |
649 |
|
|
*/ |
650 |
jsr166 |
1.18 |
public void testSerialization() throws Exception { |
651 |
jsr166 |
1.25 |
Map x = map5(); |
652 |
|
|
Map y = serialClone(x); |
653 |
dl |
1.2 |
|
654 |
jsr166 |
1.25 |
assertTrue(x != y); |
655 |
|
|
assertEquals(x.size(), y.size()); |
656 |
|
|
assertEquals(x, y); |
657 |
|
|
assertEquals(y, x); |
658 |
dl |
1.2 |
} |
659 |
dl |
1.6 |
|
660 |
|
|
/** |
661 |
|
|
* SetValue of an EntrySet entry sets value in the map. |
662 |
|
|
*/ |
663 |
|
|
public void testSetValueWriteThrough() { |
664 |
jsr166 |
1.14 |
// Adapted from a bug report by Eric Zoerner |
665 |
dl |
1.6 |
ConcurrentHashMap map = new ConcurrentHashMap(2, 5.0f, 1); |
666 |
|
|
assertTrue(map.isEmpty()); |
667 |
|
|
for (int i = 0; i < 20; i++) |
668 |
|
|
map.put(new Integer(i), new Integer(i)); |
669 |
|
|
assertFalse(map.isEmpty()); |
670 |
|
|
Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next(); |
671 |
dl |
1.29 |
// Unless it happens to be first (in which case remainder of |
672 |
|
|
// test is skipped), remove a possibly-colliding key from map |
673 |
|
|
// which, under some implementations, may cause entry1 to be |
674 |
|
|
// cloned in map |
675 |
|
|
if (!entry1.getKey().equals(new Integer(16))) { |
676 |
|
|
map.remove(new Integer(16)); |
677 |
|
|
entry1.setValue("XYZ"); |
678 |
|
|
assertTrue(map.containsValue("XYZ")); // fails if write-through broken |
679 |
|
|
} |
680 |
dl |
1.6 |
} |
681 |
jsr166 |
1.14 |
|
682 |
dl |
1.1 |
} |