1 8 package org.apache.avalon.excalibur.cache.test; 9 10 import org.apache.avalon.excalibur.cache.Cache; 11 import org.apache.avalon.excalibur.cache.LRUCache; 12 13 18 public class LRUCacheTestCase 19 extends AbstractCacheTestCase 20 { 21 public LRUCacheTestCase( final String name ) 22 { 23 super( name ); 24 } 25 26 protected void setUp() 27 { 28 m_cache = new LRUCache( 10 ); 29 } 30 31 private static final String KEY1 = "key 1"; 32 private static final String VALUE1 = "value 1"; 33 private static final String VALUE1_1 = "value 1-1"; 34 private static final String KEY2 = "key 2"; 35 private static final String VALUE2 = "value 2"; 36 private static final String KEY3 = "key 3"; 37 private static final String VALUE3 = "value 3"; 38 39 public void testPutGet() 40 { 41 final Cache cache = new LRUCache( 3 ); 42 43 assertTrue( !cache.containsKey( KEY1 ) ); 44 assertNull( cache.put( KEY1, VALUE1 ) ); 45 assertTrue( cache.containsKey( KEY1 ) ); 46 assertEquals( VALUE1, cache.get( KEY1 ) ); 47 48 assertTrue( !cache.containsKey( KEY2 ) ); 49 assertNull( cache.put( KEY2, VALUE2 ) ); 50 assertTrue( cache.containsKey( KEY2 ) ); 51 assertEquals( VALUE2, cache.get( KEY2 ) ); 52 53 assertTrue( !cache.containsKey( KEY3 ) ); 54 assertNull( cache.put( KEY3, VALUE3 ) ); 55 assertTrue( cache.containsKey( KEY3 ) ); 56 assertEquals( VALUE3, cache.get( KEY3 ) ); 57 58 assertTrue( cache.containsKey( KEY1 ) ); 59 assertEquals( VALUE1, cache.put( KEY1, VALUE1_1 ) ); 60 assertTrue( cache.containsKey( KEY1 ) ); 61 assertEquals( VALUE1_1, cache.get( KEY1 ) ); 62 63 cache.clear(); 64 assertEquals( 0, cache.size() ); 65 assertTrue( !cache.containsKey( KEY1 ) ); 66 assertTrue( !cache.containsKey( KEY2 ) ); 67 assertTrue( !cache.containsKey( KEY3 ) ); 68 } 69 70 public void testRemove() 71 { 72 final Cache cache = new LRUCache( 3 ); 73 74 assertTrue( !cache.containsKey( KEY1 ) ); 75 assertNull( cache.put( KEY1, VALUE1 ) ); 76 assertTrue( cache.containsKey( KEY1 ) ); 77 assertEquals( VALUE1, cache.get( KEY1 ) ); 78 79 assertTrue( !cache.containsKey( KEY2 ) ); 80 assertNull( cache.put( KEY2, VALUE2 ) ); 81 assertTrue( cache.containsKey( KEY2 ) ); 82 assertEquals( VALUE2, cache.get( KEY2 ) ); 83 84 assertTrue( !cache.containsKey( KEY3 ) ); 85 assertNull( cache.put( KEY3, VALUE3 ) ); 86 assertTrue( cache.containsKey( KEY3 ) ); 87 assertEquals( VALUE3, cache.get( KEY3 ) ); 88 89 assertEquals( VALUE1, cache.remove( KEY1 ) ); 90 assertTrue( !cache.containsKey( KEY1 ) ); 91 assertTrue( cache.containsKey( KEY2 ) ); 92 assertTrue( cache.containsKey( KEY3 ) ); 93 94 assertEquals( VALUE2, cache.remove( KEY2 ) ); 95 assertTrue( !cache.containsKey( KEY1 ) ); 96 assertTrue( !cache.containsKey( KEY2 ) ); 97 assertTrue( cache.containsKey( KEY3 ) ); 98 99 assertEquals( VALUE3, cache.remove( KEY3 ) ); 100 assertTrue( !cache.containsKey( KEY1 ) ); 101 assertTrue( !cache.containsKey( KEY2 ) ); 102 assertTrue( !cache.containsKey( KEY3 ) ); 103 104 cache.clear(); 105 assertEquals( 0, cache.size() ); 106 assertTrue( !cache.containsKey( KEY1 ) ); 107 assertTrue( !cache.containsKey( KEY2 ) ); 108 assertTrue( !cache.containsKey( KEY3 ) ); 109 } 110 } 111 | Popular Tags |