KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > metadata > ReferenceMapTest


1 package org.apache.ojb.broker.metadata;
2
3 import junit.framework.TestCase;
4 import org.apache.ojb.broker.util.ReferenceMap;
5
6
7 public class ReferenceMapTest extends TestCase
8 {
9     private ReferenceMap referenceMap = null;
10
11     public ReferenceMapTest(String JavaDoc name)
12     {
13         super(name);
14     }
15
16     public static void main(String JavaDoc[] args)
17     {
18         String JavaDoc[] arr = {ReferenceMapTest.class.getName()};
19         junit.textui.TestRunner.main(arr);
20     }
21
22
23     protected void setUp()
24             throws Exception JavaDoc
25     {
26         super.setUp();
27         /*
28         todo: verify the constructors
29         */

30         referenceMap = new ReferenceMap(ReferenceMap.WEAK, ReferenceMap.HARD, 10, 0.75f, true);
31     }
32
33     protected void tearDown()
34             throws Exception JavaDoc
35     {
36         referenceMap = null;
37         super.tearDown();
38     }
39
40     public void testWeakIdentityMap()
41     {
42         String JavaDoc key1 = new String JavaDoc("abc");
43         String JavaDoc key2 = new String JavaDoc("abc");
44         String JavaDoc value1 = new String JavaDoc("abc");
45         String JavaDoc value2 = new String JavaDoc("abc");
46
47         assertNotSame("different references", key1, key2);
48         assertEquals("identical strings", key1, key2);
49
50         referenceMap.put(key1, "nonsence");
51         assertEquals("size", 1, referenceMap.size());
52
53         //if we put the same value size will remain the same
54
referenceMap.put(key1, value1);
55         assertEquals("size", 1, referenceMap.size());
56
57         //if we put equal value size will increase
58
referenceMap.put(key2, value2);
59         assertEquals("size", 2, referenceMap.size());
60
61         // test containsKey
62
assertTrue("ref1 is there", referenceMap.containsKey(key1));
63         assertTrue("ref2 is there", referenceMap.containsKey(key2));
64
65         // test remove
66
assertSame("key1=>value1", value1, referenceMap.remove(key1));
67         assertEquals("size", 1, referenceMap.size());
68         referenceMap.put(key1, value1); // put it back
69
assertEquals("size", 2, referenceMap.size());
70
71
72         key2 = ""; // will weaken ref2 key in referenceMap
73
gc();
74
75         assertEquals("GC didn't release weak references", 1, referenceMap.size());
76
77         assertFalse("ref2 is not there", referenceMap.containsKey(key2));
78         assertTrue("ref1 is there", referenceMap.containsKey(key1));
79
80         if (key1.length() < 0) // lets do something with ref1 to avoid GC
81
fail(key1); // never
82

83
84         // check NULL
85
//referenceMap.put(key1, null);
86

87     }
88
89     public static void gc()
90     {
91         try
92         {
93             // trigger GC
94
byte[][] tooLarge = new byte[1000000000][1000000000];
95             fail("you have too much RAM");
96         }
97         catch (OutOfMemoryError JavaDoc ex)
98         {
99             System.gc(); // ignore
100
}
101     }
102
103
104 }
105
Popular Tags