View Javadoc
1   package org.apache.commons.jcs.auxiliary.disk;
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.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  /** Tests for the abstract disk cache. It's largely tested by actual instances. */
43  public class AbstractDiskCacheUnitTest
44      extends TestCase
45  {
46      /**
47       * Verify that update and get work.
48       * <p>
49       * @throws IOException
50       */
51      public void testUpdateGet_allowed()
52          throws IOException
53      {
54          // SETUP
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          // DO WORK
69          ICacheElement<String, String> result = diskCache.get( key );
70  
71          // VERIFY
72          //System.out.println( diskCache.getStats() );
73          assertNotNull( "Item should be in the map.", result );
74      }
75  
76      /**
77       * Verify that alive is set to false..
78       * <p>
79       * @throws IOException
80       */
81      public void testDispose()
82          throws IOException
83      {
84          // SETUP
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          // DO WORK
99          diskCache.dispose();
100 
101         // VERIFY
102         assertFalse( "disk cache should not be alive.", diskCache.isAlive() );
103         assertEquals( "Status should be disposed", CacheStatus.DISPOSED, diskCache.getStatus() );
104     }
105 
106     /**
107      * Verify that removeAll is prohibited.
108      * <p>
109      * @throws IOException
110      */
111     public void testRemoveAll_notAllowed()
112         throws IOException
113     {
114         // SETUP
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         // DO WORK
132         diskCache.removeAll();
133         String result = stringWriter.toString();
134 
135         // VERIFY
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      * Verify that removeAll is allowed.
142      * <p>
143      * @throws IOException
144      */
145     public void testRemoveAll_allowed()
146         throws IOException
147     {
148         // SETUP
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         // DO WORK
163         diskCache.removeAll();
164 
165         // VERIFY
166         assertNull( "Item should not be in the map.", diskCache.get( key ) );
167     }
168 
169     /** Concrete, testable instance. */
170     protected static class AbstractDiskCacheTestInstance<K, V>
171         extends AbstractDiskCache<K, V>
172     {
173         /** Internal map */
174         protected Map<K, ICacheElement<K, V>> map = new HashMap<K, ICacheElement<K, V>>();
175 
176         /** used by the abstract aux class */
177         protected IDiskCacheAttributes diskCacheAttributes;
178 
179         /**
180          * Creates the disk cache.
181          * <p>
182          * @param attr
183          */
184         public AbstractDiskCacheTestInstance( IDiskCacheAttributes attr )
185         {
186             super( attr );
187             diskCacheAttributes = attr;
188             setAlive(true);
189         }
190 
191         /**
192          * The location on disk
193          * <p>
194          * @return "memory"
195          */
196         @Override
197         protected String getDiskLocation()
198         {
199             return "memory";
200         }
201 
202         /**
203          * Return the keys in this cache.
204          * <p>
205          * @see org.apache.commons.jcs.auxiliary.disk.AbstractDiskCache#getKeySet()
206          */
207         @Override
208         public Set<K> getKeySet() throws IOException
209         {
210             return new HashSet<K>(map.keySet());
211         }
212 
213         /**
214          * @return map.size()
215          */
216         @Override
217         public int getSize()
218         {
219             return map.size();
220         }
221 
222         /**
223          * @throws IOException
224          */
225         @Override
226         protected void processDispose()
227             throws IOException
228         {
229             //System.out.println( "processDispose" );
230         }
231 
232         /**
233          * @param key
234          * @return ICacheElement
235          * @throws IOException
236          */
237         @Override
238         protected ICacheElement<K, V> processGet( K key )
239             throws IOException
240         {
241             //System.out.println( "processGet: " + key );
242             return map.get( key );
243         }
244 
245         /**
246          * @param pattern
247          * @return Collections.EMPTY_MAP
248          * @throws IOException
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          * @param key
259          * @return false
260          * @throws IOException
261          */
262         @Override
263         protected boolean processRemove( K key )
264             throws IOException
265         {
266             return map.remove( key ) != null;
267         }
268 
269         /**
270          * @throws IOException
271          */
272         @Override
273         protected void processRemoveAll()
274             throws IOException
275         {
276             //System.out.println( "processRemoveAll" );
277             map.clear();
278         }
279 
280         /**
281          * @param cacheElement
282          * @throws IOException
283          */
284         @Override
285         protected void processUpdate( ICacheElement<K, V> cacheElement )
286             throws IOException
287         {
288             //System.out.println( "processUpdate: " + cacheElement );
289             map.put( cacheElement.getKey(), cacheElement );
290         }
291 
292         /**
293          * @return null
294          */
295         @Override
296         public AuxiliaryCacheAttributes getAuxiliaryCacheAttributes()
297         {
298             return diskCacheAttributes;
299         }
300     }
301 }