1 package org.apache.commons.jcs.engine.memory.mru;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import junit.framework.TestCase;
23 import org.apache.commons.jcs.JCS;
24 import org.apache.commons.jcs.access.CacheAccess;
25 import org.apache.commons.jcs.access.exception.CacheException;
26 import org.apache.commons.jcs.engine.CacheElement;
27 import org.apache.commons.jcs.engine.behavior.ICacheElement;
28 import org.apache.commons.jcs.engine.control.CompositeCache;
29 import org.apache.commons.jcs.engine.control.CompositeCacheManager;
30
31 import java.util.HashSet;
32 import java.util.Map;
33 import java.util.Set;
34
35
36
37
38
39
40 public class MRUMemoryCacheUnitTest
41 extends TestCase
42 {
43
44 @Override
45 public void setUp()
46 {
47 JCS.setConfigFilename( "/TestMRUCache.ccf" );
48 }
49
50
51
52
53
54
55
56 public void testLoadFromCCF()
57 throws CacheException
58 {
59 CacheAccess<String, String> cache = JCS.getInstance( "testPutGet" );
60 String memoryCacheName = cache.getCacheAttributes().getMemoryCacheName();
61 assertTrue( "Cache name should have MRU in it.", memoryCacheName.indexOf( "MRUMemoryCache" ) != -1 );
62 }
63
64
65
66
67
68
69 public void testPutGetThroughHub()
70 throws CacheException
71 {
72 CacheAccess<String, String> cache = JCS.getInstance( "testPutGetThroughHub" );
73
74 int max = cache.getCacheAttributes().getMaxObjects();
75 int items = max * 2;
76
77 for ( int i = 0; i < items; i++ )
78 {
79 cache.put( i + ":key", "myregion" + " data " + i );
80 }
81
82
83 for ( int i = max -1; i >= 0; i-- )
84 {
85 String value = cache.get( i + ":key" );
86 assertNull( "Should not have value for key [" + i + ":key" + "] in the cache." + cache.getStats(), value );
87 }
88
89
90
91 for ( int i = max + 2; i < items; i++ )
92 {
93 String value = cache.get( i + ":key" );
94 assertEquals( "myregion" + " data " + i, value );
95 }
96
97
98 Set<String> keys = new HashSet<String>();
99 for ( int i = 0; i < items; i++ )
100 {
101 keys.add( i + ":key" );
102 }
103
104 Map<String, ICacheElement<String, String>> elements = cache.getCacheElements( keys );
105 for ( int i = max-1; i >= 0; i-- )
106 {
107 assertNull( "Should not have value for key [" + i + ":key" + "] in the cache." + cache.getStats(), elements.get( i + ":key" ) );
108 }
109 for ( int i = max + 2; i < items; i++ )
110 {
111 ICacheElement<String, String> element = elements.get( i + ":key" );
112 assertNotNull( "element " + i + ":key is missing", element );
113 assertEquals( "value " + i + ":key", "myregion" + " data " + i, element.getVal() );
114 }
115 }
116
117
118
119
120
121
122 public void testPutGetThroughHubTwice()
123 throws CacheException
124 {
125 CacheAccess<String, String> cache = JCS.getInstance( "testPutGetThroughHub" );
126
127 int max = cache.getCacheAttributes().getMaxObjects();
128 int items = max * 2;
129
130 for ( int i = 0; i < items; i++ )
131 {
132 cache.put( i + ":key", "myregion" + " data " + i );
133 }
134
135 for ( int i = 0; i < items; i++ )
136 {
137 cache.put( i + ":key", "myregion" + " data " + i );
138 }
139
140
141 for ( int i = max-1; i >= 0; i-- )
142 {
143 String value = cache.get( i + ":key" );
144 assertNull( "Should not have value for key [" + i + ":key" + "] in the cache.", value );
145 }
146
147
148
149 for ( int i = max + 2; i < items; i++ )
150 {
151 String value = cache.get( i + ":key" );
152 assertEquals( "myregion" + " data " + i, value );
153 }
154
155 }
156
157
158
159
160
161
162 public void testPutRemoveThroughHub()
163 throws CacheException
164 {
165 CacheAccess<String, String> cache = JCS.getInstance( "testPutGetThroughHub" );
166
167 int max = cache.getCacheAttributes().getMaxObjects();
168 int items = max * 2;
169
170 for ( int i = 0; i < items; i++ )
171 {
172 cache.put( i + ":key", "myregion" + " data " + i );
173 }
174
175 for ( int i = 0; i < items; i++ )
176 {
177 cache.remove( i + ":key" );
178 }
179
180
181 for ( int i = max; i >= 0; i-- )
182 {
183 String value = cache.get( i + ":key" );
184 assertNull( "Should not have value for key [" + i + ":key" + "] in the cache.", value );
185 }
186 }
187
188
189
190
191
192
193 public void testClearThroughHub()
194 throws CacheException
195 {
196 CacheAccess<String, String> cache = JCS.getInstance( "testPutGetThroughHub" );
197
198 int max = cache.getCacheAttributes().getMaxObjects();
199 int items = max * 2;
200
201 for ( int i = 0; i < items; i++ )
202 {
203 cache.put( i + ":key", "myregion" + " data " + i );
204 }
205
206 cache.clear();
207
208
209 for ( int i = max; i >= 0; i-- )
210 {
211 String value = cache.get( i + ":key" );
212 assertNull( "Should not have value for key [" + i + ":key" + "] in the cache.", value );
213 }
214 }
215
216
217
218
219
220
221 public void testGetStatsThroughHub()
222 throws CacheException
223 {
224 CacheAccess<String, String> cache = JCS.getInstance( "testGetStatsThroughHub" );
225
226 int max = cache.getCacheAttributes().getMaxObjects();
227 int items = max * 2;
228
229 for ( int i = 0; i < items; i++ )
230 {
231 cache.put( i + ":key", "myregion" + " data " + i );
232 }
233
234 String stats = cache.getStats();
235
236
237
238
239 assertTrue( "Should have 200 puts", stats.indexOf( "2000" ) != -1 );
240 }
241
242
243
244
245
246
247
248 public void testGetKeyArray()
249 throws Exception
250 {
251 CompositeCacheManager cacheMgr = CompositeCacheManager.getUnconfiguredInstance();
252 cacheMgr.configure( "/TestMRUCache.ccf" );
253 CompositeCache<String, String> cache = cacheMgr.getCache( "testGetKeyArray" );
254
255 MRUMemoryCache<String, String> mru = new MRUMemoryCache<String, String>();
256 mru.initialize( cache );
257
258 int max = cache.getCacheAttributes().getMaxObjects();
259 int items = max / 2;
260
261 for ( int i = 0; i < items; i++ )
262 {
263 ICacheElement<String, String> ice = new CacheElement<String, String>( cache.getCacheName(), i + ":key", cache.getCacheName() + " data " + i );
264 ice.setElementAttributes( cache.getElementAttributes() );
265 mru.update( ice );
266 }
267
268 Set<String> keys = mru.getKeySet();
269
270 assertEquals( "Wrong number of keys.", items, keys.size() );
271 }
272
273
274
275
276
277
278 public void testRemovePartialThroughHub()
279 throws CacheException
280 {
281 CacheAccess<String, String> cache = JCS.getInstance( "testGetStatsThroughHub" );
282
283 int max = cache.getCacheAttributes().getMaxObjects();
284 int items = max / 2;
285
286 cache.put( "test", "data" );
287
288 String root = "myroot";
289
290 for ( int i = 0; i < items; i++ )
291 {
292 cache.put( root + ":" + i + ":key", "myregion" + " data " + i );
293 }
294
295
296 for ( int i = 0; i < items; i++ )
297 {
298 String value = cache.get( root + ":" + i + ":key" );
299 assertEquals( "myregion" + " data " + i, value );
300 }
301
302
303 cache.remove( root + ":" );
304
305 for ( int i = 0; i < items; i++ )
306 {
307 assertNull( "Should have been removed by partial loop.", cache.get( root + ":" + i + ":key" ) );
308 }
309
310 assertNotNull( "Other item should be in the cache.", cache.get( "test" ) );
311 }
312 }