1 package org.apache.jcs.auxiliary.disk.indexed;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import junit.framework.TestCase;
23
24 import org.apache.jcs.engine.behavior.ICacheElement;
25 import org.apache.jcs.utils.timing.SleepUtil;
26
27 /***
28 * Tests for the optimization routine.
29 * <p>
30 * @author Aaron Smuts
31 */
32 public class IndexedDiskCacheOptimizationUnitTest
33 extends TestCase
34 {
35 /***
36 * Set the optimize at remove count to 10. Add 20. Check the file size. Remove 10. Check the
37 * times optimized. Check the file size.
38 * @throws Exception
39 */
40 public void testBasicOptimization()
41 throws Exception
42 {
43
44 int removeCount = 50;
45
46 IndexedDiskCacheAttributes cattr = new IndexedDiskCacheAttributes();
47 cattr.setCacheName( "testOptimization" );
48 cattr.setMaxKeySize( removeCount * 3 );
49 cattr.setOptimizeAtRemoveCount( removeCount );
50 cattr.setMaxRecycleBinSize( removeCount * 3 );
51 cattr.setDiskPath( "target/test-sandbox/testOptimization" );
52 IndexedDiskCache disk = new IndexedDiskCache( cattr );
53
54 disk.removeAll();
55
56 int numberToInsert = removeCount * 2;
57 ICacheElement[] elements = DiskTestObjectUtil
58 .createCacheElementsWithTestObjectsOfVariableSizes( numberToInsert, cattr.getCacheName() );
59
60 for ( int i = 0; i < elements.length; i++ )
61 {
62 disk.doUpdate( elements[i] );
63 }
64
65 Thread.sleep( 1000 );
66 long sizeBeforeRemove = disk.getDataFileSize();
67 System.out.println( "file sizeBeforeRemove " + sizeBeforeRemove );
68 System.out.println( "totalSize inserted " + DiskTestObjectUtil.totalSize( elements, numberToInsert ) );
69
70
71 for ( int i = 0; i < removeCount; i++ )
72 {
73 disk.doRemove( new Integer( i ) );
74 }
75
76 SleepUtil.sleepAtLeast( 1000 );
77
78
79 long sizeAfterRemove = disk.getDataFileSize();
80 System.out.println( "file sizeAfterRemove " + sizeAfterRemove );
81 long expectedSizeAfterRemove = DiskTestObjectUtil.totalSize( elements, removeCount, elements.length );
82 System.out.println( "totalSize expected after remove " + expectedSizeAfterRemove );
83
84
85 if ( expectedSizeAfterRemove != sizeAfterRemove )
86 {
87 SleepUtil.sleepAtLeast( 2000 );
88 }
89
90 assertTrue( "The post optimization size should be smaller.", sizeAfterRemove < sizeBeforeRemove );
91 assertEquals( "The file size is not as expected size.", expectedSizeAfterRemove, sizeAfterRemove );
92 }
93 }