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