1 5 package com.opensymphony.oscache.general; 6 7 import java.util.Date ; 8 9 import com.opensymphony.oscache.base.*; 10 import com.opensymphony.oscache.base.events.CacheEntryEventListener; 11 import com.opensymphony.oscache.base.events.CacheMapAccessEventListener; 12 import com.opensymphony.oscache.extra.CacheEntryEventListenerImpl; 13 import com.opensymphony.oscache.extra.CacheMapAccessEventListenerImpl; 14 15 import junit.framework.Test; 16 import junit.framework.TestSuite; 17 18 27 public class TestGeneralCacheAdministrator extends TestAbstractCacheAdministrator { 28 private static final String KEY = "Test General Cache Admin Key"; 30 private static final int NO_REFRESH_NEEDED = CacheEntry.INDEFINITE_EXPIRY; 31 private static final int REFRESH_NEEDED = 0; 32 private static final String CONTENT = "Content for the general cache admin test"; 33 private static final String WILL_NOT_FLUSH_PATTERN = "This key won't flush"; 34 private static final String GROUP1 = "group1"; 35 private static final String GROUP2 = "group2"; 36 private static final String GROUP3 = "group3"; 37 38 private static final int NB_CACHE_HITS = 7; 40 private static final int NB_CACHE_STALE_HITS = 7; 41 private static final int NB_CACHE_MISSED = 1; 42 private static final int NB_ADD = 7; 43 private static final int NB_UPDATED = 2; 44 private static final int NB_FLUSH = 3; 45 private static final int NB_REMOVED = 0; 46 private static final int NB_GROUP_FLUSH = 2; 47 private static final int NB_PATTERN_FLUSH = 1; 48 49 static GeneralCacheAdministrator admin = null; 51 52 private CacheEntryEventListenerImpl cacheEntryEventListener = null; 54 private CacheMapAccessEventListenerImpl cacheMapAccessEventListener = null; 55 56 61 public TestGeneralCacheAdministrator(String str) { 62 super(str); 63 } 64 65 70 public static Test suite() { 71 return new TestSuite(TestGeneralCacheAdministrator.class); 72 } 73 74 79 public AbstractCacheAdministrator getAdmin() { 80 return admin; 81 } 82 83 87 public void setUp() { 88 admin = new GeneralCacheAdministrator(); 90 assertNotNull(admin); 91 cacheEntryEventListener = new CacheEntryEventListenerImpl(); 92 cacheMapAccessEventListener = new CacheMapAccessEventListenerImpl(); 93 94 admin.getCache().addCacheEventListener(cacheEntryEventListener, CacheEntryEventListener.class); 96 admin.getCache().addCacheEventListener(cacheMapAccessEventListener, CacheMapAccessEventListener.class); 97 } 98 99 102 public void testCacheEntryEventListenerCounters() { 103 populate(); 104 assertEquals(NB_ADD, cacheEntryEventListener.getEntryAddedCount()); 105 assertEquals(NB_REMOVED, cacheEntryEventListener.getEntryRemovedCount()); 106 assertEquals(NB_UPDATED, cacheEntryEventListener.getEntryUpdatedCount()); 107 assertEquals(NB_GROUP_FLUSH, cacheEntryEventListener.getGroupFlushedCount()); 108 assertEquals(NB_PATTERN_FLUSH, cacheEntryEventListener.getPatternFlushedCount()); 109 assertEquals(NB_FLUSH, cacheEntryEventListener.getEntryFlushedCount()); 110 } 111 112 115 public void testCacheMapAccessEventListenerCounters() { 116 populate(); 117 118 int missCount = cacheMapAccessEventListener.getMissCount(); 119 120 if (NB_CACHE_MISSED != missCount) { 121 fail("We expected " + NB_CACHE_MISSED + " misses but got " + missCount + "." + " This is probably due to existing disk cache, delete it and re-run" + " the test"); 122 } 123 124 assertEquals(NB_CACHE_HITS, cacheMapAccessEventListener.getHitCount()); 125 assertEquals(NB_CACHE_STALE_HITS, cacheMapAccessEventListener.getStaleHitCount()); 126 } 127 128 131 public void testFlushPattern() { 132 admin.putInCache(KEY, CONTENT); 134 135 admin.flushPattern(WILL_NOT_FLUSH_PATTERN); 137 admin.flushPattern(""); 138 admin.flushPattern(null); 139 140 assertNotNull(checkObj(KEY, NO_REFRESH_NEEDED, false)); 142 143 admin.flushPattern(KEY.substring(1, 2)); 145 assertNotNull(checkObj(KEY, NO_REFRESH_NEEDED, true)); 146 } 147 148 151 public void testGroups() { 152 admin.flushGroup(GROUP1); 154 155 admin.putInCache("1", "item 1"); admin.putInCache("2", "item 2", new String [] {GROUP1}); admin.putInCache("3", "item 3", new String [] {GROUP2}); admin.putInCache("4", "item 4", new String [] {GROUP1, GROUP2}); admin.putInCache("5", "item 5", new String [] {GROUP1, GROUP2, GROUP3}); 162 admin.flushGroup(GROUP3); assertNotNull(checkObj("5", NO_REFRESH_NEEDED, true)); 164 assertNotNull(checkObj("4", NO_REFRESH_NEEDED, false)); 165 166 admin.flushGroup(GROUP2); assertNotNull(checkObj("1", NO_REFRESH_NEEDED, false)); 168 assertNotNull(checkObj("2", NO_REFRESH_NEEDED, false)); 169 assertNotNull(checkObj("3", NO_REFRESH_NEEDED, true)); 170 assertNotNull(checkObj("4", NO_REFRESH_NEEDED, true)); 171 172 admin.flushGroup(GROUP1); assertNotNull(checkObj("1", NO_REFRESH_NEEDED, false)); 174 assertNotNull(checkObj("2", NO_REFRESH_NEEDED, true)); 175 176 admin.putInCache("A", "ABC", new String [] {"A"}); 178 admin.putInCache("A", "ABC", new String [] {"A", "B"}); 179 admin.putInCache("B", "DEF", new String [] {"B"}); 180 admin.flushGroup("B"); 181 assertNotNull(checkObj("A", NO_REFRESH_NEEDED, true)); 182 } 183 184 188 public void testPutInCacheAndGetFromCache() { 189 admin.putInCache(KEY, CONTENT); 192 193 String cacheContent = (String ) checkObj(KEY, NO_REFRESH_NEEDED, false); 194 assertTrue(CONTENT.equals(cacheContent)); 195 196 cacheContent = (String ) checkObj(KEY, REFRESH_NEEDED, true); 198 assertTrue(CONTENT.equals(cacheContent)); 199 200 invalidPutInCacheArgument(null, null); 202 admin.putInCache(KEY, null); 204 invalidGetFromCacheArgument(null, 0); 206 207 assertNull(checkObj(KEY, NO_REFRESH_NEEDED, false)); 209 210 Object obj = checkObj("Not in cache", NO_REFRESH_NEEDED, true); 212 assertNull(obj); 213 } 214 215 219 public void testPutInCacheAndGetFromCacheWithPolicy() { 220 String key = "policy"; 221 222 admin.putInCache(key, CONTENT, new DummyAlwayRefreshEntryPolicy()); 224 225 try { 227 admin.getFromCache(key, -1); 228 fail("Should have got a refresh."); 229 } catch (NeedsRefreshException nre) { 230 admin.cancelUpdate(key); 231 } 232 } 233 234 protected void tearDown() throws Exception { 235 if (admin != null) { 236 admin.getCache().removeCacheEventListener(cacheEntryEventListener, CacheEntryEventListener.class); 237 admin.getCache().removeCacheEventListener(cacheMapAccessEventListener, CacheMapAccessEventListener.class); 238 } 239 } 240 241 242 245 public void testFlushDateTomorrow() { 246 GeneralCacheAdministrator cacheAdmin = new GeneralCacheAdministrator(null); 247 248 cacheAdmin.putInCache("key1", "key1value"); 249 250 try { 251 assertNotNull(cacheAdmin.getFromCache("key1")); 252 } catch (NeedsRefreshException e1) { 253 fail("Previous cache key1 doesn't exsits in GCA for the test!"); 254 } 255 256 cacheAdmin.flushAll(new Date (System.currentTimeMillis() + 5000)); try { 258 cacheAdmin.getFromCache("key1"); 259 } catch (NeedsRefreshException e) { 260 cacheAdmin.cancelUpdate("key1"); 261 fail("NRE is thrown, but key will expire in 5s."); } 263 } 264 265 266 274 private Object checkObj(String key, int refresh, boolean exceptionExpected) { 275 Object content = null; 277 278 try { 279 content = admin.getFromCache(key, refresh); 281 282 if (exceptionExpected) { 283 fail("Expected NeedsRefreshException!"); 284 } 285 } catch (NeedsRefreshException nre) { 286 admin.cancelUpdate(key); 287 288 if (!exceptionExpected) { 289 fail("Did not expected NeedsRefreshException!"); 290 } 291 292 content = nre.getCacheContent(); 294 } 295 296 return content; 297 } 298 299 305 private void invalidGetFromCacheArgument(String key, int refresh) { 306 try { 307 admin.getFromCache(key, refresh); 309 fail("getFromCache did NOT throw an IllegalArgumentException"); 310 } catch (IllegalArgumentException ipe) { 311 } catch (NeedsRefreshException nre) { 313 admin.cancelUpdate(key); 314 315 } 317 } 318 319 325 private void invalidPutInCacheArgument(String key, Object content) { 326 try { 327 admin.putInCache(key, content); 329 fail("putInCache did NOT throw an IllegalArgumentException"); 330 } catch (IllegalArgumentException ipe) { 331 } 333 } 334 335 private void populate() { 336 for (int i = 0; i < 7; i++) { 337 String [] groups = ((i & 1) == 0) ? new String [] {GROUP1, GROUP2} : new String [] { 338 GROUP3 339 }; 340 admin.putInCache(KEY + i, CONTENT + i, groups); 341 } 342 343 checkObj("Not in cache", NO_REFRESH_NEEDED, true); 345 346 for (int i = 0; i < 7; i++) { 348 try { 349 admin.getFromCache(KEY + i, NO_REFRESH_NEEDED); 350 } catch (NeedsRefreshException e) { 351 admin.cancelUpdate(KEY + i); 352 } 353 } 354 355 for (int i = 0; i < 7; i++) { 356 try { 357 admin.getFromCache(KEY + i, 0); 358 } catch (NeedsRefreshException e) { 359 admin.cancelUpdate(KEY + i); 360 } 361 } 362 363 admin.putInCache(KEY + 1, CONTENT); 364 admin.putInCache(KEY + 2, CONTENT); 365 admin.flushPattern("blahblah"); 366 admin.flushGroup(GROUP1); 367 admin.flushGroup(GROUP2); 368 } 369 } 370 | Popular Tags |