01: package org.shiftone.cache.test;
02:
03: import junit.framework.TestCase;
04:
05: import org.shiftone.cache.Cache;
06: import org.shiftone.cache.adaptor.MapCache;
07: import org.shiftone.cache.decorator.soft.SoftCache;
08:
09: import java.lang.ref.Reference;
10:
11: import java.util.ArrayList;
12: import java.util.Hashtable;
13: import java.util.List;
14:
15: public class SoftCacheTestCase extends TestCase {
16:
17: public void testSimple() throws Exception {
18:
19: Cache cache1 = new MapCache(new Hashtable());
20: Cache cache2 = new SoftCache(cache1);
21:
22: cache2.addObject("key", "value");
23: assertEquals("value", cache2.getObject("key"));
24: }
25:
26: public void testReference() throws Exception {
27:
28: Object inObject = createBigObject();
29: Object outObject;
30: MapCache mapCache;
31: SoftCache softCache;
32:
33: mapCache = new MapCache(new Hashtable());
34: softCache = new SoftCache(mapCache);
35:
36: softCache.addObject("key", inObject);
37:
38: outObject = mapCache.getObject("key");
39:
40: assertTrue("outObject instanceof Reference",
41: outObject instanceof Reference);
42: assertTrue("cache2.getObject('key') == inObject", softCache
43: .getObject("key") == inObject);
44:
45: outObject = null;
46: inObject = null;
47:
48: for (int i = 0; i < 10; i++) {
49: Runtime.getRuntime().gc();
50: Runtime.getRuntime().runFinalization();
51: Runtime.getRuntime().gc();
52: softCache.addObject(new Integer(i), createBigObject());
53:
54: //softCache.removeExpiredElements();
55: Thread.sleep(500);
56: }
57: }
58:
59: public Object createBigObject() {
60:
61: List list = new ArrayList();
62:
63: for (int i = 0; i < 500000; i++) {
64: list.add(Integer.toOctalString(i));
65: }
66:
67: return list;
68: }
69: }
|