1 package org.jmage.cache; 2 3 import junit.framework.TestCase; 4 import org.apache.log4j.Logger; 5 6 import java.lang.reflect.Array; 7 import java.util.Properties; 8 9 /*** 10 * DiskCacheTests 11 */ 12 public class DiskCacheTests extends TestCase { 13 14 protected CacheManager cacheManager = CacheManagerImpl.getCacheManager(); 15 protected static Logger log = Logger.getLogger(DiskCacheTests.class.getName()); 16 17 public class ThreadedCacheClient implements Runnable { 18 protected Logger log = Logger.getLogger(DiskCacheTests.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 testDiskCache() throws CacheException { 44 Properties cacheProperties = new Properties(); 45 cacheProperties.setProperty("cachesize", "5"); 46 cacheProperties.setProperty("cachetype", CacheManagerImpl.DISK); 47 48 Cache diskCache = cacheManager.createCacheFor(Long.class, cacheProperties); 49 diskCache.pageIn("one", "1"); 50 diskCache.pageIn("two", "2"); 51 diskCache.pageIn("three", "3"); 52 diskCache.pageIn("four", "4"); 53 diskCache.pageIn("five", "5"); 54 diskCache.pageIn("six", "6"); 55 56 assertEquals("2", diskCache.pageOut("two")); 57 assertEquals("3", diskCache.pageOut("three")); 58 assertEquals("4", diskCache.pageOut("four")); 59 assertEquals("5", diskCache.pageOut("five")); 60 assertEquals("6", diskCache.pageOut("six")); 61 62 try { 63 String nil = (String) diskCache.pageOut("one"); 64 fail("fetching nonexisting value from cache should have barfed"); 65 } catch (CacheException e) { 66 67 } 68 69 cacheManager.destroyCacheFor(Long.class); 70 } 71 72 public void testNoParamCacheFails() throws CacheException { 73 try { 74 Properties cacheProperties = new Properties(); 75 cacheManager.createCacheFor(Object.class, cacheProperties); 76 fail("cache with no properties should have barfed"); 77 } catch (AssertionError a) { 78 assertTrue(a.getMessage().indexOf("unable to create cache for type:") > -1); 79 } 80 } 81 82 public void testDiskCacheWithCacheDir() throws CacheException { 83 Properties cacheProperties = new Properties(); 84 cacheProperties.setProperty("cachesize", "5"); 85 cacheProperties.setProperty("cachetype", CacheManagerImpl.DISK); 86 cacheProperties.setProperty("cachedir", System.getProperty("java.io.tmpdir")); 87 88 assertNotNull(cacheManager.createCacheFor(Integer.class, cacheProperties)); 89 } 90 91 public void testDiskCacheFailsWithFalseCacheDir() throws CacheException { 92 try { 93 Properties cacheProperties = new Properties(); 94 cacheProperties.setProperty("cachesize", "5"); 95 cacheProperties.setProperty("cachetype", CacheManagerImpl.DISK); 96 cacheProperties.setProperty("cachedir", " %^( alkdjfkadjfaskdjfkasdjfkllajdsflkjakslsfjklasdjkfajsk" + 97 "adlksjfakldsjfaklsjfklaljajfkalsdjfajdsflkajsdlkfjasklfjaksjfkalsjdfklasjkfa" + 98 "asdlkfjasdkljfaklsjfklasjfklasjfklasjfklasjflkasjfklasjfklasjfjasklfjaskljf" + 99 "adslkfjadkslfjkkdsjfkadsljfdklasjfkadjsklfjaslkjdfasdkfjakdsjfdlkasjfklkjka" + 100 "adsklfjasdklfjaksldjfkasjfkasjfklasjfklasjfkalsjfkldsjfklajskldjfasdkjf" + 101 "asdlkfjakldjfakljfklajfkldsjfkalsdjfdkasjfkdasjfkalsjdkfjasdkjfaksjfkaldsjfasdj" + 102 "asldkfjalskdfjaklsdjfklasdjflkasdjflkasdjflkasjflkadsjfkalsjfskdljflkasjdjfklasddk" + 103 "asldkfjalksdfjaklsdjlkjfalsjflkasjdlkfjalksjdfkalsjdfkajsdfkljaskdfasjdkfas" + 104 "asdlksfjalksdjfalskdfasjfalskdjflkasjdflasdfkjjasjfaljsdlkjflaksdjkljfklasjfdkaldfj" + 105 "alksdjfakldjfklajflkdjflkajsdklfjaklsjfkalsjfkasjdfkasdjfaklsdjfaklsjfkaflsdkfj" + 106 "akldsfjalksdfjaklsdjfalskjfdkljfalksjflkdjfalksjdfkjasdklfjaksdfjasdkfljaklsjdfkdj" + 107 "alkdsjfalksjdflksjflkasjfklasdjfajsdlkjfkasjdaklfjasklfjalsdkfjaslkdfjasdfjalsfjd" + 108 "alkdsjflaskjflaksjflksdjflaksjfalksdjflaksdjfalskjdfkljaslkdfkasfjaslkjfaklsdjjfk" + 109 "aklsdfjalsdkjfklasdjfklasdfjlkajfklsdjfalksjfalksjfdddalksjflkasjdlkfj#*&*&(*#$()*Q&)"); 110 111 cacheManager.createCacheFor(Double.class, cacheProperties); 112 fail("illegal directory should have barfed"); 113 } catch (AssertionError a) { 114 assertTrue(a.getMessage().indexOf("not a valid cache dir") > -1); 115 } 116 } 117 118 public void testMultiThreadedClients() throws CacheException, InterruptedException { 119 Properties cacheProperties = new Properties(); 120 cacheProperties.setProperty("cachesize", "5"); 121 cacheProperties.setProperty("cachetype", CacheManagerImpl.MEMORY); 122 123 Cache memoryCache = cacheManager.createCacheFor(Array.class, cacheProperties); 124 memoryCache.pageIn("one", "1"); 125 memoryCache.pageIn("two", "2"); 126 memoryCache.pageIn("three", "3"); 127 128 ThreadedCacheClient client1 = new ThreadedCacheClient(Array.class, "two", "2"); 129 Thread t1 = new Thread(client1); 130 t1.setName("thread1"); 131 t1.join(); 132 t1.run(); 133 ThreadedCacheClient client2 = new ThreadedCacheClient(Array.class, "two", "2"); 134 Thread t2 = new Thread(client2); 135 t2.setName("thread2"); 136 t2.join(); 137 t2.run(); 138 ThreadedCacheClient client3 = new ThreadedCacheClient(Array.class, "two", "2"); 139 Thread t3 = new Thread(client3); 140 t3.setName("thread3"); 141 t3.join(); 142 t3.run(); 143 ThreadedCacheClient client4 = new ThreadedCacheClient(Array.class, "two", "2"); 144 Thread t4 = new Thread(client4); 145 t4.setName("thread4"); 146 t4.join(); 147 t4.run(); 148 ThreadedCacheClient client5 = new ThreadedCacheClient(Array.class, "two", "2"); 149 Thread t5 = new Thread(client5); 150 t5.setName("thread5"); 151 t5.join(); 152 t5.run(); 153 } 154 }