1 package org.apache.commons.jcs3;
2
3 import org.apache.commons.jcs3.access.CacheAccess;
4
5 /*
6 * Licensed to the Apache Software Foundation (ASF) under one
7 * or more contributor license agreements. See the NOTICE file
8 * distributed with this work for additional information
9 * regarding copyright ownership. The ASF licenses this file
10 * to you under the Apache License, Version 2.0 (the
11 * "License"); you may not use this file except in compliance
12 * with the License. You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing,
17 * software distributed under the License is distributed on an
18 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19 * KIND, either express or implied. See the License for the
20 * specific language governing permissions and limitations
21 * under the License.
22 */
23
24 import junit.framework.TestCase;
25
26 /**
27 * Simple methods to be run by active test suites that test removal.
28 */
29 public class RemovalTestUtil
30 extends TestCase
31 {
32 /**
33 * Constructor for the TestSimpleLoad object
34 *
35 * @param testName
36 * Description of the Parameter
37 */
38 public RemovalTestUtil( final 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 * @throws Exception
51 * Description of the Exception
52 */
53 public void runTestPutThenRemoveCategorical( final int start, final int end )
54 throws Exception
55 {
56 final CacheAccess<String, String> 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 final String res = jcs.get( i + ":key" );
66 assertNotNull( "[" + i + ":key] should not be null", res );
67 }
68
69 for ( int i = start; i <= end; i++ )
70 {
71 jcs.remove( i + ":" );
72 assertNull( jcs.get( i + ":key" ) );
73 }
74 }
75
76 /**
77 * Put items in the cache in this key range. Can be used to verify that
78 * concurrent operations are not effected by things like hierchical removal.
79 *
80 * @param start
81 * int
82 * @param end
83 * int
84 * @throws Exception
85 */
86 public void runPutInRange( final int start, final int end )
87 throws Exception
88 {
89 final CacheAccess<String, String> jcs = JCS.getInstance( "testCache1" );
90
91 for ( int i = start; i <= end; i++ )
92 {
93 jcs.put( i + ":key", "data" + i );
94 }
95
96 for ( int i = end; i >= start; i-- )
97 {
98 final String res = jcs.get( i + ":key" );
99 assertNotNull( "[" + i + ":key] should not be null", res );
100 }
101 }
102
103 /**
104 * Just get from start to end.
105 *
106 * @param start
107 * int
108 * @param end
109 * int
110 * @param check
111 * boolean -- check to see if the items are in the cache.
112 * @throws Exception
113 */
114 public void runGetInRange( final int start, final int end, final boolean check )
115 throws Exception
116 {
117 final CacheAccess<String, String> jcs = JCS.getInstance( "testCache1" );
118
119 // don't care if they are found
120 for ( int i = end; i >= start; i-- )
121 {
122 final String res = jcs.get( i + ":key" );
123 if ( check )
124 {
125 assertNotNull( "[" + i + ":key] should not be null", res );
126 }
127 }
128 }
129 }