FileDocCategorySizeDatePackage
ConcurrentHashMapTest.javaAPI DocAndroid 1.5 API17174Wed May 06 22:41:02 BST 2009tests.api.java.util.concurrent

ConcurrentHashMapTest

public class ConcurrentHashMapTest extends JSR166TestCase

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

        junit.textui.TestRunner.run (suite());        
    
private static java.util.concurrent.ConcurrentHashMapmap5()
Create a map from Integers 1-5 to Strings "A"-"E".

   
        ConcurrentHashMap map = new ConcurrentHashMap(5);
        assertTrue(map.isEmpty());
        map.put(one, "A");
        map.put(two, "B");
        map.put(three, "C");
        map.put(four, "D");
        map.put(five, "E");
        assertFalse(map.isEmpty());
        assertEquals(5, map.size());
        return map;
    
private static tests.api.java.util.concurrent.ConcurrentHashMapTest$MyConcurrentHashMapmyMap5()
Create a map from Integers 1-5 to Strings "A"-"E".

        MyConcurrentHashMap map = new MyConcurrentHashMap(5);
        assertTrue(map.isEmpty());
        map.put(one, "A");
        map.put(two, "B");
        map.put(three, "C");
        map.put(four, "D");
        map.put(five, "E");
        assertFalse(map.isEmpty());
        assertEquals(5, map.size());
        return map;
    
public static junit.framework.Testsuite()

        return new TestSuite(ConcurrentHashMapTest.class);
    
public voidtestClear()
clear removes all pairs

        ConcurrentHashMap map = map5();
        map.clear();
        assertEquals(map.size(), 0);
    
public voidtestClone()
Clone creates an equal map

        // BEGIN android-changed
        MyConcurrentHashMap map =myMap5();
        try {
            MyConcurrentHashMap m2 = (MyConcurrentHashMap)(map.clone());
            assertEquals(map, m2);
        } catch (CloneNotSupportedException e) {
            fail("clone not supported");
        }
        // END android-changed
    
public voidtestConstructor1()
Cannot create with negative capacity

        try {
            new ConcurrentHashMap(-1,0,1);
            shouldThrow();
        } catch(IllegalArgumentException e){}
    
public voidtestConstructor2()
Cannot create with negative concurrency level

        try {
            new ConcurrentHashMap(1,0,-1);
            shouldThrow();
        } catch(IllegalArgumentException e){}
    
public voidtestConstructor3()
Cannot create with only negative capacity

        try {
            new ConcurrentHashMap(-1);
            shouldThrow();
        } catch(IllegalArgumentException e){}
    
public voidtestContains()
contains returns true for contained value

        ConcurrentHashMap map = map5();
        assertTrue(map.contains("A"));
        assertFalse(map.contains("Z"));
    
public voidtestContainsKey()
containsKey returns true for contained key

        ConcurrentHashMap map = map5();
        assertTrue(map.containsKey(one));
        assertFalse(map.containsKey(zero));
    
public voidtestContainsKey_NullPointerException()
containsKey(null) throws NPE

        try {
            ConcurrentHashMap c = new ConcurrentHashMap(5);
            c.containsKey(null);
            shouldThrow();
        } catch(NullPointerException e){}
    
public voidtestContainsValue()
containsValue returns true for held values

        ConcurrentHashMap map = map5();
        assertTrue(map.contains("A"));
        assertFalse(map.contains("Z"));
    
public voidtestContainsValue_NullPointerException()
containsValue(null) throws NPE

        try {
            ConcurrentHashMap c = new ConcurrentHashMap(5);
            c.containsValue(null);
            shouldThrow();
        } catch(NullPointerException e){}
    
public voidtestContains_NullPointerException()
contains(null) throws NPE

        try {
            ConcurrentHashMap c = new ConcurrentHashMap(5);
            c.contains(null);
            shouldThrow();
        } catch(NullPointerException e){}
    
public voidtestEntrySet()
entrySet contains all pairs

        ConcurrentHashMap map = map5();
        Set s = map.entrySet();
        assertEquals(5, s.size());
        Iterator it = s.iterator();
        while (it.hasNext()) {
            Map.Entry e = (Map.Entry) it.next();
            assertTrue( 
                       (e.getKey().equals(one) && e.getValue().equals("A")) ||
                       (e.getKey().equals(two) && e.getValue().equals("B")) ||
                       (e.getKey().equals(three) && e.getValue().equals("C")) ||
                       (e.getKey().equals(four) && e.getValue().equals("D")) ||
                       (e.getKey().equals(five) && e.getValue().equals("E")));
        }
    
