1 package org.apache.commons.jcs3.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.util.Arrays;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Set;
28
29 import org.apache.commons.jcs3.JCS;
30 import org.apache.commons.jcs3.access.CacheAccess;
31 import org.apache.commons.jcs3.access.exception.CacheException;
32 import org.apache.commons.jcs3.auxiliary.AbstractAuxiliaryCache;
33 import org.apache.commons.jcs3.auxiliary.AuxiliaryCacheAttributes;
34 import org.apache.commons.jcs3.engine.CacheElement;
35 import org.apache.commons.jcs3.engine.CacheStatus;
36 import org.apache.commons.jcs3.engine.CompositeCacheAttributes;
37 import org.apache.commons.jcs3.engine.ElementAttributes;
38 import org.apache.commons.jcs3.engine.behavior.ICacheElement;
39 import org.apache.commons.jcs3.engine.behavior.ICacheType.CacheType;
40 import org.apache.commons.jcs3.engine.behavior.ICompositeCacheAttributes;
41 import org.apache.commons.jcs3.engine.behavior.IElementAttributes;
42 import org.apache.commons.jcs3.engine.behavior.IElementSerializer;
43 import org.apache.commons.jcs3.engine.logging.behavior.ICacheEventLogger;
44 import org.apache.commons.jcs3.engine.stats.behavior.IStats;
45
46 import junit.framework.TestCase;
47
48
49
50
51 public class CompositeCacheDiskUsageUnitTest
52 extends TestCase
53 {
54 private static final String CACHE_NAME = "testSpoolAllowed";
55
56
57
58
59 @Override
60 public void setUp()
61 {
62 JCS.setConfigFilename( "/TestDiskCacheUsagePattern.ccf" );
63 }
64
65
66
67
68
69
70 public void testSwapConfig()
71 throws CacheException
72 {
73 final CacheAccess<String, String> swap = JCS.getInstance( "Swap" );
74 assertEquals( ICompositeCacheAttributes.DiskUsagePattern.SWAP, swap.getCacheAttributes()
75 .getDiskUsagePattern() );
76 }
77
78
79
80
81
82
83 public void testUpdateConfig()
84 throws CacheException
85 {
86 final CacheAccess<String, String> swap = JCS.getInstance( "Update" );
87 assertEquals( ICompositeCacheAttributes.DiskUsagePattern.UPDATE, swap.getCacheAttributes()
88 .getDiskUsagePattern() );
89 }
90
91
92
93
94
95 public void testSpoolAllowed()
96 {
97
98 final ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
99 cattr.setCacheName(CACHE_NAME);
100 cattr.setDiskUsagePattern( ICompositeCacheAttributes.DiskUsagePattern.SWAP );
101
102 final IElementAttributes attr = new ElementAttributes();
103
104 final CompositeCache<String, String> cache = new CompositeCache<>( cattr, attr );
105
106 final MockAuxCache<String, String> mock = new MockAuxCache<>();
107 mock.cacheType = CacheType.DISK_CACHE;
108 cache.setAuxCaches(Arrays.asList(mock));
109
110 final ICacheElement<String, String> inputElement = new CacheElement<>( CACHE_NAME, "key", "value" );
111
112
113 cache.spoolToDisk( inputElement );
114
115
116 assertEquals( "Wrong number of calls to the disk cache update.", 1, mock.updateCount );
117 assertEquals( "Wrong element updated.", inputElement, mock.lastUpdatedItem );
118 }
119
120
121
122
123
124 public void testSpoolNotAllowed()
125 {
126
127 final ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
128 cattr.setCacheName(CACHE_NAME);
129 cattr.setDiskUsagePattern( ICompositeCacheAttributes.DiskUsagePattern.UPDATE );
130
131 final IElementAttributes attr = new ElementAttributes();
132
133 final CompositeCache<String, String> cache = new CompositeCache<>( cattr, attr );
134
135 final MockAuxCache<String, String> mock = new MockAuxCache<>();
136 mock.cacheType = CacheType.DISK_CACHE;
137 cache.setAuxCaches(Arrays.asList(mock));
138
139 final ICacheElement<String, String> inputElement = new CacheElement<>( CACHE_NAME, "key", "value" );
140
141
142 cache.spoolToDisk( inputElement );
143
144
145 assertEquals( "Wrong number of calls to the disk cache update.", 0, mock.updateCount );
146 }
147
148
149
150
151
152
153
154
155
156 public void testUpdateAllowed()
157 throws IOException
158 {
159
160 final ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
161 cattr.setCacheName(CACHE_NAME);
162 cattr.setDiskUsagePattern( ICompositeCacheAttributes.DiskUsagePattern.UPDATE );
163
164 final IElementAttributes attr = new ElementAttributes();
165
166 final CompositeCache<String, String> cache = new CompositeCache<>( cattr, attr );
167
168 final MockAuxCache<String, String> mock = new MockAuxCache<>();
169 mock.cacheType = CacheType.DISK_CACHE;
170 cache.setAuxCaches(Arrays.asList(mock));
171
172 final ICacheElement<String, String> inputElement = new CacheElement<>( CACHE_NAME, "key", "value" );
173
174
175 cache.updateAuxiliaries( inputElement, true );
176
177
178 assertEquals( "Wrong number of calls to the disk cache update.", 1, mock.updateCount );
179 assertEquals( "Wrong element updated.", inputElement, mock.lastUpdatedItem );
180 }
181
182
183
184
185
186
187
188
189
190
191 public void testUpdateAllowed_localFalse()
192 throws IOException
193 {
194
195 final ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
196 cattr.setCacheName(CACHE_NAME);
197 cattr.setDiskUsagePattern( ICompositeCacheAttributes.DiskUsagePattern.UPDATE );
198
199 final IElementAttributes attr = new ElementAttributes();
200
201 final CompositeCache<String, String> cache = new CompositeCache<>( cattr, attr );
202
203 final MockAuxCache<String, String> mock = new MockAuxCache<>();
204 mock.cacheType = CacheType.DISK_CACHE;
205 cache.setAuxCaches(Arrays.asList(mock));
206
207 final ICacheElement<String, String> inputElement = new CacheElement<>( CACHE_NAME, "key", "value" );
208
209
210 cache.updateAuxiliaries( inputElement, false );
211
212
213 assertEquals( "Wrong number of calls to the disk cache update.", 1, mock.updateCount );
214 assertEquals( "Wrong element updated.", inputElement, mock.lastUpdatedItem );
215 }
216
217
218
219
220
221
222
223
224
225
226 public void testUpdateNotAllowed()
227 throws IOException
228 {
229
230 final ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
231 cattr.setCacheName(CACHE_NAME);
232 cattr.setDiskUsagePattern( ICompositeCacheAttributes.DiskUsagePattern.SWAP );
233
234 final IElementAttributes attr = new ElementAttributes();
235
236 final CompositeCache<String, String> cache = new CompositeCache<>( cattr, attr );
237
238 final MockAuxCache<String, String> mock = new MockAuxCache<>();
239 mock.cacheType = CacheType.DISK_CACHE;
240 cache.setAuxCaches(Arrays.asList(mock));
241
242 final ICacheElement<String, String> inputElement = new CacheElement<>( CACHE_NAME, "key", "value" );
243
244
245 cache.updateAuxiliaries( inputElement, true );
246
247
248 assertEquals( "Wrong number of calls to the disk cache update.", 0, mock.updateCount );
249 }
250
251
252
253
254
255
256
257
258
259 public void testUpdateAllowed_withOtherCaches()
260 throws IOException
261 {
262
263 final ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
264 cattr.setCacheName(CACHE_NAME);
265 cattr.setDiskUsagePattern( ICompositeCacheAttributes.DiskUsagePattern.UPDATE );
266
267 final IElementAttributes attr = new ElementAttributes();
268
269 final CompositeCache<String, String> cache = new CompositeCache<>( cattr, attr );
270
271 final MockAuxCache<String, String> mock = new MockAuxCache<>();
272 mock.cacheType = CacheType.DISK_CACHE;
273
274 final MockAuxCache<String, String> mockLateral = new MockAuxCache<>();
275 mockLateral.cacheType = CacheType.LATERAL_CACHE;
276 cache.setAuxCaches(Arrays.asList(mock, mockLateral));
277
278 final ICacheElement<String, String> inputElement = new CacheElement<>( CACHE_NAME, "key", "value" );
279
280
281 cache.updateAuxiliaries( inputElement, false );
282
283
284 assertEquals( "Wrong number of calls to the disk cache update.", 1, mock.updateCount );
285 assertEquals( "Wrong element updated.", inputElement, mock.lastUpdatedItem );
286
287 assertEquals( "Wrong number of calls to the lateral cache update.", 1, mockLateral.updateCount );
288 assertEquals( "Wrong element updated with lateral.", inputElement, mockLateral.lastUpdatedItem );
289 }
290
291
292
293
294 public static class MockAuxCache<K, V>
295 extends AbstractAuxiliaryCache<K, V>
296 {
297
298 public ICacheElement<K, V> lastUpdatedItem;
299
300
301 public int updateCount;
302
303
304 public CacheType cacheType = CacheType.DISK_CACHE;
305
306
307 public void reset()
308 {
309 updateCount = 0;
310 lastUpdatedItem = null;
311 }
312
313
314
315
316
317 @Override
318 public void update( final ICacheElement<K, V> ce )
319 throws IOException
320 {
321 lastUpdatedItem = ce;
322 updateCount++;
323 }
324
325
326
327
328
329
330 @Override
331 public ICacheElement<K, V> get( final K key )
332 throws IOException
333 {
334 return null;
335 }
336
337
338
339
340
341
342
343
344 @Override
345 public Map<K, ICacheElement<K, V>> getMultiple(final Set<K> keys)
346 {
347 return new HashMap<>();
348 }
349
350
351
352
353
354
355 @Override
356 public boolean remove( final K key )
357 throws IOException
358 {
359 return false;
360 }
361
362
363 @Override
364 public void removeAll()
365 throws IOException
366 {
367
368 }
369
370
371 @Override
372 public void dispose()
373 throws IOException
374 {
375
376 }
377
378
379 @Override
380 public int getSize()
381 {
382 return 0;
383 }
384
385
386 @Override
387 public CacheStatus getStatus()
388 {
389 return CacheStatus.ALIVE;
390 }
391
392
393 @Override
394 public String getCacheName()
395 {
396 return null;
397 }
398
399
400
401
402
403 @Override
404 public Set<K> getKeySet( )
405 throws IOException
406 {
407 return null;
408 }
409
410
411 @Override
412 public IStats getStatistics()
413 {
414 return null;
415 }
416
417
418 @Override
419 public String getStats()
420 {
421 return null;
422 }
423
424
425
426
427
428
429
430 @Override
431 public CacheType getCacheType()
432 {
433 return cacheType;
434 }
435
436
437
438
439 @Override
440 public AuxiliaryCacheAttributes getAuxiliaryCacheAttributes()
441 {
442 return null;
443 }
444
445
446
447
448 @Override
449 public void setCacheEventLogger( final ICacheEventLogger cacheEventLogger )
450 {
451
452
453 }
454
455
456
457
458 @Override
459 public void setElementSerializer( final IElementSerializer elementSerializer )
460 {
461
462
463 }
464
465
466 @Override
467 public String getEventLoggingExtraInfo()
468 {
469
470 return null;
471 }
472
473
474
475
476
477
478 @Override
479 public Map<K, ICacheElement<K, V>> getMatching(final String pattern)
480 throws IOException
481 {
482 return Collections.emptyMap();
483 }
484
485
486 }
487
488 }