1 5 package com.opensymphony.oscache.base; 6 7 import junit.framework.Test; 8 import junit.framework.TestCase; 9 import junit.framework.TestSuite; 10 11 18 public class TestCacheEntry extends TestCase { 19 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 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 35 public TestCacheEntry(String str) { 36 super(str); 37 } 38 39 43 public void setUp() { 44 if (entry == null) { 46 beforeCreation = System.currentTimeMillis(); 49 50 entry = new CacheEntry(ENTRY_KEY); 51 afterCreation = System.currentTimeMillis(); 52 } 53 } 54 55 60 public static Test suite() { 61 return new TestSuite(TestCacheEntry.class); 62 } 63 64 67 public void testFlush() { 68 entry.setContent(CONTENT); 70 assertTrue(!entry.needsRefresh(NO_REFRESH_NEEDED)); 71 72 entry.flush(); 74 assertTrue(entry.needsRefresh(NO_REFRESH_NEEDED)); 75 } 76 77 80 public void testGetCreated() { 81 assertBetweenOrEquals(beforeCreation, entry.getCreated(), afterCreation); 82 } 83 84 87 public void testGetKey() { 88 assertTrue(entry.getKey().equals(ENTRY_KEY)); 89 } 90 91 95 public void testGetLastUpdate() { 96 long before = System.currentTimeMillis(); 98 entry.setContent(CONTENT); 99 100 long after = System.currentTimeMillis(); 101 assertBetweenOrEquals(before, entry.getLastUpdate(), after); 102 } 103 104 107 public void testNeedsRefresh() { 108 assertTrue(entry.needsRefresh(REFRESH_NEEDED)); 112 assertTrue(!entry.needsRefresh(NO_REFRESH_NEEDED)); 113 } 114 115 119 public void testSetGetContent() { 120 entry.setContent(CONTENT); 121 assertTrue(CONTENT.equals(entry.getContent())); 122 123 entry.setContent(null); 125 assertNull(entry.getContent()); 126 } 127 128 132 private void assertBetweenOrEquals(long first, long between, long last) { 133 assertTrue(between >= first); 134 assertTrue(between <= last); 135 } 136 } 137 | Popular Tags |