1 package org.jmage.cache; 2 3 import junit.framework.TestCase; 4 import org.apache.log4j.Logger; 5 6 import java.util.List; 7 import java.util.Properties; 8 import java.util.Random; 9 10 /*** 11 * MemoryCacheTests 12 */ 13 public class MemoryCacheTests extends TestCase { 14 15 protected CacheManager cacheManager = CacheManagerImpl.getCacheManager(); 16 17 public class ThreadedCacheClient implements Runnable { 18 protected Logger log = Logger.getLogger(ThreadedCacheClient.class.getName()); 19 protected Class cacheType; 20 protected Object key; 21 protected Object value; 22 23 public ThreadedCacheClient(Class cacheType, Object key, Object value) { 24 this.cacheType = cacheType; 25 this.key = key; 26 this.value = value; 27 } 28 29 public void run() { 30 for (int i = 0; i < 5; i++) { 31 try { 32 Cache cache = cacheManager.getCacheFor(cacheType); 33 Object value = cache.pageOut(key); 34 assertEquals(value, this.value); 35 Thread.sleep((long) Math.random() * 1000l); 36 } catch (Exception e) { 37 fail("unable to retrieve String cache"); 38 } 39 } 40 } 41 } 42 43 public void testMemoryCache() throws CacheException { 44 Properties cacheProperties = new Properties(); 45 cacheProperties.setProperty("cachesize", "5"); 46 cacheProperties.setProperty("cachetype", CacheManagerImpl.MEMORY); 47 48 Cache memoryCache = cacheManager.createCacheFor(String.class, cacheProperties); 49 memoryCache.pageIn("one", "1"); 50 memoryCache.pageIn("two", "2"); 51 memoryCache.pageIn("three", "3"); 52 memoryCache.pageIn("four", "4"); 53 memoryCache.pageIn("five", "5"); 54 memoryCache.pageIn("six", "6"); 55 56 assertEquals("2", memoryCache.pageOut("two")); 57 assertEquals("3", memoryCache.pageOut("three")); 58 assertEquals("4", memoryCache.pageOut("four")); 59 assertEquals("5", memoryCache.pageOut("five")); 60 assertEquals("6", memoryCache.pageOut("six")); 61 62 try { 63 String nil = (String) memoryCache.pageOut("one"); 64 fail("fetching nonexisting value from cache should have barfed"); 65 } catch (CacheException e) { 66 67 } 68 69 cacheManager.destroyCacheFor(String.class); 70 } 71 72 public void testNoParamCacheFails() throws CacheException { 73 try { 74 Properties cacheProperties = new Properties(); 75 cacheManager.createCacheFor(StringBuffer.class, cacheProperties); 76 fail("cache with no properties should have barfed"); 77 } catch (AssertionError a) { 78 } 79 } 80 81 public void testMemoryCacheMultipleClients() throws CacheException { 82 Properties cacheProperties = new Properties(); 83 cacheProperties.setProperty("cachesize", "5"); 84 cacheProperties.setProperty("cachetype", CacheManagerImpl.MEMORY); 85 86 Cache memoryCache = cacheManager.createCacheFor(Math.class, cacheProperties); 87 memoryCache.pageIn("one", "1"); 88 89 Cache mathCache = cacheManager.getCacheFor(Math.class); 90 assertEquals("1", mathCache.pageOut("one")); 91 } 92 93 public void testMemoryCacheFailsWrongReference() throws CacheException { 94 Properties cacheProperties = new Properties(); 95 cacheProperties.setProperty("cachesize", "5"); 96 cacheProperties.setProperty("cachetype", CacheManagerImpl.MEMORY); 97 98 Cache memoryCache = cacheManager.createCacheFor(Random.class, cacheProperties); 99 memoryCache.pageIn("one", "1"); 100 101 try { 102 cacheManager.getCacheFor(List.class); 103 fail("wrong reference for memory cache should have barfed"); 104 } catch (Exception e) { 105 assertTrue(e.getMessage().indexOf("unable to find a cache for this factory type") > -1); 106 } 107 } 108 109 public void testMultiThreadedClients() throws CacheException, InterruptedException { 110 Properties cacheProperties = new Properties(); 111 cacheProperties.setProperty("cachesize", "5"); 112 cacheProperties.setProperty("cachetype", CacheManagerImpl.MEMORY); 113 114 Cache memoryCache = cacheManager.createCacheFor(Thread.class, cacheProperties); 115 memoryCache.pageIn("one", "1"); 116 memoryCache.pageIn("two", "2"); 117 memoryCache.pageIn("three", "3"); 118 119 ThreadedCacheClient client1 = new ThreadedCacheClient(Thread.class, "two", "2"); 120 Thread t1 = new Thread(client1); 121 t1.setName("thread1"); 122 t1.join(); 123 t1.run(); 124 ThreadedCacheClient client2 = new ThreadedCacheClient(Thread.class, "two", "2"); 125 Thread t2 = new Thread(client2); 126 t2.setName("thread2"); 127 t2.join(); 128 t2.run(); 129 ThreadedCacheClient client3 = new ThreadedCacheClient(Thread.class, "two", "2"); 130 Thread t3 = new Thread(client3); 131 t3.setName("thread3"); 132 t3.join(); 133 t3.run(); 134 ThreadedCacheClient client4 = new ThreadedCacheClient(Thread.class, "two", "2"); 135 Thread t4 = new Thread(client4); 136 t4.setName("thread4"); 137 t4.join(); 138 t4.run(); 139 ThreadedCacheClient client5 = new ThreadedCacheClient(Thread.class, "two", "2"); 140 Thread t5 = new Thread(client5); 141 t5.setName("thread5"); 142 t5.join(); 143 t5.run(); 144 } 145 }