2
* Copyright (c) 2002-2003 by OpenSymphony
5
package com.opensymphony.oscache.base;
7
import junit.framework.Test;
8
import junit.framework.TestCase;
9
import junit.framework.TestSuite;
12
* Test the public methods of the CacheEntry class
14
* $Id: TestCacheEntry.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 class TestCacheEntry extends TestCase {
19
// Static variables required thru the tests
20
static CacheEntry entry = null;
21
static long beforeCreation = 0;
22
static long afterCreation = 0;
23
private final String CONTENT = "Content for the cache entry test";
25
// Constants used thru the tests
26
private final String ENTRY_KEY = "Test cache entry key";
27
private final int NO_REFRESH_NEEDED = 1000000;
28
private final int REFRESH_NEEDED = 0;
33
* @param str The test name (required by JUnit)
35
public TestCacheEntry(String str) {
40
* This method is invoked before each testXXXX methods of the
41
* class. It set ups the variables required for each tests.
44
// At first invocation, create a cache entry object
46
// Log the time before and after to verify the creation time
47
// in one of the tests
48
beforeCreation = System.currentTimeMillis();
50
entry = new CacheEntry(ENTRY_KEY);
51
afterCreation = System.currentTimeMillis();
56
* This methods returns the name of this test class to JUnit
58
* @return The name of this class
60
public static Test suite() {
61
return new TestSuite(TestCacheEntry.class);
67
public void testFlush() {
68
// Set the content so it shouldn't need refresh
69
entry.setContent(CONTENT);
70
assertTrue(!entry.needsRefresh(NO_REFRESH_NEEDED));
72
// Flush the entry. It should now needs refresh
74
assertTrue(entry.needsRefresh(NO_REFRESH_NEEDED));
78
* Verify that the creation time is correct
80
public void testGetCreated() {
81
assertBetweenOrEquals(beforeCreation, entry.getCreated(), afterCreation);
85
* Retrieve the item created by the setup
87
public void testGetKey() {
88
assertTrue(entry.getKey().equals(ENTRY_KEY));
92
* Verify that the last modification time is between the time before and
93
* after the alteration of the item
95
public void testGetLastUpdate() {
96
// again. Then we ensure that the update time is between our timestamps
97
long before = System.currentTimeMillis();
98
entry.setContent(CONTENT);
100
long after = System.currentTimeMillis();
101
assertBetweenOrEquals(before, entry.getLastUpdate(), after);
105
* Verify that the "freshness detection" function properly
107
public void testNeedsRefresh() {
108
// Set the entry content so it shouldn't need refresh
109
// Invoke needsRefresh with no delay, so it should return true.
110
// Then invoke it with a big delay, so it should return false
111
assertTrue(entry.needsRefresh(REFRESH_NEEDED));
112
assertTrue(!entry.needsRefresh(NO_REFRESH_NEEDED));
116
* Set the content of the item created by setup and then retrieve it and
119
public void testSetGetContent() {
120
entry.setContent(CONTENT);
121
assertTrue(CONTENT.equals(entry.getContent()));
123
// Ensure that nulls are allowed
124
entry.setContent(null);
125
assertNull(entry.getContent());
129
* Ensure that a value is between two others. Since the execution may be
130
* very fast, equals values are also considered to be between
132
private void assertBetweenOrEquals(long first, long between, long last) {
133
assertTrue(between >= first);
134
assertTrue(between <= last);