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