1   package org.apache.jcs.engine.control;
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.Serializable;
24  import java.util.Set;
25  
26  import junit.framework.TestCase;
27  
28  import org.apache.jcs.JCS;
29  import org.apache.jcs.access.exception.CacheException;
30  import org.apache.jcs.auxiliary.AuxiliaryCache;
31  import org.apache.jcs.auxiliary.AuxiliaryCacheAttributes;
32  import org.apache.jcs.engine.CacheElement;
33  import org.apache.jcs.engine.CompositeCacheAttributes;
34  import org.apache.jcs.engine.ElementAttributes;
35  import org.apache.jcs.engine.behavior.ICacheElement;
36  import org.apache.jcs.engine.behavior.ICompositeCacheAttributes;
37  import org.apache.jcs.engine.behavior.IElementAttributes;
38  import org.apache.jcs.engine.stats.behavior.IStats;
39  
40  /***
41   * Tests of the disk usage settings for the CompositeCache.
42   * <p>
43   * @author Aaron Smuts
44   */
45  public class CompositeCacheDiskUsageUnitTest
46      extends TestCase
47  {
48      /***
49       * Test setup
50       */
51      public void setUp()
52      {
53          JCS.setConfigFilename( "/TestDiskCacheUsagePattern.ccf" );
54      }
55  
56      /***
57       * Verify that the swap region is set to the correct pattern.
58       * <p>
59       * @throws CacheException
60       */
61      public void testSwapConfig() throws CacheException
62      {
63          JCS swap = JCS.getInstance( "Swap" );
64          assertEquals( ICompositeCacheAttributes.DISK_USAGE_PATTERN_SWAP, swap.getCacheAttributes().getDiskUsagePattern() );
65      }
66  
67      /***
68       * Verify that the swap region is set to the correct pattern.
69       * <p>
70       * @throws CacheException
71       */
72      public void testUpdateConfig() throws CacheException
73      {
74          JCS swap = JCS.getInstance( "Update" );
75          assertEquals( ICompositeCacheAttributes.DISK_USAGE_PATTERN_UPDATE, swap.getCacheAttributes().getDiskUsagePattern() );
76      }
77  
78      /***
79       * Setup a disk cache. Configure the disk usage pattern to swap. Call spool. Verify that the
80       * item is put to disk.
81       */
82      public void testSpoolAllowed()
83      {
84          // SETUP
85          ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
86          cattr.setDiskUsagePattern( ICompositeCacheAttributes.DISK_USAGE_PATTERN_SWAP );
87  
88          IElementAttributes attr = new ElementAttributes();
89  
90          CompositeCache cache = new CompositeCache( "testSpoolAllowed", cattr, attr );
91  
92          MockAuxCache mock = new MockAuxCache();
93          mock.cacheType = AuxiliaryCache.DISK_CACHE;
94  
95          cache.setAuxCaches( new AuxiliaryCache[] { mock } );
96  
97          ICacheElement inputElement = new CacheElement( "testSpoolAllowed", "key", "value" );
98  
99          // DO WORK
100         cache.spoolToDisk( inputElement );
101 
102         // VERIFY
103         assertEquals( "Wrong number of calls to the disk cache update.", 1, mock.updateCount );
104         assertEquals( "Wrong element updated.", inputElement, mock.lastUpdatedItem );
105     }
106 
107     /***
108      * Setup a disk cache. Configure the disk usage pattern to not swap. Call spool. Verify that the
109      * item is not put to disk.
110      */
111     public void testSpoolNotAllowed()
112     {
113         // SETUP
114         ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
115         cattr.setDiskUsagePattern( ICompositeCacheAttributes.DISK_USAGE_PATTERN_UPDATE );
116 
117         IElementAttributes attr = new ElementAttributes();
118 
119         CompositeCache cache = new CompositeCache( "testSpoolAllowed", cattr, attr );
120 
121         MockAuxCache mock = new MockAuxCache();
122         mock.cacheType = AuxiliaryCache.DISK_CACHE;
123 
124         cache.setAuxCaches( new AuxiliaryCache[] { mock } );
125 
126         ICacheElement inputElement = new CacheElement( "testSpoolAllowed", "key", "value" );
127 
128         // DO WORK
129         cache.spoolToDisk( inputElement );
130 
131         // VERIFY
132         assertEquals( "Wrong number of calls to the disk cache update.", 0, mock.updateCount );
133     }
134 
135     /***
136      * Setup a disk cache. Configure the disk usage pattern to UPDATE. Call updateAuxiliaries.
137      * Verify that the item is put to disk.
138      * <p>
139      * This tests that the items are put to disk on a normal put when the usage pattern is set
140      * appropriately.
141      * @throws IOException
142      */
143     public void testUpdateAllowed()
144         throws IOException
145     {
146         // SETUP
147         ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
148         cattr.setDiskUsagePattern( ICompositeCacheAttributes.DISK_USAGE_PATTERN_UPDATE );
149 
150         IElementAttributes attr = new ElementAttributes();
151 
152         CompositeCache cache = new CompositeCache( "testSpoolAllowed", cattr, attr );
153 
154         MockAuxCache mock = new MockAuxCache();
155         mock.cacheType = AuxiliaryCache.DISK_CACHE;
156 
157         cache.setAuxCaches( new AuxiliaryCache[] { mock } );
158 
159         ICacheElement inputElement = new CacheElement( "testSpoolAllowed", "key", "value" );
160 
161         // DO WORK
162         cache.updateAuxiliaries( inputElement, true );
163 
164         // VERIFY
165         assertEquals( "Wrong number of calls to the disk cache update.", 1, mock.updateCount );
166         assertEquals( "Wrong element updated.", inputElement, mock.lastUpdatedItem );
167     }
168 
169     /***
170      * Setup a disk cache. Configure the disk usage pattern to UPDATE. Call updateAuxiliaries with
171      * local only set to false. Verify that the item is put to disk.
172      * <p>
173      * This tests that the items are put to disk on a normal put when the usage pattern is set
174      * appropriately. The local setting should have no impact on whether the item goes to disk.
175      * <p>
176      * @throws IOException
177      */
178     public void testUpdateAllowed_localFalse()
179         throws IOException
180     {
181         // SETUP
182         ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
183         cattr.setDiskUsagePattern( ICompositeCacheAttributes.DISK_USAGE_PATTERN_UPDATE );
184 
185         IElementAttributes attr = new ElementAttributes();
186 
187         CompositeCache cache = new CompositeCache( "testSpoolAllowed", cattr, attr );
188 
189         MockAuxCache mock = new MockAuxCache();
190         mock.cacheType = AuxiliaryCache.DISK_CACHE;
191 
192         cache.setAuxCaches( new AuxiliaryCache[] { mock } );
193 
194         ICacheElement inputElement = new CacheElement( "testSpoolAllowed", "key", "value" );
195 
196         // DO WORK
197         cache.updateAuxiliaries( inputElement, false );
198 
199         // VERIFY
200         assertEquals( "Wrong number of calls to the disk cache update.", 1, mock.updateCount );
201         assertEquals( "Wrong element updated.", inputElement, mock.lastUpdatedItem );
202     }
203 
204     /***
205      * Setup a disk cache. Configure the disk usage pattern to SWAP. Call updateAuxiliaries. Verify
206      * that the item is not put to disk.
207      * <p>
208      * This tests that the items are not put to disk on a normal put when the usage pattern is set
209      * to SWAP.
210      * <p>
211      * @throws IOException
212      */
213     public void testUpdateNotAllowed()
214         throws IOException
215     {
216         // SETUP
217         ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
218         cattr.setDiskUsagePattern( ICompositeCacheAttributes.DISK_USAGE_PATTERN_SWAP );
219 
220         IElementAttributes attr = new ElementAttributes();
221 
222         CompositeCache cache = new CompositeCache( "testSpoolAllowed", cattr, attr );
223 
224         MockAuxCache mock = new MockAuxCache();
225         mock.cacheType = AuxiliaryCache.DISK_CACHE;
226 
227         cache.setAuxCaches( new AuxiliaryCache[] { mock } );
228 
229         ICacheElement inputElement = new CacheElement( "testSpoolAllowed", "key", "value" );
230 
231         // DO WORK
232         cache.updateAuxiliaries( inputElement, true );
233 
234         // VERIFY
235         assertEquals( "Wrong number of calls to the disk cache update.", 0, mock.updateCount );
236     }
237 
238     /***
239      * Setup a disk cache. Configure the disk usage pattern to UPDATE. Call updateAuxiliaries.
240      * Verify that the item is put to disk.
241      * <p>
242      * This tests that the items are put to disk on a normal put when the usage pattern is set
243      * appropriately.
244      * @throws IOException
245      */
246     public void testUpdateAllowed_withOtherCaches()
247         throws IOException
248     {
249         // SETUP
250         ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
251         cattr.setDiskUsagePattern( ICompositeCacheAttributes.DISK_USAGE_PATTERN_UPDATE );
252 
253         IElementAttributes attr = new ElementAttributes();
254 
255         CompositeCache cache = new CompositeCache( "testSpoolAllowed", cattr, attr );
256 
257         MockAuxCache mock = new MockAuxCache();
258         mock.cacheType = AuxiliaryCache.DISK_CACHE;
259 
260         MockAuxCache mockLateral = new MockAuxCache();
261         mockLateral.cacheType = AuxiliaryCache.LATERAL_CACHE;
262 
263         cache.setAuxCaches( new AuxiliaryCache[] { mock, mockLateral } );
264 
265         ICacheElement inputElement = new CacheElement( "testSpoolAllowed", "key", "value" );
266 
267         // DO WORK
268         cache.updateAuxiliaries( inputElement, false );
269 
270         // VERIFY
271         assertEquals( "Wrong number of calls to the disk cache update.", 1, mock.updateCount );
272         assertEquals( "Wrong element updated.", inputElement, mock.lastUpdatedItem );
273 
274         assertEquals( "Wrong number of calls to the lateral cache update.", 1, mockLateral.updateCount );
275         assertEquals( "Wrong element updated with lateral.", inputElement, mockLateral.lastUpdatedItem );
276     }
277 
278     /***
279      * Used to test the disk cache functionality.
280      * <p>
281      * @author Aaron Smuts
282      */
283     public class MockAuxCache
284         implements AuxiliaryCache
285     {
286         private static final long serialVersionUID = 1L;
287 
288         /***
289          * The last item passed to update.
290          */
291         public ICacheElement lastUpdatedItem;
292 
293         /***
294          * The number of times update was called.
295          */
296         public int updateCount = 0;
297 
298         /***
299          * The type that should be returned from getCacheType.
300          */
301         public int cacheType = AuxiliaryCache.DISK_CACHE;
302 
303         /***
304          * Resets counters and catchers.
305          */
306         public void reset()
307         {
308             updateCount = 0;
309             lastUpdatedItem = null;
310         }
311 
312         /*
313          * (non-Javadoc)
314          * @see org.apache.jcs.auxiliary.AuxiliaryCache#update(org.apache.jcs.engine.behavior.ICacheElement)
315          */
316         public void update( ICacheElement ce )
317             throws IOException
318         {
319             lastUpdatedItem = ce;
320             updateCount++;
321         }
322 
323         /*
324          * (non-Javadoc)
325          * @see org.apache.jcs.auxiliary.AuxiliaryCache#get(java.io.Serializable)
326          */
327         public ICacheElement get( Serializable key )
328             throws IOException
329         {
330             // TODO Auto-generated method stub
331             return null;
332         }
333 
334         /*
335          * (non-Javadoc)
336          * @see org.apache.jcs.auxiliary.AuxiliaryCache#remove(java.io.Serializable)
337          */
338         public boolean remove( Serializable key )
339             throws IOException
340         {
341             // TODO Auto-generated method stub
342             return false;
343         }
344 
345         /*
346          * (non-Javadoc)
347          * @see org.apache.jcs.auxiliary.AuxiliaryCache#removeAll()
348          */
349         public void removeAll()
350             throws IOException
351         {
352             // TODO Auto-generated method stub
353 
354         }
355 
356         /*
357          * (non-Javadoc)
358          * @see org.apache.jcs.auxiliary.AuxiliaryCache#dispose()
359          */
360         public void dispose()
361             throws IOException
362         {
363             // TODO Auto-generated method stub
364 
365         }
366 
367         /*
368          * (non-Javadoc)
369          * @see org.apache.jcs.auxiliary.AuxiliaryCache#getSize()
370          */
371         public int getSize()
372         {
373             // TODO Auto-generated method stub
374             return 0;
375         }
376 
377         /*
378          * (non-Javadoc)
379          * @see org.apache.jcs.auxiliary.AuxiliaryCache#getStatus()
380          */
381         public int getStatus()
382         {
383             // TODO Auto-generated method stub
384             return 0;
385         }
386 
387         /*
388          * (non-Javadoc)
389          * @see org.apache.jcs.auxiliary.AuxiliaryCache#getCacheName()
390          */
391         public String getCacheName()
392         {
393             // TODO Auto-generated method stub
394             return null;
395         }
396 
397         /*
398          * (non-Javadoc)
399          * @see org.apache.jcs.auxiliary.AuxiliaryCache#getGroupKeys(java.lang.String)
400          */
401         public Set getGroupKeys( String group )
402             throws IOException
403         {
404             // TODO Auto-generated method stub
405             return null;
406         }
407 
408         /*
409          * (non-Javadoc)
410          * @see org.apache.jcs.auxiliary.AuxiliaryCache#getStatistics()
411          */
412         public IStats getStatistics()
413         {
414             // TODO Auto-generated method stub
415             return null;
416         }
417 
418         /*
419          * (non-Javadoc)
420          * @see org.apache.jcs.engine.behavior.ICache#getStats()
421          */
422         public String getStats()
423         {
424             // TODO Auto-generated method stub
425             return null;
426         }
427 
428         /***
429          * Returns the setup cache type. This allows you to use this mock as multiple cache types.
430          * <p>
431          * (non-Javadoc)
432          * @see org.apache.jcs.engine.behavior.ICacheType#getCacheType()
433          */
434         public int getCacheType()
435         {
436             return cacheType;
437         }
438 
439         /***
440          * @return Returns the AuxiliaryCacheAttributes.
441          */
442         public AuxiliaryCacheAttributes getAuxiliaryCacheAttributes()
443         {
444             return null;
445         }
446     }
447 
448 }