1 package org.shiftone.cache.util; 2 3 4 5 import junit.framework.TestCase; 6 import org.shiftone.cache.Cache; 7 import org.shiftone.cache.CacheFactory; 8 9 import java.util.Random ; 10 11 12 19 public abstract class TestCaseBase extends TestCase 20 { 21 22 static Random random = new Random (System.currentTimeMillis()); 24 private CacheFactory cacheFactory = getCacheFactory(); 25 26 32 public TestCaseBase(String name) 33 { 34 super(name); 35 } 36 37 38 41 public abstract CacheFactory getCacheFactory(); 42 43 44 47 public Cache newCache(long timeoutMilliSeconds, int maxSize) 48 { 49 return cacheFactory.newInstance("testCaseBase", timeoutMilliSeconds, maxSize); 50 } 51 52 53 56 public void test10000UniquePuts() 57 { 58 59 Cache cache = newCache(1000 * 60, 1000); 60 61 for (int i = 0; i < 10000; i++) 62 { 63 cache.addObject("k-" + i, "v-" + i); 64 } 65 66 assertEquals(1000, cache.size()); 67 cache.clear(); 68 assertEquals(0, cache.size()); 69 } 70 71 72 75 public void test10000Puts() 76 { 77 78 Cache cache = newCache(1000 * 60, 1000); 79 80 for (int i = 0; i < 10000; i++) 81 { 82 cache.addObject("k-" + (i % 500), "v-" + i); 83 } 84 85 assertEquals(500, cache.size()); 86 cache.clear(); 87 assertEquals(0, cache.size()); 88 } 89 90 91 94 public void test1000Puts10000GetsSameKey() 95 { 96 97 Cache cache = newCache(1000 * 60, 500); 98 99 for (int i = 0; i < 1000; i++) 100 { 101 cache.addObject("k-" + i, "v-" + i); 102 } 103 104 assertEquals(500, cache.size()); 105 106 for (int i = 0; i < 10000; i++) 107 { 108 assertNotNull(cache.getObject("k-987")); 109 } 110 } 111 112 113 116 public void test10000Puts99999Gets() 117 { 118 119 Cache cache = newCache(1000 * 60, 1000); 120 String key = null; 121 122 cache.addObject("k-0", "v-0"); 123 124 for (int i = 0; i < 10000; i++) 125 { 126 key = "k-" + (i % 200); 127 128 cache.addObject(key, "v-" + i); 129 assertNotNull(key, cache.getObject(key)); 130 } 131 132 assertEquals(200, cache.size()); 133 } 134 135 136 139 public void test1Put10000GetsSameKey2Puts() 140 { 141 142 Cache cache = newCache(1000 * 60, 2); 143 144 cache.addObject("x", "x"); 145 cache.addObject("y", "y"); 146 147 for (int i = 0; i < 10000; i++) 148 { 149 assertNotNull(cache.getObject("x")); 150 assertNotNull(cache.getObject("y")); 151 } 152 153 assertEquals(2, cache.size()); 154 cache.addObject("a", "a"); 155 cache.addObject("b", "b"); 156 assertEquals(2, cache.size()); 157 assertNotNull(cache.getObject("b")); 158 } 159 160 161 164 public void testCacheOfSize1() 165 { 166 167 Cache cache = newCache(1000 * 60, 1); 168 169 for (int i = 0; i < 20; i++) 170 { 171 cache.addObject("k-" + i, "v-" + i); 172 } 173 174 assertEquals(1, cache.size()); 175 } 176 177 178 194 195 198 public static String randKey() 199 { 200 return String.valueOf(Math.abs(random.nextInt())); 201 } 202 203 204 207 public static String randValue() 208 { 209 210 StringBuffer sb = new StringBuffer (); 211 212 for (int i = 0; i < 100; i++) 213 { 214 sb.append(randKey()); 215 } 216 217 return sb.toString(); 218 } 219 } 220 | Popular Tags |