KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > cache > test > LRUCacheTestCase


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

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 /**
14  * TestCase for LRUCache.
15  *
16  * @author <a HREF="mailto:colus@apache.org">Eung-ju Park</a>
17  */

18 public class LRUCacheTestCase
19     extends AbstractCacheTestCase
20 {
21     public LRUCacheTestCase( final String JavaDoc 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 JavaDoc KEY1 = "key 1";
32     private static final String JavaDoc VALUE1 = "value 1";
33     private static final String JavaDoc VALUE1_1 = "value 1-1";
34     private static final String JavaDoc KEY2 = "key 2";
35     private static final String JavaDoc VALUE2 = "value 2";
36     private static final String JavaDoc KEY3 = "key 3";
37     private static final String JavaDoc 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