KickJava   Java API By Example, From Geeks To Geeks.

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


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

5 package com.opensymphony.oscache.base.algorithm;
6
7 import com.opensymphony.oscache.base.Config;
8 import com.opensymphony.oscache.base.persistence.PersistenceListener;
9 import com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener;
10 import com.opensymphony.oscache.plugins.diskpersistence.TestDiskPersistenceListener;
11
12 import java.util.Iterator JavaDoc;
13 import java.util.Properties JavaDoc;
14
15 /**
16  * Test class for the QueueCache class, which is the base class for FIFO
17  * and LIFO algorithm classes. All the public methods of QueueCache are tested
18  * here.
19  *
20  * $Id: TestQueueCache.java,v 1.1 2005/06/17 05:07:07 dres Exp $
21  * @version $Revision: 1.1 $
22  * @author <a HREF="mailto:abergevin@pyxis-tech.com">Alain Bergevin</a>
23  */

24 public abstract class TestQueueCache extends TestAbstractCache {
25     /**
26      * Entry content
27      */

28     protected final String JavaDoc CONTENT = "Test Queue Cache content";
29
30     /**
31      * Entry key
32      */

33     protected final String JavaDoc KEY = "Test Queue Cache key";
34
35     /**
36      * Constructor
37      * <p>
38      * @param str The test name (required by JUnit)
39      */

40     public TestQueueCache(String JavaDoc str) {
41         super(str);
42     }
43
44     /**
45      * Test the specific algorithms
46      */

47     public abstract void testRemoveItem();
48
49     /**
50      * Test the clear
51      */

52     public void testClear() {
53         getCache().clear();
54         assertEquals(0, getCache().size());
55     }
56
57     /**
58      * Test the ContainsKey method
59      */

60     public void testContainsKey() {
61         getCache().put(KEY, CONTENT);
62         assertTrue(getCache().containsKey(KEY));
63         getCache().clear();
64     }
65
66     /**
67      * Test the get method
68      */

69     public void testGet() {
70         // Add an entry and verify that it is there
71
getCache().put(KEY, CONTENT);
72         assertTrue(getCache().get(KEY).equals(CONTENT));
73
74         // Call with invalid parameters
75
try {
76             getCache().get(null);
77             fail("Get called with null parameters!");
78         } catch (Exception JavaDoc e) { /* This is what we expect */
79         }
80
81         getCache().clear();
82     }
83
84     /**
85      * Test the getter and setter for the max entries
86      */

87     public void testGetSetMaxEntries() {
88         // Check that the cache is full, then chop it by one and assert that
89
// an element has been removed
90
for (int count = 0; count < MAX_ENTRIES; count++) {
91             getCache().put(KEY + count, CONTENT + count);
92         }
93
94         assertEquals(MAX_ENTRIES, getCache().size());
95         getCache().setMaxEntries(MAX_ENTRIES - 1);
96         assertEquals(MAX_ENTRIES - 1, getCache().getMaxEntries());
97         assertEquals(MAX_ENTRIES - 1, getCache().size());
98
99         // Specify an invalid capacity
100
try {
101             getCache().setMaxEntries(INVALID_MAX_ENTRIES);
102             fail("Cache capacity set with an invalid argument");
103         } catch (Exception JavaDoc e) {
104             // This is what we expect
105
}
106
107         getCache().clear();
108     }
109
110     /**
111      * Test the iterator
112      */

113     public void testIterator() {
114         // Verify that the iterator returns MAX_ENTRIES and no more elements
115
int nbEntries = getCache().size();
116         Iterator JavaDoc iterator = getCache().entrySet().iterator();
117         assertNotNull(iterator);
118
119         for (int count = 0; count < nbEntries; count++) {
120             assertNotNull(iterator.next());
121         }
122
123         assertTrue(!iterator.hasNext());
124     }
125
126     /**
127      * Test the put method
128      */

129     public void testPut() {
130         // Put elements in cache
131
for (int count = 0; count < MAX_ENTRIES; count++) {
132             getCache().put(KEY + count, CONTENT + count);
133         }
134
135         // Call with invalid parameters
136
try {
137             getCache().put(null, null);
138             fail("Put called with null parameters!");
139         } catch (Exception JavaDoc e) { /* This is what we expect */
140         }
141
142         getCache().clear();
143     }
144
145     /**
146      * Test the put method with overflow parameter set
147      */

148     public void testPutOverflow() {
149         // Create a listener
150
PersistenceListener listener = new DiskPersistenceListener();
151
152         Properties JavaDoc p = new Properties JavaDoc();
153         p.setProperty("cache.path", TestDiskPersistenceListener.CACHEDIR);
154         p.setProperty("cache.memory", "true");
155         p.setProperty("cache.persistence.overflow.only", "true");
156         p.setProperty("cache.persistence.class", "com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener");
157         listener.configure(new Config(p));
158         getCache().setPersistenceListener(listener);
159         getCache().clear();
160         getCache().setMaxEntries(MAX_ENTRIES);
161         getCache().setOverflowPersistence(true);
162
163         if (getCache() instanceof UnlimitedCache) {
164             return; // nothing to test since memory will never overflow.
165
}
166
167         // Put elements in cache
168
for (int count = 0; count <= MAX_ENTRIES; count++) {
169             getCache().put(KEY + count, CONTENT + count);
170         }
171
172         try {
173             int numPersisted = 0;
174
175             // Check that number of elements persisted == 1 if it is an overflow cache or all
176
// if it is not overflow and writes every time.
177
for (int count = 0; count <= MAX_ENTRIES; count++) {
178                 if (getCache().getPersistenceListener().isStored(KEY + count)) {
179                     numPersisted++;
180                 }
181             }
182
183             if (getCache().isOverflowPersistence()) {
184                 assertTrue("Only 1 element should have been persisted ", numPersisted == 1);
185             } else {
186                 assertTrue("All elements should have been persisted ", numPersisted == (MAX_ENTRIES + 1));
187             }
188         } catch (Exception JavaDoc e) {
189             fail();
190         }
191
192         getCache().clear();
193     }
194
195     /**
196      * Test the remove from cache
197      */

198     public void testRemove() {
199         getCache().put(KEY, CONTENT);
200
201         // Remove the object and assert the return
202
assertNotNull(getCache().remove(KEY));
203         getCache().clear();
204     }
205 }
206
Popular Tags