1 package org.apache.commons.jcs3.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 org.apache.commons.jcs3.TestLogConfigurationUtil;
31 import org.apache.commons.jcs3.auxiliary.AuxiliaryCacheAttributes;
32 import org.apache.commons.jcs3.auxiliary.disk.behavior.IDiskCacheAttributes;
33 import org.apache.commons.jcs3.auxiliary.disk.indexed.IndexedDiskCacheAttributes;
34 import org.apache.commons.jcs3.engine.CacheElement;
35 import org.apache.commons.jcs3.engine.CacheStatus;
36 import org.apache.commons.jcs3.engine.ElementAttributes;
37 import org.apache.commons.jcs3.engine.behavior.ICacheElement;
38 import org.apache.commons.jcs3.engine.behavior.IElementAttributes;
39
40 import junit.framework.TestCase;
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 final String cacheName = "testUpdateGet_allowed";
56 final IDiskCacheAttributes diskCacheAttributes = new IndexedDiskCacheAttributes();
57 diskCacheAttributes.setCacheName( cacheName );
58
59 final AbstractDiskCacheTestInstance<String, String> diskCache = new AbstractDiskCacheTestInstance<>( diskCacheAttributes );
60
61 final String key = "myKey";
62 final String value = "myValue";
63 final IElementAttributes elementAttributes = new ElementAttributes();
64 final ICacheElement<String, String> cacheElement = new CacheElement<>( cacheName, key, value, elementAttributes );
65
66 diskCache.update( cacheElement );
67
68
69 final 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 final String cacheName = "testDispose";
86 final IDiskCacheAttributes diskCacheAttributes = new IndexedDiskCacheAttributes();
87 diskCacheAttributes.setCacheName( cacheName );
88
89 final AbstractDiskCacheTestInstance<String, String> diskCache = new AbstractDiskCacheTestInstance<>( diskCacheAttributes );
90
91 final String key = "myKey";
92 final String value = "myValue";
93 final IElementAttributes elementAttributes = new ElementAttributes();
94 final ICacheElement<String, String> cacheElement = new CacheElement<>( 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 final StringWriter stringWriter = new StringWriter();
116 TestLogConfigurationUtil.configureLogger( stringWriter, AbstractDiskCache.class.getName() );
117
118 final IDiskCacheAttributes diskCacheAttributes = new IndexedDiskCacheAttributes();
119 diskCacheAttributes.setAllowRemoveAll( false );
120
121 final AbstractDiskCacheTestInstance<String, String> diskCache = new AbstractDiskCacheTestInstance<>( diskCacheAttributes );
122
123 final String cacheName = "testRemoveAll_notAllowed";
124 final String key = "myKey";
125 final String value = "myValue";
126 final IElementAttributes elementAttributes = new ElementAttributes();
127 final ICacheElement<String, String> cacheElement = new CacheElement<>( cacheName, key, value, elementAttributes );
128
129 diskCache.update( cacheElement );
130
131
132 diskCache.removeAll();
133 final 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 final IDiskCacheAttributes diskCacheAttributes = new IndexedDiskCacheAttributes();
150 diskCacheAttributes.setAllowRemoveAll( true );
151
152 final AbstractDiskCacheTestInstance<String, String> diskCache = new AbstractDiskCacheTestInstance<>( diskCacheAttributes );
153
154 final String cacheName = "testRemoveAll_allowed";
155 final String key = "myKey";
156 final String value = "myValue";
157 final IElementAttributes elementAttributes = new ElementAttributes();
158 final ICacheElement<String, String> cacheElement = new CacheElement<>( 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<>();
175
176
177 protected IDiskCacheAttributes diskCacheAttributes;
178
179
180
181
182
183
184 public AbstractDiskCacheTestInstance( final 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<>(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( final 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( final String pattern )
252 throws IOException
253 {
254 return Collections.emptyMap();
255 }
256
257
258
259
260
261
262 @Override
263 protected boolean processRemove( final 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( final 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 }