1 package org.apache.commons.jcs3.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 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
37
38 public class SoftReferenceMemoryCacheUnitTest
39 extends TestCase
40 {
41
42 @Override
43 public void setUp()
44 {
45 JCS.setConfigFilename( "/TestSoftReferenceCache.ccf" );
46 }
47
48
49
50
51 @Override
52 protected void tearDown() throws Exception
53 {
54 CompositeCacheManager.getInstance().shutDown();
55 }
56
57
58
59
60
61
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
74
75
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
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
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
113 }
114
115
116
117
118
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
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
148
149
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
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
176
177
178
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
207
208
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
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
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 }