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