001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.oscache.base.algorithm;
006:
007: import com.opensymphony.oscache.base.Config;
008: import com.opensymphony.oscache.base.persistence.PersistenceListener;
009: import com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener;
010: import com.opensymphony.oscache.plugins.diskpersistence.TestDiskPersistenceListener;
011:
012: import java.util.Iterator;
013: import java.util.Properties;
014:
015: /**
016: * Test class for the QueueCache class, which is the base class for FIFO
017: * and LIFO algorithm classes. All the public methods of QueueCache are tested
018: * here.
019: *
020: * $Id: TestQueueCache.java 383 2006-09-10 22:00:01Z larst $
021: * @version $Revision: 383 $
022: * @author <a href="mailto:abergevin@pyxis-tech.com">Alain Bergevin</a>
023: */
024: public abstract class TestQueueCache extends TestAbstractCache {
025: /**
026: * Entry content
027: */
028: protected final String CONTENT = "Test Queue Cache content";
029:
030: /**
031: * Entry key
032: */
033: protected final String KEY = "Test Queue Cache key";
034:
035: /**
036: * Constructor
037: * <p>
038: * @param str The test name (required by JUnit)
039: */
040: public TestQueueCache(String str) {
041: super (str);
042: }
043:
044: /**
045: * Test the specific algorithms
046: */
047: public abstract void testRemoveItem();
048:
049: /**
050: * Test the clear
051: */
052: public void testClear() {
053: getCache().clear();
054: assertEquals(0, getCache().size());
055: }
056:
057: /**
058: * Test the ContainsKey method
059: */
060: public void testContainsKey() {
061: getCache().put(KEY, CONTENT);
062: assertTrue(getCache().containsKey(KEY));
063: getCache().clear();
064: }
065:
066: /**
067: * Test the get method
068: */
069: public void testGet() {
070: // Add an entry and verify that it is there
071: getCache().put(KEY, CONTENT);
072: assertTrue(getCache().get(KEY).equals(CONTENT));
073:
074: // Call with invalid parameters
075: try {
076: getCache().get(null);
077: fail("Get called with null parameters!");
078: } catch (Exception e) { /* This is what we expect */
079: }
080:
081: getCache().clear();
082: }
083:
084: /**
085: * Test the getter and setter for the max entries
086: */
087: public void testGetSetMaxEntries() {
088: // Check that the cache is full, then chop it by one and assert that
089: // an element has been removed
090: for (int count = 0; count < MAX_ENTRIES; count++) {
091: getCache().put(KEY + count, CONTENT + count);
092: }
093:
094: assertEquals(MAX_ENTRIES, getCache().size());
095: getCache().setMaxEntries(MAX_ENTRIES - 1);
096: assertEquals(MAX_ENTRIES - 1, getCache().getMaxEntries());
097: assertEquals(MAX_ENTRIES - 1, getCache().size());
098:
099: // Specify an invalid capacity
100: try {
101: getCache().setMaxEntries(INVALID_MAX_ENTRIES);
102: fail("Cache capacity set with an invalid argument");
103: } catch (Exception 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 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 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 p = new Properties();
153: p.setProperty("cache.path",
154: TestDiskPersistenceListener.CACHEDIR);
155: p.setProperty("cache.memory", "true");
156: p.setProperty("cache.persistence.overflow.only", "true");
157: p
158: .setProperty("cache.persistence.class",
159: "com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener");
160: listener.configure(new Config(p));
161: getCache().setPersistenceListener(listener);
162: getCache().clear();
163: getCache().setMaxEntries(MAX_ENTRIES);
164: getCache().setOverflowPersistence(true);
165:
166: if (getCache() instanceof UnlimitedCache) {
167: return; // nothing to test since memory will never overflow.
168: }
169:
170: // Put elements in cache
171: for (int count = 0; count <= MAX_ENTRIES; count++) {
172: getCache().put(KEY + count, CONTENT + count);
173: }
174:
175: try {
176: int numPersisted = 0;
177:
178: // Check that number of elements persisted == 1 if it is an overflow cache or all
179: // if it is not overflow and writes every time.
180: for (int count = 0; count <= MAX_ENTRIES; count++) {
181: if (getCache().getPersistenceListener().isStored(
182: KEY + count)) {
183: numPersisted++;
184: }
185: }
186:
187: if (getCache().isOverflowPersistence()) {
188: assertTrue(
189: "Only 1 element should have been persisted ",
190: numPersisted == 1);
191: } else {
192: assertTrue("All elements should have been persisted ",
193: numPersisted == (MAX_ENTRIES + 1));
194: }
195: } catch (Exception e) {
196: fail();
197: }
198:
199: getCache().clear();
200: }
201:
202: /**
203: * Test if bug CACHE-255 disappeared.
204: */
205: public void testBugCache255() {
206: if (!getCache().isMemoryCaching()) {
207: return; // nothing to test since memory won't be used.
208: }
209: if (getCache() instanceof UnlimitedCache) {
210: return; // nothing to test since memory will never overflow.
211: }
212:
213: // fill up the cache
214: for (int count = 0; count < MAX_ENTRIES; count++) {
215: getCache().put(KEY + count, CONTENT + count);
216: }
217:
218: // get the old value
219: Object oldValue = getCache().put(KEY + MAX_ENTRIES,
220: CONTENT + MAX_ENTRIES);
221:
222: assertEquals("Evicted object content should be the same",
223: CONTENT + "0", oldValue);
224:
225: getCache().clear();
226: }
227:
228: /**
229: * Test the remove from cache
230: */
231: public void testRemove() {
232: getCache().put(KEY, CONTENT);
233:
234: // Remove the object and assert the return
235: assertNotNull(getCache().remove(KEY));
236: getCache().clear();
237: }
238: }
|