1 package org.apache.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
24 import org.apache.jcs.JCS;
25 import org.apache.jcs.access.exception.CacheException;
26 import org.apache.jcs.engine.CacheElement;
27 import org.apache.jcs.engine.behavior.ICacheElement;
28 import org.apache.jcs.engine.control.CompositeCache;
29 import org.apache.jcs.engine.control.CompositeCacheManager;
30
31 /***
32 * Tests for the test MRU implementation that uses the java linked list class.
33 * This is more a set of tests for the hub than for the MRU, since we don't care
34 * about the MRU.
35 *
36 * @author Aaron Smuts
37 *
38 */
39 public class MRUMemoryCacheUnitTest
40 extends TestCase
41 {
42
43 /***
44 * Test setup
45 */
46 public void setUp()
47 {
48 JCS.setConfigFilename( "/TestMRUCache.ccf" );
49 }
50
51 /***
52 * Verify that the mru gets used by a non-defined region when it is set as
53 * the defualt in the default region.
54 *
55 * @throws CacheException
56 */
57 public void testLoadFromCCF()
58 throws CacheException
59 {
60 JCS cache = JCS.getInstance( "testPutGet" );
61 String memoryCacheName = cache.getCacheAttributes().getMemoryCacheName();
62 assertTrue( "Cache name should have MRU in it.", memoryCacheName.indexOf( "MRUMemoryCache" ) != -1 );
63 }
64
65 /***
66 * put twice as many as the max. verify that the second half is in the
67 * cache.
68 *
69 * @throws CacheException
70 */
71 public void testPutGetThroughHub()
72 throws CacheException
73 {
74 JCS cache = JCS.getInstance( "testPutGetThroughHub" );
75
76 int max = cache.getCacheAttributes().getMaxObjects();
77 int items = max * 2;
78
79 for ( int i = 0; i < items; i++ )
80 {
81 cache.put( i + ":key", "myregion" + " data " + i );
82 }
83
84
85 for ( int i = max; i >= 0; i-- )
86 {
87 String value = (String) cache.get( i + ":key" );
88 assertNull( "Should not have value for key [" + i + ":key" + "] in the cache.", value );
89 }
90
91
92
93 for ( int i = max + 2; i < items; i++ )
94 {
95 String value = (String) cache.get( i + ":key" );
96 assertEquals( "myregion" + " data " + i, value );
97 }
98
99 }
100
101 /***
102 * Put twice as many as the max, twice. verify that the second half is in
103 * the cache.
104 *
105 * @throws CacheException
106 */
107 public void testPutGetThroughHubTwice()
108 throws CacheException
109 {
110 JCS cache = JCS.getInstance( "testPutGetThroughHub" );
111
112 int max = cache.getCacheAttributes().getMaxObjects();
113 int items = max * 2;
114
115 for ( int i = 0; i < items; i++ )
116 {
117 cache.put( i + ":key", "myregion" + " data " + i );
118 }
119
120 for ( int i = 0; i < items; i++ )
121 {
122 cache.put( i + ":key", "myregion" + " data " + i );
123 }
124
125
126 for ( int i = max; i >= 0; i-- )
127 {
128 String value = (String) cache.get( i + ":key" );
129 assertNull( "Should not have value for key [" + i + ":key" + "] in the cache.", value );
130 }
131
132
133
134 for ( int i = max + 2; i < items; i++ )
135 {
136 String value = (String) cache.get( i + ":key" );
137 assertEquals( "myregion" + " data " + i, value );
138 }
139
140 }
141
142 /***
143 * put the max and remove each. verify that they are all null.
144 *
145 * @throws CacheException
146 */
147 public void testPutRemoveThroughHub()
148 throws CacheException
149 {
150 JCS cache = JCS.getInstance( "testPutGetThroughHub" );
151
152 int max = cache.getCacheAttributes().getMaxObjects();
153 int items = max * 2;
154
155 for ( int i = 0; i < items; i++ )
156 {
157 cache.put( i + ":key", "myregion" + " data " + i );
158 }
159
160 for ( int i = 0; i < items; i++ )
161 {
162 cache.remove( i + ":key" );
163 }
164
165
166 for ( int i = max; i >= 0; i-- )
167 {
168 String value = (String) cache.get( i + ":key" );
169 assertNull( "Should not have value for key [" + i + ":key" + "] in the cache.", value );
170 }
171 }
172
173 /***
174 * put the max and clear. verify that no elements remain.
175 *
176 * @throws CacheException
177 */
178 public void testClearThroughHub()
179 throws CacheException
180 {
181 JCS cache = JCS.getInstance( "testPutGetThroughHub" );
182
183 int max = cache.getCacheAttributes().getMaxObjects();
184 int items = max * 2;
185
186 for ( int i = 0; i < items; i++ )
187 {
188 cache.put( i + ":key", "myregion" + " data " + i );
189 }
190
191 cache.clear();
192
193
194 for ( int i = max; i >= 0; i-- )
195 {
196 String value = (String) cache.get( i + ":key" );
197 assertNull( "Should not have value for key [" + i + ":key" + "] in the cache.", value );
198 }
199 }
200
201 /***
202 * put twice the max and clear. verify that no elements remain.
203 *
204 * @throws CacheException
205 */
206 public void testGetStatsThroughHub()
207 throws CacheException
208 {
209 JCS cache = JCS.getInstance( "testGetStatsThroughHub" );
210
211 int max = cache.getCacheAttributes().getMaxObjects();
212 int items = max * 2;
213
214 for ( int i = 0; i < items; i++ )
215 {
216 cache.put( i + ":key", "myregion" + " data " + i );
217 }
218
219 String stats = cache.getStats();
220
221 System.out.println( stats );
222
223
224 assertTrue( "Should have 200 puts", stats.indexOf( "2000" ) != -1 );
225 }
226
227 /***
228 * Put half the max and clear. get the key array and verify that it has the
229 * correct number of items.
230 *
231 * @throws Exception
232 */
233 public void testGetKeyArray()
234 throws Exception
235 {
236 CompositeCacheManager cacheMgr = CompositeCacheManager.getUnconfiguredInstance();
237 cacheMgr.configure( "/TestMRUCache.ccf" );
238 CompositeCache cache = cacheMgr.getCache( "testGetKeyArray" );
239
240 MRUMemoryCache mru = new MRUMemoryCache();
241 mru.initialize( cache );
242
243 int max = cache.getCacheAttributes().getMaxObjects();
244 int items = max / 2;
245
246 for ( int i = 0; i < items; i++ )
247 {
248 ICacheElement ice = new CacheElement( cache.getCacheName(), i + ":key", cache.getCacheName() + " data " + i );
249 ice.setElementAttributes( cache.getElementAttributes() );
250 mru.update( ice );
251 }
252
253 Object[] keys = mru.getKeyArray();
254
255 assertEquals( "Wrong number of keys.", items, keys.length );
256 }
257
258
259 /***
260 * Add a few keys with the delimeter. Remove them.
261 *
262 *
263 * @throws CacheException
264 */
265 public void testRemovePartialThroughHub()
266 throws CacheException
267 {
268 JCS cache = JCS.getInstance( "testGetStatsThroughHub" );
269
270 int max = cache.getCacheAttributes().getMaxObjects();
271 int items = max / 2;
272
273 cache.put( "test", "data" );
274
275 String root = "myroot";
276
277 for ( int i = 0; i < items; i++ )
278 {
279 cache.put( root + ":" + i + ":key", "myregion" + " data " + i );
280 }
281
282
283 for ( int i = 0; i < items; i++ )
284 {
285 String value = (String) cache.get( root + ":" + i + ":key" );
286 assertEquals( "myregion" + " data " + i, value );
287 }
288
289
290 cache.remove( root + ":" );
291
292 for ( int i = 0; i < items; i++ )
293 {
294 assertNull( "Should have been removed by partial loop.", cache.get( root + ":" + i + ":key" ) );
295 }
296
297 assertNotNull( "Other item should be in the cache.", cache.get( "test" ) );
298
299 }
300 }