public voidtestEnumeration()
enumeration returns an enumeration containing the correct elements

        ConcurrentHashMap map = map5();
        Enumeration e = map.elements();
        int count = 0;
        while(e.hasMoreElements()){
            count++;
            e.nextElement();
        }
        assertEquals(5, count);
    
public voidtestEquals()
Maps with same contents are equal

        ConcurrentHashMap map1 = map5();
        ConcurrentHashMap map2 = map5();
        assertEquals(map1, map2);
        assertEquals(map2, map1);
        map1.clear();
        assertFalse(map1.equals(map2));
        assertFalse(map2.equals(map1));
    
public voidtestGet()
get returns the correct element at the given key, or null if not present

        ConcurrentHashMap map = map5();
        assertEquals("A", (String)map.get(one));
        ConcurrentHashMap empty = new ConcurrentHashMap();
        assertNull(map.get("anything"));
    
public voidtestGet_NullPointerException()
get(null) throws NPE

        try {
            ConcurrentHashMap c = new ConcurrentHashMap(5);
            c.get(null);
            shouldThrow();
        } catch(NullPointerException e){}
    
public voidtestIsEmpty()
isEmpty is true of empty map and false for non-empty

        ConcurrentHashMap empty = new ConcurrentHashMap();
        ConcurrentHashMap map = map5();
        assertTrue(empty.isEmpty());
        assertFalse(map.isEmpty());
    
public voidtestKeySet()
keySet returns a Set containing all the keys

        ConcurrentHashMap map = map5();
        Set s = map.keySet();
        assertEquals(5, s.size());
        assertTrue(s.contains(one));
        assertTrue(s.contains(two));
        assertTrue(s.contains(three));
        assertTrue(s.contains(four));
        assertTrue(s.contains(five));
    
public voidtestKeys()
keys returns an enumeration containing all the keys from the map

        ConcurrentHashMap map = map5();
        Enumeration e = map.keys();
        int count = 0;
        while(e.hasMoreElements()){
            count++;
            e.nextElement();
        }
        assertEquals(5, count);
    
public voidtestPut1_NullPointerException()
put(null,x) throws NPE

        try {
            ConcurrentHashMap c = new ConcurrentHashMap(5);
            c.put(null, "whatever");
            shouldThrow();
        } catch(NullPointerException e){}
    
public voidtestPut2_NullPointerException()
put(x, null) throws NPE

        try {
            ConcurrentHashMap c = new ConcurrentHashMap(5);
            c.put("whatever", null);
            shouldThrow();
        } catch(NullPointerException e){}
    
public voidtestPutAll()
putAll adds all key-value pairs from the given map

        ConcurrentHashMap empty = new ConcurrentHashMap();
        ConcurrentHashMap map = map5();
        empty.putAll(map);
        assertEquals(5, empty.size());
        assertTrue(empty.containsKey(one));
        assertTrue(empty.containsKey(two));
        assertTrue(empty.containsKey(three));
        assertTrue(empty.containsKey(four));
        assertTrue(empty.containsKey(five));
    
public voidtestPutIfAbsent()
putIfAbsent works when the given key is not present

        ConcurrentHashMap map = map5();
        map.putIfAbsent(six, "Z");
        assertTrue(map.containsKey(six));
    
public voidtestPutIfAbsent1_NullPointerException()
putIfAbsent(null, x) throws NPE

        try {
            ConcurrentHashMap c = new ConcurrentHashMap(5);
            c.putIfAbsent(null, "whatever");
            shouldThrow();
        } catch(NullPointerException e){}
    
public voidtestPutIfAbsent2()
putIfAbsent does not add the pair if the key is already present

        ConcurrentHashMap map = map5();
        assertEquals("A", map.putIfAbsent(one, "Z"));
    
public voidtestPutIfAbsent2_NullPointerException()
putIfAbsent(x, null) throws NPE

        try {
            ConcurrentHashMap c = new ConcurrentHashMap(5);
            c.putIfAbsent("whatever", null);
            shouldThrow();
        } catch(NullPointerException e){}
    
public voidtestRemove()
remove removes the correct key-value pair from the map

        ConcurrentHashMap map = map5();
        map.remove(five);
        assertEquals(4, map.size());
        assertFalse(map.containsKey(five));
    
public voidtestRemove1_NullPointerException()
remove(null) throws NPE

        try {
            ConcurrentHashMap c = new ConcurrentHashMap(5);
            c.put("sadsdf", "asdads");
            c.remove(null);
            shouldThrow();
        } catch(NullPointerException e){}
    
public voidtestRemove2()
remove(key,value) removes only if pair present

        ConcurrentHashMap map = map5();
        map.remove(five, "E");
        assertEquals(4, map.size());
        assertFalse(map.containsKey(five));
        map.remove(four, "A");
        assertEquals(4, map.size());
        assertTrue(map.containsKey(four));

    
