001: package org.shiftone.cache.policy.lru;
002:
003: import org.shiftone.cache.CacheFactory;
004: import org.shiftone.cache.util.TestCaseBase;
005:
006: /**
007: * Class LruCacheTestCase
008: *
009: *
010: * @author <a href="mailto:jeff@shiftone.org">Jeff Drost</a>
011: * @version $Revision: 1.2 $
012: */
013: public class LruCacheTestCase extends TestCaseBase {
014:
015: public LruCacheTestCase(String name) {
016: super (name);
017: }
018:
019: /**
020: * Method getCacheFactory
021: */
022: public CacheFactory getCacheFactory() {
023: return new LruCacheFactory();
024: }
025:
026: /**
027: * Method testLru5
028: */
029: public void testLru5() {
030:
031: LruCache cache = new LruCache("lru", 5000, 5);
032:
033: cache.addObject("A", new Integer(123456789));
034: cache.addObject("B", "123456789");
035: cache.addObject("C", new Long(123456789));
036: cache.addObject("D", Boolean.TRUE);
037: cache.addObject("E", new Float(1.2345));
038: assertEquals("EDCBA", cache.dumpFifoKeys());
039: assertEquals("EDCBA", cache.dumpLruKeys());
040: assertNotNull(cache.getObject("C"));
041: assertEquals("EDCBA", cache.dumpFifoKeys());
042: assertEquals("CEDBA", cache.dumpLruKeys());
043: cache.addObject("F", new Object());
044: assertEquals("FEDCB", cache.dumpFifoKeys());
045: assertEquals("FCEDB", cache.dumpLruKeys());
046: }
047:
048: /**
049: * Method testReValue
050: */
051: public void testLru10() {
052:
053: LruCache cache = new LruCache("lru", 5000, 10);
054:
055: cache.addObject("A", "A"); //1
056: assertEquals("A", cache.dumpLruKeys());
057: assertEquals("A", cache.dumpFifoKeys());
058: cache.addObject("B", "B"); //2
059: assertEquals("BA", cache.dumpLruKeys());
060: assertEquals("BA", cache.dumpFifoKeys());
061: cache.addObject("C", "C"); //3
062: assertEquals("CBA", cache.dumpLruKeys());
063: assertEquals("CBA", cache.dumpFifoKeys());
064: cache.addObject("D", "D"); //4
065: assertEquals("DCBA", cache.dumpLruKeys());
066: assertEquals("DCBA", cache.dumpFifoKeys());
067: cache.addObject("E", "E"); //5
068: assertEquals("EDCBA", cache.dumpLruKeys());
069: assertEquals("EDCBA", cache.dumpFifoKeys());
070: cache.addObject("F", "F"); //6
071: assertEquals("FEDCBA", cache.dumpLruKeys());
072: assertEquals("FEDCBA", cache.dumpFifoKeys());
073: cache.addObject("G", "G"); //7
074: assertEquals("GFEDCBA", cache.dumpLruKeys());
075: assertEquals("GFEDCBA", cache.dumpFifoKeys());
076: cache.addObject("H", "H"); //8
077: assertEquals("HGFEDCBA", cache.dumpLruKeys());
078: assertEquals("HGFEDCBA", cache.dumpFifoKeys());
079: cache.addObject("I", "I"); //9
080: assertEquals("IHGFEDCBA", cache.dumpLruKeys());
081: assertEquals("IHGFEDCBA", cache.dumpFifoKeys());
082: cache.addObject("J", "J"); //10
083: assertEquals("JIHGFEDCBA", cache.dumpLruKeys());
084: assertEquals("JIHGFEDCBA", cache.dumpFifoKeys());
085:
086: // this should bump out A
087: cache.addObject("K", "K"); //11
088: assertEquals("KJIHGFEDCB", cache.dumpLruKeys());
089: assertEquals("KJIHGFEDCB", cache.dumpFifoKeys());
090:
091: // observe the effect of getObject
092: assertNotNull(cache.getObject("E"));
093: assertEquals("EKJIHGFDCB", cache.dumpLruKeys());
094: assertEquals("KJIHGFEDCB", cache.dumpFifoKeys());
095: assertNotNull(cache.getObject("G"));
096: assertEquals("GEKJIHFDCB", cache.dumpLruKeys());
097: assertEquals("KJIHGFEDCB", cache.dumpFifoKeys());
098: assertNotNull(cache.getObject("C"));
099: assertEquals("CGEKJIHFDB", cache.dumpLruKeys());
100: assertEquals("KJIHGFEDCB", cache.dumpFifoKeys());
101: assertNotNull(cache.getObject("E"));
102: assertEquals("ECGKJIHFDB", cache.dumpLruKeys());
103: assertEquals("KJIHGFEDCB", cache.dumpFifoKeys());
104: assertNotNull(cache.getObject("J"));
105: assertEquals("JECGKIHFDB", cache.dumpLruKeys());
106: assertEquals("KJIHGFEDCB", cache.dumpFifoKeys());
107: assertNotNull(cache.getObject("J"));
108: assertEquals("JECGKIHFDB", cache.dumpLruKeys());
109: assertEquals("KJIHGFEDCB", cache.dumpFifoKeys());
110: assertNotNull(cache.getObject("E"));
111: assertEquals("EJCGKIHFDB", cache.dumpLruKeys());
112: assertEquals("KJIHGFEDCB", cache.dumpFifoKeys());
113: assertNotNull(cache.getObject("B"));
114: assertEquals("BEJCGKIHFD", cache.dumpLruKeys());
115: assertEquals("KJIHGFEDCB", cache.dumpFifoKeys());
116: assertNotNull(cache.getObject("F"));
117: assertEquals("FBEJCGKIHD", cache.dumpLruKeys());
118: assertEquals("KJIHGFEDCB", cache.dumpFifoKeys());
119: }
120: }
|