~ubuntu-branches/ubuntu/gutsy/oscache/gutsy

« back to all changes in this revision

Viewing changes to src/core/test/com/opensymphony/oscache/base/algorithm/TestQueueCache.java

  • Committer: Bazaar Package Importer
  • Author(s): Kalle Kivimaa
  • Date: 2004-08-13 14:00:00 UTC
  • Revision ID: james.westby@ubuntu.com-20040813140000-lyugvinublk1x8y2
Tags: upstream-2.0.2
ImportĀ upstreamĀ versionĀ 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.Iterator;
 
8
 
 
9
/**
 
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
 
12
 * here.
 
13
 *
 
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>
 
17
 */
 
18
public abstract class TestQueueCache extends TestAbstractCache {
 
19
    /**
 
20
     * Entry content
 
21
     */
 
22
    protected final String CONTENT = "Test Queue Cache content";
 
23
 
 
24
    /**
 
25
     * Entry key
 
26
     */
 
27
    protected final String KEY = "Test Queue Cache key";
 
28
 
 
29
    /**
 
30
     * Constructor
 
31
     * <p>
 
32
     * @param str The test name (required by JUnit)
 
33
     */
 
34
    public TestQueueCache(String str) {
 
35
        super(str);
 
36
    }
 
37
 
 
38
    /**
 
39
     * Test the specific algorithms
 
40
     */
 
41
    public abstract void testRemoveItem();
 
42
 
 
43
    /**
 
44
     * Test the clear
 
45
     */
 
46
    public void testClear() {
 
47
        getCache().clear();
 
48
        assertEquals(0, getCache().size());
 
49
    }
 
50
 
 
51
    /**
 
52
     * Test the ContainsKey method
 
53
     */
 
54
    public void testContainsKey() {
 
55
        getCache().put(KEY, CONTENT);
 
56
        assertTrue(getCache().containsKey(KEY));
 
57
        getCache().clear();
 
58
    }
 
59
 
 
60
    /**
 
61
     * Test the get method
 
62
     */
 
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));
 
67
 
 
68
        // Call with invalid parameters
 
69
        try {
 
70
            getCache().get(null);
 
71
            fail("Get called with null parameters!");
 
72
        } catch (Exception e) { /* This is what we expect */
 
73
        }
 
74
 
 
75
        getCache().clear();
 
76
    }
 
77
 
 
78
    /**
 
79
     * Test the getter and setter for the max entries
 
80
     */
 
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);
 
86
        }
 
87
 
 
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());
 
92
 
 
93
        // Specify an invalid capacity
 
94
        try {
 
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
 
99
        }
 
100
 
 
101
        getCache().clear();
 
102
    }
 
103
 
 
104
    /**
 
105
     * Test the iterator
 
106
     */
 
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);
 
112
 
 
113
        for (int count = 0; count < nbEntries; count++) {
 
114
            assertNotNull(iterator.next());
 
115
        }
 
116
 
 
117
        assertTrue(!iterator.hasNext());
 
118
    }
 
119
 
 
120
    /**
 
121
     * Test the put method
 
122
     */
 
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);
 
127
        }
 
128
 
 
129
        // Call with invalid parameters
 
130
        try {
 
131
            getCache().put(null, null);
 
132
            fail("Put called with null parameters!");
 
133
        } catch (Exception e) { /* This is what we expect */
 
134
        }
 
135
 
 
136
        getCache().clear();
 
137
    }
 
138
 
 
139
    /**
 
140
     * Test the remove from cache
 
141
     */
 
142
    public void testRemove() {
 
143
        getCache().put(KEY, CONTENT);
 
144
 
 
145
        // Remove the object and assert the return
 
146
        assertNotNull(getCache().remove(KEY));
 
147
        getCache().clear();
 
148
    }
 
149
}