View Javadoc
1   package org.apache.commons.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 java.io.IOException;
23  
24  import org.apache.commons.jcs.auxiliary.disk.DiskTestObject;
25  import org.apache.commons.jcs.auxiliary.disk.behavior.IDiskCacheAttributes.DiskLimitType;
26  import org.apache.commons.jcs.engine.CacheElement;
27  import org.apache.commons.jcs.engine.behavior.ICacheElement;
28  
29  public class IndexDiskCacheSizeUnitTest extends IndexDiskCacheUnitTestAbstract {
30  
31  	@Override
32  	public IndexedDiskCacheAttributes getCacheAttributes() {
33  		IndexedDiskCacheAttributes ret = new IndexedDiskCacheAttributes();
34  		ret.setDiskLimitType(DiskLimitType.SIZE);
35  		return ret;
36  	}
37  	  public void testRecycleBin()
38  		        throws IOException
39  		    {
40  		        IndexedDiskCacheAttributes cattr = getCacheAttributes();
41  		        cattr.setCacheName( "testRemoveItems" );
42  		        cattr.setOptimizeAtRemoveCount( 7 );
43  		        cattr.setMaxKeySize( 8); // 1kb DiskTestObject takes 1420 bytes, so 5*1420 = 7100, so to keep 5 ojbects, we need max key size of 8
44  		        cattr.setMaxPurgatorySize( 0 );
45  		        cattr.setDiskPath( "target/test-sandbox/BreakIndexTest" );
46  		        IndexedDiskCache<String, DiskTestObject> disk = new IndexedDiskCache<String, DiskTestObject>( cattr );
47  
48  		        String[] test = { "a", "bb", "ccc", "dddd", "eeeee", "ffffff", "ggggggg", "hhhhhhhhh", "iiiiiiiiii" };
49  		        String[] expect = { null, "bb", "ccc", null, null, "ffffff", null, "hhhhhhhhh", "iiiiiiiiii" };
50  		        DiskTestObject value = DiskTestObjectUtil.createCacheElementsWithTestObjects( 1, 1, cattr .getCacheName())[0].getVal();
51  		        //System.out.println( "------------------------- testRecycleBin " );
52  
53  		        for ( int i = 0; i < 6; i++ )
54  		        {
55  		            ICacheElement<String, DiskTestObject> element = new CacheElement<String, DiskTestObject>( "testRecycleBin", "key:" + test[i], value);
56  		            //System.out.println( "About to add " + "key:" + test[i] + " i = " + i );
57  		            disk.processUpdate( element );
58  		        }
59  
60  		        for ( int i = 3; i < 5; i++ )
61  		        {
62  		            //System.out.println( "About to remove " + "key:" + test[i] + " i = " + i );
63  		            disk.remove( "key:" + test[i] );
64  		        }
65  
66  		        // there was a bug where 7 would try to be put in the empty slot left by 4's removal, but it
67  		        // will not fit.
68  		        for ( int i = 7; i < 9; i++ )
69  		        {
70  		            ICacheElement<String, DiskTestObject> element = new CacheElement<String, DiskTestObject>( "testRecycleBin", "key:" + test[i], value);
71  		            //System.out.println( "About to add " + "key:" + test[i] + " i = " + i );
72  		            disk.processUpdate( element );
73  		        }
74  
75  		        try
76  		        {
77  		            for ( int i = 0; i < 9; i++ )
78  		            {
79  		                ICacheElement<String, DiskTestObject> element = disk.get( "key:" + test[i] );
80  		                if ( element != null )
81  		                {
82  		                    //System.out.println( "element = " + element.getVal() );
83  		                }
84  		                else
85  		                {
86  		                    //System.out.println( "null --" + "key:" + test[i] );
87  		                }
88  
89  		                String expectedValue = expect[i];
90  		                if ( expectedValue == null )
91  		                {
92  		                    assertNull( "Expected a null element", element );
93  		                }
94  		                else
95  		                {
96  		                    assertNotNull( "The element for key [" + "key:" + test[i] + "] should not be null. i = " + i,
97  		                                   element );
98  		                    assertEquals( "Elements contents do not match expected", element.getVal(), value );
99  		                }
100 		            }
101 		        }
102 		        catch ( Exception e )
103 		        {
104 		            e.printStackTrace();
105 		            fail( "Should not get an exception: " + e.toString() );
106 		        }
107 
108 		        disk.removeAll();
109 		    }
110 }