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 public class IndexedDiskCacheConcurrentUnitTest
39 extends TestCase
40 {
41
42
43
44
45 private static final int items = 200;
46
47
48
49
50
51
52 public IndexedDiskCacheConcurrentUnitTest( final String testName )
53 {
54 super( testName );
55 }
56
57
58
59
60
61
62 public static Test suite()
63 {
64 final ActiveTestSuite suite = new ActiveTestSuite();
65
66 suite.addTest( new IndexedDiskCacheConcurrentUnitTest( "testIndexedDiskCache1" )
67 {
68 @Override
69 public void runTest()
70 throws Exception
71 {
72 this.runTestForRegion( "indexedRegion1" );
73 }
74 } );
75
76 suite.addTest( new IndexedDiskCacheConcurrentUnitTest( "testIndexedDiskCache2" )
77 {
78 @Override
79 public void runTest()
80 throws Exception
81 {
82 this.runTestForRegion( "indexedRegion2" );
83 }
84 } );
85
86 suite.addTest( new IndexedDiskCacheConcurrentUnitTest( "testIndexedDiskCache3" )
87 {
88 @Override
89 public void runTest()
90 throws Exception
91 {
92 this.runTestForRegion( "indexedRegion3" );
93 }
94 } );
95
96 suite.addTest( new IndexedDiskCacheConcurrentUnitTest( "testIndexedDiskCache4" )
97 {
98 @Override
99 public void runTest()
100 throws Exception
101 {
102 this.runTestForRegionInRange( "indexedRegion3", 300, 600 );
103 }
104 } );
105
106 return suite;
107 }
108
109
110
111
112 @Override
113 public void setUp()
114 {
115 JCS.setConfigFilename( "/TestDiskCache.ccf" );
116 }
117
118
119
120
121
122
123
124
125
126
127
128 public void runTestForRegion( final String region )
129 throws Exception
130 {
131 final CacheAccess<String, String> jcs = JCS.getInstance( region );
132
133
134 for ( int i = 0; i <= items; i++ )
135 {
136 jcs.put( i + ":key", region + " data " + i );
137 }
138
139
140 for ( int i = 0; i <= items; i++ )
141 {
142 final String value = jcs.get( i + ":key" );
143
144 assertEquals( region + " data " + i, value );
145 }
146
147
148 final Set<String> keys = new HashSet<>();
149 for ( int i = 0; i <= items; i++ )
150 {
151 keys.add( i + ":key" );
152 }
153
154 final Map<String, ICacheElement<String, String>> elements = jcs.getCacheElements( keys );
155 for ( int i = 0; i <= items; i++ )
156 {
157 final ICacheElement<String, String> element = elements.get( i + ":key" );
158 assertNotNull( "element " + i + ":key is missing", element );
159 assertEquals( "value " + i + ":key", region + " data " + i, element.getVal() );
160 }
161
162
163 for ( int i = 0; i <= items; i++ )
164 {
165 jcs.remove( i + ":key" );
166 }
167
168
169
170 for ( int i = 0; i <= items; i++ )
171 {
172 assertNull( "Removed key should be null: " + i + ":key" + "\n stats " + jcs.getStats(), jcs
173 .get( i + ":key" ) );
174 }
175 }
176
177
178
179
180
181
182
183
184
185
186
187
188
189 public void runTestForRegionInRange( final String region, final int start, final int end )
190 throws Exception
191 {
192 final CacheAccess<String, String> jcs = JCS.getInstance( region );
193
194
195 for ( int i = start; i <= end; i++ )
196 {
197 jcs.put( i + ":key", region + " data " + i );
198 }
199
200
201 for ( int i = start; i <= end; i++ )
202 {
203 final String value = jcs.get( i + ":key" );
204
205 assertEquals( region + " data " + i, value );
206 }
207
208
209 final Set<String> keys = new HashSet<>();
210 for ( int i = start; i <= end; i++ )
211 {
212 keys.add( i + ":key" );
213 }
214
215 final Map<String, ICacheElement<String, String>> elements = jcs.getCacheElements( keys );
216 for ( int i = start; i <= end; i++ )
217 {
218 final ICacheElement<String, String> element = elements.get( i + ":key" );
219 assertNotNull( "element " + i + ":key is missing", element );
220 assertEquals( "value " + i + ":key", region + " data " + i, element.getVal() );
221 }
222
223
224 for ( int i = start; i <= end; i++ )
225 {
226 jcs.remove( i + ":key" );
227 }
228
229
230
231
232
233 for ( int i = start; i <= end; i++ )
234 {
235 assertNull( "Removed key should be null: " + i + ":key " + "\n stats " + jcs.getStats(), jcs.get( i
236 + ":key" ) );
237 }
238 }
239 }