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