1 package org.apache.commons.jcs3.auxiliary.disk.indexed;
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
24 import org.apache.commons.jcs3.auxiliary.disk.behavior.IDiskCacheAttributes.DiskLimitType;
25 import org.apache.commons.jcs3.engine.CacheElement;
26 import org.apache.commons.jcs3.engine.behavior.ICacheElement;
27
28 public class IndexDiskCacheCountUnitTest extends IndexDiskCacheUnitTestAbstract {
29
30 @Override
31 public IndexedDiskCacheAttributes getCacheAttributes() {
32 final IndexedDiskCacheAttributes ret = new IndexedDiskCacheAttributes();
33 ret.setDiskLimitType(DiskLimitType.COUNT);
34 return ret;
35 }
36 public void testRecycleBin()
37 throws IOException
38 {
39 final IndexedDiskCacheAttributes cattr = getCacheAttributes();
40 cattr.setCacheName( "testRemoveItems" );
41 cattr.setOptimizeAtRemoveCount( 7 );
42 cattr.setMaxKeySize( 5 );
43 cattr.setMaxPurgatorySize( 0 );
44 cattr.setDiskPath( "target/test-sandbox/BreakIndexTest" );
45 final IndexedDiskCache<String, String> disk = new IndexedDiskCache<>( cattr );
46
47 final String[] test = { "a", "bb", "ccc", "dddd", "eeeee", "ffffff", "ggggggg", "hhhhhhhhh", "iiiiiiiiii" };
48 final String[] expect = { null, "bb", "ccc", null, null, "ffffff", null, "hhhhhhhhh", "iiiiiiiiii" };
49
50
51
52 for ( int i = 0; i < 6; i++ )
53 {
54 final ICacheElement<String, String> element = new CacheElement<>( "testRecycleBin", "key:" + test[i], test[i] );
55
56 disk.processUpdate( element );
57 }
58
59 for ( int i = 3; i < 5; i++ )
60 {
61
62 disk.remove( "key:" + test[i] );
63 }
64
65
66
67 for ( int i = 7; i < 9; i++ )
68 {
69 final ICacheElement<String, String> element = new CacheElement<>( "testRecycleBin", "key:" + test[i], test[i] );
70
71 disk.processUpdate( element );
72 }
73
74 try
75 {
76 for ( int i = 0; i < 9; i++ )
77 {
78 final ICacheElement<String, String> element = disk.get( "key:" + test[i] );
79 if ( element != null )
80 {
81
82 }
83 else
84 {
85
86 }
87
88 final String expectedValue = expect[i];
89 if ( expectedValue == null )
90 {
91 assertNull( "Expected a null element", element );
92 }
93 else
94 {
95 assertNotNull( "The element for key [" + "key:" + test[i] + "] should not be null. i = " + i,
96 element );
97 assertEquals( "Elements contents do not match expected", element.getVal(), expectedValue );
98 }
99 }
100 }
101 catch ( final Exception e )
102 {
103 e.printStackTrace();
104 fail( "Should not get an exception: " + e.toString() );
105 }
106
107 disk.removeAll();
108 }
109 }