1 10 11 package com.triactive.jdo.util.test; 12 13 import com.triactive.jdo.util.ReferenceValueMap; 14 import java.util.Arrays ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 import java.util.Set ; 18 import junit.framework.TestCase; 19 import org.apache.log4j.Category; 20 21 22 29 30 public abstract class ReferenceValueMapTestCase extends TestCase 31 { 32 private static final Category LOG = Category.getInstance(ReferenceValueMapTestCase.class); 33 34 private static final int NUM_TEST_ENTRIES = 50; 35 36 37 43 44 public ReferenceValueMapTestCase(String name) 45 { 46 super(name); 47 } 48 49 50 protected abstract ReferenceValueMap newReferenceValueMap(); 51 52 53 public void testMemoryReclamation() 54 { 55 ReferenceValueMap map = newReferenceValueMap(); 56 Runtime rt = Runtime.getRuntime(); 57 58 rt.gc(); 59 60 int added = 0; 61 int size; 62 long freeMem; 63 64 68 do 69 { 70 freeMem = rt.freeMemory(); 71 String key = "" + added; 72 Object value = new Integer (added++); 73 74 map.put(key, value); 75 76 size = map.size(); 77 } while (size == added); 78 79 assertTrue(size < added); 80 81 LOG.info((added - size) + " entries out of " + added + " cleared when free memory was " + (int)(freeMem / 1024) + "KB"); 82 } 83 84 85 public void testBasicFunction() 86 { 87 ReferenceValueMap map = newReferenceValueMap(); 88 String [] keyArray = new String [NUM_TEST_ENTRIES]; 89 Integer [] valueArray = new Integer [NUM_TEST_ENTRIES]; 90 91 for (int i = 0; i < keyArray.length; i++) 92 { 93 keyArray[i] = "" + i; 94 valueArray[i] = new Integer (i); 95 96 map.put(keyArray[i], valueArray[i]); 97 } 98 99 checkMapContents(map, keyArray, valueArray); 100 101 Map map2 = (Map)map.clone(); 102 map.clear(); 103 104 assertEquals(0, map.size()); 105 106 map.putAll(map2); 107 108 checkMapContents(map, keyArray, valueArray); 109 110 assertEquals(NUM_TEST_ENTRIES, map.size()); 111 } 112 113 114 118 119 protected void checkMapContents(Map map, String [] keyArray, Integer [] valueArray) 120 { 121 assertEquals(keyArray.length, map.size()); 122 123 for (int i = 0; i < keyArray.length; i++) 124 assertTrue(map.containsKey(keyArray[i])); 125 126 assertTrue(!map.containsKey("bitbucket")); 127 128 for (int i = 0; i < valueArray.length; i++) 129 assertTrue(map.containsValue(valueArray[i])); 130 131 assertTrue(!map.containsValue("bitbucket")); 132 133 for (int i = 0; i < keyArray.length; i++) 134 assertEquals(i, ((Integer )map.get(keyArray[i])).intValue()); 135 136 Set set = map.entrySet(); 137 Iterator it = set.iterator(); 138 139 while (it.hasNext()) 140 { 141 Map.Entry entry = (Map.Entry)it.next(); 142 String s = (String )entry.getKey(); 143 Integer i = (Integer )entry.getValue(); 144 assertTrue(map.containsKey(s)); 145 assertTrue(map.containsValue(i)); 146 } 147 148 assertTrue(map.keySet().containsAll(Arrays.asList(keyArray))); 149 assertTrue(map.values().containsAll(Arrays.asList(valueArray))); 150 } 151 } 152 | Popular Tags |