1 package org.apache.commons.jcs3.auxiliary.disk.indexed;
2
3 import java.util.HashSet;
4 import java.util.Map;
5 import java.util.Set;
6
7 import org.apache.commons.jcs3.JCS;
8 import org.apache.commons.jcs3.access.CacheAccess;
9 import org.apache.commons.jcs3.engine.behavior.ICacheElement;
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 import junit.extensions.ActiveTestSuite;
31 import junit.framework.Test;
32 import junit.framework.TestCase;
33
34
35
36
37
38
39 public class IndexedDiskCacheNoMemoryUnitTest
40 extends TestCase
41 {
42
43
44
45
46 private static final int items = 2000;
47
48
49
50
51 public IndexedDiskCacheNoMemoryUnitTest( final String testName )
52 {
53 super( testName );
54 }
55
56
57
58
59
60
61 public static Test suite()
62 {
63 final ActiveTestSuite suite = new ActiveTestSuite();
64
65 suite.addTest( new IndexedDiskCacheNoMemoryUnitTest( "testIndexedDiskCache1" )
66 {
67 @Override
68 public void runTest()
69 throws Exception
70 {
71 this.runTestForRegion( "indexedRegion1" );
72 }
73 } );
74
75 suite.addTest( new IndexedDiskCacheNoMemoryUnitTest( "testIndexedDiskCache2" )
76 {
77 @Override
78 public void runTest()
79 throws Exception
80 {
81 this.runTestForRegion( "indexedRegion2" );
82 }
83 } );
84
85 suite.addTest( new IndexedDiskCacheNoMemoryUnitTest( "testIndexedDiskCache3" )
86 {
87 @Override
88 public void runTest()
89 throws Exception
90 {
91 this.runTestForRegion( "indexedRegion3" );
92 }
93 } );
94
95 return suite;
96 }
97
98
99
100
101 @Override
102 public void setUp()
103 {
104 JCS.setConfigFilename( "/TestDiskCacheNoMemory.ccf" );
105 }
106
107
108
109
110
111
112
113
114
115
116
117 public void runTestForRegion( final String region )
118 throws Exception
119 {
120 final CacheAccess<String, String> jcs = JCS.getInstance( region );
121
122
123
124 for ( int i = 0; i <= items; i++ )
125 {
126 jcs.put( i + ":key", region + " data " + i );
127 }
128
129
130
131 for ( int i = 0; i <= items; i++ )
132 {
133 final String value = jcs.get( i + ":key" );
134
135 assertEquals( region + " data " + i, value );
136 }
137
138
139 final Set<String> keys = new HashSet<>();
140 for ( int i = 0; i <= items; i++ )
141 {
142 keys.add( i + ":key" );
143 }
144
145 final Map<String, ICacheElement<String, String>> elements = jcs.getCacheElements( keys );
146 for ( int i = 0; i <= items; i++ )
147 {
148 final ICacheElement<String, String> element = elements.get( i + ":key" );
149 assertNotNull( "element " + i + ":key is missing", element );
150 assertEquals( "value " + i + ":key", region + " data " + i, element.getVal() );
151 }
152
153
154 for ( int i = 0; i <= items; i++ )
155 {
156 jcs.remove( i + ":key" );
157 }
158
159
160 for ( int i = 0; i <= items; i++ )
161 {
162 assertNull( "Removed key should be null: " + i + ":key" + "\n stats " + jcs.getStats(), jcs.get( i + ":key" ) );
163 }
164
165
166
167 }
168 }