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   * Verify that basic removal functionality works.
26   */
27  public class JCSRemovalSimpleConcurrentTest
28      extends TestCase
29  {
30      /***
31       * Constructor for the TestDiskCache object.
32       *
33       * @param testName
34       */
35      public JCSRemovalSimpleConcurrentTest( String testName )
36      {
37          super( testName );
38      }
39  
40      /***
41       * Test setup
42       */
43      public void setUp()
44          throws Exception
45      {
46          JCS.setConfigFilename( "/TestRemoval.ccf" );
47          JCS.getInstance( "testCache1" );
48      }
49  
50      /***
51       * Main method passes this test to the text test runner.
52       *
53       * @param args
54       */
55      public static void main( String args[] )
56      {
57          String[] testCaseName = { JCSRemovalSimpleConcurrentTest.class.getName() };
58          junit.textui.TestRunner.main( testCaseName );
59      }
60  
61      /***
62       * Verify that 2 level deep hierchical removal works.
63       *
64       * @throws Exception
65       */
66      public void testTwoDeepRemoval()
67          throws Exception
68      {
69  
70          System.out.println( "------------------------------------------" );
71          System.out.println( "testTwoDeepRemoval" );
72  
73          int count = 500;
74          JCS jcs = JCS.getInstance( "testCache1" );
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              String res = (String) jcs.get( "key:" + i + ":anotherpart" );
84              if ( res == null )
85              {
86                  assertNotNull( "[key:" + i + ":anotherpart] should not be null, " + jcs.getStats(), res );
87              }
88          }
89          System.out.println( "Confirmed that " + count + " items could be found" );
90  
91          for ( int i = 0; i <= count; i++ )
92          {
93              jcs.remove( "key:" + i + ":" );
94              assertNull( jcs.getStats(), jcs.get( "key:" + i + ":anotherpart" ) );
95          }
96          System.out.println( "Confirmed that " + count + " items were removed" );
97  
98          System.out.println( jcs.getStats() );
99  
100     }
101 
102     /***
103      * Verify that 1 level deep hierchical removal works.
104      *
105      * @throws Exception
106      */
107     public void testSingleDepthRemoval()
108         throws Exception
109     {
110 
111         System.out.println( "------------------------------------------" );
112         System.out.println( "testSingleDepthRemoval" );
113 
114         int count = 500;
115         JCS jcs = JCS.getInstance( "testCache1" );
116 
117         for ( int i = 0; i <= count; i++ )
118         {
119             jcs.put( i + ":key", "data" + i );
120         }
121 
122         for ( int i = count; i >= 0; i-- )
123         {
124             String res = (String) jcs.get( i + ":key" );
125             if ( res == null )
126             {
127                 assertNotNull( "[" + i + ":key] should not be null", res );
128             }
129         }
130         System.out.println( "Confirmed that " + count + " items could be found" );
131 
132         for ( int i = 0; i <= count; i++ )
133         {
134             jcs.remove( i + ":" );
135             assertNull( jcs.get( i + ":key" ) );
136         }
137         System.out.println( "Confirmed that " + count + " items were removed" );
138 
139         System.out.println( jcs.getStats() );
140 
141     }
142 
143     /***
144      * Verify that clear removes everyting as it should.
145      *
146      * @throws Exception
147      */
148     public void testClear()
149         throws Exception
150     {
151 
152         System.out.println( "------------------------------------------" );
153         System.out.println( "testRemoveAll" );
154 
155         int count = 500;
156         JCS jcs = JCS.getInstance( "testCache1" );
157 
158         for ( int i = 0; i <= count; i++ )
159         {
160             jcs.put( i + ":key", "data" + i );
161         }
162 
163         for ( int i = count; i >= 0; i-- )
164         {
165             String res = (String) jcs.get( i + ":key" );
166             if ( res == null )
167             {
168                 assertNotNull( "[" + i + ":key] should not be null", res );
169             }
170         }
171         System.out.println( "Confirmed that " + count + " items could be found" );
172 
173         System.out.println( jcs.getStats() );
174 
175         jcs.clear();
176 
177         for ( int i = count; i >= 0; i-- )
178         {
179             String res = (String) jcs.get( i + ":key" );
180             if ( res != null )
181             {
182                 assertNull( "[" + i + ":key] should be null after remvoeall" + jcs.getStats(), res );
183             }
184         }
185         System.out.println( "Confirmed that all items were removed" );
186 
187     }
188 
189     /***
190      * Verify that we can clear repeatedly without error.
191      *
192      * @throws Exception
193      */
194     public void testClearRepeatedlyWithoutError()
195         throws Exception
196     {
197 
198         System.out.println( "------------------------------------------" );
199         System.out.println( "testRemoveAll" );
200 
201         int count = 500;
202         JCS jcs = JCS.getInstance( "testCache1" );
203 
204         jcs.clear();
205 
206         for ( int i = 0; i <= count; i++ )
207         {
208             jcs.put( i + ":key", "data" + i );
209         }
210 
211         for ( int i = count; i >= 0; i-- )
212         {
213             String res = (String) jcs.get( i + ":key" );
214             if ( res == null )
215             {
216                 assertNotNull( "[" + i + ":key] should not be null", res );
217             }
218         }
219         System.out.println( "Confirmed that " + count + " items could be found" );
220 
221         System.out.println( jcs.getStats() );
222 
223         for ( int i = count; i >= 0; i-- )
224         {
225             jcs.put( i + ":key", "data" + i );
226             jcs.clear();
227             String res = (String) jcs.get( i + ":key" );
228             if ( res != null )
229             {
230                 assertNull( "[" + i + ":key] should be null after remvoeall" + jcs.getStats(), res );
231             }
232         }
233         System.out.println( "Confirmed that all items were removed" );
234 
235     }
236 }