001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.oscache.base.algorithm;
006:
007: import java.util.Enumeration;
008: import java.util.HashMap;
009: import java.util.Map;
010: import java.util.Set;
011:
012: import com.opensymphony.oscache.base.CacheEntry;
013: import com.opensymphony.oscache.base.Config;
014: import com.opensymphony.oscache.base.persistence.CachePersistenceException;
015: import com.opensymphony.oscache.base.persistence.PersistenceListener;
016:
017: import junit.framework.TestCase;
018:
019: /**
020: * Test class for the AbstractCache class. It tests all public methods of
021: * the AbstractCache and assert the results. It is design to run under JUnit.
022: *
023: * $Id: TestAbstractCache.java 425 2007-03-18 09:45:03Z larst $
024: * @version $Revision: 425 $
025: * @author <a href="mailto:abergevin@pyxis-tech.com">Alain Bergevin</a>
026: */
027: public abstract class TestAbstractCache extends TestCase {
028: /**
029: * Invalid cache capacity
030: */
031: protected final int INVALID_MAX_ENTRIES = 0;
032:
033: /**
034: * Cache capacity
035: */
036: protected final int MAX_ENTRIES = 3;
037:
038: /**
039: * Constructor
040: * <p>
041: * @param str The test name (required by JUnit)
042: */
043: protected TestAbstractCache(String str) {
044: super (str);
045: }
046:
047: /**
048: * Test the method that verify if the cache contains a specific key
049: */
050: public abstract void testContainsKey();
051:
052: /**
053: * Test the get from the cache
054: */
055: public abstract void testGet();
056:
057: /**
058: * Test the capacity setting
059: */
060: public void testGetSetMaxEntries() {
061: getCache().setMaxEntries(MAX_ENTRIES);
062: assertEquals(MAX_ENTRIES, getCache().getMaxEntries());
063:
064: // Specify an invalid capacity
065: try {
066: getCache().setMaxEntries(INVALID_MAX_ENTRIES);
067: fail("Cache capacity set with an invalid argument");
068: } catch (Exception e) {
069: // This is what we expect
070: }
071: }
072:
073: /**
074: * Test the setting of the memory cache
075: */
076: public void testGetSetMemoryCache() {
077: getCache().setMemoryCaching(true);
078: assertTrue(getCache().isMemoryCaching());
079: }
080:
081: /**
082: * Test the iterator retrieval
083: */
084: public abstract void testIterator();
085:
086: /**
087: * Test the put into the cache
088: */
089: public abstract void testPut();
090:
091: /**
092: * Test the remove from the cache
093: */
094: public abstract void testRemove();
095:
096: /**
097: * Test the specific details about the cache algorithm
098: */
099: public abstract void testRemoveItem();
100:
101: /**
102: * Test the PersistenceListener setter. Since the persistance listener is
103: * an interface, just call the setter with null
104: */
105: public void testSetPersistenceListener() {
106: getCache().setPersistenceListener(null);
107: }
108:
109: // Abstract method that returns an instance of an admin
110: protected abstract AbstractConcurrentReadCache getCache();
111:
112: /**
113: * Test that groups are correctly updated on puts and removes
114: * See CACHE-188 and maybe CACHE-244
115: */
116: public void testGroups() {
117: String KEY = "testkey";
118: String KEY2 = "testkey2";
119: String GROUP_NAME = "group1";
120: CacheEntry entry = new CacheEntry(KEY, null);
121: entry.setContent("testvalue");
122: entry.setGroups(new String[] { GROUP_NAME });
123: getCache().put(KEY, entry);
124:
125: Map m = getCache().getGroupsForReading();
126: assertNotNull("group must exist", m.get(GROUP_NAME));
127: try {
128: Set group = (Set) m.get(GROUP_NAME);
129: assertEquals(1, group.size());
130: Object keyFromGroup = group.iterator().next();
131: assertEquals(KEY, keyFromGroup);
132: } catch (ClassCastException e) {
133: fail("group should have been a java.util.Set but is a "
134: + m.get(GROUP_NAME).getClass().getName());
135: }
136:
137: assertNotNull(getCache().remove(KEY));
138:
139: m = getCache().getGroupsForReading();
140: assertNull("group should have been deleted (see CACHE-188)", m
141: .get(GROUP_NAME));
142: getCache().clear();
143:
144: // Test if persistence options are correctly considered for groups
145: try {
146: PersistenceListener listener = new MockPersistenceListener();
147: getCache().setPersistenceListener(listener);
148: getCache().setOverflowPersistence(false);
149: getCache().put(KEY, entry);
150: assertTrue(listener.isStored(KEY));
151: Set group = listener.retrieveGroup(GROUP_NAME);
152: assertNotNull(group);
153: assertTrue(group.contains(KEY));
154:
155: getCache().remove(KEY);
156: assertFalse(listener.isStored(KEY));
157: getCache().clear();
158:
159: // test overflow persistence
160: getCache().setOverflowPersistence(true);
161: getCache().setMaxEntries(1);
162: getCache().put(KEY, entry);
163: assertFalse(listener.isStored(KEY));
164: // is it correct that the group is persisted, even when we use overflow only?
165: // assertFalse(listener.isGroupStored(GROUP_NAME));
166:
167: CacheEntry entry2 = new CacheEntry(KEY2);
168: entry2.setContent("testvalue");
169: entry2.setGroups(new String[] { GROUP_NAME });
170: getCache().put(KEY2, entry2);
171: // oldest must have been persisted to disk:
172: assertTrue(listener.isStored(KEY));
173: assertFalse(listener.isStored(KEY2));
174: assertNotNull(getCache().get(KEY2));
175: } catch (CachePersistenceException e) {
176: e.printStackTrace();
177: fail("Excpetion was thrown");
178: }
179: }
180:
181: public void testMisc() {
182: getCache().clear();
183: assertTrue(getCache().capacity() > 0);
184:
185: final String KEY = "testkeymisc";
186: final String CONTENT = "testkeymisc";
187:
188: CacheEntry entry = new CacheEntry(KEY, null);
189: entry.setContent(CONTENT);
190:
191: if (getCache().contains(entry) == false) {
192: getCache().put(KEY, entry);
193: }
194: assertTrue(getCache().contains(entry));
195:
196: CacheEntry entry2 = new CacheEntry(KEY + "2", null);
197: entry.setContent(CONTENT + "2");
198: getCache().put(entry2.getKey(), entry2);
199:
200: Enumeration enumeration = getCache().elements();
201: assertTrue(enumeration.hasMoreElements());
202: while (enumeration.hasMoreElements())
203: enumeration.nextElement();
204: }
205:
206: private static class MockPersistenceListener implements
207: PersistenceListener {
208:
209: private Map entries = new HashMap();
210: private Map groups = new HashMap();
211:
212: public void clear() throws CachePersistenceException {
213: entries.clear();
214: groups.clear();
215: }
216:
217: public PersistenceListener configure(Config config) {
218: return this ;
219: }
220:
221: public boolean isGroupStored(String groupName)
222: throws CachePersistenceException {
223: return groups.containsKey(groupName);
224: }
225:
226: public boolean isStored(String key)
227: throws CachePersistenceException {
228: return entries.containsKey(key);
229: }
230:
231: public void remove(String key) throws CachePersistenceException {
232: entries.remove(key);
233: }
234:
235: public void removeGroup(String groupName)
236: throws CachePersistenceException {
237: groups.remove(groupName);
238: }
239:
240: public Object retrieve(String key)
241: throws CachePersistenceException {
242: return entries.get(key);
243: }
244:
245: public Set retrieveGroup(String groupName)
246: throws CachePersistenceException {
247: return (Set) groups.get(groupName);
248: }
249:
250: public void store(String key, Object obj)
251: throws CachePersistenceException {
252: entries.put(key, obj);
253: }
254:
255: public void storeGroup(String groupName, Set group)
256: throws CachePersistenceException {
257: groups.put(groupName, group);
258: }
259: }
260: }
|