1 5 package com.opensymphony.oscache.base; 6 7 import java.util.Properties ; 8 9 import com.opensymphony.oscache.general.GeneralCacheAdministrator; 10 11 import junit.framework.Assert; 12 import junit.framework.Test; 13 import junit.framework.TestCase; 14 import junit.framework.TestSuite; 15 16 23 public class TestCache extends TestCase { 24 private static Cache map = null; 26 private final String CONTENT = "Content for the cache test"; 27 28 private final String ENTRY_KEY = "Test cache key"; 30 private final int NO_REFRESH_NEEDED = CacheEntry.INDEFINITE_EXPIRY; 31 private final int REFRESH_NEEDED = 0; 32 33 38 public TestCache(String str) { 39 super(str); 40 } 41 42 46 public void setUp() { 47 if (map == null) { 49 GeneralCacheAdministrator admin = new GeneralCacheAdministrator(); 50 map = admin.getCache(); 51 assertNotNull(map); 52 } 53 } 54 55 60 public static Test suite() { 61 return new TestSuite(TestCache.class); 62 } 63 64 67 public void testFlushPattern() { 68 map.putInCache(ENTRY_KEY, CONTENT); 70 map.flushPattern(ENTRY_KEY + "do not flush"); 71 getBackContent(map, CONTENT, NO_REFRESH_NEEDED, false); 72 73 map.flushPattern(ENTRY_KEY.substring(1, 2)); 75 getBackContent(map, CONTENT, NO_REFRESH_NEEDED, true); 76 77 map.flushPattern(""); 79 map.flushPattern(null); 80 } 81 82 86 public void testBug174CacheOverflow() throws Exception { 87 88 Properties p = new Properties (); 89 p.setProperty(AbstractCacheAdministrator.CACHE_ALGORITHM_KEY, "com.opensymphony.oscache.base.algorithm.LRUCache"); 90 p.setProperty(AbstractCacheAdministrator.CACHE_CAPACITY_KEY, "100"); 91 GeneralCacheAdministrator admin = new GeneralCacheAdministrator(p); 92 93 int cacheCapacity = 100; 94 int maxAddedCacheEntries = cacheCapacity*10; 95 String baseCacheKey= "baseKey"; 96 String cacheValue ="same_value"; 97 98 admin.setCacheCapacity(cacheCapacity); 99 100 Cache cache = admin.getCache(); 101 102 for (int keyIndex=0; keyIndex<maxAddedCacheEntries; keyIndex++) { 104 String key = baseCacheKey + keyIndex; 105 admin.putInCache(key, cacheValue); 106 } 107 108 Assert.assertEquals("expected cache to be at its full capacity", cacheCapacity , cache.getSize()); 109 Assert.assertTrue("expected cache overflows to have cleaned UpdateState instances. got [" + cache.getNbUpdateState() + "] updates while max is [" + cacheCapacity + "]", cache.getNbUpdateState() <= cacheCapacity); 110 } 111 112 116 public void testBug174CacheOverflowAndUpdate() throws Exception { 117 Properties p = new Properties (); 118 p.setProperty(AbstractCacheAdministrator.CACHE_ALGORITHM_KEY, "com.opensymphony.oscache.base.algorithm.LRUCache"); 119 p.setProperty(AbstractCacheAdministrator.CACHE_CAPACITY_KEY, "100"); 120 GeneralCacheAdministrator admin = new GeneralCacheAdministrator(p); 121 122 int cacheCapacity = 100; 123 int maxAddedCacheEntries = cacheCapacity*10; 124 String baseCacheKey= "baseKey"; 125 String cacheValue ="same_value"; 126 127 admin.setCacheCapacity(cacheCapacity); 128 129 Cache cache = admin.getCache(); 130 131 132 for (int keyIndex=0; keyIndex<maxAddedCacheEntries; keyIndex++) { 135 String key = baseCacheKey + keyIndex; 136 admin.putInCache(key, cacheValue); 137 138 try { 139 admin.getFromCache(key, 0); 140 fail("expected element [" + key + "] not to be present"); 141 } catch (NeedsRefreshException e) { 142 admin.putInCache(key, cacheValue); 143 } 144 } 145 146 Assert.assertEquals("expected cache to be at its full capacity", cacheCapacity , cache.getSize()); 147 Assert.assertTrue("expected cache overflows to have cleaned UpdateState instances. Nb states is:" + cache.getNbUpdateState() + " expected max="+ cacheCapacity, cache.getNbUpdateState() <= cacheCapacity); 148 } 149 150 151 155 public void testBug174CacheMissesNonBlocking() throws Exception { 156 testBug174CacheMisses(false); 157 } 158 159 163 public void testBug174CacheMissesBlocking() throws Exception { 164 testBug174CacheMisses(true); 165 } 166 167 171 public void testBug174CacheMisses(boolean block) throws Exception { 172 Properties p = new Properties (); 173 p.setProperty(AbstractCacheAdministrator.CACHE_ALGORITHM_KEY, "com.opensymphony.oscache.base.algorithm.LRUCache"); 174 p.setProperty(AbstractCacheAdministrator.CACHE_CAPACITY_KEY, "100"); 175 if (block) { 176 p.setProperty(AbstractCacheAdministrator.CACHE_BLOCKING_KEY, "true"); 177 } 178 GeneralCacheAdministrator admin = new GeneralCacheAdministrator(p); 179 180 int cacheCapacity = 100; 181 int maxAddedCacheEntries = cacheCapacity*10; 182 String baseCacheKey= "baseKey"; 183 String cacheValue ="same_value"; 184 185 admin.setCacheCapacity(cacheCapacity); 186 187 Cache cache = admin.getCache(); 188 189 for (int keyIndex=0; keyIndex<maxAddedCacheEntries; keyIndex++) { 191 String key = baseCacheKey + keyIndex; 192 try { 193 admin.getFromCache(key); 194 fail("expected element [" + key + "] not to be present"); 195 } catch (NeedsRefreshException e) { 196 admin.cancelUpdate(key); 197 } 198 } 199 200 Assert.assertTrue("expected cache accesses to not leak past cache capacity. Nb states is:" + cache.getNbUpdateState() + " expected max="+ cacheCapacity, cache.getNbUpdateState() < cacheCapacity); 201 } 202 203 206 public void testPutGetFromCache() { 207 map.putInCache(ENTRY_KEY, CONTENT); 209 getBackContent(map, CONTENT, NO_REFRESH_NEEDED, false); 210 getBackContent(map, CONTENT, REFRESH_NEEDED, true); 211 212 214 215 try { 216 assertNull(map.getFromCache("", NO_REFRESH_NEEDED)); 217 } catch (NeedsRefreshException nre) { 218 map.cancelUpdate(""); 219 } catch (Exception e) { 220 } 221 222 try { 223 assertNull(map.getFromCache(null, NO_REFRESH_NEEDED)); 224 } catch (NeedsRefreshException nre) { 225 map.cancelUpdate(null); 226 } catch (Exception e) { 227 } 228 } 229 230 233 public void testPutGetFromCacheWithPolicy() { 234 map.putInCache(ENTRY_KEY + "policy", CONTENT, new DummyAlwayRefreshEntryPolicy()); 236 237 try { 239 map.getFromCache(ENTRY_KEY + "policy", -1); 240 fail("Should have got a refresh."); 241 } catch (NeedsRefreshException nre) { 242 map.cancelUpdate(ENTRY_KEY + "policy"); 243 } 244 } 245 246 protected void tearDown() throws Exception { 247 if (map != null) { 248 map.clear(); 249 } 250 } 251 252 260 private void getBackContent(Cache map, Object content, int refresh, boolean exceptionExpected) { 261 try { 262 assertEquals(content, map.getFromCache(ENTRY_KEY, refresh)); 263 264 if (exceptionExpected) { 265 fail("NeedsRefreshException should have been thrown!"); 266 } 267 } catch (NeedsRefreshException nre) { 268 map.cancelUpdate(ENTRY_KEY); 269 270 if (!exceptionExpected) { 271 fail("NeedsRefreshException shouldn't have been thrown!"); 272 } 273 } 274 } 275 } 276 | Popular Tags |