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 |
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.*; |
12 |
< |
import java.util.Enumeration; |
13 |
< |
import java.io.*; |
11 |
> |
import java.util.concurrent.ConcurrentHashMap; |
12 |
|
|
13 |
|
public class ConcurrentHashMapTest extends JSR166TestCase { |
14 |
|
public static void main(String[] args) { |
15 |
< |
junit.textui.TestRunner.run (suite()); |
15 |
> |
junit.textui.TestRunner.run(suite()); |
16 |
|
} |
17 |
|
public static Test suite() { |
18 |
|
return new TestSuite(ConcurrentHashMapTest.class); |
19 |
|
} |
20 |
|
|
21 |
|
/** |
22 |
< |
* Create a map from Integers 1-5 to Strings "A"-"E". |
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); |
35 |
|
} |
36 |
|
|
37 |
|
/** |
38 |
< |
* clear removes all pairs |
38 |
> |
* clear removes all pairs |
39 |
|
*/ |
40 |
|
public void testClear() { |
41 |
|
ConcurrentHashMap map = map5(); |
42 |
|
map.clear(); |
43 |
< |
assertEquals(map.size(), 0); |
43 |
> |
assertEquals(0, map.size()); |
44 |
|
} |
45 |
|
|
46 |
|
/** |
47 |
< |
* Maps with same contents are equal |
47 |
> |
* Maps with same contents are equal |
48 |
|
*/ |
49 |
|
public void testEquals() { |
50 |
|
ConcurrentHashMap map1 = map5(); |
57 |
|
} |
58 |
|
|
59 |
|
/** |
60 |
< |
* contains returns true for contained value |
60 |
> |
* contains returns true for contained value |
61 |
|
*/ |
62 |
|
public void testContains() { |
63 |
|
ConcurrentHashMap map = map5(); |
66 |
|
} |
67 |
|
|
68 |
|
/** |
69 |
< |
* containsKey returns true for contained key |
69 |
> |
* containsKey returns true for contained key |
70 |
|
*/ |
71 |
|
public void testContainsKey() { |
72 |
|
ConcurrentHashMap map = map5(); |
75 |
|
} |
76 |
|
|
77 |
|
/** |
78 |
< |
* containsValue returns true for held values |
78 |
> |
* containsValue returns true for held values |
79 |
|
*/ |
80 |
|
public void testContainsValue() { |
81 |
|
ConcurrentHashMap map = map5(); |
84 |
|
} |
85 |
|
|
86 |
|
/** |
87 |
< |
* enumeration returns an enumeration containing the correct |
88 |
< |
* elements |
87 |
> |
* enumeration returns an enumeration containing the correct |
88 |
> |
* elements |
89 |
|
*/ |
90 |
|
public void testEnumeration() { |
91 |
|
ConcurrentHashMap map = map5(); |
99 |
|
} |
100 |
|
|
101 |
|
/** |
102 |
< |
* get returns the correct element at the given key, |
103 |
< |
* or null if not present |
102 |
> |
* get returns the correct element at the given key, |
103 |
> |
* or null if not present |
104 |
|
*/ |
105 |
|
public void testGet() { |
106 |
|
ConcurrentHashMap map = map5(); |
110 |
|
} |
111 |
|
|
112 |
|
/** |
113 |
< |
* isEmpty is true of empty map and false for non-empty |
113 |
> |
* isEmpty is true of empty map and false for non-empty |
114 |
|
*/ |
115 |
|
public void testIsEmpty() { |
116 |
|
ConcurrentHashMap empty = new ConcurrentHashMap(); |
120 |
|
} |
121 |
|
|
122 |
|
/** |
123 |
< |
* keys returns an enumeration containing all the keys from the map |
123 |
> |
* keys returns an enumeration containing all the keys from the map |
124 |
|
*/ |
125 |
|
public void testKeys() { |
126 |
|
ConcurrentHashMap map = map5(); |
134 |
|
} |
135 |
|
|
136 |
|
/** |
137 |
< |
* keySet returns a Set containing all the keys |
137 |
> |
* keySet returns a Set containing all the keys |
138 |
|
*/ |
139 |
|
public void testKeySet() { |
140 |
|
ConcurrentHashMap map = map5(); |
148 |
|
} |
149 |
|
|
150 |
|
/** |
151 |
< |
* keySet.toArray returns contains all keys |
151 |
> |
* keySet.toArray returns contains all keys |
152 |
|
*/ |
153 |
|
public void testKeySetToArray() { |
154 |
|
ConcurrentHashMap map = map5(); |
161 |
|
} |
162 |
|
|
163 |
|
/** |
164 |
< |
* Values.toArray contains all values |
164 |
> |
* Values.toArray contains all values |
165 |
|
*/ |
166 |
|
public void testValuesToArray() { |
167 |
|
ConcurrentHashMap map = map5(); |
177 |
|
} |
178 |
|
|
179 |
|
/** |
180 |
< |
* entrySet.toArray contains all entries |
180 |
> |
* entrySet.toArray contains all entries |
181 |
|
*/ |
182 |
|
public void testEntrySetToArray() { |
183 |
|
ConcurrentHashMap map = map5(); |
224 |
|
} |
225 |
|
|
226 |
|
/** |
227 |
< |
* putAll adds all key-value pairs from the given map |
227 |
> |
* putAll adds all key-value pairs from the given map |
228 |
|
*/ |
229 |
|
public void testPutAll() { |
230 |
|
ConcurrentHashMap empty = new ConcurrentHashMap(); |
239 |
|
} |
240 |
|
|
241 |
|
/** |
242 |
< |
* putIfAbsent works when the given key is not present |
242 |
> |
* putIfAbsent works when the given key is not present |
243 |
|
*/ |
244 |
|
public void testPutIfAbsent() { |
245 |
|
ConcurrentHashMap map = map5(); |
248 |
|
} |
249 |
|
|
250 |
|
/** |
251 |
< |
* putIfAbsent does not add the pair if the key is already present |
251 |
> |
* putIfAbsent does not add the pair if the key is already present |
252 |
|
*/ |
253 |
|
public void testPutIfAbsent2() { |
254 |
|
ConcurrentHashMap map = map5(); |
256 |
|
} |
257 |
|
|
258 |
|
/** |
259 |
< |
* replace fails when the given key is not present |
259 |
> |
* replace fails when the given key is not present |
260 |
|
*/ |
261 |
|
public void testReplace() { |
262 |
|
ConcurrentHashMap map = map5(); |
265 |
|
} |
266 |
|
|
267 |
|
/** |
268 |
< |
* replace succeeds if the key is already present |
268 |
> |
* replace succeeds if the key is already present |
269 |
|
*/ |
270 |
|
public void testReplace2() { |
271 |
|
ConcurrentHashMap map = map5(); |
273 |
|
assertEquals("Z", map.get(one)); |
274 |
|
} |
275 |
|
|
278 |
– |
|
276 |
|
/** |
277 |
|
* replace value fails when the given key not mapped to expected value |
278 |
|
*/ |
293 |
|
assertEquals("Z", map.get(one)); |
294 |
|
} |
295 |
|
|
299 |
– |
|
296 |
|
/** |
297 |
< |
* remove removes the correct key-value pair from the map |
297 |
> |
* remove removes the correct key-value pair from the map |
298 |
|
*/ |
299 |
|
public void testRemove() { |
300 |
|
ConcurrentHashMap map = map5(); |
314 |
|
map.remove(four, "A"); |
315 |
|
assertEquals(4, map.size()); |
316 |
|
assertTrue(map.containsKey(four)); |
321 |
– |
|
317 |
|
} |
318 |
|
|
319 |
|
/** |
320 |
< |
* size returns the correct values |
320 |
> |
* size returns the correct values |
321 |
|
*/ |
322 |
|
public void testSize() { |
323 |
|
ConcurrentHashMap map = map5(); |
333 |
|
ConcurrentHashMap map = map5(); |
334 |
|
String s = map.toString(); |
335 |
|
for (int i = 1; i <= 5; ++i) { |
336 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
336 |
> |
assertTrue(s.contains(String.valueOf(i))); |
337 |
|
} |
338 |
|
} |
339 |
|
|
346 |
|
try { |
347 |
|
new ConcurrentHashMap(-1,0,1); |
348 |
|
shouldThrow(); |
349 |
< |
} catch (IllegalArgumentException e) {} |
349 |
> |
} catch (IllegalArgumentException success) {} |
350 |
|
} |
351 |
|
|
352 |
|
/** |
356 |
|
try { |
357 |
|
new ConcurrentHashMap(1,0,-1); |
358 |
|
shouldThrow(); |
359 |
< |
} catch (IllegalArgumentException e) {} |
359 |
> |
} catch (IllegalArgumentException success) {} |
360 |
|
} |
361 |
|
|
362 |
|
/** |
366 |
|
try { |
367 |
|
new ConcurrentHashMap(-1); |
368 |
|
shouldThrow(); |
369 |
< |
} catch (IllegalArgumentException e) {} |
369 |
> |
} catch (IllegalArgumentException success) {} |
370 |
|
} |
371 |
|
|
372 |
|
/** |
377 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
378 |
|
c.get(null); |
379 |
|
shouldThrow(); |
380 |
< |
} catch (NullPointerException e) {} |
380 |
> |
} catch (NullPointerException success) {} |
381 |
|
} |
382 |
|
|
383 |
|
/** |
388 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
389 |
|
c.containsKey(null); |
390 |
|
shouldThrow(); |
391 |
< |
} catch (NullPointerException e) {} |
391 |
> |
} catch (NullPointerException success) {} |
392 |
|
} |
393 |
|
|
394 |
|
/** |
399 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
400 |
|
c.containsValue(null); |
401 |
|
shouldThrow(); |
402 |
< |
} catch (NullPointerException e) {} |
402 |
> |
} catch (NullPointerException success) {} |
403 |
|
} |
404 |
|
|
405 |
|
/** |
410 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
411 |
|
c.contains(null); |
412 |
|
shouldThrow(); |
413 |
< |
} catch (NullPointerException e) {} |
413 |
> |
} catch (NullPointerException success) {} |
414 |
|
} |
415 |
|
|
416 |
|
/** |
421 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
422 |
|
c.put(null, "whatever"); |
423 |
|
shouldThrow(); |
424 |
< |
} catch (NullPointerException e) {} |
424 |
> |
} catch (NullPointerException success) {} |
425 |
|
} |
426 |
|
|
427 |
|
/** |
432 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
433 |
|
c.put("whatever", null); |
434 |
|
shouldThrow(); |
435 |
< |
} catch (NullPointerException e) {} |
435 |
> |
} catch (NullPointerException success) {} |
436 |
|
} |
437 |
|
|
438 |
|
/** |
443 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
444 |
|
c.putIfAbsent(null, "whatever"); |
445 |
|
shouldThrow(); |
446 |
< |
} catch (NullPointerException e) {} |
446 |
> |
} catch (NullPointerException success) {} |
447 |
|
} |
448 |
|
|
449 |
|
/** |
454 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
455 |
|
c.replace(null, "whatever"); |
456 |
|
shouldThrow(); |
457 |
< |
} catch (NullPointerException e) {} |
457 |
> |
} catch (NullPointerException success) {} |
458 |
|
} |
459 |
|
|
460 |
|
/** |
465 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
466 |
|
c.replace(null, one, "whatever"); |
467 |
|
shouldThrow(); |
468 |
< |
} catch (NullPointerException e) {} |
468 |
> |
} catch (NullPointerException success) {} |
469 |
|
} |
470 |
|
|
471 |
|
/** |
476 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
477 |
|
c.putIfAbsent("whatever", null); |
478 |
|
shouldThrow(); |
479 |
< |
} catch (NullPointerException e) {} |
479 |
> |
} catch (NullPointerException success) {} |
480 |
|
} |
481 |
|
|
487 |
– |
|
482 |
|
/** |
483 |
|
* replace(x, null) throws NPE |
484 |
|
*/ |
487 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
488 |
|
c.replace("whatever", null); |
489 |
|
shouldThrow(); |
490 |
< |
} catch (NullPointerException e) {} |
490 |
> |
} catch (NullPointerException success) {} |
491 |
|
} |
492 |
|
|
493 |
|
/** |
498 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
499 |
|
c.replace("whatever", null, "A"); |
500 |
|
shouldThrow(); |
501 |
< |
} catch (NullPointerException e) {} |
501 |
> |
} catch (NullPointerException success) {} |
502 |
|
} |
503 |
|
|
504 |
|
/** |
509 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
510 |
|
c.replace("whatever", one, null); |
511 |
|
shouldThrow(); |
512 |
< |
} catch (NullPointerException e) {} |
512 |
> |
} catch (NullPointerException success) {} |
513 |
|
} |
514 |
|
|
521 |
– |
|
515 |
|
/** |
516 |
|
* remove(null) throws NPE |
517 |
|
*/ |
521 |
|
c.put("sadsdf", "asdads"); |
522 |
|
c.remove(null); |
523 |
|
shouldThrow(); |
524 |
< |
} catch (NullPointerException e) {} |
524 |
> |
} catch (NullPointerException success) {} |
525 |
|
} |
526 |
|
|
527 |
|
/** |
533 |
|
c.put("sadsdf", "asdads"); |
534 |
|
c.remove(null, "whatever"); |
535 |
|
shouldThrow(); |
536 |
< |
} catch (NullPointerException e) {} |
536 |
> |
} catch (NullPointerException success) {} |
537 |
|
} |
538 |
|
|
539 |
|
/** |
540 |
|
* remove(x, null) returns false |
541 |
|
*/ |
542 |
|
public void testRemove3() { |
543 |
< |
try { |
544 |
< |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
545 |
< |
c.put("sadsdf", "asdads"); |
553 |
< |
assertFalse(c.remove("sadsdf", null)); |
554 |
< |
} catch (NullPointerException e) { |
555 |
< |
fail(); |
556 |
< |
} |
543 |
> |
ConcurrentHashMap c = new ConcurrentHashMap(5); |
544 |
> |
c.put("sadsdf", "asdads"); |
545 |
> |
assertFalse(c.remove("sadsdf", null)); |
546 |
|
} |
547 |
|
|
548 |
|
/** |
549 |
|
* A deserialized map equals original |
550 |
|
*/ |
551 |
< |
public void testSerialization() { |
552 |
< |
ConcurrentHashMap q = map5(); |
553 |
< |
|
554 |
< |
try { |
555 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
556 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
557 |
< |
out.writeObject(q); |
558 |
< |
out.close(); |
570 |
< |
|
571 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
572 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
573 |
< |
ConcurrentHashMap r = (ConcurrentHashMap)in.readObject(); |
574 |
< |
assertEquals(q.size(), r.size()); |
575 |
< |
assertTrue(q.equals(r)); |
576 |
< |
assertTrue(r.equals(q)); |
577 |
< |
} catch (Exception e) { |
578 |
< |
e.printStackTrace(); |
579 |
< |
unexpectedException(); |
580 |
< |
} |
551 |
> |
public void testSerialization() throws Exception { |
552 |
> |
Map x = map5(); |
553 |
> |
Map y = serialClone(x); |
554 |
> |
|
555 |
> |
assertTrue(x != y); |
556 |
> |
assertEquals(x.size(), y.size()); |
557 |
> |
assertEquals(x, y); |
558 |
> |
assertEquals(y, x); |
559 |
|
} |
560 |
|
|
583 |
– |
|
561 |
|
/** |
562 |
|
* SetValue of an EntrySet entry sets value in the map. |
563 |
|
*/ |