2
* Copyright (c) 2002-2003 by OpenSymphony
5
package com.opensymphony.oscache.base.algorithm;
7
import java.util.Iterator;
10
* Test class for the QueueCache class, which is the base class for FIFO
11
* and LIFO algorithm classes. All the public methods of QueueCache are tested
14
* $Id: TestQueueCache.java,v 1.1 2003/07/19 09:58:49 chris_miller Exp $
15
* @version $Revision: 1.1 $
16
* @author <a href="mailto:abergevin@pyxis-tech.com">Alain Bergevin</a>
18
public abstract class TestQueueCache extends TestAbstractCache {
22
protected final String CONTENT = "Test Queue Cache content";
27
protected final String KEY = "Test Queue Cache key";
32
* @param str The test name (required by JUnit)
34
public TestQueueCache(String str) {
39
* Test the specific algorithms
41
public abstract void testRemoveItem();
46
public void testClear() {
48
assertEquals(0, getCache().size());
52
* Test the ContainsKey method
54
public void testContainsKey() {
55
getCache().put(KEY, CONTENT);
56
assertTrue(getCache().containsKey(KEY));
63
public void testGet() {
64
// Add an entry and verify that it is there
65
getCache().put(KEY, CONTENT);
66
assertTrue(getCache().get(KEY).equals(CONTENT));
68
// Call with invalid parameters
71
fail("Get called with null parameters!");
72
} catch (Exception e) { /* This is what we expect */
79
* Test the getter and setter for the max entries
81
public void testGetSetMaxEntries() {
82
// Check that the cache is full, then chop it by one and assert that
83
// an element has been removed
84
for (int count = 0; count < MAX_ENTRIES; count++) {
85
getCache().put(KEY + count, CONTENT + count);
88
assertEquals(MAX_ENTRIES, getCache().size());
89
getCache().setMaxEntries(MAX_ENTRIES - 1);
90
assertEquals(MAX_ENTRIES - 1, getCache().getMaxEntries());
91
assertEquals(MAX_ENTRIES - 1, getCache().size());
93
// Specify an invalid capacity
95
getCache().setMaxEntries(INVALID_MAX_ENTRIES);
96
fail("Cache capacity set with an invalid argument");
97
} catch (Exception e) {
98
// This is what we expect
107
public void testIterator() {
108
// Verify that the iterator returns MAX_ENTRIES and no more elements
109
int nbEntries = getCache().size();
110
Iterator iterator = getCache().entrySet().iterator();
111
assertNotNull(iterator);
113
for (int count = 0; count < nbEntries; count++) {
114
assertNotNull(iterator.next());
117
assertTrue(!iterator.hasNext());
121
* Test the put method
123
public void testPut() {
124
// Put elements in cache
125
for (int count = 0; count < MAX_ENTRIES; count++) {
126
getCache().put(KEY + count, CONTENT + count);
129
// Call with invalid parameters
131
getCache().put(null, null);
132
fail("Put called with null parameters!");
133
} catch (Exception e) { /* This is what we expect */
140
* Test the remove from cache
142
public void testRemove() {
143
getCache().put(KEY, CONTENT);
145
// Remove the object and assert the return
146
assertNotNull(getCache().remove(KEY));