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