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