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

« back to all changes in this revision

Viewing changes to src/core/test/com/opensymphony/oscache/base/TestCacheEntry.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;
 
6
 
 
7
import junit.framework.Test;
 
8
import junit.framework.TestCase;
 
9
import junit.framework.TestSuite;
 
10
 
 
11
/**
 
12
 * Test the public methods of the CacheEntry class
 
13
 *
 
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>
 
17
 */
 
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";
 
24
 
 
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;
 
29
 
 
30
    /**
 
31
     * Class constructor
 
32
     * <p>
 
33
     * @param str The test name (required by JUnit)
 
34
     */
 
35
    public TestCacheEntry(String str) {
 
36
        super(str);
 
37
    }
 
38
 
 
39
    /**
 
40
     * This method is invoked before each testXXXX methods of the
 
41
     * class. It set ups the variables required for each tests.
 
42
     */
 
43
    public void setUp() {
 
44
        // At first invocation, create a cache entry object
 
45
        if (entry == null) {
 
46
            // Log the time before and after to verify the creation time
 
47
            // in one of the tests
 
48
            beforeCreation = System.currentTimeMillis();
 
49
 
 
50
            entry = new CacheEntry(ENTRY_KEY);
 
51
            afterCreation = System.currentTimeMillis();
 
52
        }
 
53
    }
 
54
 
 
55
    /**
 
56
     * This methods returns the name of this test class to JUnit
 
57
     * <p>
 
58
     * @return The name of this class
 
59
     */
 
60
    public static Test suite() {
 
61
        return new TestSuite(TestCacheEntry.class);
 
62
    }
 
63
 
 
64
    /**
 
65
     * Verify the flush
 
66
     */
 
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));
 
71
 
 
72
        // Flush the entry. It should now needs refresh
 
73
        entry.flush();
 
74
        assertTrue(entry.needsRefresh(NO_REFRESH_NEEDED));
 
75
    }
 
76
 
 
77
    /**
 
78
     * Verify that the creation time is correct
 
79
     */
 
80
    public void testGetCreated() {
 
81
        assertBetweenOrEquals(beforeCreation, entry.getCreated(), afterCreation);
 
82
    }
 
83
 
 
84
    /**
 
85
     * Retrieve the item created by the setup
 
86
     */
 
87
    public void testGetKey() {
 
88
        assertTrue(entry.getKey().equals(ENTRY_KEY));
 
89
    }
 
90
 
 
91
    /**
 
92
     * Verify that the last modification time is between the time before and
 
93
     * after the alteration of the item
 
94
     */
 
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);
 
99
 
 
100
        long after = System.currentTimeMillis();
 
101
        assertBetweenOrEquals(before, entry.getLastUpdate(), after);
 
102
    }
 
103
 
 
104
    /**
 
105
     * Verify that the "freshness detection" function properly
 
106
     */
 
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));
 
113
    }
 
114
 
 
115
    /**
 
116
     * Set the content of the item created by setup and then retrieve it and
 
117
     * validate it
 
118
     */
 
119
    public void testSetGetContent() {
 
120
        entry.setContent(CONTENT);
 
121
        assertTrue(CONTENT.equals(entry.getContent()));
 
122
 
 
123
        // Ensure that nulls are allowed
 
124
        entry.setContent(null);
 
125
        assertNull(entry.getContent());
 
126
    }
 
127
 
 
128
    /**
 
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
 
131
     */
 
132
    private void assertBetweenOrEquals(long first, long between, long last) {
 
133
        assertTrue(between >= first);
 
134
        assertTrue(between <= last);
 
135
    }
 
136
}