1 25 26 package org.objectweb.perseus.cache; 27 28 import junit.framework.TestCase; 29 import org.objectweb.perseus.cache.api.CacheEntry; 30 import org.objectweb.perseus.cache.api.CacheException; 31 import org.objectweb.perseus.cache.api.CacheEventListener; 32 import org.objectweb.perseus.cache.api.CacheEvent; 33 import org.objectweb.perseus.cache.lib.BasicCacheManager; 34 import org.objectweb.perseus.cache.lib.BasicCacheEntryFactory; 35 import org.objectweb.perseus.cache.replacement.lib.FIFOReplacementManager; 36 import org.objectweb.perseus.cache.replacement.lib.AbstractReplacementManager; 37 import org.objectweb.perseus.cache.replacement.lib.MRUReplacementManager; 38 import org.objectweb.perseus.cache.replacement.lib.LRUReplacementManager; 39 import org.objectweb.util.monolog.Monolog; 40 import org.objectweb.util.monolog.api.Logger; 41 import org.objectweb.util.monolog.api.LoggerFactory; 42 import org.objectweb.util.monolog.api.BasicLevel; 43 44 public class TestCache extends TestCase { 45 46 static final int FIFO = 0; 47 static final int LRU = 1; 48 static final int MRU = 2; 49 50 private BasicCacheManager cm; 51 52 Object id1, id2, id3, id4; 53 Object value1, value2, value3, value4; 54 static Logger logger = null; 55 static LoggerFactory loggerFactory = null; 56 57 public TestCache(String s) { 58 super(s); 59 if (logger == null) { 60 loggerFactory = Monolog.getMonologFactory(); 61 logger = loggerFactory 62 .getLogger("org.objectweb.perseus.cache.test"); 63 } 64 } 65 66 protected void setUp() throws CacheException { 67 cm = new BasicCacheManager(); 68 BasicCacheEntryFactory cef = new BasicCacheEntryFactory(); 69 cm.bindFc("logger", logger); 70 cm.bindFc("monolog-factory", loggerFactory); 71 cm.bindFc(BasicCacheManager.CACHE_ENTRY_FACTORY_BINDING, cef); 72 cm.setMaxObjects(3); 73 74 id1 = new Integer (1); 75 id2 = new Integer (2); 76 id3 = new Integer (3); 77 id4 = new Integer (4); 78 value1 = new Object (); 79 value2 = new Object (); 80 value3 = new Object (); 81 value4 = new Object (); 82 } 83 protected void tearDown() throws Exception { 84 super.tearDown(); 85 cm = null; 86 id1 = null; 87 id2 = null; 88 id3 = null; 89 id4 = null; 90 value1 = null; 91 value2 = null; 92 value3 = null; 93 value4 = null; 94 } 95 96 97 private void setUp(int type, int cacheSize, int autoCleanSize, int threshold) { 98 AbstractReplacementManager rm; 99 switch (type) { 100 case FIFO: 101 rm = new FIFOReplacementManager(); 102 break; 103 case LRU: 104 rm = new LRUReplacementManager(); 105 break; 106 default: 107 rm = new MRUReplacementManager(); 108 break; 109 } 110 cm.bindFc(BasicCacheManager.REPLACEMENT_MANAGER_BINDING, rm); 111 cm.bindFc("logger", logger); 112 cm.bindFc("monolog-factory", loggerFactory); 113 rm.bindFc(AbstractReplacementManager.UNBIND_MANAGER_BINDING, cm); 114 rm.bindFc("logger", logger); 115 rm.bindFc("monolog-factory", loggerFactory); 116 try { 117 cm.setMaxObjects(cacheSize); 118 cm.setAutoCleanSize("" + autoCleanSize); 119 cm.setAutoCleanThreshold("" + threshold); 120 } catch (Exception e) { 121 fail(e.getMessage()); 122 } 123 } 124 125 public void testCache1() throws CacheException { 126 setUp(FIFO, 3, 1, 0); 127 CacheEntry ce1 = cm.bind(id1, value1); 128 CacheEntry ce2 = cm.bind(id2, value2); 129 130 CacheEntry ce = cm.lookup(id1); 131 assertNotNull(ce); 132 assertTrue(ce.getCeIdentifier() == id1); 133 assertTrue(ce.getCeObject() == value1); 134 135 ce = cm.lookup(id3); 136 assertNull(ce); 137 138 try { 139 cm.bind(id1, value1); 140 fail("illegal rebind undetected"); 141 } catch (CacheException e) { 142 } 143 144 try { 145 cm.bind(id1, value2); 146 fail("illegal rebind undetected"); 147 } catch (CacheException e) { 148 } 149 150 cm.fix(ce1); 151 cm.unfix(ce1); 152 153 try { 154 cm.unfix(ce2); 155 fail("unfix protocol error undetected"); 156 } catch (CacheException e) { 157 } 158 159 cm.touch(ce2); 160 } 161 162 public void testFIFOCache() throws CacheException { 163 setUp(FIFO, 3, 1, 0); 164 CacheEntry ce1 = cm.bind(id1, value1); 166 logger.log(BasicLevel.DEBUG,"ce1=" + ce1); 167 CacheEntry ce2 = cm.bind(id2, value2); 168 CacheEntry ce3 = cm.bind(id3, value3); 169 logger.log(BasicLevel.DEBUG,"ce3=" + ce3); 170 171 ce1 = null; 173 CacheEventListener cel = new TestCacheEventListener(id1); 175 cm.bindFc(BasicCacheManager.CACHE_LISTENER_BINDING, cel); 176 CacheEntry ce4 = cm.bind(id4, value4); 177 cm.unbindFc(BasicCacheManager.CACHE_LISTENER_BINDING); 178 179 cm.fix(ce2); 181 cm.touch(ce4); 183 184 ce3 = null; 186 cel = new TestCacheEventListener(id3); 188 cm.bindFc(BasicCacheManager.CACHE_LISTENER_BINDING, cel); 189 cm.bind(id1, value1); 190 cm.unbindFc(BasicCacheManager.CACHE_LISTENER_BINDING); 191 } 192 193 public void testLRUCache() throws CacheException { 194 setUp(LRU, 3, 1, 0); 195 CacheEntry ce1 = cm.bind(id1, value1); 196 logger.log(BasicLevel.DEBUG,"ce1=" + ce1); 197 CacheEntry ce2 = cm.bind(id2, value2); 198 CacheEntry ce3 = cm.bind(id3, value3); 199 200 ce1 = null; 202 CacheEventListener cel = new TestCacheEventListener(id1); 204 cm.bindFc(BasicCacheManager.CACHE_LISTENER_BINDING, cel); 205 CacheEntry ce4 = cm.bind(id4, value4); 206 cm.unbindFc(BasicCacheManager.CACHE_LISTENER_BINDING); 207 208 cm.touch(ce4); 210 cm.touch(ce3); 211 cm.touch(ce2); 212 cm.fix(ce4); 214 215 ce3 = null; 217 cel = new TestCacheEventListener(id3); 219 cm.bindFc(BasicCacheManager.CACHE_LISTENER_BINDING, cel); 220 cm.bind(id1, value1); 221 cm.unbindFc(BasicCacheManager.CACHE_LISTENER_BINDING); 222 } 223 224 public void testMRUCache() throws CacheException { 225 setUp(MRU, 3, 1, 0); 226 CacheEntry ce1 = cm.bind(id1, value1); 227 CacheEntry ce2 = cm.bind(id2, value2); 228 CacheEntry ce3 = cm.bind(id3, value3); 229 logger.log(BasicLevel.DEBUG,"ce3=" + ce3); 230 231 ce3 = null; 233 CacheEventListener cel = new TestCacheEventListener(id3); 235 cm.bindFc(BasicCacheManager.CACHE_LISTENER_BINDING, cel); 236 CacheEntry ce4 = cm.bind(id4, value4); 237 cm.unbindFc(BasicCacheManager.CACHE_LISTENER_BINDING); 238 239 cm.touch(ce4); 241 cm.touch(ce2); 242 cm.touch(ce1); 243 cm.fix(ce1); 245 246 ce2 = null; 248 cel = new TestCacheEventListener(id2); 250 cm.bindFc(BasicCacheManager.CACHE_LISTENER_BINDING, cel); 251 cm.bind(id3, value3); 252 cm.unbindFc(BasicCacheManager.CACHE_LISTENER_BINDING); 253 } 254 255 public void testCacheFull() throws CacheException { 256 setUp(FIFO, 3, 1, 0); 257 cm.fix(cm.bind(id1, value1)); 258 cm.fix(cm.bind(id2, value2)); 259 cm.fix(cm.bind(id3, value3)); 260 try { 261 cm.bind(id4, value4); 262 fail("Cache full error undetected"); 263 } catch (CacheException e) { 264 } 265 } 266 267 static class TestCacheEventListener implements CacheEventListener { 268 269 private Object expected; 270 271 public TestCacheEventListener(Object expected) { 272 this.expected = expected; 273 } 274 275 public void entryBound(CacheEvent event) { 276 } 277 278 public void entryUnbound(CacheEvent event) { 279 if (event.getCeIdentifier() != expected) { 280 throw new RuntimeException ( 281 "Unexpected entry unbound " + event.getCeIdentifier()); 282 } 283 } 284 } 285 286 public void testCacheThreshold() throws CacheException { 287 setUp(FIFO, 3, 1, 1); 288 cm.bind(id1, value1); 289 cm.bind(id2, value2); 290 value1 = null; 291 Thread.yield(); 292 try { 293 Thread.sleep(500); 294 } catch (InterruptedException e) { 295 } 296 Object o = cm.lookup(id1); 297 assertNull("element not unbound: " + o, o); 298 cm.getBgcleaner().stop(); 299 try { 300 cm.getBgcleaner().getThread().join(1000); 301 } catch (InterruptedException e) { 302 } 303 assertTrue("The Background cleaner is aliver", 304 !cm.getBgcleaner().getThread().isAlive()); 305 } 306 public void testCacheThreshold2() throws CacheException { 307 setUp(FIFO, 3, 1, 2); 308 cm.bind(id1, value1); 309 cm.bind(id2, value2); 310 Thread.yield(); 311 Object o = cm.lookup(id1); 312 assertNotNull("element not unbound: " + o, o); 313 cm.getBgcleaner().stop(); 314 try { 315 cm.getBgcleaner().getThread().join(1000); 316 } catch (InterruptedException e) { 317 } 318 assertTrue("The Background cleaner is aliver", 319 !cm.getBgcleaner().getThread().isAlive()); 320 } 321 } 322 | Popular Tags |