1 64 65 package com.jcorporate.expresso.core.cache.tests; 66 67 import com.jcorporate.expresso.core.cache.CacheException; 68 import com.jcorporate.expresso.core.cache.CacheManager; 69 import com.jcorporate.expresso.core.cache.CacheSystem; 70 import com.jcorporate.expresso.core.dbobj.ValidValue; 71 import com.jcorporate.expresso.services.test.ExpressoTestCase; 72 import com.jcorporate.expresso.services.test.TestSystemInitializer; 73 import junit.framework.Test; 74 import junit.framework.TestSuite; 75 import org.apache.log4j.Logger; 76 77 86 87 public class TTLTest extends ExpressoTestCase { 88 89 private Logger log = Logger.getLogger(TTLTest.class); 90 91 private String cacheName = "testcache"; 92 93 94 public TTLTest(String name) throws Exception { 95 super(name); 96 } 97 98 public static void main(String [] args) { 99 junit.textui.TestRunner.run(suite()); 101 102 } 103 104 105 110 public static Test suite() { 111 TestSuite suite = new TestSuite(TTLTest.class); 112 return suite; 113 } 114 115 116 120 public void setUp() 121 throws Exception { 122 try { 123 CacheManager.createCache(TestSystemInitializer.getTestContext(), cacheName, false); 124 125 126 CacheManager.createCache(TestSystemInitializer.getTestContext(), cacheName + "O", true); 127 } catch (java.util.ConcurrentModificationException cme) { 128 log.error("Error Setting Up", cme); 129 throw cme; 130 } 131 } 132 133 136 public synchronized void testUnorderedCacheTTL() { 137 try { 138 CacheSystem cs = CacheManager.getCacheSystem(TestSystemInitializer.getTestContext()); 139 140 ValidValue b = new ValidValue("b", "BE!"); 141 142 if (CacheManager.existsCache(TestSystemInitializer.getTestContext(), cacheName)) { 143 if (log.isInfoEnabled()) { 144 log.info("Clearing cache: " + cacheName); 145 } 146 CacheManager.clear(TestSystemInitializer.getTestContext(), cacheName); 147 } 148 149 cs.addItem(cacheName, b, 500); 150 151 Thread.sleep(1000); 152 153 ValidValue retrieved = (ValidValue) CacheManager.getItem(TestSystemInitializer.getTestContext(), 154 cacheName, 155 "b"); 156 157 assertTrue("retrieved should expires and be null", retrieved == null); 158 } catch (CacheException ex) { 159 ex.printStackTrace(); 160 fail("Cache exception manipulating cache"); 161 } catch (Exception e) { 162 e.printStackTrace(); 163 fail("Exception manipulating cache"); 164 } 165 } 166 167 170 public synchronized void testOrderedCacheTTL() { 171 try { 172 CacheSystem cs = CacheManager.getCacheSystem(TestSystemInitializer.getTestContext()); 173 174 ValidValue b = new ValidValue("b", "BE!"); 175 176 if (CacheManager.existsCache(TestSystemInitializer.getTestContext(), cacheName + "O")) { 177 if (log.isInfoEnabled()) { 178 log.info("Clearing cache: " + cacheName + "O"); 179 } 180 CacheManager.clear(TestSystemInitializer.getTestContext(), cacheName + "O"); 181 } 182 183 cs.addItem(cacheName + "O", b, 500); 184 185 Thread.sleep(1000); 186 187 ValidValue retrieved = (ValidValue) cs.getItem(cacheName + "O", 188 "b"); 189 190 assertTrue("retrieved should expire and be null", retrieved == null); 191 192 cs.addItem(cacheName + "0", b, 500); 193 194 Thread.sleep(1000); 195 java.util.List l = cs.getItems(cacheName + "0"); 196 assertTrue("retrieved should expire and be null", l == null); 197 198 } catch (CacheException ex) { 199 ex.printStackTrace(); 200 fail("Cache exception manipulating cache"); 201 } catch (Exception e) { 202 e.printStackTrace(); 203 fail("Exception manipulating cache"); 204 } 205 } 206 207 208 } | Popular Tags |