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   * Simple methods to be run by active test suites that test removal.
27   *
28   */
29  public class RemovalTestUtil
30      extends TestCase
31  {
32  
33      /**
34       * Constructor for the TestSimpleLoad object
35       *
36       * @param testName
37       *            Description of the Parameter
38       */
39      public RemovalTestUtil( String testName )
40      {
41          super( testName );
42      }
43  
44      /**
45       * Adds elements in the range specified and then removes them using the
46       * categorical or substring removal method.
47       *
48       * @param start
49       * @param end
50       *
51       * @throws Exception
52       *                Description of the Exception
53       */
54      public void runTestPutThenRemoveCategorical( int start, int end )
55          throws Exception
56      {
57          CacheAccess<String, String> jcs = JCS.getInstance( "testCache1" );
58  
59          for ( int i = start; i <= end; i++ )
60          {
61              jcs.put( i + ":key", "data" + i );
62          }
63  
64          for ( int i = end; i >= start; i-- )
65          {
66              String res = jcs.get( i + ":key" );
67              assertNotNull( "[" + i + ":key] should not be null", res );
68          }
69  
70          for ( int i = start; i <= end; i++ )
71          {
72              jcs.remove( i + ":" );
73              assertNull( jcs.get( i + ":key" ) );
74          }
75      }
76  
77      /**
78       * Put items in the cache in this key range. Can be used to verify that
79       * concurrent operations are not effected by things like hierchical removal.
80       *
81       * @param start
82       *            int
83       * @param end
84       *            int
85       * @throws Exception
86       */
87      public void runPutInRange( int start, int end )
88          throws Exception
89      {
90          CacheAccess<String, String> jcs = JCS.getInstance( "testCache1" );
91  
92          for ( int i = start; i <= end; i++ )
93          {
94              jcs.put( i + ":key", "data" + i );
95          }
96  
97          for ( int i = end; i >= start; i-- )
98          {
99              String res = jcs.get( i + ":key" );
100             assertNotNull( "[" + i + ":key] should not be null", res );
101         }
102     }
103 
104     /**
105      * Just get from start to end.
106      *
107      * @param start
108      *            int
109      * @param end
110      *            int
111      * @param check
112      *            boolean -- check to see if the items are in the cache.
113      * @throws Exception
114      */
115     public void runGetInRange( int start, int end, boolean check )
116         throws Exception
117     {
118         CacheAccess<String, String> jcs = JCS.getInstance( "testCache1" );
119 
120         // don't care if they are found
121         for ( int i = end; i >= start; i-- )
122         {
123             String res = jcs.get( i + ":key" );
124             if ( check )
125             {
126                 assertNotNull( "[" + i + ":key] should not be null", res );
127             }
128         }
129     }
130 }