1 16 package net.sf.dozer.util.mapping.cache; 17 18 import net.sf.dozer.util.mapping.DozerTestBase; 19 20 23 public class CacheTest extends DozerTestBase { 24 25 public void testPutGetFromCache() throws Exception { 26 Cache cache = new Cache(getRandomString(), 50); 27 int numCacheEntriesToAdd = 45; 28 for (int i = 0; i < numCacheEntriesToAdd; i++) { 29 Object key = CacheKeyFactory.createKey(new Object [] {"testkey",String.valueOf(i)}); 30 31 assertNull("cache entry should not already exist", cache.get(key)); 32 33 CacheEntry cacheEntry = new CacheEntry(key, "testvalue" + i); 34 cache.put(cacheEntry); 35 36 CacheEntry cacheEntry2 = cache.get(key); 37 assertNotNull("cache entry should not be null", cacheEntry2); 38 assertEquals("cache entries should be equal", cacheEntry, cacheEntry2); 39 assertSame("cache entries should be same instance", cacheEntry, cacheEntry2); 40 } 41 assertEquals("invlid cache size", numCacheEntriesToAdd, cache.getSize()); 42 assertEquals("invlid cache hit count", numCacheEntriesToAdd, cache.getHitCount()); 43 assertEquals("invlid cache miss count", numCacheEntriesToAdd, cache.getMissCount()); 44 } 45 46 public void testMaximumCacheSize() throws Exception { 47 int maxSize = 25; 48 Cache cache = new Cache(getRandomString(), maxSize); 49 for (int i = 0; i < maxSize + 125; i++) { 51 CacheEntry cacheEntry = new CacheEntry("testkey" + i, "testvalue" + i); 52 cache.put(cacheEntry); 53 } 54 assertEquals("cache size should not exceed max size", maxSize, cache.getSize()); 55 } 56 57 public void testMaximumCacheSize_Zero() throws Exception { 58 int maxSize = 0; 59 Cache cache = new Cache(getRandomString(), maxSize); 60 for (int i = 0; i < maxSize + 5; i++) { 62 CacheEntry cacheEntry = new CacheEntry("testkey" + i, "testvalue" + i); 63 cache.put(cacheEntry); 64 } 65 assertEquals("cache size should not exceed max size", maxSize, cache.getSize()); 66 } 67 68 public void testClear() throws Exception { 69 Cache cache = new Cache(getRandomString(), 50); 70 Object key = CacheKeyFactory.createKey(new Object [] {"testkey"}); 71 CacheEntry cacheEntry = new CacheEntry(key, "testvalue"); 72 cache.put(cacheEntry); 73 74 assertEquals("cache should contain entry", 1, cache.getSize()); 75 cache.clear(); 76 assertEquals("cache should have been cleared", 0, cache.getSize()); 77 } 78 79 public void testGetMaxSize() { 80 int maxSize = 550; 81 Cache cache = new Cache(getRandomString(), maxSize); 82 83 assertEquals("invalid max size", maxSize, cache.getMaxSize()); 84 } 85 86 public void testGetNull() { 87 Cache cache = new Cache(getRandomString(), 5); 88 try { 89 cache.get(null); 90 fail("should have thrown exception"); 91 } catch (IllegalArgumentException e) { 92 } 93 } 94 95 public void testPutNull() { 96 Cache cache = new Cache(getRandomString(), 5); 97 try { 98 cache.put(null); 99 fail("should have thrown exception"); 100 } catch (IllegalArgumentException e) { 101 } 102 } 103 } 104 | Popular Tags |