KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > util > IdentityWeakHashMapTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.object.util;
5
6 import java.util.WeakHashMap JavaDoc;
7
8 import junit.framework.TestCase;
9
10 /**
11  * @author steve
12  */

13 public class IdentityWeakHashMapTest extends TestCase {
14   public void tests() throws Exception JavaDoc {
15     examineIdentityWeakHashMap();
16     examineWeakHashMap();
17   }
18   
19   private void examineIdentityWeakHashMap() {
20     IdentityWeakHashMap map = new IdentityWeakHashMap();
21     TestObj k1 = new TestObj();
22     TestObj k2 = new TestObj();
23     map.put(k1, new Integer JavaDoc(1));
24     assertTrue(map.size() == 1);
25     assertTrue(map.containsKey(k1));
26     assertFalse(map.containsKey(new TestObj()));
27     map.put(k2, new Integer JavaDoc(2));
28     assertTrue(map.size() == 2);
29     assertTrue(map.containsKey(k1));
30     assertTrue(map.containsKey(k2));
31     map.put(k1, new Integer JavaDoc(3));
32     assertTrue(map.size()==2);
33     assertTrue(map.containsKey(k1));
34     assertTrue(map.get(k1).equals(new Integer JavaDoc(3)));
35     
36     TestObj[] objects = new TestObj[1000];
37     for(int i=0;i<objects.length;i++){
38       objects[i] = new TestObj();
39       map.put(objects[i], new Long JavaDoc(i));
40     }
41     
42     for(int i=0;i<objects.length;i++){
43       map.put(objects[i], new Long JavaDoc(i));
44     }
45     
46     for(int i=0;i<objects.length;i++){
47       map.remove(objects[i]);
48     }
49     System.out.println("size:"+map.size());
50     assertTrue(map.size()==2);
51   }
52   
53   private void examineWeakHashMap() {
54     WeakHashMap JavaDoc map = new WeakHashMap JavaDoc();
55     TestObj k1 = new TestObj();
56     TestObj k2 = new TestObj();
57     map.put(k1, new Integer JavaDoc(1));
58     assertTrue(map.size() == 1);
59     assertTrue(map.containsKey(k1));
60     assertTrue(map.containsKey(new TestObj()));
61     map.put(k2, new Integer JavaDoc(2));
62     assertFalse(map.size() == 2);
63     assertTrue(map.size() == 1);
64     assertTrue(map.containsKey(k1));
65     assertTrue(map.containsKey(k2));
66     map.put(k1, new Integer JavaDoc(3));
67     assertTrue(map.size()==1);
68     assertTrue(map.containsKey(k1));
69     assertTrue(map.get(k1).equals(new Integer JavaDoc(3)));
70     
71     TestObj[] objects = new TestObj[1000];
72     for(int i=0;i<objects.length;i++){
73       objects[i] = new TestObj();
74       map.put(objects[i], new Long JavaDoc(i));
75     }
76     
77     for(int i=0;i<objects.length;i++){
78       map.put(objects[i], new Long JavaDoc(i));
79     }
80     
81     map.remove(objects[0]);
82     System.out.println("size:"+map.size());
83     assertTrue(map.size()==0);
84   }
85   
86   private class TestObj {
87     public int hashCode(){
88       return 10;
89     }
90     
91     public boolean equals(Object JavaDoc obj){
92       return true;
93     }
94   }
95 }
96
Popular Tags