1   package org.apache.jcs.auxiliary.disk.indexed;
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 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          // SETUP
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          // DO WORK
71          for ( int i = 0; i < removeCount; i++ )
72          {
73              disk.doRemove( new Integer( i ) );
74          }
75  
76          SleepUtil.sleepAtLeast( 1000 );
77  
78          // VERIFY
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          // test is prone to failure for timing reasons.
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  }