1 package org.apache.commons.jcs.access;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.HashSet;
23 import java.util.Map;
24 import java.util.Set;
25
26 import org.apache.commons.jcs.JCS;
27 import org.apache.commons.jcs.access.exception.CacheException;
28 import org.apache.commons.jcs.access.exception.ObjectExistsException;
29 import org.apache.commons.jcs.engine.CompositeCacheAttributes;
30 import org.apache.commons.jcs.engine.ElementAttributes;
31 import org.apache.commons.jcs.engine.behavior.ICacheElement;
32 import org.apache.commons.jcs.engine.behavior.ICompositeCacheAttributes;
33 import org.apache.commons.jcs.engine.behavior.IElementAttributes;
34
35 import junit.framework.TestCase;
36
37
38
39
40
41
42 public class CacheAccessUnitTest
43 extends TestCase
44 {
45
46
47
48
49 public void testPutSafe()
50 throws Exception
51 {
52 CacheAccess<String, String> access = JCS.getInstance( "test" );
53 assertNotNull( "We should have an access class", access );
54
55 String key = "mykey";
56 String value = "myvalue";
57
58 access.put( key, value );
59
60 String returnedValue1 = access.get( key );
61 assertEquals( "Wrong value returned.", value, returnedValue1 );
62
63 try
64 {
65 access.putSafe( key, "someothervalue" );
66 fail( "We should have received an exception since this key is already in the cache." );
67 }
68 catch ( CacheException e )
69 {
70 assertTrue( "Wrong type of exception.", e instanceof ObjectExistsException );
71 assertTrue( "Should have the key in the error message.", e.getMessage().indexOf( "[" + key + "]" ) != -1 );
72 }
73
74 String returnedValue2 = access.get( key );
75 assertEquals( "Wrong value returned. Should still be the original.", value, returnedValue2 );
76 }
77
78
79
80
81
82 public void testPutNullKey()
83 throws Exception
84 {
85 CacheAccess<String, String> access = JCS.getInstance( "test" );
86 assertNotNull( "We should have an access class", access );
87
88 String key = null;
89 String value = "myvalue";
90
91 try
92 {
93 access.put( key, value );
94 fail( "Should not have been able to put a null key." );
95 }
96 catch ( CacheException e )
97 {
98 assertTrue( "Should have the word null in the error message.", e.getMessage().indexOf( "null" ) != -1 );
99 }
100 }
101
102
103
104
105
106 public void testPutNullValue()
107 throws Exception
108 {
109 CacheAccess<String, String> access = JCS.getInstance( "test" );
110 assertNotNull( "We should have an access class", access );
111
112 String key = "myKey";
113 String value = null;
114
115 try
116 {
117 access.put( key, value );
118 fail( "Should not have been able to put a null object." );
119 }
120 catch ( CacheException e )
121 {
122 assertTrue( "Should have the word null in the error message.", e.getMessage().indexOf( "null" ) != -1 );
123 }
124 }
125
126
127
128
129
130 public void testSetDefaultElementAttributes()
131 throws Exception
132 {
133 CacheAccess<String, String> access = JCS.getInstance( "test" );
134 assertNotNull( "We should have an access class", access );
135
136 long maxLife = 9876;
137 IElementAttributes attr = new ElementAttributes();
138 attr.setMaxLife(maxLife);
139
140 access.setDefaultElementAttributes( attr );
141
142 assertEquals( "Wrong element attributes.", attr.getMaxLife(), access.getDefaultElementAttributes()
143 .getMaxLife() );
144
145 String key = "mykey";
146 String value = "myvalue";
147
148 access.put( key, value );
149
150 ICacheElement<String, String> element = access.getCacheElement( key );
151
152 assertEquals( "Wrong max life. Should have the new value.", maxLife, element.getElementAttributes()
153 .getMaxLife() );
154 }
155
156
157
158
159
160 public void testGetCacheElements()
161 throws Exception
162 {
163
164 CacheAccess<String, String> access = JCS.getInstance( "test" );
165 assertNotNull( "We should have an access class", access );
166
167 String keyOne = "mykeyone";
168 String keyTwo = "mykeytwo";
169 String keyThree = "mykeythree";
170 String valueOne = "myvalueone";
171 String valueTwo = "myvaluetwo";
172 String valueThree = "myvaluethree";
173
174 access.put( keyOne, valueOne );
175 access.put( keyTwo, valueTwo );
176 access.put( keyThree, valueThree );
177
178 Set<String> input = new HashSet<String>();
179 input.add( keyOne );
180 input.add( keyTwo );
181
182
183 Map<String, ICacheElement<String, String>> result = access.getCacheElements( input );
184
185
186 assertEquals( "map size", 2, result.size() );
187 ICacheElement<String, String> elementOne = result.get( keyOne );
188 assertEquals( "value one", keyOne, elementOne.getKey() );
189 assertEquals( "value one", valueOne, elementOne.getVal() );
190 ICacheElement<String, String> elementTwo = result.get( keyTwo );
191 assertEquals( "value two", keyTwo, elementTwo.getKey() );
192 assertEquals( "value two", valueTwo, elementTwo.getVal() );
193 }
194
195
196
197
198
199 public void testRegionDefiniton()
200 throws Exception
201 {
202 CacheAccess<String, String> access = JCS.getInstance( "test" );
203 assertNotNull( "We should have an access class", access );
204 }
205
206
207
208
209
210 public void testRegionDefinitonWithAttributes()
211 throws Exception
212 {
213 ICompositeCacheAttributes ca = new CompositeCacheAttributes();
214
215 long maxIdleTime = 8765;
216 ca.setMaxMemoryIdleTimeSeconds( maxIdleTime );
217
218 CacheAccess<String, String> access = JCS.getInstance( "testRegionDefinitonWithAttributes", ca );
219 assertNotNull( "We should have an access class", access );
220
221 ICompositeCacheAttributes ca2 = access.getCacheAttributes();
222 assertEquals( "Wrong idle time setting.", ca.getMaxMemoryIdleTimeSeconds(), ca2.getMaxMemoryIdleTimeSeconds() );
223 }
224
225
226
227
228
229
230 public void testRegionDefinitonWithBothAttributes()
231 throws Exception
232 {
233 ICompositeCacheAttributes ca = new CompositeCacheAttributes();
234
235 long maxIdleTime = 8765;
236 ca.setMaxMemoryIdleTimeSeconds( maxIdleTime );
237
238 long maxLife = 9876;
239 IElementAttributes attr = new ElementAttributes();
240 attr.setMaxLife(maxLife);
241
242 CacheAccess<String, String> access = JCS.getInstance( "testRegionDefinitonWithAttributes", ca, attr );
243 assertNotNull( "We should have an access class", access );
244
245 ICompositeCacheAttributes ca2 = access.getCacheAttributes();
246 assertEquals( "Wrong idle time setting.", ca.getMaxMemoryIdleTimeSeconds(), ca2.getMaxMemoryIdleTimeSeconds() );
247 }
248
249
250
251
252
253
254 public void testGetMatching_Normal()
255 throws Exception
256 {
257
258 int maxMemorySize = 1000;
259 String keyprefix1 = "MyPrefix1";
260 String keyprefix2 = "MyPrefix2";
261 String memoryCacheClassName = "org.apache.commons.jcs.engine.memory.lru.LRUMemoryCache";
262 ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
263 cattr.setMemoryCacheName( memoryCacheClassName );
264 cattr.setMaxObjects( maxMemorySize );
265
266 long maxLife = 9876;
267 IElementAttributes attr = new ElementAttributes();
268 attr.setMaxLife(maxLife);
269
270 CacheAccess<String, Integer> access = JCS.getInstance( "testGetMatching_Normal", cattr, attr );
271
272
273 int numToInsertPrefix1 = 10;
274
275 for ( int i = 0; i < numToInsertPrefix1; i++ )
276 {
277 access.put( keyprefix1 + String.valueOf( i ), Integer.valueOf( i ) );
278 }
279
280 int numToInsertPrefix2 = 50;
281
282 for ( int i = 0; i < numToInsertPrefix2; i++ )
283 {
284 access.put( keyprefix2 + String.valueOf( i ), Integer.valueOf( i ) );
285 }
286
287 Map<String, Integer> result1 = access.getMatching( keyprefix1 + ".+" );
288 Map<String, Integer> result2 = access.getMatching( keyprefix2 + "\\S+" );
289
290
291 assertEquals( "Wrong number returned 1:", numToInsertPrefix1, result1.size() );
292 assertEquals( "Wrong number returned 2:", numToInsertPrefix2, result2.size() );
293
294
295
296 for (Map.Entry<String, Integer> entry : result1.entrySet())
297 {
298 Object value = entry.getValue();
299 assertFalse( "Should not be a cache element.", value instanceof ICacheElement );
300 }
301 }
302
303
304
305
306
307
308 public void testGetMatchingElements_Normal()
309 throws Exception
310 {
311
312 int maxMemorySize = 1000;
313 String keyprefix1 = "MyPrefix1";
314 String keyprefix2 = "MyPrefix2";
315 String memoryCacheClassName = "org.apache.commons.jcs.engine.memory.lru.LRUMemoryCache";
316 ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
317 cattr.setMemoryCacheName( memoryCacheClassName );
318 cattr.setMaxObjects( maxMemorySize );
319
320 long maxLife = 9876;
321 IElementAttributes attr = new ElementAttributes();
322 attr.setMaxLife(maxLife);
323
324 CacheAccess<String, Integer> access = JCS.getInstance( "testGetMatching_Normal", cattr, attr );
325
326
327 int numToInsertPrefix1 = 10;
328
329 for ( int i = 0; i < numToInsertPrefix1; i++ )
330 {
331 access.put( keyprefix1 + String.valueOf( i ), Integer.valueOf( i ) );
332 }
333
334 int numToInsertPrefix2 = 50;
335
336 for ( int i = 0; i < numToInsertPrefix2; i++ )
337 {
338 access.put( keyprefix2 + String.valueOf( i ), Integer.valueOf( i ) );
339 }
340
341 Map<String, ICacheElement<String, Integer>> result1 = access.getMatchingCacheElements( keyprefix1 + "\\S+" );
342 Map<String, ICacheElement<String, Integer>> result2 = access.getMatchingCacheElements( keyprefix2 + ".+" );
343
344
345 assertEquals( "Wrong number returned 1:", numToInsertPrefix1, result1.size() );
346 assertEquals( "Wrong number returned 2:", numToInsertPrefix2, result2.size() );
347
348
349
350 for (Map.Entry<String, ICacheElement<String, Integer>> entry : result1.entrySet())
351 {
352 Object value = entry.getValue();
353 assertTrue( "Should be a cache element.", value instanceof ICacheElement );
354 }
355 }
356 }