KickJava   Java API By Example, From Geeks To Geeks.

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


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.TimeMapLRUCache;
12
13 /**
14  * TestCase for TimeMapLRUCache
15
16  * @author <a HREF="alag@users.sourceforge.net">Alexis Agahi</a>
17  */

18 public class TimeMapLRUCacheTestCase
19     extends AbstractCacheTestCase
20 {
21     public TimeMapLRUCacheTestCase( final String JavaDoc name )
22     {
23         super( name );
24     }
25
26     protected void setUp()
27     {
28         m_cache = new TimeMapLRUCache( 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     private static final String JavaDoc KEY4 = "key 4";
39     private static final String JavaDoc VALUE4 = "value 4";
40
41     public void testPutGet()
42     {
43
44         final Cache cache = new TimeMapLRUCache( 3 );
45
46         assertTrue( !cache.containsKey( KEY1 ) );
47         assertNull( cache.put( KEY1, VALUE1 ) );
48         assertTrue( cache.containsKey( KEY1 ) );
49         assertEquals( VALUE1, cache.get( KEY1 ) );
50
51         assertTrue( !cache.containsKey( KEY2 ) );
52         assertNull( cache.put( KEY2, VALUE2 ) );
53         assertTrue( cache.containsKey( KEY2 ) );
54         assertEquals( VALUE2, cache.get( KEY2 ) );
55
56         assertTrue( !cache.containsKey( KEY3 ) );
57         assertNull( cache.put( KEY3, VALUE3 ) );
58         assertTrue( cache.containsKey( KEY3 ) );
59         assertEquals( VALUE3, cache.get( KEY3 ) );
60
61         assertTrue( cache.containsKey( KEY1 ) );
62         assertEquals( VALUE1, cache.put( KEY1, VALUE1_1 ) );
63         assertTrue( cache.containsKey( KEY1 ) );
64         assertEquals( VALUE1_1, cache.get( KEY1 ) );
65
66         cache.clear();
67         assertEquals( 0, cache.size() );
68         assertTrue( !cache.containsKey( KEY1 ) );
69         assertTrue( !cache.containsKey( KEY2 ) );
70         assertTrue( !cache.containsKey( KEY3 ) );
71
72         assertNull( cache.put( KEY1, VALUE1 ) );
73         assertNull( cache.put( KEY2, VALUE2 ) );
74         assertNull( cache.put( KEY3, VALUE3 ) );
75         assertNull( cache.put( KEY4, VALUE4 ) );
76         assertTrue( !cache.containsKey( KEY1 ) );
77         assertNull( cache.put( KEY1, VALUE1 ) );
78         assertTrue( !cache.containsKey( KEY2 ) );
79         assertNull( cache.put( KEY2, VALUE2 ) );
80         assertTrue( !cache.containsKey( KEY3 ) );
81
82         cache.clear();
83         assertNull( cache.put( KEY1, VALUE1 ) );
84         assertNull( cache.put( KEY2, VALUE2 ) );
85         assertNull( cache.put( KEY3, VALUE3 ) );
86         cache.get( KEY1 );
87         assertNull( cache.put( KEY4, VALUE4 ) );
88         assertTrue( cache.containsKey( KEY1 ) );
89         assertTrue( !cache.containsKey( KEY2 ) );
90         cache.clear();
91
92     }
93
94     public void testRemove()
95     {
96
97         final Cache cache = new TimeMapLRUCache( 3 );
98
99         assertTrue( !cache.containsKey( KEY1 ) );
100         assertNull( cache.put( KEY1, VALUE1 ) );
101         assertTrue( cache.containsKey( KEY1 ) );
102         assertEquals( VALUE1, cache.get( KEY1 ) );
103
104         assertTrue( !cache.containsKey( KEY2 ) );
105         assertNull( cache.put( KEY2, VALUE2 ) );
106         assertTrue( cache.containsKey( KEY2 ) );
107         assertEquals( VALUE2, cache.get( KEY2 ) );
108
109         assertTrue( !cache.containsKey( KEY3 ) );
110         assertNull( cache.put( KEY3, VALUE3 ) );
111         assertTrue( cache.containsKey( KEY3 ) );
112         assertEquals( VALUE3, cache.get( KEY3 ) );
113
114         assertEquals( VALUE1, cache.remove( KEY1 ) );
115         assertTrue( !cache.containsKey( KEY1 ) );
116         assertTrue( cache.containsKey( KEY2 ) );
117         assertTrue( cache.containsKey( KEY3 ) );
118
119         assertEquals( VALUE2, cache.remove( KEY2 ) );
120         assertTrue( !cache.containsKey( KEY1 ) );
121         assertTrue( !cache.containsKey( KEY2 ) );
122         assertTrue( cache.containsKey( KEY3 ) );
123
124         assertEquals( VALUE3, cache.remove( KEY3 ) );
125         assertTrue( !cache.containsKey( KEY1 ) );
126         assertTrue( !cache.containsKey( KEY2 ) );
127         assertTrue( !cache.containsKey( KEY3 ) );
128
129         cache.clear();
130         assertEquals( 0, cache.size() );
131         assertTrue( !cache.containsKey( KEY1 ) );
132         assertTrue( !cache.containsKey( KEY2 ) );
133         assertTrue( !cache.containsKey( KEY3 ) );
134     }
135 }
136
Popular Tags