KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > oscache > base > TestCache


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.oscache.base;
6
7 import java.util.Properties JavaDoc;
8
9 import com.opensymphony.oscache.general.GeneralCacheAdministrator;
10
11 import junit.framework.Assert;
12 import junit.framework.Test;
13 import junit.framework.TestCase;
14 import junit.framework.TestSuite;
15
16 /**
17  * Test the public methods of the Cache class
18  *
19  * $Id: TestCache.java,v 1.2 2006/04/02 06:53:58 ltorunski Exp $
20  * @version $Revision: 1.2 $
21  * @author <a HREF="mailto:abergevin@pyxis-tech.com">Alain Bergevin</a>
22  */

23 public class TestCache extends TestCase {
24     // Static variables required thru all the tests
25
private static Cache map = null;
26     private final String JavaDoc CONTENT = "Content for the cache test";
27
28     // Constants needed thru all the tests
29
private final String JavaDoc ENTRY_KEY = "Test cache key";
30     private final int NO_REFRESH_NEEDED = CacheEntry.INDEFINITE_EXPIRY;
31     private final int REFRESH_NEEDED = 0;
32
33     /**
34      * Class constructor.
35      * <p>
36      * @param str The test name (required by JUnit)
37      */

38     public TestCache(String JavaDoc str) {
39         super(str);
40     }
41
42     /**
43      * This method is invoked before each testXXXX methods of the
44      * class. It set ups the variables required for each tests.
45      */

46     public void setUp() {
47         // At first invocation, create a new Cache
48
if (map == null) {
49             GeneralCacheAdministrator admin = new GeneralCacheAdministrator();
50             map = admin.getCache();
51             assertNotNull(map);
52         }
53     }
54
55     /**
56      * This methods returns the name of this test class to JUnit
57      * <p>
58      * @return The name of this class
59      */

60     public static Test suite() {
61         return new TestSuite(TestCache.class);
62     }
63
64     /**
65      * Verify that items may still be flushed by key pattern
66      */

67     public void testFlushPattern() {
68         // Try to flush with a bad pattern and ensure that our data is still there
69
map.putInCache(ENTRY_KEY, CONTENT);
70         map.flushPattern(ENTRY_KEY + "do not flush");
71         getBackContent(map, CONTENT, NO_REFRESH_NEEDED, false);
72
73         // Flush our map for real
74
map.flushPattern(ENTRY_KEY.substring(1, 2));
75         getBackContent(map, CONTENT, NO_REFRESH_NEEDED, true);
76
77         // Check invalid values
78
map.flushPattern("");
79         map.flushPattern(null);
80     }
81
82     /**
83      * Tests that with a very large amount of keys that added and trigger cache overflows, there is no memory leak
84      * @throws Exception
85      */

86     public void testBug174CacheOverflow() throws Exception JavaDoc {
87         
88         Properties JavaDoc p = new Properties JavaDoc();
89         p.setProperty(AbstractCacheAdministrator.CACHE_ALGORITHM_KEY, "com.opensymphony.oscache.base.algorithm.LRUCache");
90         p.setProperty(AbstractCacheAdministrator.CACHE_CAPACITY_KEY, "100");
91         GeneralCacheAdministrator admin = new GeneralCacheAdministrator(p);
92         
93         int cacheCapacity = 100;
94         int maxAddedCacheEntries = cacheCapacity*10;
95         String JavaDoc baseCacheKey= "baseKey";
96         String JavaDoc cacheValue ="same_value";
97
98         admin.setCacheCapacity(cacheCapacity);
99         
100         Cache cache = admin.getCache();
101
102         //Add lots of different keys to trigger cache overflow
103
for (int keyIndex=0; keyIndex<maxAddedCacheEntries; keyIndex++) {
104             String JavaDoc key = baseCacheKey + keyIndex;
105             admin.putInCache(key, cacheValue);
106         }
107         
108         Assert.assertEquals("expected cache to be at its full capacity", cacheCapacity , cache.getSize());
109         Assert.assertTrue("expected cache overflows to have cleaned UpdateState instances. got [" + cache.getNbUpdateState() + "] updates while max is [" + cacheCapacity + "]", cache.getNbUpdateState() <= cacheCapacity);
110     }
111
112     /**
113      * Tests that with a very large amount of keys that added and trigger cache overflows, there is no memory leak
114      * @throws Exception
115      */

116     public void testBug174CacheOverflowAndUpdate() throws Exception JavaDoc {
117         Properties JavaDoc p = new Properties JavaDoc();
118         p.setProperty(AbstractCacheAdministrator.CACHE_ALGORITHM_KEY, "com.opensymphony.oscache.base.algorithm.LRUCache");
119         p.setProperty(AbstractCacheAdministrator.CACHE_CAPACITY_KEY, "100");
120         GeneralCacheAdministrator admin = new GeneralCacheAdministrator(p);
121         
122         int cacheCapacity = 100;
123         int maxAddedCacheEntries = cacheCapacity*10;
124         String JavaDoc baseCacheKey= "baseKey";
125         String JavaDoc cacheValue ="same_value";
126
127         admin.setCacheCapacity(cacheCapacity);
128         
129         Cache cache = admin.getCache();
130
131         
132         //Add lots of different keys to trigger cache overflow, mixed with updates
133
//FIXME: we may need different threads to enter branches recovering from current update.
134
for (int keyIndex=0; keyIndex<maxAddedCacheEntries; keyIndex++) {
135             String JavaDoc key = baseCacheKey + keyIndex;
136             admin.putInCache(key, cacheValue);
137             
138             try {
139                 admin.getFromCache(key, 0);
140                 fail("expected element [" + key + "] not to be present");
141             } catch (NeedsRefreshException e) {
142                 admin.putInCache(key, cacheValue);
143             }
144         }
145         
146         Assert.assertEquals("expected cache to be at its full capacity", cacheCapacity , cache.getSize());
147         Assert.assertTrue("expected cache overflows to have cleaned UpdateState instances. Nb states is:" + cache.getNbUpdateState() + " expected max="+ cacheCapacity, cache.getNbUpdateState() <= cacheCapacity);
148     }
149
150     
151     /**
152      * Tests that with a very large amount of keys accessed and cancelled, there is no memory leak
153      * @throws Exception
154      */

155     public void testBug174CacheMissesNonBlocking() throws Exception JavaDoc {
156         testBug174CacheMisses(false);
157     }
158     
159     /**
160      * Tests that with a very large amount of keys accessed and cancelled, there is no memory leak
161      * @throws Exception
162      */

163     public void testBug174CacheMissesBlocking() throws Exception JavaDoc {
164         testBug174CacheMisses(true);
165     }
166
167     /**
168      * Tests that with a very large amount of keys accessed and cancelled, there is no memory leak
169      * @throws Exception
170      */

171     public void testBug174CacheMisses(boolean block) throws Exception JavaDoc {
172         Properties JavaDoc p = new Properties JavaDoc();
173         p.setProperty(AbstractCacheAdministrator.CACHE_ALGORITHM_KEY, "com.opensymphony.oscache.base.algorithm.LRUCache");
174         p.setProperty(AbstractCacheAdministrator.CACHE_CAPACITY_KEY, "100");
175         if (block) {
176             p.setProperty(AbstractCacheAdministrator.CACHE_BLOCKING_KEY, "true");
177         }
178         GeneralCacheAdministrator admin = new GeneralCacheAdministrator(p);
179         
180         int cacheCapacity = 100;
181         int maxAddedCacheEntries = cacheCapacity*10;
182         String JavaDoc baseCacheKey= "baseKey";
183         String JavaDoc cacheValue ="same_value";
184         
185         admin.setCacheCapacity(cacheCapacity);
186         
187         Cache cache = admin.getCache();
188
189         //Access lots of different keys to trigger cache overflow
190
for (int keyIndex=0; keyIndex<maxAddedCacheEntries; keyIndex++) {
191             String JavaDoc key = baseCacheKey + keyIndex;
192             try {
193                 admin.getFromCache(key);
194                 fail("expected element [" + key + "] not to be present");
195             } catch (NeedsRefreshException e) {
196                 admin.cancelUpdate(key);
197             }
198         }
199         
200         Assert.assertTrue("expected cache accesses to not leak past cache capacity. Nb states is:" + cache.getNbUpdateState() + " expected max="+ cacheCapacity, cache.getNbUpdateState() < cacheCapacity);
201     }
202     
203     /**
204      * Verify that we can put item in the cache and that they are correctly retrieved
205      */

206     public void testPutGetFromCache() {
207         // We put content in the cache and get it back with and without refresh
208
map.putInCache(ENTRY_KEY, CONTENT);
209         getBackContent(map, CONTENT, NO_REFRESH_NEEDED, false);
210         getBackContent(map, CONTENT, REFRESH_NEEDED, true);
211
212         // Test with invalid values
213

214         /** TODO Verify this logic */
215         try {
216             assertNull(map.getFromCache("", NO_REFRESH_NEEDED));
217         } catch (NeedsRefreshException nre) {
218             map.cancelUpdate("");
219         } catch (Exception JavaDoc e) {
220         }
221
222         try {
223             assertNull(map.getFromCache(null, NO_REFRESH_NEEDED));
224         } catch (NeedsRefreshException nre) {
225             map.cancelUpdate(null);
226         } catch (Exception JavaDoc e) {
227         }
228     }
229
230     /**
231      * Verify that we can put item in the cache and that they are correctly retrieved
232      */

233     public void testPutGetFromCacheWithPolicy() {
234         // We put content in the cache and get it back
235
map.putInCache(ENTRY_KEY + "policy", CONTENT, new DummyAlwayRefreshEntryPolicy());
236
237         // Should get a refresh
238
try {
239             map.getFromCache(ENTRY_KEY + "policy", -1);
240             fail("Should have got a refresh.");
241         } catch (NeedsRefreshException nre) {
242             map.cancelUpdate(ENTRY_KEY + "policy");
243         }
244     }
245
246     protected void tearDown() throws Exception JavaDoc {
247         if (map != null) {
248             map.clear();
249         }
250     }
251
252     /**
253      * Retrieve the content in the cache
254      * <p>
255      * @param map The Cache in which the data is stored
256      * @param content The content expected to be retrieved
257      * @param refresh Time interval to determine if the cache object needs refresh
258      * @param exceptionExpected Specify if a NeedsRefreshException is expected
259      */

260     private void getBackContent(Cache map, Object JavaDoc content, int refresh, boolean exceptionExpected) {
261         try {
262             assertEquals(content, map.getFromCache(ENTRY_KEY, refresh));
263
264             if (exceptionExpected) {
265                 fail("NeedsRefreshException should have been thrown!");
266             }
267         } catch (NeedsRefreshException nre) {
268             map.cancelUpdate(ENTRY_KEY);
269
270             if (!exceptionExpected) {
271                 fail("NeedsRefreshException shouldn't have been thrown!");
272             }
273         }
274     }
275 }
276
Popular Tags