public voidtestRemove2_NullPointerException()
remove(null, x) throws NPE

        try {
            ConcurrentHashMap c = new ConcurrentHashMap(5);
            c.put("sadsdf", "asdads");
            c.remove(null, "whatever");
            shouldThrow();
        } catch(NullPointerException e){}
    
public voidtestReplace()
replace fails when the given key is not present

        ConcurrentHashMap map = map5();
        assertNull(map.replace(six, "Z"));
        assertFalse(map.containsKey(six));
    
public voidtestReplace2()
replace succeeds if the key is already present

        ConcurrentHashMap map = map5();
        assertNotNull(map.replace(one, "Z"));
        assertEquals("Z", map.get(one));
    
public voidtestReplace2_NullPointerException()
replace(x, null) throws NPE

        try {
            ConcurrentHashMap c = new ConcurrentHashMap(5);
            c.replace("whatever", null);
            shouldThrow();
        } catch(NullPointerException e){}
    
public voidtestReplaceValue()
replace value fails when the given key not mapped to expected value

        ConcurrentHashMap map = map5();
        assertEquals("A", map.get(one));
        assertFalse(map.replace(one, "Z", "Z"));
        assertEquals("A", map.get(one));
    
public voidtestReplaceValue2()
replace value succeeds when the given key mapped to expected value

        ConcurrentHashMap map = map5();
        assertEquals("A", map.get(one));
        assertTrue(map.replace(one, "A", "Z"));
        assertEquals("Z", map.get(one));
    
public voidtestReplaceValue2_NullPointerException()
replace(x, null, y) throws NPE

        try {
            ConcurrentHashMap c = new ConcurrentHashMap(5);
            c.replace("whatever", null, "A");
            shouldThrow();
        } catch(NullPointerException e){}
    
public voidtestReplaceValue3_NullPointerException()
replace(x, y, null) throws NPE

        try {
            ConcurrentHashMap c = new ConcurrentHashMap(5);
            c.replace("whatever", one, null);
            shouldThrow();
        } catch(NullPointerException e){}
    
public voidtestReplaceValue_NullPointerException()
replace(null, x, y) throws NPE

        try {
            ConcurrentHashMap c = new ConcurrentHashMap(5);
            c.replace(null, one, "whatever");
            shouldThrow();
        } catch(NullPointerException e){}
    
public voidtestReplace_NullPointerException()
replace(null, x) throws NPE

        try {
            ConcurrentHashMap c = new ConcurrentHashMap(5);
            c.replace(null, "whatever");
            shouldThrow();
        } catch(NullPointerException e){}
    
public voidtestSerialization()
A deserialized map equals original

        ConcurrentHashMap q = map5();

        try {
            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
            out.writeObject(q);
            out.close();

            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
            ConcurrentHashMap r = (ConcurrentHashMap)in.readObject();
            assertEquals(q.size(), r.size());
            assertTrue(q.equals(r));
            assertTrue(r.equals(q));
        } catch(Exception e){
            e.printStackTrace();
            unexpectedException();
        }
    
public voidtestSetValueWriteThrough()
SetValue of an EntrySet entry sets value in the map.

        // Adapted from a bug report by Eric Zoerner 
        ConcurrentHashMap map = new ConcurrentHashMap(2, 5.0f, 1);
        assertTrue(map.isEmpty());
        for (int i = 0; i < 20; i++)
            map.put(new Integer(i), new Integer(i));
        assertFalse(map.isEmpty());
        Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next();
        
        // assert that entry1 is not 16
        assertTrue("entry is 16, test not valid",
                   !entry1.getKey().equals(new Integer(16)));
        
        // remove 16 (a different key) from map 
        // which just happens to cause entry1 to be cloned in map
        map.remove(new Integer(16));
        entry1.setValue("XYZ");
        assertTrue(map.containsValue("XYZ")); // fails
    
public voidtestSize()
size returns the correct values

        ConcurrentHashMap map = map5();
        ConcurrentHashMap empty = new ConcurrentHashMap();
        assertEquals(0, empty.size());
        assertEquals(5, map.size());
    
public voidtestToString()
toString contains toString of elements

        ConcurrentHashMap map = map5();
        String s = map.toString();
        for (int i = 1; i <= 5; ++i) {
            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
        }
    
public voidtestValues()
values collection contains all values

        ConcurrentHashMap map = map5();
        Collection s = map.values();
        assertEquals(5, s.size());
        assertTrue(s.contains("A"));
        assertTrue(s.contains("B"));
        assertTrue(s.contains("C"));
        assertTrue(s.contains("D"));
        assertTrue(s.contains("E"));