01: package org.apache.ojb.broker.metadata;
02:
03: import junit.framework.TestCase;
04: import org.apache.ojb.broker.util.ReferenceMap;
05:
06: public class ReferenceMapTest extends TestCase {
07: private ReferenceMap referenceMap = null;
08:
09: public ReferenceMapTest(String name) {
10: super (name);
11: }
12:
13: public static void main(String[] args) {
14: String[] arr = { ReferenceMapTest.class.getName() };
15: junit.textui.TestRunner.main(arr);
16: }
17:
18: protected void setUp() throws Exception {
19: super .setUp();
20: /*
21: todo: verify the constructors
22: */
23: referenceMap = new ReferenceMap(ReferenceMap.WEAK,
24: ReferenceMap.HARD, 10, 0.75f, true);
25: }
26:
27: protected void tearDown() throws Exception {
28: referenceMap = null;
29: super .tearDown();
30: }
31:
32: public void testWeakIdentityMap() {
33: String key1 = new String("abc");
34: String key2 = new String("abc");
35: String value1 = new String("abc");
36: String value2 = new String("abc");
37:
38: assertNotSame("different references", key1, key2);
39: assertEquals("identical strings", key1, key2);
40:
41: referenceMap.put(key1, "nonsence");
42: assertEquals("size", 1, referenceMap.size());
43:
44: //if we put the same value size will remain the same
45: referenceMap.put(key1, value1);
46: assertEquals("size", 1, referenceMap.size());
47:
48: //if we put equal value size will increase
49: referenceMap.put(key2, value2);
50: assertEquals("size", 2, referenceMap.size());
51:
52: // test containsKey
53: assertTrue("ref1 is there", referenceMap.containsKey(key1));
54: assertTrue("ref2 is there", referenceMap.containsKey(key2));
55:
56: // test remove
57: assertSame("key1=>value1", value1, referenceMap.remove(key1));
58: assertEquals("size", 1, referenceMap.size());
59: referenceMap.put(key1, value1); // put it back
60: assertEquals("size", 2, referenceMap.size());
61:
62: key2 = ""; // will weaken ref2 key in referenceMap
63: gc();
64:
65: assertEquals("GC didn't release weak references", 1,
66: referenceMap.size());
67:
68: assertFalse("ref2 is not there", referenceMap.containsKey(key2));
69: assertTrue("ref1 is there", referenceMap.containsKey(key1));
70:
71: if (key1.length() < 0) // lets do something with ref1 to avoid GC
72: fail(key1); // never
73:
74: // check NULL
75: //referenceMap.put(key1, null);
76:
77: }
78:
79: public static void gc() {
80: try {
81: // trigger GC
82: byte[][] tooLarge = new byte[1000000000][1000000000];
83: fail("you have too much RAM");
84: } catch (OutOfMemoryError ex) {
85: System.gc(); // ignore
86: }
87: }
88:
89: }
|