1 16 package net.sf.dozer.util.mapping.cache; 17 18 import java.util.HashSet ; 19 import java.util.Set ; 20 21 import net.sf.dozer.util.mapping.DozerTestBase; 22 import net.sf.dozer.util.mapping.MappingException; 23 import net.sf.dozer.util.mapping.exception.NotFoundException; 24 25 28 public class DozerCacheManagerTest extends DozerTestBase { 29 private DozerCacheManager cacheMgr; 30 31 protected void setUp() throws Exception { 32 super.setUp(); 33 cacheMgr = DozerCacheManager.createNew(); 34 } 35 36 public void testCreateNew() throws Exception { 37 DozerCacheManager cacheMgr2 = DozerCacheManager.createNew(); 38 39 assertFalse("cache mgrs should not be equal", cacheMgr.equals(cacheMgr2)); 40 assertNotSame("cache mgrs should not be same instance", cacheMgr, cacheMgr2); 41 } 42 43 public void testGetInstance() throws Exception { 44 DozerCacheManager cacheMgr = DozerCacheManager.getInstance(); 45 DozerCacheManager cacheMgr2 = DozerCacheManager.getInstance(); 46 47 assertEquals("cache mgrs should be equal", cacheMgr, cacheMgr2); 48 assertSame("cache mgrs should be the same instance", cacheMgr, cacheMgr2); 49 } 50 51 public void testAddGetExistsCache() throws Exception { 52 String cacheName = getRandomString(); 53 cacheMgr.addCache(cacheName, 1); 54 55 boolean cacheExists = cacheMgr.cacheExists(cacheName); 56 assertTrue("cache should exist", cacheExists); 57 58 Cache cache = cacheMgr.getCache(cacheName); 59 assertNotNull("cache should not be null", cache); 60 assertEquals("cache should be empty", cache.getSize(), 0); 61 assertEquals("invalid cache name", cacheName, cache.getName()); 62 } 63 64 public void testGetUnknownCache() throws Exception { 65 String cacheName = getRandomString(); 66 boolean cacheExists = cacheMgr.cacheExists(cacheName); 67 assertFalse("cache should not exist", cacheExists); 68 69 try { 70 cacheMgr.getCache(cacheName); 71 fail("trying to get an unknown cache should have thrown a MappingException"); 72 } catch (NotFoundException e) { 73 } 74 } 75 76 public void testAddDuplicateCachesSingleton() throws Exception { 77 String cacheName = getRandomString(); 78 cacheMgr.addCache(cacheName, 1); 79 80 try { 81 cacheMgr.addCache(cacheName, 1); 83 fail("trying to add duplicate caches should have thrown an ObjectExistsException"); 84 } catch (MappingException e) { 85 } 86 } 87 88 public void testAddDuplicateCachesNonSingleton() throws Exception { 89 DozerCacheManager cacheMgr2 = DozerCacheManager.createNew(); 93 94 String cacheName = getRandomString(); 96 cacheMgr.addCache(cacheName, 1); 97 cacheMgr2.addCache(cacheName, 1); 98 99 assertTrue("cache should exist in cache mgr1", cacheMgr.cacheExists(cacheName)); 100 assertTrue("cache should also exist in cache mgr2", cacheMgr2.cacheExists(cacheName)); 101 102 Cache cache1 = cacheMgr.getCache(cacheName); 103 Cache cache2 = cacheMgr2.getCache(cacheName); 104 105 assertFalse("caches should not be the same instance", cache1 == cache2); 106 assertEquals("invalid cache name", cacheName, cache1.getName()); 107 assertEquals("invalid cache name for cache2", cacheName, cache2.getName()); 108 } 109 110 public void testGetStatisticTypes() { 111 String name = getRandomString(); 112 String name2 = name + "-2"; 113 cacheMgr.addCache(name, 100); 114 cacheMgr.addCache(name2, 100); 115 116 Set expected = new HashSet (); 117 expected.add(name); 118 expected.add(name2); 119 120 assertEquals("invalid cache names types found", expected, cacheMgr.getCacheNames()); 121 } 122 123 public void testClearAllCacheEntries() { 124 String name = getRandomString(); 125 Cache cache = new Cache(name, 5); 126 CacheEntry entry = new CacheEntry(getRandomString(), "value"); 127 cache.put(entry); 128 cacheMgr.addCache(cache); 129 130 assertEquals("invalid initial cache entry size", 1, cacheMgr.getCache(name).getEntries().size()); 131 cacheMgr.clearAllEntries(); 132 assertEquals("invalid cache entry size after clearAll", 0, cacheMgr.getCache(name).getEntries().size()); 133 } 134 135 public void testGetCaches() { 136 String name = getRandomString(); 137 Cache cache = new Cache(name, 5); 138 Cache cache2 = new Cache(name+"2", 5); 139 cacheMgr.addCache(cache); 140 cacheMgr.addCache(cache2); 141 142 Set expected = new HashSet (); 143 expected.add(cache); 144 expected.add(cache2); 145 146 assertEquals("invalid caches found", expected, cacheMgr.getCaches()); 147 } 148 } 149 | Popular Tags |