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/licenses/publicdomain |
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.*; |
12 |
import java.util.Enumeration; |
13 |
import java.io.*; |
14 |
|
15 |
public class ConcurrentHashMapTest extends JSR166TestCase{ |
16 |
public static void main(String[] args) { |
17 |
junit.textui.TestRunner.run (suite()); |
18 |
} |
19 |
public static Test suite() { |
20 |
return new TestSuite(ConcurrentHashMapTest.class); |
21 |
} |
22 |
|
23 |
/** |
24 |
* Create a map from Integers 1-5 to Strings "A"-"E". |
25 |
*/ |
26 |
private static ConcurrentHashMap map5() { |
27 |
ConcurrentHashMap map = new ConcurrentHashMap(5); |
28 |
assertTrue(map.isEmpty()); |
29 |
map.put(one, "A"); |
30 |
map.put(two, "B"); |
31 |
map.put(three, "C"); |
32 |
map.put(four, "D"); |
33 |
map.put(five, "E"); |
34 |
assertFalse(map.isEmpty()); |
35 |
assertEquals(5, map.size()); |
36 |
return map; |
37 |
} |
38 |
|
39 |
/** |
40 |
* clear removes all pairs |
41 |
*/ |
42 |
public void testClear() { |
43 |
ConcurrentHashMap map = map5(); |
44 |
map.clear(); |
45 |
assertEquals(map.size(), 0); |
46 |
} |
47 |
|
48 |
/** |
49 |
* Maps with same contents are equal |
50 |
*/ |
51 |
public void testEquals() { |
52 |
ConcurrentHashMap map1 = map5(); |
53 |
ConcurrentHashMap map2 = map5(); |
54 |
assertEquals(map1, map2); |
55 |
assertEquals(map2, map1); |
56 |
map1.clear(); |
57 |
assertFalse(map1.equals(map2)); |
58 |
assertFalse(map2.equals(map1)); |
59 |
} |
60 |
|
61 |
/** |
62 |
* contains returns true for contained value |
63 |
*/ |
64 |
public void testContains() { |
65 |
ConcurrentHashMap map = map5(); |
66 |
assertTrue(map.contains("A")); |
67 |
assertFalse(map.contains("Z")); |
68 |
} |
69 |
|
70 |
/** |
71 |
* containsKey returns true for contained key |
72 |
*/ |
73 |
public void testContainsKey() { |
74 |
ConcurrentHashMap map = map5(); |
75 |
assertTrue(map.containsKey(one)); |
76 |
assertFalse(map.containsKey(zero)); |
77 |
} |
78 |
|
79 |
/** |
80 |
* containsValue returns true for held values |
81 |
*/ |
82 |
public void testContainsValue() { |
83 |
ConcurrentHashMap map = map5(); |
84 |
assertTrue(map.containsValue("A")); |
85 |
assertFalse(map.containsValue("Z")); |
86 |
} |
87 |
|
88 |
/** |
89 |
* enumeration returns an enumeration containing the correct |
90 |
* elements |
91 |
*/ |
92 |
public void testEnumeration() { |
93 |
ConcurrentHashMap map = map5(); |
94 |
Enumeration e = map.elements(); |
95 |
int count = 0; |
96 |
while(e.hasMoreElements()){ |
97 |
count++; |
98 |
e.nextElement(); |
99 |
} |
100 |
assertEquals(5, count); |
101 |
} |
102 |
|
103 |
/** |
104 |
* get returns the correct element at the given key, |
105 |
* or null if not present |
106 |
*/ |
107 |
public void testGet() { |
108 |
ConcurrentHashMap map = map5(); |
109 |
assertEquals("A", (String)map.get(one)); |
110 |
ConcurrentHashMap empty = new ConcurrentHashMap(); |
111 |
assertNull(map.get("anything")); |
112 |
} |
113 |
|
114 |
/** |
115 |
* isEmpty is true of empty map and false for non-empty |
116 |
*/ |
117 |
public void testIsEmpty() { |
118 |
ConcurrentHashMap empty = new ConcurrentHashMap(); |
119 |
ConcurrentHashMap map = map5(); |
120 |
assertTrue(empty.isEmpty()); |
121 |
assertFalse(map.isEmpty()); |
122 |
} |
123 |
|
124 |
/** |
125 |
* keys returns an enumeration containing all the keys from the map |
126 |
*/ |
127 |
public void testKeys() { |
128 |
ConcurrentHashMap map = map5(); |
129 |
Enumeration e = map.keys(); |
130 |
int count = 0; |
131 |
while(e.hasMoreElements()){ |
132 |
count++; |
133 |
e.nextElement(); |
134 |
} |
135 |
assertEquals(5, count); |
136 |
} |
137 |
|
138 |
/** |
139 |
* keySet returns a Set containing all the keys |
140 |
*/ |
141 |
public void testKeySet() { |
142 |
ConcurrentHashMap map = map5(); |
143 |
Set s = map.keySet(); |
144 |
assertEquals(5, s.size()); |
145 |
assertTrue(s.contains(one)); |
146 |
assertTrue(s.contains(two)); |
147 |
assertTrue(s.contains(three)); |
148 |
assertTrue(s.contains(four)); |
149 |
assertTrue(s.contains(five)); |
150 |
} |
151 |
|
152 |
/** |
153 |
* values collection contains all values |
154 |
*/ |
155 |
public void testValues() { |
156 |
ConcurrentHashMap map = map5(); |
157 |
Collection s = map.values(); |
158 |
assertEquals(5, s.size()); |
159 |
assertTrue(s.contains("A")); |
160 |
assertTrue(s.contains("B")); |
161 |
assertTrue(s.contains("C")); |
162 |
assertTrue(s.contains("D")); |
163 |
assertTrue(s.contains("E")); |
164 |
} |
165 |
|
166 |
/** |
167 |
* entrySet contains all pairs |
168 |
*/ |
169 |
public void testEntrySet() { |
170 |
ConcurrentHashMap map = map5(); |
171 |
Set s = map.entrySet(); |
172 |
assertEquals(5, s.size()); |
173 |
Iterator it = s.iterator(); |
174 |
while (it.hasNext()) { |
175 |
Map.Entry e = (Map.Entry) it.next(); |
176 |
assertTrue( |
177 |
(e.getKey().equals(one) && e.getValue().equals("A")) || |
178 |
(e.getKey().equals(two) && e.getValue().equals("B")) || |
179 |
(e.getKey().equals(three) && e.getValue().equals("C")) || |
180 |
(e.getKey().equals(four) && e.getValue().equals("D")) || |
181 |
(e.getKey().equals(five) && e.getValue().equals("E"))); |
182 |
} |
183 |
} |
184 |
|
185 |
/** |
186 |
* putAll adds all key-value pairs from the given map |
187 |
*/ |
188 |
public void testPutAll() { |
189 |
ConcurrentHashMap empty = new ConcurrentHashMap(); |
190 |
ConcurrentHashMap map = map5(); |
191 |
empty.putAll(map); |
192 |
assertEquals(5, empty.size()); |
193 |
assertTrue(empty.containsKey(one)); |
194 |
assertTrue(empty.containsKey(two)); |
195 |
assertTrue(empty.containsKey(three)); |
196 |
assertTrue(empty.containsKey(four)); |
197 |
assertTrue(empty.containsKey(five)); |
198 |
} |
199 |
|
200 |
/** |
201 |
* putIfAbsent works when the given key is not present |
202 |
*/ |
203 |
public void testPutIfAbsent() { |
204 |
ConcurrentHashMap map = map5(); |
205 |
map.putIfAbsent(six, "Z"); |
206 |
assertTrue(map.containsKey(six)); |
207 |
} |
208 |
|
209 |
/** |
210 |
* putIfAbsent does not add the pair if the key is already present |
211 |
*/ |
212 |
public void testPutIfAbsent2() { |
213 |
ConcurrentHashMap map = map5(); |
214 |
assertEquals("A", map.putIfAbsent(one, "Z")); |
215 |
} |
216 |
|
217 |
/** |
218 |
* replace fails when the given key is not present |
219 |
*/ |
220 |
public void testReplace() { |
221 |
ConcurrentHashMap map = map5(); |
222 |
assertNull(map.replace(six, "Z")); |
223 |
assertFalse(map.containsKey(six)); |
224 |
} |
225 |
|
226 |
/** |
227 |
* replace succeeds if the key is already present |
228 |
*/ |
229 |
public void testReplace2() { |
230 |
ConcurrentHashMap map = map5(); |
231 |
assertNotNull(map.replace(one, "Z")); |
232 |
assertEquals("Z", map.get(one)); |
233 |
} |
234 |
|
235 |
|
236 |
/** |
237 |
* replace value fails when the given key not mapped to expected value |
238 |
*/ |
239 |
public void testReplaceValue() { |
240 |
ConcurrentHashMap map = map5(); |
241 |
assertEquals("A", map.get(one)); |
242 |
assertFalse(map.replace(one, "Z", "Z")); |
243 |
assertEquals("A", map.get(one)); |
244 |
} |
245 |
|
246 |
/** |
247 |
* replace value succeeds when the given key mapped to expected value |
248 |
*/ |
249 |
public void testReplaceValue2() { |
250 |
ConcurrentHashMap map = map5(); |
251 |
assertEquals("A", map.get(one)); |
252 |
assertTrue(map.replace(one, "A", "Z")); |
253 |
assertEquals("Z", map.get(one)); |
254 |
} |
255 |
|
256 |
|
257 |
/** |
258 |
* remove removes the correct key-value pair from the map |
259 |
*/ |
260 |
public void testRemove() { |
261 |
ConcurrentHashMap map = map5(); |
262 |
map.remove(five); |
263 |
assertEquals(4, map.size()); |
264 |
assertFalse(map.containsKey(five)); |
265 |
} |
266 |
|
267 |
/** |
268 |
* remove(key,value) removes only if pair present |
269 |
*/ |
270 |
public void testRemove2() { |
271 |
ConcurrentHashMap map = map5(); |
272 |
map.remove(five, "E"); |
273 |
assertEquals(4, map.size()); |
274 |
assertFalse(map.containsKey(five)); |
275 |
map.remove(four, "A"); |
276 |
assertEquals(4, map.size()); |
277 |
assertTrue(map.containsKey(four)); |
278 |
|
279 |
} |
280 |
|
281 |
/** |
282 |
* size returns the correct values |
283 |
*/ |
284 |
public void testSize() { |
285 |
ConcurrentHashMap map = map5(); |
286 |
ConcurrentHashMap empty = new ConcurrentHashMap(); |
287 |
assertEquals(0, empty.size()); |
288 |
assertEquals(5, map.size()); |
289 |
} |
290 |
|
291 |
/** |
292 |
* toString contains toString of elements |
293 |
*/ |
294 |
public void testToString() { |
295 |
ConcurrentHashMap map = map5(); |
296 |
String s = map.toString(); |
297 |
for (int i = 1; i <= 5; ++i) { |
298 |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
299 |
} |
300 |
} |
301 |
|
302 |
// Exception tests |
303 |
|
304 |
/** |
305 |
* Cannot create with negative capacity |
306 |
*/ |
307 |
public void testConstructor1() { |
308 |
try { |
309 |
new ConcurrentHashMap(-1,0,1); |
310 |
shouldThrow(); |
311 |
} catch(IllegalArgumentException e){} |
312 |
} |
313 |
|
314 |
/** |
315 |
* Cannot create with negative concurrency level |
316 |
*/ |
317 |
public void testConstructor2() { |
318 |
try { |
319 |
new ConcurrentHashMap(1,0,-1); |
320 |
shouldThrow(); |
321 |
} catch(IllegalArgumentException e){} |
322 |
} |
323 |
|
324 |
/** |
325 |
* Cannot create with only negative capacity |
326 |
*/ |
327 |
public void testConstructor3() { |
328 |
try { |
329 |
new ConcurrentHashMap(-1); |
330 |
shouldThrow(); |
331 |
} catch(IllegalArgumentException e){} |
332 |
} |
333 |
|
334 |
/** |
335 |
* get(null) throws NPE |
336 |
*/ |
337 |
public void testGet_NullPointerException() { |
338 |
try { |
339 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
340 |
c.get(null); |
341 |
shouldThrow(); |
342 |
} catch(NullPointerException e){} |
343 |
} |
344 |
|
345 |
/** |
346 |
* containsKey(null) throws NPE |
347 |
*/ |
348 |
public void testContainsKey_NullPointerException() { |
349 |
try { |
350 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
351 |
c.containsKey(null); |
352 |
shouldThrow(); |
353 |
} catch(NullPointerException e){} |
354 |
} |
355 |
|
356 |
/** |
357 |
* containsValue(null) throws NPE |
358 |
*/ |
359 |
public void testContainsValue_NullPointerException() { |
360 |
try { |
361 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
362 |
c.containsValue(null); |
363 |
shouldThrow(); |
364 |
} catch(NullPointerException e){} |
365 |
} |
366 |
|
367 |
/** |
368 |
* contains(null) throws NPE |
369 |
*/ |
370 |
public void testContains_NullPointerException() { |
371 |
try { |
372 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
373 |
c.contains(null); |
374 |
shouldThrow(); |
375 |
} catch(NullPointerException e){} |
376 |
} |
377 |
|
378 |
/** |
379 |
* put(null,x) throws NPE |
380 |
*/ |
381 |
public void testPut1_NullPointerException() { |
382 |
try { |
383 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
384 |
c.put(null, "whatever"); |
385 |
shouldThrow(); |
386 |
} catch(NullPointerException e){} |
387 |
} |
388 |
|
389 |
/** |
390 |
* put(x, null) throws NPE |
391 |
*/ |
392 |
public void testPut2_NullPointerException() { |
393 |
try { |
394 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
395 |
c.put("whatever", null); |
396 |
shouldThrow(); |
397 |
} catch(NullPointerException e){} |
398 |
} |
399 |
|
400 |
/** |
401 |
* putIfAbsent(null, x) throws NPE |
402 |
*/ |
403 |
public void testPutIfAbsent1_NullPointerException() { |
404 |
try { |
405 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
406 |
c.putIfAbsent(null, "whatever"); |
407 |
shouldThrow(); |
408 |
} catch(NullPointerException e){} |
409 |
} |
410 |
|
411 |
/** |
412 |
* replace(null, x) throws NPE |
413 |
*/ |
414 |
public void testReplace_NullPointerException() { |
415 |
try { |
416 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
417 |
c.replace(null, "whatever"); |
418 |
shouldThrow(); |
419 |
} catch(NullPointerException e){} |
420 |
} |
421 |
|
422 |
/** |
423 |
* replace(null, x, y) throws NPE |
424 |
*/ |
425 |
public void testReplaceValue_NullPointerException() { |
426 |
try { |
427 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
428 |
c.replace(null, one, "whatever"); |
429 |
shouldThrow(); |
430 |
} catch(NullPointerException e){} |
431 |
} |
432 |
|
433 |
/** |
434 |
* putIfAbsent(x, null) throws NPE |
435 |
*/ |
436 |
public void testPutIfAbsent2_NullPointerException() { |
437 |
try { |
438 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
439 |
c.putIfAbsent("whatever", null); |
440 |
shouldThrow(); |
441 |
} catch(NullPointerException e){} |
442 |
} |
443 |
|
444 |
|
445 |
/** |
446 |
* replace(x, null) throws NPE |
447 |
*/ |
448 |
public void testReplace2_NullPointerException() { |
449 |
try { |
450 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
451 |
c.replace("whatever", null); |
452 |
shouldThrow(); |
453 |
} catch(NullPointerException e){} |
454 |
} |
455 |
|
456 |
/** |
457 |
* replace(x, null, y) throws NPE |
458 |
*/ |
459 |
public void testReplaceValue2_NullPointerException() { |
460 |
try { |
461 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
462 |
c.replace("whatever", null, "A"); |
463 |
shouldThrow(); |
464 |
} catch(NullPointerException e){} |
465 |
} |
466 |
|
467 |
/** |
468 |
* replace(x, y, null) throws NPE |
469 |
*/ |
470 |
public void testReplaceValue3_NullPointerException() { |
471 |
try { |
472 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
473 |
c.replace("whatever", one, null); |
474 |
shouldThrow(); |
475 |
} catch(NullPointerException e){} |
476 |
} |
477 |
|
478 |
|
479 |
/** |
480 |
* remove(null) throws NPE |
481 |
*/ |
482 |
public void testRemove1_NullPointerException() { |
483 |
try { |
484 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
485 |
c.put("sadsdf", "asdads"); |
486 |
c.remove(null); |
487 |
shouldThrow(); |
488 |
} catch(NullPointerException e){} |
489 |
} |
490 |
|
491 |
/** |
492 |
* remove(null, x) throws NPE |
493 |
*/ |
494 |
public void testRemove2_NullPointerException() { |
495 |
try { |
496 |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
497 |
c.put("sadsdf", "asdads"); |
498 |
c.remove(null, "whatever"); |
499 |
shouldThrow(); |
500 |
} catch(NullPointerException e){} |
501 |
} |
502 |
|
503 |
/** |
504 |
* A deserialized map equals original |
505 |
*/ |
506 |
public void testSerialization() { |
507 |
ConcurrentHashMap q = map5(); |
508 |
|
509 |
try { |
510 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
511 |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
512 |
out.writeObject(q); |
513 |
out.close(); |
514 |
|
515 |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
516 |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
517 |
ConcurrentHashMap r = (ConcurrentHashMap)in.readObject(); |
518 |
assertEquals(q.size(), r.size()); |
519 |
assertTrue(q.equals(r)); |
520 |
assertTrue(r.equals(q)); |
521 |
} catch(Exception e){ |
522 |
e.printStackTrace(); |
523 |
unexpectedException(); |
524 |
} |
525 |
} |
526 |
|
527 |
|
528 |
/** |
529 |
* SetValue of an EntrySet entry sets value in the map. |
530 |
*/ |
531 |
public void testSetValueWriteThrough() { |
532 |
// Adapted from a bug report by Eric Zoerner |
533 |
ConcurrentHashMap map = new ConcurrentHashMap(2, 5.0f, 1); |
534 |
assertTrue(map.isEmpty()); |
535 |
for (int i = 0; i < 20; i++) |
536 |
map.put(new Integer(i), new Integer(i)); |
537 |
assertFalse(map.isEmpty()); |
538 |
Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next(); |
539 |
|
540 |
// assert that entry1 is not 16 |
541 |
assertTrue("entry is 16, test not valid", |
542 |
!entry1.getKey().equals(new Integer(16))); |
543 |
|
544 |
// remove 16 (a different key) from map |
545 |
// which just happens to cause entry1 to be cloned in map |
546 |
map.remove(new Integer(16)); |
547 |
entry1.setValue("XYZ"); |
548 |
assertTrue(map.containsValue("XYZ")); // fails |
549 |
} |
550 |
|
551 |
} |