KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > oscache > base > algorithm > TestAbstractCache


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 JavaDoc;
8 import java.util.Map JavaDoc;
9 import java.util.Set JavaDoc;
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.4 2006/06/24 07:20:07 ltorunski Exp $
23  * @version $Revision: 1.4 $
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 JavaDoc 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 JavaDoc 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       String JavaDoc KEY = "testkey";
117       String JavaDoc KEY2 = "testkey2";
118       String JavaDoc GROUP_NAME = "group1";
119       CacheEntry entry = new CacheEntry(KEY, null);
120       entry.setContent("testvalue");
121       entry.setGroups(new String JavaDoc[] {GROUP_NAME});
122       getCache().put(KEY, entry);
123
124       Map JavaDoc m = getCache().getGroupsForReading();
125       assertNotNull("group must exist", m.get(GROUP_NAME));
126       try {
127         Set JavaDoc group = (Set JavaDoc)m.get(GROUP_NAME);
128         assertEquals(1, group.size());
129         Object JavaDoc keyFromGroup = group.iterator().next();
130         assertEquals(KEY, keyFromGroup);
131       } catch (ClassCastException JavaDoc 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       // Test if persistence options are correctly considered for groups
143
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 JavaDoc 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         // test overflow persistence
158
getCache().setOverflowPersistence(true);
159         getCache().setMaxEntries(1);
160         getCache().put(KEY, entry);
161         assertFalse(listener.isStored(KEY));
162         // is it correct that the group is persisted, even when we use overflow only?
163
// assertFalse(listener.isGroupStored(GROUP_NAME));
164

165         CacheEntry entry2 = new CacheEntry(KEY2);
166         entry2.setContent("testvalue");
167         entry2.setGroups(new String JavaDoc[] {GROUP_NAME});
168         getCache().put(KEY2, entry2);
169         // oldest must have been persisted to disk:
170
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 JavaDoc entries = new HashMap JavaDoc();
183       private Map JavaDoc groups = new HashMap JavaDoc();
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 JavaDoc groupName) throws CachePersistenceException {
195         return groups.containsKey(groupName);
196       }
197
198       public boolean isStored(String JavaDoc key) throws CachePersistenceException {
199         return entries.containsKey(key);
200       }
201
202       public void remove(String JavaDoc key) throws CachePersistenceException {
203         entries.remove(key);
204       }
205
206       public void removeGroup(String JavaDoc groupName) throws CachePersistenceException {
207         groups.remove(groupName);
208       }
209
210       public Object JavaDoc retrieve(String JavaDoc key) throws CachePersistenceException {
211         return entries.get(key);
212       }
213
214       public Set JavaDoc retrieveGroup(String JavaDoc groupName) throws CachePersistenceException {
215         return (Set JavaDoc)groups.get(groupName);
216       }
217
218       public void store(String JavaDoc key, Object JavaDoc obj) throws CachePersistenceException {
219         entries.put(key, obj);
220       }
221
222       public void storeGroup(String JavaDoc groupName, Set JavaDoc group) throws CachePersistenceException {
223         groups.put(groupName, group);
224       }
225     }
226 }
227
Popular Tags