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) { |
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); |
40 |
|
public void testClear() { |
41 |
|
ConcurrentHashMap map = map5(); |
42 |
|
map.clear(); |
43 |
< |
assertEquals(map.size(), 0); |
43 |
> |
assertEquals(0, map.size()); |
44 |
|
} |
45 |
|
|
46 |
|
/** |
549 |
|
* A deserialized map equals original |
550 |
|
*/ |
551 |
|
public void testSerialization() throws Exception { |
552 |
< |
ConcurrentHashMap q = map5(); |
552 |
> |
Map x = map5(); |
553 |
> |
Map y = serialClone(x); |
554 |
|
|
555 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
556 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
557 |
< |
out.writeObject(q); |
558 |
< |
out.close(); |
560 |
< |
|
561 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
562 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
563 |
< |
ConcurrentHashMap r = (ConcurrentHashMap)in.readObject(); |
564 |
< |
assertEquals(q.size(), r.size()); |
565 |
< |
assertTrue(q.equals(r)); |
566 |
< |
assertTrue(r.equals(q)); |
555 |
> |
assertTrue(x != y); |
556 |
> |
assertEquals(x.size(), y.size()); |
557 |
> |
assertEquals(x, y); |
558 |
> |
assertEquals(y, x); |
559 |
|
} |
560 |
|
|
561 |
|
/** |
569 |
|
map.put(new Integer(i), new Integer(i)); |
570 |
|
assertFalse(map.isEmpty()); |
571 |
|
Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next(); |
572 |
< |
|
573 |
< |
// assert that entry1 is not 16 |
574 |
< |
assertTrue("entry is 16, test not valid", |
575 |
< |
!entry1.getKey().equals(new Integer(16))); |
576 |
< |
|
577 |
< |
// remove 16 (a different key) from map |
578 |
< |
// which just happens to cause entry1 to be cloned in map |
579 |
< |
map.remove(new Integer(16)); |
580 |
< |
entry1.setValue("XYZ"); |
589 |
< |
assertTrue(map.containsValue("XYZ")); // fails |
572 |
> |
// Unless it happens to be first (in which case remainder of |
573 |
> |
// test is skipped), remove a possibly-colliding key from map |
574 |
> |
// which, under some implementations, may cause entry1 to be |
575 |
> |
// cloned in map |
576 |
> |
if (!entry1.getKey().equals(new Integer(16))) { |
577 |
> |
map.remove(new Integer(16)); |
578 |
> |
entry1.setValue("XYZ"); |
579 |
> |
assertTrue(map.containsValue("XYZ")); // fails if write-through broken |
580 |
> |
} |
581 |
|
} |
582 |
|
|
583 |
|
} |