1 /*
2 * Copyright (c) 2002-2003 by OpenSymphony
3 * All rights reserved.
4 */
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 /**
19 * Test class for the AbstractCache class. It tests all public methods of
20 * the AbstractCache and assert the results. It is design to run under JUnit.
21 *
22 * $Id: TestAbstractCache.java,v 1.3 2006/06/17 08:02:28 ltorunski Exp $
23 * @version $Revision: 1.3 $
24 * @author <a href="mailto:abergevin@pyxis-tech.com">Alain Bergevin</a>
25 */
26 public abstract class TestAbstractCache extends TestCase {
27 /**
28 * Invalid cache capacity
29 */
30 protected final int INVALID_MAX_ENTRIES = 0;
31
32 /**
33 * Cache capacity
34 */
35 protected final int MAX_ENTRIES = 3;
36
37 /**
38 * Constructor
39 * <p>
40 * @param str The test name (required by JUnit)
41 */
42 protected TestAbstractCache(String str) {
43 super(str);
44 }
45
46 /**
47 * Test the method that verify if the cache contains a specific key
48 */
49 public abstract void testContainsKey();
50
51 /**
52 * Test the get from the cache
53 */
54 public abstract void testGet();
55
56 /**
57 * Test the capacity setting
58 */
59 public void testGetSetMaxEntries() {
60 getCache().setMaxEntries(MAX_ENTRIES);
61 assertEquals(MAX_ENTRIES, getCache().getMaxEntries());
62
63 // Specify an invalid capacity
64 try {
65 getCache().setMaxEntries(INVALID_MAX_ENTRIES);
66 fail("Cache capacity set with an invalid argument");
67 } catch (Exception e) {
68 // This is what we expect
69 }
70 }
71
72 /**
73 * Test the setting of the memory cache
74 */
75 public void testGetSetMemoryCache() {
76 getCache().setMemoryCaching(true);
77 assertTrue(getCache().isMemoryCaching());
78 }
79
80 /**
81 * Test the iterator retrieval
82 */
83 public abstract void testIterator();
84
85 /**
86 * Test the put into the cache
87 */
88 public abstract void testPut();
89
90 /**
91 * Test the remove from the cache
92 */
93 public abstract void testRemove();
94
95 /**
96 * Test the specific details about the cache algorithm
97 */
98 public abstract void testRemoveItem();
99
100 /**
101 * Test the PersistenceListener setter. Since the persistance listener is
102 * an interface, just call the setter with null
103 */
104 public void testSetPersistenceListener() {
105 getCache().setPersistenceListener(null);
106 }
107
108 // Abstract method that returns an instance of an admin
109 protected abstract AbstractConcurrentReadCache getCache();
110
111 /**
112 * Test that groups are correctly updated on puts and removes
113 * See CACHE-188 and maybe CACHE-244
114 */
115 public void testGroups() {
116 /* TODO uncomment for 2.3.2 and for fixing issues CACHE-188 and maybe CACHE-244
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.get(GROUP_NAME));
141 getCache().clear();
142
143 // Test if persistence options are correctly considered for groups
144 try {
145 PersistenceListener listener = new MockPersistenceListener();
146 getCache().setPersistenceListener(listener);
147 getCache().setOverflowPersistence(false);
148 getCache().put(KEY, entry);
149 assertTrue(listener.isStored(KEY));
150 Set group = listener.retrieveGroup(GROUP_NAME);
151 assertNotNull(group);
152 assertTrue(group.contains(KEY));
153
154 getCache().remove(KEY);
155 assertFalse(listener.isStored(KEY));
156 getCache().clear();
157
158 // test overflow persistence
159 getCache().setOverflowPersistence(true);
160 getCache().setMaxEntries(1);
161 getCache().put(KEY, entry);
162 assertFalse(listener.isStored(KEY));
163 // is it correct that the group is persisted, even when we use overflow only?
164 // assertFalse(listener.isGroupStored(GROUP_NAME));
165
166 CacheEntry entry2 = new CacheEntry(KEY2);
167 entry2.setContent("testvalue");
168 entry2.setGroups(new String[] {GROUP_NAME});
169 getCache().put(KEY2, entry2);
170 // oldest must have been persisted to disk:
171 assertTrue(listener.isStored(KEY));
172 assertFalse(listener.isStored(KEY2));
173 assertNotNull(getCache().get(KEY2));
174 } catch (CachePersistenceException e) {
175 e.printStackTrace();
176 fail("Excpetion was thrown");
177 }
178 */
179 }
180
181
182 private static class MockPersistenceListener implements PersistenceListener {
183
184 private Map entries = new HashMap();
185 private Map groups = new HashMap();
186
187 public void clear() throws CachePersistenceException {
188 entries.clear();
189 groups.clear();
190 }
191
192 public PersistenceListener configure(Config config) {
193 return this;
194 }
195
196 public boolean isGroupStored(String groupName) throws CachePersistenceException {
197 return groups.containsKey(groupName);
198 }
199
200 public boolean isStored(String key) throws CachePersistenceException {
201 return entries.containsKey(key);
202 }
203
204 public void remove(String key) throws CachePersistenceException {
205 entries.remove(key);
206 }
207
208 public void removeGroup(String groupName) throws CachePersistenceException {
209 groups.remove(groupName);
210 }
211
212 public Object retrieve(String key) throws CachePersistenceException {
213 return entries.get(key);
214 }
215
216 public Set retrieveGroup(String groupName) throws CachePersistenceException {
217 return (Set)groups.get(groupName);
218 }
219
220 public void store(String key, Object obj) throws CachePersistenceException {
221 entries.put(key, obj);
222 }
223
224 public void storeGroup(String groupName, Set group) throws CachePersistenceException {
225 groups.put(groupName, group);
226 }
227 }
228 }