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   * Verify that basic removal functionality works.
27   */
28  public class JCSRemovalSimpleConcurrentTest
29      extends TestCase
30  {
31      /**
32       * @param testName
33       */
34      public JCSRemovalSimpleConcurrentTest( String testName )
35      {
36          super( testName );
37      }
38  
39      /**
40       * Test setup
41       * <p>
42       * @throws Exception
43       */
44      @Override
45      public void setUp()
46          throws Exception
47      {
48          JCS.setConfigFilename( "/TestRemoval.ccf" );
49          JCS.getInstance( "testCache1" );
50      }
51  
52      /**
53       * Main method passes this test to the text test runner.
54       * <p>
55       * @param args
56       */
57      public static void main( String args[] )
58      {
59          String[] testCaseName = { JCSRemovalSimpleConcurrentTest.class.getName() };
60          junit.textui.TestRunner.main( testCaseName );
61      }
62  
63      /**
64       * Verify that 2 level deep hierchical removal works.
65       * <p>
66       * @throws Exception
67       */
68      public void testTwoDeepRemoval()
69          throws Exception
70      {
71          int count = 500;
72          CacheAccess<String, String> jcs = JCS.getInstance( "testCache1" );
73  
74          for ( int i = 0; i <= count; i++ )
75          {
76              jcs.put( "key:" + i + ":anotherpart", "data" + i );
77          }
78  
79          for ( int i = count; i >= 0; i-- )
80          {
81              String res = jcs.get( "key:" + i + ":anotherpart" );
82              assertNotNull( "[key:" + i + ":anotherpart] should not be null, " + jcs.getStats(), res );
83          }
84  
85          for ( int i = 0; i <= count; i++ )
86          {
87              jcs.remove( "key:" + i + ":" );
88              assertNull( jcs.getStats(), jcs.get( "key:" + i + ":anotherpart" ) );
89          }
90  
91      }
92  
93      /**
94       * Verify that 1 level deep hierchical removal works.
95       *
96       * @throws Exception
97       */
98      public void testSingleDepthRemoval()
99          throws Exception
100     {
101 
102         int count = 500;
103         CacheAccess<String, String> jcs = JCS.getInstance( "testCache1" );
104 
105         for ( int i = 0; i <= count; i++ )
106         {
107             jcs.put( i + ":key", "data" + i );
108         }
109 
110         for ( int i = count; i >= 0; i-- )
111         {
112             String res = jcs.get( i + ":key" );
113             assertNotNull( "[" + i + ":key] should not be null", res );
114         }
115 
116         for ( int i = 0; i <= count; i++ )
117         {
118             jcs.remove( i + ":" );
119             assertNull( jcs.get( i + ":key" ) );
120         }
121     }
122 
123     /**
124      * Verify that clear removes everyting as it should.
125      * <p>
126      * @throws Exception
127      */
128     public void testClear()
129         throws Exception
130     {
131 
132         int count = 500;
133         CacheAccess<String, String> jcs = JCS.getInstance( "testCache1" );
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             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             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         int count = 500;
166         CacheAccess<String, String> jcs = JCS.getInstance( "testCache1" );
167 
168         jcs.clear();
169 
170         for ( int i = 0; i <= count; i++ )
171         {
172             jcs.put( i + ":key", "data" + i );
173         }
174 
175         for ( int i = count; i >= 0; i-- )
176         {
177             String res = jcs.get( i + ":key" );
178             assertNotNull( "[" + i + ":key] should not be null", res );
179         }
180 
181         for ( int i = count; i >= 0; i-- )
182         {
183             jcs.put( i + ":key", "data" + i );
184             jcs.clear();
185             String res = jcs.get( i + ":key" );
186             if ( res != null )
187             {
188                 assertNull( "[" + i + ":key] should be null after remvoeall" + jcs.getStats(), res );
189             }
190         }
191     }
192 }