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 name) 12 { 13 super(name); 14 } 15 16 public static void main(String [] args) 17 { 18 String [] arr = {ReferenceMapTest.class.getName()}; 19 junit.textui.TestRunner.main(arr); 20 } 21 22 23 protected void setUp() 24 throws Exception 25 { 26 super.setUp(); 27 30 referenceMap = new ReferenceMap(ReferenceMap.WEAK, ReferenceMap.HARD, 10, 0.75f, true); 31 } 32 33 protected void tearDown() 34 throws Exception 35 { 36 referenceMap = null; 37 super.tearDown(); 38 } 39 40 public void testWeakIdentityMap() 41 { 42 String key1 = new String ("abc"); 43 String key2 = new String ("abc"); 44 String value1 = new String ("abc"); 45 String value2 = new String ("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 referenceMap.put(key1, value1); 55 assertEquals("size", 1, referenceMap.size()); 56 57 referenceMap.put(key2, value2); 59 assertEquals("size", 2, referenceMap.size()); 60 61 assertTrue("ref1 is there", referenceMap.containsKey(key1)); 63 assertTrue("ref2 is there", referenceMap.containsKey(key2)); 64 65 assertSame("key1=>value1", value1, referenceMap.remove(key1)); 67 assertEquals("size", 1, referenceMap.size()); 68 referenceMap.put(key1, value1); assertEquals("size", 2, referenceMap.size()); 70 71 72 key2 = ""; 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) fail(key1); 83 84 87 } 88 89 public static void gc() 90 { 91 try 92 { 93 byte[][] tooLarge = new byte[1000000000][1000000000]; 95 fail("you have too much RAM"); 96 } 97 catch (OutOfMemoryError ex) 98 { 99 System.gc(); } 101 } 102 103 104 } 105 | Popular Tags |