View Javadoc
1   package org.apache.commons.jcs;
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  import org.apache.commons.jcs.access.CacheAccess;
24  
25  /**
26   *
27   * @author Aaron Smuts
28   *
29   */
30  public class ZeroSizeCacheUnitTest
31      extends TestCase
32  {
33      /** number to get each loop */
34      private static int items = 20000;
35  
36      /**
37       * Test setup
38       * <p>
39       * @throws Exception
40       */
41      @Override
42      public void setUp()
43          throws Exception
44      {
45          JCS.setConfigFilename( "/TestZeroSizeCache.ccf" );
46          JCS.getInstance( "testCache1" );
47      }
48  
49      /**
50       * Verify that a 0 size cache does not result in errors. You should be able
51       * to disable a region this way.
52       * @throws Exception
53       *
54       */
55      public void testPutGetRemove()
56          throws Exception
57      {
58          CacheAccess<String, String> jcs = JCS.getInstance( "testCache1" );
59  
60          for ( int i = 0; i <= items; i++ )
61          {
62              jcs.put( i + ":key", "data" + i );
63          }
64  
65          // all the gets should be null
66          for ( int i = items; i >= 0; i-- )
67          {
68              String res = jcs.get( i + ":key" );
69              assertNull( "[" + i + ":key] should be null", res );
70          }
71  
72          // test removal, should be no exceptions
73          jcs.remove( "300:key" );
74  
75          // allow the shrinker to run
76          Thread.sleep( 500 );
77  
78          // do it again.
79          for ( int i = 0; i <= items; i++ )
80          {
81              jcs.put( i + ":key", "data" + i );
82          }
83  
84          for ( int i = items; i >= 0; i-- )
85          {
86              String res = jcs.get( i + ":key" );
87              assertNull( "[" + i + ":key] should be null", res );
88          }
89      }
90  }