Code example for Hashtable

Methods: clone, contains, put, size

0
    public void test_putLjava_lang_ObjectLjava_lang_Object() { 
        // Test for method java.lang.Object 
        // java.util.Hashtable.put(java.lang.Object, java.lang.Object) 
        Hashtable h = hashtableClone(ht100);
        Integer key = new Integer(100);
        h.put("Value 100", key);
        assertTrue("Key/Value not inserted", h.size() == 1 && (h.contains(key)));
 
        // Put into "full" table 
        h = hashtableClone(htfull);
        h.put("Value 100", key);
        assertTrue("Key/Value not inserted into full table", h.size() == 8
                && (h.contains(key)));
 
        try { 
            h.put(null, key);
            fail("NullPointerException expected");
        } catch (NullPointerException e) {
            //expected 
        } 
 
        try { 
            h.put("Value 100", null);
            fail("NullPointerException expected");
        } catch (NullPointerException e) {
            //expected 
        } 
    } 
 
    /** 
     * java.util.Hashtable#putAll(java.util.Map) 
     */ 
    public void test_putAllLjava_util_Map() { 
        // Test for method void java.util.Hashtable.putAll(java.util.Map) 
        Hashtable h = new Hashtable();
        h.putAll(ht10);
        Enumeration e = keyVector.elements();
        while (e.hasMoreElements()) {
            Object x = e.nextElement();
            assertTrue("Failed to put all elements", h.get(x).equals(
                    ht10.get(x)));
        } 
 
        try { 
            h.putAll(null);
            fail("NullPointerException expected");
        } catch (NullPointerException ee) {
            //expected 
        } 
    } 
 
    /** 
     * java.util.Hashtable#remove(java.lang.Object) 
     */ 
    public void test_removeLjava_lang_Object() { 
        // Test for method java.lang.Object 
        // java.util.Hashtable.remove(java.lang.Object) 
        Hashtable h = hashtableClone(htfull);
        Object k = h.remove("FKey 0");
        assertTrue("Remove failed", !h.containsKey("FKey 0") || k == null);
        assertNull(h.remove("FKey 0"));
 
        try { 
            h.remove(null);
            fail("NullPointerException expected");
        } catch (NullPointerException e) {
            //expected 
        } 
    } 
 
    /** 
     * java.util.Hashtable#size() 
     */ 
    public void test_size() { 
        // Test for method int java.util.Hashtable.size() 
        assertTrue("Returned invalid size", ht10.size() == 10
                && (ht100.size() == 0));
 
        final Hashtable ht = new Hashtable();
        ht.put("0", "");
        Thread t1 = new Thread() {
            public void run() { 
                while (ht.size() > 0)
                    ; 
                ht.put("final", "");
            } 
        }; 
        t1.start();
        for (int i = 1; i < 10000; i++) {
            synchronized (ht) {
                ht.remove(String.valueOf(i - 1));
                ht.put(String.valueOf(i), "");
            } 
            int size;
            if ((size = ht.size()) != 1) {
                String result = "Size is not 1: " + size + " " + ht;
                // terminate the thread 
                ht.clear();
                fail(result);
            } 
        } 
        // terminate the thread 
        ht.clear();
    } 
 
    /** 
     * java.util.Hashtable#toString() 
     */ 
    public void test_toString() { 
        // Test for method java.lang.String java.util.Hashtable.toString() 
        Hashtable h = new Hashtable();
        assertEquals("Incorrect toString for Empty table",
                "{}", h.toString());
 
        h.put("one", "1");
        h.put("two", h);
        h.put(h, "3");
        h.put(h, h);
        String result = h.toString();
        assertTrue("should contain self ref", result.indexOf("(this") > -1);
    } 
 
    /** 
     * java.util.Hashtable#values() 
     */ 
    public void test_values() { 
        // Test for method java.util.Collection java.util.Hashtable.values() 
        Collection c = ht10.values();
        Enumeration e = elmVector.elements();
        while (e.hasMoreElements())
            assertTrue("Returned incorrect values", c.contains(e.nextElement()));
 
// BEGIN android-removed 
// implementation dependent 
//        assertEquals("Not synchronized", 
//                "java.util.Collections$SynchronizedCollection", c.getClass().getName()); 
// END android-removed 
 
        Hashtable myHashtable = new Hashtable();
        for (int i = 0; i < 100; i++)
            myHashtable.put(new Integer(i), new Integer(i));
        Collection values = myHashtable.values();
        new Support_UnmodifiableCollectionTest( 
                "Test Returned Collection From Hashtable.values()", values)
                .runTest(); 
        values.remove(new Integer(0));
        assertTrue(
                "Removing from the values collection should remove from the original map", 
                !myHashtable.containsValue(new Integer(0)));
    } 
 
    /** 
     * Regression Test for JIRA 2181 
     */ 
    public void test_entrySet_remove() 
    { 
        Hashtable<String,String> hashtable = new Hashtable<String,String>();
        hashtable.put("my.nonexistent.prop", "AAA");
        hashtable.put( "parse.error", "BBB" );
        Iterator<Map.Entry<String,String>> iterator =
            hashtable.entrySet().iterator();
        while(iterator.hasNext())
        { 
            Map.Entry entry = iterator.next();
            final Object value = entry.getValue();
            if(value.equals("AAA"))
            { 
               iterator.remove();
            } 
        } 
        assertFalse(hashtable.containsKey("my.nonexistent.prop"));
    } 
 
    class Mock_Hashtable extends Hashtable {
        boolean flag = false;
 
        public Mock_Hashtable(int i) {
            super(i);
        } 
 
        @Override 
        protected void rehash() { 
            flag = true;
            super.rehash(); 
        } 
 
        public boolean isRehashed() { 
            return flag;
        } 
    } 
 
    public void test_rehash() { 
        Mock_Hashtable mht = new Mock_Hashtable(5);
 
        assertFalse(mht.isRehashed());
        for(int i = 0; i < 10; i++) {
            mht.put(i, "New value");
        } 
        assertTrue(mht.isRehashed());
    } 
 
    protected Hashtable hashtableClone(Hashtable s) {
        return (Hashtable) s.clone();
    } 
 
    /** 
     * Sets up the fixture, for example, open a network connection. This method 
     * is called before a test is executed.