1 package org.apache.commons.jcs.auxiliary.disk;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.IOException;
23 import java.io.StringWriter;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.Map;
28 import java.util.Set;
29
30 import junit.framework.TestCase;
31
32 import org.apache.commons.jcs.TestLogConfigurationUtil;
33 import org.apache.commons.jcs.auxiliary.AuxiliaryCacheAttributes;
34 import org.apache.commons.jcs.auxiliary.disk.behavior.IDiskCacheAttributes;
35 import org.apache.commons.jcs.auxiliary.disk.indexed.IndexedDiskCacheAttributes;
36 import org.apache.commons.jcs.engine.CacheElement;
37 import org.apache.commons.jcs.engine.CacheStatus;
38 import org.apache.commons.jcs.engine.ElementAttributes;
39 import org.apache.commons.jcs.engine.behavior.ICacheElement;
40 import org.apache.commons.jcs.engine.behavior.IElementAttributes;
41
42
43 public class AbstractDiskCacheUnitTest
44 extends TestCase
45 {
46
47
48
49
50
51 public void testUpdateGet_allowed()
52 throws IOException
53 {
54
55 String cacheName = "testUpdateGet_allowed";
56 IDiskCacheAttributes diskCacheAttributes = new IndexedDiskCacheAttributes();
57 diskCacheAttributes.setCacheName( cacheName );
58
59 AbstractDiskCacheTestInstance<String, String> diskCache = new AbstractDiskCacheTestInstance<String, String>( diskCacheAttributes );
60
61 String key = "myKey";
62 String value = "myValue";
63 IElementAttributes elementAttributes = new ElementAttributes();
64 ICacheElement<String, String> cacheElement = new CacheElement<String, String>( cacheName, key, value, elementAttributes );
65
66 diskCache.update( cacheElement );
67
68
69 ICacheElement<String, String> result = diskCache.get( key );
70
71
72
73 assertNotNull( "Item should be in the map.", result );
74 }
75
76
77
78
79
80
81 public void testDispose()
82 throws IOException
83 {
84
85 String cacheName = "testDispose";
86 IDiskCacheAttributes diskCacheAttributes = new IndexedDiskCacheAttributes();
87 diskCacheAttributes.setCacheName( cacheName );
88
89 AbstractDiskCacheTestInstance<String, String> diskCache = new AbstractDiskCacheTestInstance<String, String>( diskCacheAttributes );
90
91 String key = "myKey";
92 String value = "myValue";
93 IElementAttributes elementAttributes = new ElementAttributes();
94 ICacheElement<String, String> cacheElement = new CacheElement<String, String>( cacheName, key, value, elementAttributes );
95
96 diskCache.update( cacheElement );
97
98
99 diskCache.dispose();
100
101
102 assertFalse( "disk cache should not be alive.", diskCache.isAlive() );
103 assertEquals( "Status should be disposed", CacheStatus.DISPOSED, diskCache.getStatus() );
104 }
105
106
107
108
109
110
111 public void testRemoveAll_notAllowed()
112 throws IOException
113 {
114
115 StringWriter stringWriter = new StringWriter();
116 TestLogConfigurationUtil.configureLogger( stringWriter, AbstractDiskCache.class.getName() );
117
118 IDiskCacheAttributes diskCacheAttributes = new IndexedDiskCacheAttributes();
119 diskCacheAttributes.setAllowRemoveAll( false );
120
121 AbstractDiskCacheTestInstance<String, String> diskCache = new AbstractDiskCacheTestInstance<String, String>( diskCacheAttributes );
122
123 String cacheName = "testRemoveAll_notAllowed";
124 String key = "myKey";
125 String value = "myValue";
126 IElementAttributes elementAttributes = new ElementAttributes();
127 ICacheElement<String, String> cacheElement = new CacheElement<String, String>( cacheName, key, value, elementAttributes );
128
129 diskCache.update( cacheElement );
130
131
132 diskCache.removeAll();
133 String result = stringWriter.toString();
134
135
136 assertTrue( "Should say not allowed.", result.indexOf( "set to false" ) != -1 );
137 assertNotNull( "Item should be in the map.", diskCache.get( key ) );
138 }
139
140
141
142
143
144
145 public void testRemoveAll_allowed()
146 throws IOException
147 {
148
149 IDiskCacheAttributes diskCacheAttributes = new IndexedDiskCacheAttributes();
150 diskCacheAttributes.setAllowRemoveAll( true );
151
152 AbstractDiskCacheTestInstance<String, String> diskCache = new AbstractDiskCacheTestInstance<String, String>( diskCacheAttributes );
153
154 String cacheName = "testRemoveAll_allowed";
155 String key = "myKey";
156 String value = "myValue";
157 IElementAttributes elementAttributes = new ElementAttributes();
158 ICacheElement<String, String> cacheElement = new CacheElement<String, String>( cacheName, key, value, elementAttributes );
159
160 diskCache.update( cacheElement );
161
162
163 diskCache.removeAll();
164
165
166 assertNull( "Item should not be in the map.", diskCache.get( key ) );
167 }
168
169
170 protected static class AbstractDiskCacheTestInstance<K, V>
171 extends AbstractDiskCache<K, V>
172 {
173
174 protected Map<K, ICacheElement<K, V>> map = new HashMap<K, ICacheElement<K, V>>();
175
176
177 protected IDiskCacheAttributes diskCacheAttributes;
178
179
180
181
182
183
184 public AbstractDiskCacheTestInstance( IDiskCacheAttributes attr )
185 {
186 super( attr );
187 diskCacheAttributes = attr;
188 setAlive(true);
189 }
190
191
192
193
194
195
196 @Override
197 protected String getDiskLocation()
198 {
199 return "memory";
200 }
201
202
203
204
205
206
207 @Override
208 public Set<K> getKeySet() throws IOException
209 {
210 return new HashSet<K>(map.keySet());
211 }
212
213
214
215
216 @Override
217 public int getSize()
218 {
219 return map.size();
220 }
221
222
223
224
225 @Override
226 protected void processDispose()
227 throws IOException
228 {
229
230 }
231
232
233
234
235
236
237 @Override
238 protected ICacheElement<K, V> processGet( K key )
239 throws IOException
240 {
241
242 return map.get( key );
243 }
244
245
246
247
248
249
250 @Override
251 protected Map<K, ICacheElement<K, V>> processGetMatching( String pattern )
252 throws IOException
253 {
254 return Collections.emptyMap();
255 }
256
257
258
259
260
261
262 @Override
263 protected boolean processRemove( K key )
264 throws IOException
265 {
266 return map.remove( key ) != null;
267 }
268
269
270
271
272 @Override
273 protected void processRemoveAll()
274 throws IOException
275 {
276
277 map.clear();
278 }
279
280
281
282
283
284 @Override
285 protected void processUpdate( ICacheElement<K, V> cacheElement )
286 throws IOException
287 {
288
289 map.put( cacheElement.getKey(), cacheElement );
290 }
291
292
293
294
295 @Override
296 public AuxiliaryCacheAttributes getAuxiliaryCacheAttributes()
297 {
298 return diskCacheAttributes;
299 }
300 }
301 }