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.*; |
14 |
|
|
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 |
|
*/ |
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 { |
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 |
|
} |