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