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. |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
12 |
|
import java.util.Enumeration; |
13 |
|
import java.io.*; |
14 |
|
|
15 |
< |
public class ConcurrentHashMapTest extends JSR166TestCase{ |
15 |
> |
public class ConcurrentHashMapTest extends JSR166TestCase { |
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run (suite()); |
17 |
> |
junit.textui.TestRunner.run (suite()); |
18 |
|
} |
19 |
|
public static Test suite() { |
20 |
|
return new TestSuite(ConcurrentHashMapTest.class); |
23 |
|
/** |
24 |
|
* Create a map from Integers 1-5 to Strings "A"-"E". |
25 |
|
*/ |
26 |
< |
private static ConcurrentHashMap map5() { |
26 |
> |
private static ConcurrentHashMap map5() { |
27 |
|
ConcurrentHashMap map = new ConcurrentHashMap(5); |
28 |
|
assertTrue(map.isEmpty()); |
29 |
|
map.put(one, "A"); |
66 |
|
assertTrue(map.contains("A")); |
67 |
|
assertFalse(map.contains("Z")); |
68 |
|
} |
69 |
< |
|
69 |
> |
|
70 |
|
/** |
71 |
|
* containsKey returns true for contained key |
72 |
|
*/ |
93 |
|
ConcurrentHashMap map = map5(); |
94 |
|
Enumeration e = map.elements(); |
95 |
|
int count = 0; |
96 |
< |
while(e.hasMoreElements()){ |
96 |
> |
while (e.hasMoreElements()) { |
97 |
|
count++; |
98 |
|
e.nextElement(); |
99 |
|
} |
128 |
|
ConcurrentHashMap map = map5(); |
129 |
|
Enumeration e = map.keys(); |
130 |
|
int count = 0; |
131 |
< |
while(e.hasMoreElements()){ |
131 |
> |
while (e.hasMoreElements()) { |
132 |
|
count++; |
133 |
|
e.nextElement(); |
134 |
|
} |
150 |
|
} |
151 |
|
|
152 |
|
/** |
153 |
+ |
* keySet.toArray returns contains all keys |
154 |
+ |
*/ |
155 |
+ |
public void testKeySetToArray() { |
156 |
+ |
ConcurrentHashMap map = map5(); |
157 |
+ |
Set s = map.keySet(); |
158 |
+ |
Object[] ar = s.toArray(); |
159 |
+ |
assertTrue(s.containsAll(Arrays.asList(ar))); |
160 |
+ |
assertEquals(5, ar.length); |
161 |
+ |
ar[0] = m10; |
162 |
+ |
assertFalse(s.containsAll(Arrays.asList(ar))); |
163 |
+ |
} |
164 |
+ |
|
165 |
+ |
/** |
166 |
+ |
* Values.toArray contains all values |
167 |
+ |
*/ |
168 |
+ |
public void testValuesToArray() { |
169 |
+ |
ConcurrentHashMap map = map5(); |
170 |
+ |
Collection v = map.values(); |
171 |
+ |
Object[] ar = v.toArray(); |
172 |
+ |
ArrayList s = new ArrayList(Arrays.asList(ar)); |
173 |
+ |
assertEquals(5, ar.length); |
174 |
+ |
assertTrue(s.contains("A")); |
175 |
+ |
assertTrue(s.contains("B")); |
176 |
+ |
assertTrue(s.contains("C")); |
177 |
+ |
assertTrue(s.contains("D")); |
178 |
+ |
assertTrue(s.contains("E")); |
179 |
+ |
} |
180 |
+ |
|
181 |
+ |
/** |
182 |
+ |
* entrySet.toArray contains all entries |
183 |
+ |
*/ |
184 |
+ |
public void testEntrySetToArray() { |
185 |
+ |
ConcurrentHashMap map = map5(); |
186 |
+ |
Set s = map.entrySet(); |
187 |
+ |
Object[] ar = s.toArray(); |
188 |
+ |
assertEquals(5, ar.length); |
189 |
+ |
for (int i = 0; i < 5; ++i) { |
190 |
+ |
assertTrue(map.containsKey(((Map.Entry)(ar[i])).getKey())); |
191 |
+ |
assertTrue(map.containsValue(((Map.Entry)(ar[i])).getValue())); |
192 |
+ |
} |
193 |
+ |
} |
194 |
+ |
|
195 |
+ |
/** |
196 |
|
* values collection contains all values |
197 |
|
*/ |
198 |
|
public void testValues() { |
216 |
|
Iterator it = s.iterator(); |
217 |
|
while (it.hasNext()) { |
218 |
|
Map.Entry e = (Map.Entry) it.next(); |
219 |
< |
assertTrue( |
219 |
> |
assertTrue( |
220 |
|
(e.getKey().equals(one) && e.getValue().equals("A")) || |
221 |
|
(e.getKey().equals(two) && e.getValue().equals("B")) || |
222 |
|
(e.getKey().equals(three) && e.getValue().equals("C")) || |
340 |
|
for (int i = 1; i <= 5; ++i) { |
341 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
342 |
|
} |
343 |
< |
} |
343 |
> |
} |
344 |
|
|
345 |
|
// Exception tests |
346 |
< |
|
346 |
> |
|
347 |
|
/** |
348 |
< |
* Cannot create with negative capacity |
348 |
> |
* Cannot create with negative capacity |
349 |
|
*/ |
350 |
|
public void testConstructor1() { |
351 |
|
try { |
352 |
|
new ConcurrentHashMap(-1,0,1); |
353 |
|
shouldThrow(); |
354 |
< |
} catch(IllegalArgumentException e){} |
354 |
> |
} catch (IllegalArgumentException e) {} |
355 |
|
} |
356 |
|
|
357 |
|
/** |
361 |
|
try { |
362 |
|
new ConcurrentHashMap(1,0,-1); |
363 |
|
shouldThrow(); |
364 |
< |
} catch(IllegalArgumentException e){} |
364 |
> |
} catch (IllegalArgumentException e) {} |
365 |
|
} |
366 |
|
|
367 |
|
/** |
371 |
|
try { |
372 |
|
new ConcurrentHashMap(-1); |
373 |
|
shouldThrow(); |
374 |
< |
} catch(IllegalArgumentException e){} |
374 |
> |
} catch (IllegalArgumentException e) {} |
375 |
|
} |
376 |
|
|
377 |
|
/** |
382 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
383 |
|
c.get(null); |
384 |
|
shouldThrow(); |
385 |
< |
} catch(NullPointerException e){} |
385 |
> |
} catch (NullPointerException e) {} |
386 |
|
} |
387 |
|
|
388 |
|
/** |
393 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
394 |
|
c.containsKey(null); |
395 |
|
shouldThrow(); |
396 |
< |
} catch(NullPointerException e){} |
396 |
> |
} catch (NullPointerException e) {} |
397 |
|
} |
398 |
|
|
399 |
|
/** |
404 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
405 |
|
c.containsValue(null); |
406 |
|
shouldThrow(); |
407 |
< |
} catch(NullPointerException e){} |
407 |
> |
} catch (NullPointerException e) {} |
408 |
|
} |
409 |
|
|
410 |
|
/** |
415 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
416 |
|
c.contains(null); |
417 |
|
shouldThrow(); |
418 |
< |
} catch(NullPointerException e){} |
418 |
> |
} catch (NullPointerException e) {} |
419 |
|
} |
420 |
|
|
421 |
|
/** |
426 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
427 |
|
c.put(null, "whatever"); |
428 |
|
shouldThrow(); |
429 |
< |
} catch(NullPointerException e){} |
429 |
> |
} catch (NullPointerException e) {} |
430 |
|
} |
431 |
|
|
432 |
|
/** |
437 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
438 |
|
c.put("whatever", null); |
439 |
|
shouldThrow(); |
440 |
< |
} catch(NullPointerException e){} |
440 |
> |
} catch (NullPointerException e) {} |
441 |
|
} |
442 |
|
|
443 |
|
/** |
448 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
449 |
|
c.putIfAbsent(null, "whatever"); |
450 |
|
shouldThrow(); |
451 |
< |
} catch(NullPointerException e){} |
451 |
> |
} catch (NullPointerException e) {} |
452 |
|
} |
453 |
|
|
454 |
|
/** |
459 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
460 |
|
c.replace(null, "whatever"); |
461 |
|
shouldThrow(); |
462 |
< |
} catch(NullPointerException e){} |
462 |
> |
} catch (NullPointerException e) {} |
463 |
|
} |
464 |
|
|
465 |
|
/** |
470 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
471 |
|
c.replace(null, one, "whatever"); |
472 |
|
shouldThrow(); |
473 |
< |
} catch(NullPointerException e){} |
473 |
> |
} catch (NullPointerException e) {} |
474 |
|
} |
475 |
|
|
476 |
|
/** |
481 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
482 |
|
c.putIfAbsent("whatever", null); |
483 |
|
shouldThrow(); |
484 |
< |
} catch(NullPointerException e){} |
484 |
> |
} catch (NullPointerException e) {} |
485 |
|
} |
486 |
|
|
487 |
|
|
493 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
494 |
|
c.replace("whatever", null); |
495 |
|
shouldThrow(); |
496 |
< |
} catch(NullPointerException e){} |
496 |
> |
} catch (NullPointerException e) {} |
497 |
|
} |
498 |
|
|
499 |
|
/** |
504 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
505 |
|
c.replace("whatever", null, "A"); |
506 |
|
shouldThrow(); |
507 |
< |
} catch(NullPointerException e){} |
507 |
> |
} catch (NullPointerException e) {} |
508 |
|
} |
509 |
|
|
510 |
|
/** |
515 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
516 |
|
c.replace("whatever", one, null); |
517 |
|
shouldThrow(); |
518 |
< |
} catch(NullPointerException e){} |
518 |
> |
} catch (NullPointerException e) {} |
519 |
|
} |
520 |
|
|
521 |
|
|
528 |
|
c.put("sadsdf", "asdads"); |
529 |
|
c.remove(null); |
530 |
|
shouldThrow(); |
531 |
< |
} catch(NullPointerException e){} |
531 |
> |
} catch (NullPointerException e) {} |
532 |
|
} |
533 |
|
|
534 |
|
/** |
540 |
|
c.put("sadsdf", "asdads"); |
541 |
|
c.remove(null, "whatever"); |
542 |
|
shouldThrow(); |
543 |
< |
} catch(NullPointerException e){} |
543 |
> |
} catch (NullPointerException e) {} |
544 |
|
} |
545 |
|
|
546 |
|
/** |
551 |
|
ConcurrentHashMap c = new ConcurrentHashMap(5); |
552 |
|
c.put("sadsdf", "asdads"); |
553 |
|
assertFalse(c.remove("sadsdf", null)); |
554 |
< |
} catch(NullPointerException e){ |
554 |
> |
} catch (NullPointerException e) { |
555 |
|
fail(); |
556 |
|
} |
557 |
|
} |
574 |
|
assertEquals(q.size(), r.size()); |
575 |
|
assertTrue(q.equals(r)); |
576 |
|
assertTrue(r.equals(q)); |
577 |
< |
} catch(Exception e){ |
577 |
> |
} catch (Exception e) { |
578 |
|
e.printStackTrace(); |
579 |
|
unexpectedException(); |
580 |
|
} |
585 |
|
* SetValue of an EntrySet entry sets value in the map. |
586 |
|
*/ |
587 |
|
public void testSetValueWriteThrough() { |
588 |
< |
// Adapted from a bug report by Eric Zoerner |
588 |
> |
// Adapted from a bug report by Eric Zoerner |
589 |
|
ConcurrentHashMap map = new ConcurrentHashMap(2, 5.0f, 1); |
590 |
|
assertTrue(map.isEmpty()); |
591 |
|
for (int i = 0; i < 20; i++) |
592 |
|
map.put(new Integer(i), new Integer(i)); |
593 |
|
assertFalse(map.isEmpty()); |
594 |
|
Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next(); |
595 |
< |
|
595 |
> |
|
596 |
|
// assert that entry1 is not 16 |
597 |
|
assertTrue("entry is 16, test not valid", |
598 |
|
!entry1.getKey().equals(new Integer(16))); |
599 |
< |
|
600 |
< |
// remove 16 (a different key) from map |
599 |
> |
|
600 |
> |
// remove 16 (a different key) from map |
601 |
|
// which just happens to cause entry1 to be cloned in map |
602 |
|
map.remove(new Integer(16)); |
603 |
|
entry1.setValue("XYZ"); |
604 |
|
assertTrue(map.containsValue("XYZ")); // fails |
605 |
|
} |
606 |
< |
|
606 |
> |
|
607 |
|
} |