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