View Javadoc
1   package org.apache.commons.jcs.access;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Tests the methods of the cache access class.
39   * <p>
40   * @author Aaron Smuts
41   */
42  public class CacheAccessUnitTest
43      extends TestCase
44  {
45      /**
46       * Verify that we get an object exists exception if the item is in the cache.
47       * @throws Exception
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       * Try to put a null key and verify that we get an exception.
80       * @throws Exception
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      * Try to put a null value and verify that we get an exception.
104      * @throws Exception
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      * Verify that elements that go in the region after this call take the new attributes.
128      * @throws Exception
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      * Verify that getCacheElements returns the elements requested based on the key.
158      * @throws Exception
159      */
160     public void testGetCacheElements()
161         throws Exception
162     {
163         //SETUP
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         //DO WORK
183         Map<String, ICacheElement<String, String>> result = access.getCacheElements( input );
184 
185         //VERIFY
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      * Verify that we can get a region using the define region method.
197      * @throws Exception
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      * Verify that we can get a region using the define region method with cache attributes.
208      * @throws Exception
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      * Verify that we can get a region using the define region method with cache attributes and
227      * element attributes.
228      * @throws Exception
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      * Verify we can get some matching elements..
251      * <p>
252      * @throws Exception
253      */
254     public void testGetMatching_Normal()
255         throws Exception
256     {
257         // SETUP
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         // DO WORK
273         int numToInsertPrefix1 = 10;
274         // insert with prefix1
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         // insert with prefix1
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         // VERIFY
291         assertEquals( "Wrong number returned 1:", numToInsertPrefix1, result1.size() );
292         assertEquals( "Wrong number returned 2:", numToInsertPrefix2, result2.size() );
293         //System.out.println( result1 );
294 
295         // verify that the elements are unwrapped
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      * Verify we can get some matching elements..
305      * <p>
306      * @throws Exception
307      */
308     public void testGetMatchingElements_Normal()
309         throws Exception
310     {
311         // SETUP
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         // DO WORK
327         int numToInsertPrefix1 = 10;
328         // insert with prefix1
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         // insert with prefix1
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         // VERIFY
345         assertEquals( "Wrong number returned 1:", numToInsertPrefix1, result1.size() );
346         assertEquals( "Wrong number returned 2:", numToInsertPrefix2, result2.size() );
347         //System.out.println( result1 );
348 
349         // verify that the elements are wrapped
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 }