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