View Javadoc
1   package org.apache.commons.jcs.engine.memory.lru;
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 junit.framework.TestCase;
23  import org.apache.commons.jcs.JCS;
24  import org.apache.commons.jcs.access.CacheAccess;
25  import org.apache.commons.jcs.access.exception.CacheException;
26  import org.apache.commons.jcs.engine.CacheElement;
27  import org.apache.commons.jcs.engine.behavior.ICacheElement;
28  import org.apache.commons.jcs.engine.control.CompositeCache;
29  import org.apache.commons.jcs.engine.control.CompositeCacheManager;
30  
31  import java.util.HashSet;
32  import java.util.Map;
33  import java.util.Set;
34  
35  /**
36   * Tests for the test LHMLRU implementation.
37   * <p>
38   * @author Aaron Smuts
39   */
40  public class LHMLRUMemoryCacheUnitTest
41      extends TestCase
42  {
43      /** Test setup */
44      @Override
45      public void setUp()
46      {
47          JCS.setConfigFilename( "/TestLHMLRUCache.ccf" );
48      }
49  
50      /**
51       * Verify that the mru gets used by a non-defined region when it is set as the default in the
52       * default region.
53       * <p>
54       * @throws CacheException
55       */
56      public void testLoadFromCCF()
57          throws CacheException
58      {
59          CacheAccess<String, String> cache = JCS.getInstance( "testLoadFromCCF" );
60          String memoryCacheName = cache.getCacheAttributes().getMemoryCacheName();
61          assertTrue( "Cache name should have LHMLRU in it.", memoryCacheName.indexOf( "LHMLRUMemoryCache" ) != -1 );
62      }
63  
64      /**
65       * put twice as many as the max.  verify that the second half is in the cache.
66       * <p>
67       * @throws CacheException
68       */
69      public void testPutGetThroughHub()
70          throws CacheException
71      {
72          CacheAccess<String, String> cache = JCS.getInstance( "testPutGetThroughHub" );
73  
74          int max = cache.getCacheAttributes().getMaxObjects();
75          int items = max * 2;
76  
77          for ( int i = 0; i < items; i++ )
78          {
79              cache.put( i + ":key", "myregion" + " data " + i );
80          }
81  
82          // Test that first items are not in the cache
83          for ( int i = max -1; i >= 0; i-- )
84          {
85              String value = cache.get( i + ":key" );
86              assertNull( "Should not have value for key [" + i + ":key" + "] in the cache." + cache.getStats(), value );
87          }
88  
89          // Test that last items are in cache
90          // skip 2 for the buffer.
91          for ( int i = max + 2; i < items; i++ )
92          {
93              String value = cache.get( i + ":key" );
94              assertEquals( "myregion" + " data " + i, value );
95          }
96  
97          // Test that getMultiple returns all the items remaining in cache and none of the missing ones
98          Set<String> keys = new HashSet<String>();
99          for ( int i = 0; i < items; i++ )
100         {
101             keys.add( i + ":key" );
102         }
103 
104         Map<String, ICacheElement<String, String>> elements = cache.getCacheElements( keys );
105         for ( int i = max-1; i >= 0; i-- )
106         {
107             assertNull( "Should not have value for key [" + i + ":key" + "] in the cache." + cache.getStats(), elements.get( i + ":key" ) );
108         }
109         for ( int i = max + 2; i < items; i++ )
110         {
111             ICacheElement<String, String> element = elements.get( i + ":key" );
112             assertNotNull( "element " + i + ":key is missing", element );
113             assertEquals( "value " + i + ":key", "myregion" + " data " + i, element.getVal() );
114         }
115     }
116 
117     /**
118      * Put twice as many as the max, twice. verify that the second half is in the cache.
119      * <p>
120      * @throws CacheException
121      */
122     public void testPutGetThroughHubTwice()
123         throws CacheException
124     {
125         CacheAccess<String, String> cache = JCS.getInstance( "testPutGetThroughHubTwice" );
126 
127         int max = cache.getCacheAttributes().getMaxObjects();
128         int items = max * 2;
129 
130         for ( int i = 0; i < items; i++ )
131         {
132             cache.put( i + ":key", "myregion" + " data " + i );
133         }
134 
135         for ( int i = 0; i < items; i++ )
136         {
137             cache.put( i + ":key", "myregion" + " data " + i );
138         }
139 
140         // Test that first items are not in the cache
141         for ( int i = max -1; i >= 0; i-- )
142         {
143             String value = cache.get( i + ":key" );
144             assertNull( "Should not have value for key [" + i + ":key" + "] in the cache.", value );
145         }
146 
147         // Test that last items are in cache
148         // skip 2 for the buffer.
149         for ( int i = max + 2; i < items; i++ )
150         {
151             String value = cache.get( i + ":key" );
152             assertEquals( "myregion" + " data " + i, value );
153         }
154 
155     }
156 
157     /**
158      * put the max and remove each. verify that they are all null.
159      * <p>
160      * @throws CacheException
161      */
162     public void testPutRemoveThroughHub()
163         throws CacheException
164     {
165         CacheAccess<String, String> cache = JCS.getInstance( "testPutRemoveThroughHub" );
166 
167         int max = cache.getCacheAttributes().getMaxObjects();
168         int items = max * 2;
169 
170         for ( int i = 0; i < items; i++ )
171         {
172             cache.put( i + ":key", "myregion" + " data " + i );
173         }
174 
175         for ( int i = 0; i < items; i++ )
176         {
177             cache.remove( i + ":key" );
178         }
179 
180         // Test that first items are not in the cache
181         for ( int i = max; i >= 0; i-- )
182         {
183             String value = cache.get( i + ":key" );
184             assertNull( "Should not have value for key [" + i + ":key" + "] in the cache.", value );
185         }
186     }
187 
188     /**
189      * put the max and clear. verify that no elements remain.
190      * <p>
191      * @throws CacheException
192      */
193     public void testClearThroughHub()
194         throws CacheException
195     {
196         CacheAccess<String, String> cache = JCS.getInstance( "testClearThroughHub" );
197 
198         int max = cache.getCacheAttributes().getMaxObjects();
199         int items = max * 2;
200 
201         for ( int i = 0; i < items; i++ )
202         {
203             cache.put( i + ":key", "myregion" + " data " + i );
204         }
205 
206         cache.clear();
207 
208         // Test that first items are not in the cache
209         for ( int i = max; i >= 0; i-- )
210         {
211             String value = cache.get( i + ":key" );
212             assertNull( "Should not have value for key [" + i + ":key" + "] in the cache.", value );
213         }
214     }
215 
216     /**
217      * Get stats.
218      * <p>
219      * @throws CacheException
220      */
221     public void testGetStatsThroughHub()
222         throws CacheException
223     {
224         CacheAccess<String, String> cache = JCS.getInstance( "testGetStatsThroughHub" );
225 
226         int max = cache.getCacheAttributes().getMaxObjects();
227         int items = max * 2;
228 
229         for ( int i = 0; i < items; i++ )
230         {
231             cache.put( i + ":key", "myregion" + " data " + i );
232         }
233 
234         String stats = cache.getStats();
235 
236         //System.out.println( stats );
237 
238         // TODO improve stats check
239         assertTrue( "Should have 200 puts" + stats, stats.indexOf( "200" ) != -1 );
240     }
241 
242     /**
243      * Put half the max and clear. get the key array and verify that it has the correct number of
244      * items.
245      * <p>
246      * @throws Exception
247      */
248     public void testGetKeyArray()
249         throws Exception
250     {
251         CompositeCacheManager cacheMgr = CompositeCacheManager.getUnconfiguredInstance();
252         cacheMgr.configure( "/TestLHMLRUCache.ccf" );
253         CompositeCache<String, String> cache = cacheMgr.getCache( "testGetKeyArray" );
254 
255         LHMLRUMemoryCache<String, String> mru = new LHMLRUMemoryCache<String, String>();
256         mru.initialize( cache );
257 
258         int max = cache.getCacheAttributes().getMaxObjects();
259         int items = max / 2;
260 
261         for ( int i = 0; i < items; i++ )
262         {
263             ICacheElement<String, String> ice = new CacheElement<String, String>( cache.getCacheName(), i + ":key", cache.getCacheName() + " data " + i );
264             ice.setElementAttributes( cache.getElementAttributes() );
265             mru.update( ice );
266         }
267 
268         Set<String> keys = mru.getKeySet();
269 
270         assertEquals( "Wrong number of keys.", items, keys.size() );
271     }
272 
273     /**
274      * Add a few keys with the delimiter. Remove them.
275      * <p>
276      * @throws CacheException
277      */
278     public void testRemovePartialThroughHub()
279         throws CacheException
280     {
281         CacheAccess<String, String> cache = JCS.getInstance( "testRemovePartialThroughHub" );
282 
283         int max = cache.getCacheAttributes().getMaxObjects();
284         int items = max / 2;
285 
286         cache.put( "test", "data" );
287 
288         String root = "myroot";
289 
290         for ( int i = 0; i < items; i++ )
291         {
292             cache.put( root + ":" + i + ":key", "myregion" + " data " + i );
293         }
294 
295         // Test that last items are in cache
296         for ( int i = 0; i < items; i++ )
297         {
298             String value = cache.get( root + ":" + i + ":key" );
299             assertEquals( "myregion" + " data " + i, value );
300         }
301 
302         // remove partial
303         cache.remove( root + ":" );
304 
305         for ( int i = 0; i < items; i++ )
306         {
307             assertNull( "Should have been removed by partial loop.", cache.get( root + ":" + i + ":key" ) );
308         }
309 
310         assertNotNull( "Other item should be in the cache.", cache.get( "test" ) );
311     }
312 }