001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.oscache.base;
006:
007: import junit.framework.Test;
008: import junit.framework.TestCase;
009: import junit.framework.TestSuite;
010:
011: /**
012: * Test the public methods of the CacheEntry class
013: *
014: * $Id: TestCacheEntry.java 254 2005-06-17 05:07:38Z dres $
015: * @version $Revision: 254 $
016: * @author <a href="mailto:abergevin@pyxis-tech.com">Alain Bergevin</a>
017: */
018: public class TestCacheEntry extends TestCase {
019: // Static variables required thru the tests
020: static CacheEntry entry = null;
021: static long beforeCreation = 0;
022: static long afterCreation = 0;
023: private final String CONTENT = "Content for the cache entry test";
024:
025: // Constants used thru the tests
026: private final String ENTRY_KEY = "Test cache entry key";
027: private final int NO_REFRESH_NEEDED = 1000000;
028: private final int REFRESH_NEEDED = 0;
029:
030: /**
031: * Class constructor
032: * <p>
033: * @param str The test name (required by JUnit)
034: */
035: public TestCacheEntry(String str) {
036: super (str);
037: }
038:
039: /**
040: * This method is invoked before each testXXXX methods of the
041: * class. It set ups the variables required for each tests.
042: */
043: public void setUp() {
044: // At first invocation, create a cache entry object
045: if (entry == null) {
046: // Log the time before and after to verify the creation time
047: // in one of the tests
048: beforeCreation = System.currentTimeMillis();
049:
050: entry = new CacheEntry(ENTRY_KEY);
051: afterCreation = System.currentTimeMillis();
052: }
053: }
054:
055: /**
056: * This methods returns the name of this test class to JUnit
057: * <p>
058: * @return The name of this class
059: */
060: public static Test suite() {
061: return new TestSuite(TestCacheEntry.class);
062: }
063:
064: /**
065: * Verify the flush
066: */
067: public void testFlush() {
068: // Set the content so it shouldn't need refresh
069: entry.setContent(CONTENT);
070: assertTrue(!entry.needsRefresh(NO_REFRESH_NEEDED));
071:
072: // Flush the entry. It should now needs refresh
073: entry.flush();
074: assertTrue(entry.needsRefresh(NO_REFRESH_NEEDED));
075: }
076:
077: /**
078: * Verify that the creation time is correct
079: */
080: public void testGetCreated() {
081: assertBetweenOrEquals(beforeCreation, entry.getCreated(),
082: afterCreation);
083: }
084:
085: /**
086: * Retrieve the item created by the setup
087: */
088: public void testGetKey() {
089: assertTrue(entry.getKey().equals(ENTRY_KEY));
090: }
091:
092: /**
093: * Verify that the last modification time is between the time before and
094: * after the alteration of the item
095: */
096: public void testGetLastUpdate() {
097: // again. Then we ensure that the update time is between our timestamps
098: long before = System.currentTimeMillis();
099: entry.setContent(CONTENT);
100:
101: long after = System.currentTimeMillis();
102: assertBetweenOrEquals(before, entry.getLastUpdate(), after);
103: }
104:
105: /**
106: * Verify that the "freshness detection" function properly
107: */
108: public void testNeedsRefresh() {
109: // Set the entry content so it shouldn't need refresh
110: // Invoke needsRefresh with no delay, so it should return true.
111: // Then invoke it with a big delay, so it should return false
112: assertTrue(entry.needsRefresh(REFRESH_NEEDED));
113: assertTrue(!entry.needsRefresh(NO_REFRESH_NEEDED));
114: }
115:
116: /**
117: * Set the content of the item created by setup and then retrieve it and
118: * validate it
119: */
120: public void testSetGetContent() {
121: entry.setContent(CONTENT);
122: assertTrue(CONTENT.equals(entry.getContent()));
123:
124: // Ensure that nulls are allowed
125: entry.setContent(null);
126: assertNull(entry.getContent());
127: }
128:
129: /**
130: * Ensure that a value is between two others. Since the execution may be
131: * very fast, equals values are also considered to be between
132: */
133: private void assertBetweenOrEquals(long first, long between,
134: long last) {
135: assertTrue(between >= first);
136: assertTrue(between <= last);
137: }
138: }
|