1 5 package com.opensymphony.oscache.base.algorithm; 6 7 import java.util.HashMap ; 8 import java.util.Map ; 9 import java.util.Set ; 10 11 import com.opensymphony.oscache.base.CacheEntry; 12 import com.opensymphony.oscache.base.Config; 13 import com.opensymphony.oscache.base.persistence.CachePersistenceException; 14 import com.opensymphony.oscache.base.persistence.PersistenceListener; 15 16 import junit.framework.TestCase; 17 18 26 public abstract class TestAbstractCache extends TestCase { 27 30 protected final int INVALID_MAX_ENTRIES = 0; 31 32 35 protected final int MAX_ENTRIES = 3; 36 37 42 protected TestAbstractCache(String str) { 43 super(str); 44 } 45 46 49 public abstract void testContainsKey(); 50 51 54 public abstract void testGet(); 55 56 59 public void testGetSetMaxEntries() { 60 getCache().setMaxEntries(MAX_ENTRIES); 61 assertEquals(MAX_ENTRIES, getCache().getMaxEntries()); 62 63 try { 65 getCache().setMaxEntries(INVALID_MAX_ENTRIES); 66 fail("Cache capacity set with an invalid argument"); 67 } catch (Exception e) { 68 } 70 } 71 72 75 public void testGetSetMemoryCache() { 76 getCache().setMemoryCaching(true); 77 assertTrue(getCache().isMemoryCaching()); 78 } 79 80 83 public abstract void testIterator(); 84 85 88 public abstract void testPut(); 89 90 93 public abstract void testRemove(); 94 95 98 public abstract void testRemoveItem(); 99 100 104 public void testSetPersistenceListener() { 105 getCache().setPersistenceListener(null); 106 } 107 108 protected abstract AbstractConcurrentReadCache getCache(); 110 111 115 public void testGroups() { 116 String KEY = "testkey"; 117 String KEY2 = "testkey2"; 118 String GROUP_NAME = "group1"; 119 CacheEntry entry = new CacheEntry(KEY, null); 120 entry.setContent("testvalue"); 121 entry.setGroups(new String [] {GROUP_NAME}); 122 getCache().put(KEY, entry); 123 124 Map m = getCache().getGroupsForReading(); 125 assertNotNull("group must exist", m.get(GROUP_NAME)); 126 try { 127 Set group = (Set )m.get(GROUP_NAME); 128 assertEquals(1, group.size()); 129 Object keyFromGroup = group.iterator().next(); 130 assertEquals(KEY, keyFromGroup); 131 } catch (ClassCastException e) { 132 fail("group should have been a java.util.Set but is a " + 133 m.get(GROUP_NAME).getClass().getName()); 134 } 135 136 assertNotNull(getCache().remove(KEY)); 137 138 m = getCache().getGroupsForReading(); 139 assertNull("group should have been deleted (see CACHE-188)", m.get(GROUP_NAME)); 140 getCache().clear(); 141 142 try { 144 PersistenceListener listener = new MockPersistenceListener(); 145 getCache().setPersistenceListener(listener); 146 getCache().setOverflowPersistence(false); 147 getCache().put(KEY, entry); 148 assertTrue(listener.isStored(KEY)); 149 Set group = listener.retrieveGroup(GROUP_NAME); 150 assertNotNull(group); 151 assertTrue(group.contains(KEY)); 152 153 getCache().remove(KEY); 154 assertFalse(listener.isStored(KEY)); 155 getCache().clear(); 156 157 getCache().setOverflowPersistence(true); 159 getCache().setMaxEntries(1); 160 getCache().put(KEY, entry); 161 assertFalse(listener.isStored(KEY)); 162 165 CacheEntry entry2 = new CacheEntry(KEY2); 166 entry2.setContent("testvalue"); 167 entry2.setGroups(new String [] {GROUP_NAME}); 168 getCache().put(KEY2, entry2); 169 assertTrue(listener.isStored(KEY)); 171 assertFalse(listener.isStored(KEY2)); 172 assertNotNull(getCache().get(KEY2)); 173 } catch (CachePersistenceException e) { 174 e.printStackTrace(); 175 fail("Excpetion was thrown"); 176 } 177 } 178 179 180 private static class MockPersistenceListener implements PersistenceListener { 181 182 private Map entries = new HashMap (); 183 private Map groups = new HashMap (); 184 185 public void clear() throws CachePersistenceException { 186 entries.clear(); 187 groups.clear(); 188 } 189 190 public PersistenceListener configure(Config config) { 191 return this; 192 } 193 194 public boolean isGroupStored(String groupName) throws CachePersistenceException { 195 return groups.containsKey(groupName); 196 } 197 198 public boolean isStored(String key) throws CachePersistenceException { 199 return entries.containsKey(key); 200 } 201 202 public void remove(String key) throws CachePersistenceException { 203 entries.remove(key); 204 } 205 206 public void removeGroup(String groupName) throws CachePersistenceException { 207 groups.remove(groupName); 208 } 209 210 public Object retrieve(String key) throws CachePersistenceException { 211 return entries.get(key); 212 } 213 214 public Set retrieveGroup(String groupName) throws CachePersistenceException { 215 return (Set )groups.get(groupName); 216 } 217 218 public void store(String key, Object obj) throws CachePersistenceException { 219 entries.put(key, obj); 220 } 221 222 public void storeGroup(String groupName, Set group) throws CachePersistenceException { 223 groups.put(groupName, group); 224 } 225 } 226 } 227 | Popular Tags |