1 package org.apache.jcs.engine.control;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.IOException;
23 import java.io.Serializable;
24 import java.util.Set;
25
26 import junit.framework.TestCase;
27
28 import org.apache.jcs.JCS;
29 import org.apache.jcs.access.exception.CacheException;
30 import org.apache.jcs.auxiliary.AuxiliaryCache;
31 import org.apache.jcs.auxiliary.AuxiliaryCacheAttributes;
32 import org.apache.jcs.engine.CacheElement;
33 import org.apache.jcs.engine.CompositeCacheAttributes;
34 import org.apache.jcs.engine.ElementAttributes;
35 import org.apache.jcs.engine.behavior.ICacheElement;
36 import org.apache.jcs.engine.behavior.ICompositeCacheAttributes;
37 import org.apache.jcs.engine.behavior.IElementAttributes;
38 import org.apache.jcs.engine.stats.behavior.IStats;
39
40 /***
41 * Tests of the disk usage settings for the CompositeCache.
42 * <p>
43 * @author Aaron Smuts
44 */
45 public class CompositeCacheDiskUsageUnitTest
46 extends TestCase
47 {
48 /***
49 * Test setup
50 */
51 public void setUp()
52 {
53 JCS.setConfigFilename( "/TestDiskCacheUsagePattern.ccf" );
54 }
55
56 /***
57 * Verify that the swap region is set to the correct pattern.
58 * <p>
59 * @throws CacheException
60 */
61 public void testSwapConfig() throws CacheException
62 {
63 JCS swap = JCS.getInstance( "Swap" );
64 assertEquals( ICompositeCacheAttributes.DISK_USAGE_PATTERN_SWAP, swap.getCacheAttributes().getDiskUsagePattern() );
65 }
66
67 /***
68 * Verify that the swap region is set to the correct pattern.
69 * <p>
70 * @throws CacheException
71 */
72 public void testUpdateConfig() throws CacheException
73 {
74 JCS swap = JCS.getInstance( "Update" );
75 assertEquals( ICompositeCacheAttributes.DISK_USAGE_PATTERN_UPDATE, swap.getCacheAttributes().getDiskUsagePattern() );
76 }
77
78 /***
79 * Setup a disk cache. Configure the disk usage pattern to swap. Call spool. Verify that the
80 * item is put to disk.
81 */
82 public void testSpoolAllowed()
83 {
84
85 ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
86 cattr.setDiskUsagePattern( ICompositeCacheAttributes.DISK_USAGE_PATTERN_SWAP );
87
88 IElementAttributes attr = new ElementAttributes();
89
90 CompositeCache cache = new CompositeCache( "testSpoolAllowed", cattr, attr );
91
92 MockAuxCache mock = new MockAuxCache();
93 mock.cacheType = AuxiliaryCache.DISK_CACHE;
94
95 cache.setAuxCaches( new AuxiliaryCache[] { mock } );
96
97 ICacheElement inputElement = new CacheElement( "testSpoolAllowed", "key", "value" );
98
99
100 cache.spoolToDisk( inputElement );
101
102
103 assertEquals( "Wrong number of calls to the disk cache update.", 1, mock.updateCount );
104 assertEquals( "Wrong element updated.", inputElement, mock.lastUpdatedItem );
105 }
106
107 /***
108 * Setup a disk cache. Configure the disk usage pattern to not swap. Call spool. Verify that the
109 * item is not put to disk.
110 */
111 public void testSpoolNotAllowed()
112 {
113
114 ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
115 cattr.setDiskUsagePattern( ICompositeCacheAttributes.DISK_USAGE_PATTERN_UPDATE );
116
117 IElementAttributes attr = new ElementAttributes();
118
119 CompositeCache cache = new CompositeCache( "testSpoolAllowed", cattr, attr );
120
121 MockAuxCache mock = new MockAuxCache();
122 mock.cacheType = AuxiliaryCache.DISK_CACHE;
123
124 cache.setAuxCaches( new AuxiliaryCache[] { mock } );
125
126 ICacheElement inputElement = new CacheElement( "testSpoolAllowed", "key", "value" );
127
128
129 cache.spoolToDisk( inputElement );
130
131
132 assertEquals( "Wrong number of calls to the disk cache update.", 0, mock.updateCount );
133 }
134
135 /***
136 * Setup a disk cache. Configure the disk usage pattern to UPDATE. Call updateAuxiliaries.
137 * Verify that the item is put to disk.
138 * <p>
139 * This tests that the items are put to disk on a normal put when the usage pattern is set
140 * appropriately.
141 * @throws IOException
142 */
143 public void testUpdateAllowed()
144 throws IOException
145 {
146
147 ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
148 cattr.setDiskUsagePattern( ICompositeCacheAttributes.DISK_USAGE_PATTERN_UPDATE );
149
150 IElementAttributes attr = new ElementAttributes();
151
152 CompositeCache cache = new CompositeCache( "testSpoolAllowed", cattr, attr );
153
154 MockAuxCache mock = new MockAuxCache();
155 mock.cacheType = AuxiliaryCache.DISK_CACHE;
156
157 cache.setAuxCaches( new AuxiliaryCache[] { mock } );
158
159 ICacheElement inputElement = new CacheElement( "testSpoolAllowed", "key", "value" );
160
161
162 cache.updateAuxiliaries( inputElement, true );
163
164
165 assertEquals( "Wrong number of calls to the disk cache update.", 1, mock.updateCount );
166 assertEquals( "Wrong element updated.", inputElement, mock.lastUpdatedItem );
167 }
168
169 /***
170 * Setup a disk cache. Configure the disk usage pattern to UPDATE. Call updateAuxiliaries with
171 * local only set to false. Verify that the item is put to disk.
172 * <p>
173 * This tests that the items are put to disk on a normal put when the usage pattern is set
174 * appropriately. The local setting should have no impact on whether the item goes to disk.
175 * <p>
176 * @throws IOException
177 */
178 public void testUpdateAllowed_localFalse()
179 throws IOException
180 {
181
182 ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
183 cattr.setDiskUsagePattern( ICompositeCacheAttributes.DISK_USAGE_PATTERN_UPDATE );
184
185 IElementAttributes attr = new ElementAttributes();
186
187 CompositeCache cache = new CompositeCache( "testSpoolAllowed", cattr, attr );
188
189 MockAuxCache mock = new MockAuxCache();
190 mock.cacheType = AuxiliaryCache.DISK_CACHE;
191
192 cache.setAuxCaches( new AuxiliaryCache[] { mock } );
193
194 ICacheElement inputElement = new CacheElement( "testSpoolAllowed", "key", "value" );
195
196
197 cache.updateAuxiliaries( inputElement, false );
198
199
200 assertEquals( "Wrong number of calls to the disk cache update.", 1, mock.updateCount );
201 assertEquals( "Wrong element updated.", inputElement, mock.lastUpdatedItem );
202 }
203
204 /***
205 * Setup a disk cache. Configure the disk usage pattern to SWAP. Call updateAuxiliaries. Verify
206 * that the item is not put to disk.
207 * <p>
208 * This tests that the items are not put to disk on a normal put when the usage pattern is set
209 * to SWAP.
210 * <p>
211 * @throws IOException
212 */
213 public void testUpdateNotAllowed()
214 throws IOException
215 {
216
217 ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
218 cattr.setDiskUsagePattern( ICompositeCacheAttributes.DISK_USAGE_PATTERN_SWAP );
219
220 IElementAttributes attr = new ElementAttributes();
221
222 CompositeCache cache = new CompositeCache( "testSpoolAllowed", cattr, attr );
223
224 MockAuxCache mock = new MockAuxCache();
225 mock.cacheType = AuxiliaryCache.DISK_CACHE;
226
227 cache.setAuxCaches( new AuxiliaryCache[] { mock } );
228
229 ICacheElement inputElement = new CacheElement( "testSpoolAllowed", "key", "value" );
230
231
232 cache.updateAuxiliaries( inputElement, true );
233
234
235 assertEquals( "Wrong number of calls to the disk cache update.", 0, mock.updateCount );
236 }
237
238 /***
239 * Setup a disk cache. Configure the disk usage pattern to UPDATE. Call updateAuxiliaries.
240 * Verify that the item is put to disk.
241 * <p>
242 * This tests that the items are put to disk on a normal put when the usage pattern is set
243 * appropriately.
244 * @throws IOException
245 */
246 public void testUpdateAllowed_withOtherCaches()
247 throws IOException
248 {
249
250 ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
251 cattr.setDiskUsagePattern( ICompositeCacheAttributes.DISK_USAGE_PATTERN_UPDATE );
252
253 IElementAttributes attr = new ElementAttributes();
254
255 CompositeCache cache = new CompositeCache( "testSpoolAllowed", cattr, attr );
256
257 MockAuxCache mock = new MockAuxCache();
258 mock.cacheType = AuxiliaryCache.DISK_CACHE;
259
260 MockAuxCache mockLateral = new MockAuxCache();
261 mockLateral.cacheType = AuxiliaryCache.LATERAL_CACHE;
262
263 cache.setAuxCaches( new AuxiliaryCache[] { mock, mockLateral } );
264
265 ICacheElement inputElement = new CacheElement( "testSpoolAllowed", "key", "value" );
266
267
268 cache.updateAuxiliaries( inputElement, false );
269
270
271 assertEquals( "Wrong number of calls to the disk cache update.", 1, mock.updateCount );
272 assertEquals( "Wrong element updated.", inputElement, mock.lastUpdatedItem );
273
274 assertEquals( "Wrong number of calls to the lateral cache update.", 1, mockLateral.updateCount );
275 assertEquals( "Wrong element updated with lateral.", inputElement, mockLateral.lastUpdatedItem );
276 }
277
278 /***
279 * Used to test the disk cache functionality.
280 * <p>
281 * @author Aaron Smuts
282 */
283 public class MockAuxCache
284 implements AuxiliaryCache
285 {
286 private static final long serialVersionUID = 1L;
287
288 /***
289 * The last item passed to update.
290 */
291 public ICacheElement lastUpdatedItem;
292
293 /***
294 * The number of times update was called.
295 */
296 public int updateCount = 0;
297
298 /***
299 * The type that should be returned from getCacheType.
300 */
301 public int cacheType = AuxiliaryCache.DISK_CACHE;
302
303 /***
304 * Resets counters and catchers.
305 */
306 public void reset()
307 {
308 updateCount = 0;
309 lastUpdatedItem = null;
310 }
311
312
313
314
315
316 public void update( ICacheElement ce )
317 throws IOException
318 {
319 lastUpdatedItem = ce;
320 updateCount++;
321 }
322
323
324
325
326
327 public ICacheElement get( Serializable key )
328 throws IOException
329 {
330
331 return null;
332 }
333
334
335
336
337
338 public boolean remove( Serializable key )
339 throws IOException
340 {
341
342 return false;
343 }
344
345
346
347
348
349 public void removeAll()
350 throws IOException
351 {
352
353
354 }
355
356
357
358
359
360 public void dispose()
361 throws IOException
362 {
363
364
365 }
366
367
368
369
370
371 public int getSize()
372 {
373
374 return 0;
375 }
376
377
378
379
380
381 public int getStatus()
382 {
383
384 return 0;
385 }
386
387
388
389
390
391 public String getCacheName()
392 {
393
394 return null;
395 }
396
397
398
399
400
401 public Set getGroupKeys( String group )
402 throws IOException
403 {
404
405 return null;
406 }
407
408
409
410
411
412 public IStats getStatistics()
413 {
414
415 return null;
416 }
417
418
419
420
421
422 public String getStats()
423 {
424
425 return null;
426 }
427
428 /***
429 * Returns the setup cache type. This allows you to use this mock as multiple cache types.
430 * <p>
431 * (non-Javadoc)
432 * @see org.apache.jcs.engine.behavior.ICacheType#getCacheType()
433 */
434 public int getCacheType()
435 {
436 return cacheType;
437 }
438
439 /***
440 * @return Returns the AuxiliaryCacheAttributes.
441 */
442 public AuxiliaryCacheAttributes getAuxiliaryCacheAttributes()
443 {
444 return null;
445 }
446 }
447
448 }