KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > TestLRUMap


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import junit.framework.Test;
25
26 /**
27  * Tests LRUMap.
28  *
29  * @version $Revision: 1.29 $ $Date: 2004/02/18 01:20:35 $
30  *
31  * @author James Strachan
32  * @author Morgan Delagrange
33  * @author Stephen Colebourne
34  */

35 public class TestLRUMap extends TestSequencedHashMap {
36     
37     public TestLRUMap(String JavaDoc testName) {
38         super(testName);
39     }
40
41     public static Test suite() {
42         return BulkTest.makeSuite(TestLRUMap.class);
43     }
44
45     public static void main(String JavaDoc args[]) {
46         String JavaDoc[] testCaseName = { TestLRUMap.class.getName() };
47         junit.textui.TestRunner.main(testCaseName);
48     }
49
50     //-----------------------------------------------------------------------
51
public Map JavaDoc makeEmptyMap() {
52         LRUMap map = new LRUMap();
53         return map;
54     }
55
56     /**
57      * Override as test uses iterator() and getKey() in combination which doesn't work.
58      */

59     public String JavaDoc[] ignoredTests() {
60         return new String JavaDoc[] {"TestLRUMap.bulkTestMapEntrySet.testMapEntrySetIteratorEntry"};
61     }
62
63     //-----------------------------------------------------------------------
64
public void testRemoveLRU() {
65         LRUMap map2 = new LRUMap(3);
66         map2.put(new Integer JavaDoc(1),"foo");
67         map2.put(new Integer JavaDoc(2),"foo");
68         map2.put(new Integer JavaDoc(3),"foo");
69         map2.put(new Integer JavaDoc(4),"foo"); // removes 1 since max size exceeded
70
map2.removeLRU(); // should be Integer(2)
71

72         assertTrue("Second to last value should exist",map2.get(new Integer JavaDoc(3)).equals("foo"));
73         assertTrue("First value inserted should not exist", map2.get(new Integer JavaDoc(1)) == null);
74     }
75
76     public void testMultiplePuts() {
77         LRUMap map2 = new LRUMap(2);
78         map2.put(new Integer JavaDoc(1),"foo");
79         map2.put(new Integer JavaDoc(2),"bar");
80         map2.put(new Integer JavaDoc(3),"foo");
81         map2.put(new Integer JavaDoc(4),"bar");
82
83         assertTrue("last value should exist",map2.get(new Integer JavaDoc(4)).equals("bar"));
84         assertTrue("LRU should not exist", map2.get(new Integer JavaDoc(1)) == null);
85     }
86
87     /**
88      * Confirm that putAll(Map) does not cause the LRUMap
89      * to exceed its maxiumum size.
90      */

91     public void testPutAll() {
92         LRUMap map2 = new LRUMap(3);
93         map2.put(new Integer JavaDoc(1),"foo");
94         map2.put(new Integer JavaDoc(2),"foo");
95         map2.put(new Integer JavaDoc(3),"foo");
96
97         HashMap JavaDoc hashMap = new HashMap JavaDoc();
98         hashMap.put(new Integer JavaDoc(4),"foo");
99
100         map2.putAll(hashMap);
101
102         assertTrue("max size is 3, but actual size is " + map2.size(),
103                    map2.size() == 3);
104         assertTrue("map should contain the Integer(4) object",
105                    map2.containsKey(new Integer JavaDoc(4)));
106     }
107
108     /**
109      * Test that the size of the map is reduced immediately
110      * when setMaximumSize(int) is called
111      */

112     public void testSetMaximumSize() {
113         LRUMap map = new LRUMap(6);
114         map.put("1","1");
115         map.put("2","2");
116         map.put("3","3");
117         map.put("4","4");
118         map.put("5","5");
119         map.put("6","6");
120         map.setMaximumSize(3);
121
122         assertTrue("map should have size = 3, but actually = " + map.size(),
123                    map.size() == 3);
124     }
125
126     public void testGetPromotion() {
127         LRUMap map = new LRUMap(3);
128         map.put("1","1");
129         map.put("2","2");
130         map.put("3","3");
131         // LRU is now 1 (then 2 then 3)
132

133         // promote 1 to top
134
// eviction order is now 2,3,1
135
map.get("1");
136         
137         // add another value, forcing a remove
138
// 2 should be evicted (then 3,1,4)
139
map.put("4","4");
140         
141         Iterator JavaDoc keyIterator = map.keySet().iterator();
142         Object JavaDoc[] keys = new Object JavaDoc[3];
143         for (int i = 0; keyIterator.hasNext() ; ++i) {
144             keys[i] = keyIterator.next();
145         }
146
147         assertTrue("first evicted should be 3, was " + keys[0], keys[0].equals("3"));
148         assertTrue("second evicted should be 1, was " + keys[1], keys[1].equals("1"));
149         assertTrue("third evicted should be 4, was " + keys[2], keys[2].equals("4"));
150         
151     }
152
153     /**
154      * You should be able to subclass LRUMap and perform a
155      * custom action when items are removed automatically
156      * by the LRU algorithm (the removeLRU() method).
157      */

158     public void testLRUSubclass() {
159         LRUCounter counter = new LRUCounter(3);
160         // oldest <--> newest
161
// 1
162
counter.put("1","foo");
163         // 1 2
164
counter.put("2","foo");
165         // 1 2 3
166
counter.put("3","foo");
167         // 2 3 1
168
counter.put("1","foo");
169         // 3 1 4 (2 goes out)
170
counter.put("4","foo");
171         // 1 4 5 (3 goes out)
172
counter.put("5","foo");
173         // 4 5 2 (1 goes out)
174
counter.put("2","foo");
175         // 4 2
176
counter.remove("5");
177
178         assertTrue("size should be 2, but was " + counter.size(), counter.size() == 2);
179         assertTrue("removedCount should be 3 but was " + counter.removedCount,
180                    counter.removedCount == 3);
181
182         assertTrue("first removed was '2'",counter.list.get(0).equals("2"));
183         assertTrue("second removed was '3'",counter.list.get(1).equals("3"));
184         assertTrue("third removed was '1'",counter.list.get(2).equals("1"));
185
186         assertTrue("oldest key is '4'",counter.get(0).equals("4"));
187         assertTrue("newest key is '2'",counter.get(1).equals("2"));
188     }
189
190
191     protected void entrySetEqualsMap(Set JavaDoc set, Map JavaDoc m) {
192         // Overridden because LRUMap.get(Object) actually alters the map,
193
// so there's no way to verify that the entry set and map contain
194
// the same entries
195
}
196
197     private class LRUCounter extends LRUMap {
198         int removedCount = 0;
199         ArrayList JavaDoc list = new ArrayList JavaDoc(3);
200
201         LRUCounter(int i) {
202             super(i);
203         }
204
205         protected void processRemovedLRU(Object JavaDoc key, Object JavaDoc value) {
206             ++removedCount;
207             list.add(key);
208         }
209     }
210
211 }
212
Popular Tags