1 package org.apache.commons.jcs3;
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 org.apache.commons.jcs3.access.CacheAccess;
23
24 /*
25 * Licensed to the Apache Software Foundation (ASF) under one
26 * or more contributor license agreements. See the NOTICE file
27 * distributed with this work for additional information
28 * regarding copyright ownership. The ASF licenses this file
29 * to you under the Apache License, Version 2.0 (the
30 * "License"); you may not use this file except in compliance
31 * with the License. You may obtain a copy of the License at
32 *
33 * http://www.apache.org/licenses/LICENSE-2.0
34 *
35 * Unless required by applicable law or agreed to in writing,
36 * software distributed under the License is distributed on an
37 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
38 * KIND, either express or implied. See the License for the
39 * specific language governing permissions and limitations
40 * under the License.
41 */
42
43 import junit.framework.TestCase;
44
45 /**
46 * Verify that basic removal functionality works.
47 */
48 public class JCSRemovalSimpleConcurrentTest
49 extends TestCase
50 {
51 private CacheAccess<String, String> jcs;
52
53 /**
54 * Test setup
55 * <p>
56 * @throws Exception
57 */
58 @Override
59 public void setUp()
60 throws Exception
61 {
62 JCS.setConfigFilename( "/TestRemoval.ccf" );
63 jcs = JCS.getInstance( "testCache1" );
64 }
65
66 /**
67 * Verify that 2 level deep hierchical removal works.
68 * <p>
69 * @throws Exception
70 */
71 public void testTwoDeepRemoval()
72 throws Exception
73 {
74 final int count = 500;
75
76 for ( int i = 0; i <= count; i++ )
77 {
78 jcs.put( "key:" + i + ":anotherpart", "data" + i );
79 }
80
81 for ( int i = count; i >= 0; i-- )
82 {
83 final String res = jcs.get( "key:" + i + ":anotherpart" );
84 assertNotNull( "[key:" + i + ":anotherpart] should not be null, " + jcs.getStats(), res );
85 }
86
87 for ( int i = 0; i <= count; i++ )
88 {
89 jcs.remove( "key:" + i + ":" );
90 assertNull( jcs.getStats(), jcs.get( "key:" + i + ":anotherpart" ) );
91 }
92
93 }
94
95 /**
96 * Verify that 1 level deep hierchical removal works.
97 *
98 * @throws Exception
99 */
100 public void testSingleDepthRemoval()
101 throws Exception
102 {
103
104 final int count = 500;
105
106 for ( int i = 0; i <= count; i++ )
107 {
108 jcs.put( i + ":key", "data" + i );
109 }
110
111 for ( int i = count; i >= 0; i-- )
112 {
113 final String res = jcs.get( i + ":key" );
114 assertNotNull( "[" + i + ":key] should not be null", res );
115 }
116
117 for ( int i = 0; i <= count; i++ )
118 {
119 jcs.remove( i + ":" );
120 assertNull( jcs.get( i + ":key" ) );
121 }
122 }
123
124 /**
125 * Verify that clear removes everyting as it should.
126 * <p>
127 * @throws Exception
128 */
129 public void testClear()
130 throws Exception
131 {
132
133 final int count = 500;
134
135 for ( int i = 0; i <= count; i++ )
136 {
137 jcs.put( i + ":key", "data" + i );
138 }
139
140 for ( int i = count; i >= 0; i-- )
141 {
142 final String res = jcs.get( i + ":key" );
143 assertNotNull( "[" + i + ":key] should not be null", res );
144 }
145 jcs.clear();
146
147 for ( int i = count; i >= 0; i-- )
148 {
149 final String res = jcs.get( i + ":key" );
150 if ( res != null )
151 {
152 assertNull( "[" + i + ":key] should be null after remvoeall" + jcs.getStats(), res );
153 }
154 }
155 }
156
157 /**
158 * Verify that we can clear repeatedly without error.
159 *
160 * @throws Exception
161 */
162 public void testClearRepeatedlyWithoutError()
163 throws Exception
164 {
165 final int count = 500;
166
167 jcs.clear();
168
169 for ( int i = 0; i <= count; i++ )
170 {
171 jcs.put( i + ":key", "data" + i );
172 }
173
174 for ( int i = count; i >= 0; i-- )
175 {
176 final String res = jcs.get( i + ":key" );
177 assertNotNull( "[" + i + ":key] should not be null", res );
178 }
179
180 for ( int i = count; i >= 0; i-- )
181 {
182 jcs.put( i + ":key", "data" + i );
183 jcs.clear();
184 final String res = jcs.get( i + ":key" );
185 if ( res != null )
186 {
187 assertNull( "[" + i + ":key] should be null after remvoeall" + jcs.getStats(), res );
188 }
189 }
190 }
191 }