1 package org.apache.commons.jcs.engine.memory.soft;
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 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
38
39
40
41 public class SoftReferenceMemoryCacheUnitTest
42 extends TestCase
43 {
44
45 @Override
46 public void setUp()
47 {
48 JCS.setConfigFilename( "/TestSoftReferenceCache.ccf" );
49 }
50
51
52
53
54 @Override
55 protected void tearDown() throws Exception
56 {
57 CompositeCacheManager.getInstance().shutDown();
58 }
59
60
61
62
63
64
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
77
78
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
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
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
116 }
117
118
119
120
121
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
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
151
152
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
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
179
180
181
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
210
211
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
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
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 }