View Javadoc
1   package org.apache.commons.jcs.auxiliary.disk.block;
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.extensions.ActiveTestSuite;
23  import junit.framework.Test;
24  import junit.framework.TestCase;
25  import org.apache.commons.jcs.JCS;
26  import org.apache.commons.jcs.access.CacheAccess;
27  import org.apache.commons.jcs.engine.behavior.ICacheElement;
28  
29  import java.util.HashSet;
30  import java.util.Map;
31  import java.util.Set;
32  
33  /**
34   * Test which exercises the block disk cache. This one uses three different
35   * regions for three threads.
36   */
37  public class BlockDiskCacheConcurrentUnitTest
38      extends TestCase
39  {
40      /**
41       * Number of items to cache, twice the configured maxObjects for the memory
42       * cache regions.
43       */
44      private static int items = 200;
45  
46      /**
47       * Constructor for the TestDiskCache object.
48       *
49       * @param testName
50       * @throws Exception
51       */
52      public BlockDiskCacheConcurrentUnitTest( String testName )
53          throws Exception
54      {
55          super( testName );
56      }
57  
58      /**
59       * Main method passes this test to the text test runner.
60       *
61       * @param args
62       */
63      public static void main( String args[] )
64      {
65          String[] testCaseName = { BlockDiskCacheConcurrentUnitTest.class.getName() };
66          junit.textui.TestRunner.main( testCaseName );
67      }
68  
69      /**
70       * A unit test suite for JUnit
71       *
72       * @return The test suite
73       * @throws Exception
74       */
75      public static Test suite()
76          throws Exception
77      {
78          ActiveTestSuite suite = new ActiveTestSuite();
79  
80          JCS.setConfigFilename( "/TestBlockDiskCache.ccf" );
81          JCS.getInstance( "indexedRegion1" ).clear();
82          JCS.getInstance( "indexedRegion2" ).clear();
83          JCS.getInstance( "indexedRegion3" ).clear();
84  
85          suite.addTest( new BlockDiskCacheConcurrentUnitTest( "testBlockDiskCache1" )
86          {
87              @Override
88              public void runTest()
89                  throws Exception
90              {
91                  this.runTestForRegion( "indexedRegion1" );
92              }
93          } );
94  
95          suite.addTest( new BlockDiskCacheConcurrentUnitTest( "testBlockDiskCache2" )
96          {
97              @Override
98              public void runTest()
99                  throws Exception
100             {
101                 this.runTestForRegion( "indexedRegion2" );
102             }
103         } );
104 
105         suite.addTest( new BlockDiskCacheConcurrentUnitTest( "testBlockDiskCache3" )
106         {
107             @Override
108             public void runTest()
109                 throws Exception
110             {
111                 this.runTestForRegion( "indexedRegion3" );
112             }
113         } );
114 
115         suite.addTest( new BlockDiskCacheConcurrentUnitTest( "testBlockDiskCache4" )
116         {
117             @Override
118             public void runTest()
119                 throws Exception
120             {
121                 this.runTestForRegionInRange( "indexedRegion3", 300, 600 );
122             }
123         } );
124 
125         return suite;
126     }
127 
128     /**
129      * Test setup
130      */
131     @Override
132     public void setUp()
133     {
134         JCS.setConfigFilename( "/TestBlockDiskCache.ccf" );
135     }
136 
137     /**
138      * Adds items to cache, gets them, and removes them. The item count is more
139      * than the size of the memory cache, so items should spool to disk.
140      *
141      * @param region
142      *            Name of the region to access
143      *
144      * @throws Exception
145      *                If an error occurs
146      */
147     public void runTestForRegion( String region )
148         throws Exception
149     {
150         CacheAccess<String, String> jcs = JCS.getInstance( region );
151 
152         // Add items to cache
153         for ( int i = 0; i <= items; i++ )
154         {
155             jcs.put( i + ":key", region + " data " + i );
156         }
157 
158         // Test that all items are in cache
159         for ( int i = 0; i <= items; i++ )
160         {
161             String value = jcs.get( i + ":key" );
162 
163             assertEquals( region + " data " + i, value );
164         }
165 
166         // Test that getElements returns all the expected values
167         Set<String> keys = new HashSet<String>();
168         for ( int i = 0; i <= items; i++ )
169         {
170             keys.add( i + ":key" );
171         }
172 
173         Map<String, ICacheElement<String, String>> elements = jcs.getCacheElements( keys );
174         for ( int i = 0; i <= items; i++ )
175         {
176             ICacheElement<String, String> element = elements.get( i + ":key" );
177             assertNotNull( "element " + i + ":key is missing", element );
178             assertEquals( "value " + i + ":key", region + " data " + i, element.getVal() );
179         }
180 
181         // Remove all the items
182         for ( int i = 0; i <= items; i++ )
183         {
184             jcs.remove( i + ":key" );
185         }
186 
187         // Verify removal
188         // another thread may have inserted since
189         for ( int i = 0; i <= items; i++ )
190         {
191             assertNull( "Removed key should be null: " + i + ":key" + "\n stats " + jcs.getStats(), jcs
192                 .get( i + ":key" ) );
193         }
194     }
195 
196     /**
197      * Adds items to cache, gets them, and removes them. The item count is more
198      * than the size of the memory cache, so items should spool to disk.
199      *
200      * @param region
201      *            Name of the region to access
202      * @param start
203      * @param end
204      *
205      * @throws Exception
206      *                If an error occurs
207      */
208     public void runTestForRegionInRange( String region, int start, int end )
209         throws Exception
210     {
211         CacheAccess<String, String> jcs = JCS.getInstance( region );
212 
213         // Add items to cache
214         for ( int i = start; i <= end; i++ )
215         {
216             jcs.put( i + ":key", region + " data " + i );
217         }
218 
219         // Test that all items are in cache
220         for ( int i = start; i <= end; i++ )
221         {
222             String value = jcs.get( i + ":key" );
223 
224             assertEquals( region + " data " + i, value );
225         }
226 
227         // Test that getElements returns all the expected values
228         Set<String> keys = new HashSet<String>();
229         for ( int i = start; i <= end; i++ )
230         {
231             keys.add( i + ":key" );
232         }
233 
234         Map<String, ICacheElement<String, String>> elements = jcs.getCacheElements( keys );
235         for ( int i = start; i <= end; i++ )
236         {
237             ICacheElement<String, String> element = elements.get( i + ":key" );
238             assertNotNull( "element " + i + ":key is missing", element );
239             assertEquals( "value " + i + ":key", region + " data " + i, element.getVal() );
240         }
241 
242         // Remove all the items
243         for ( int i = start; i <= end; i++ )
244         {
245             jcs.remove( i + ":key" );
246         }
247 
248 //        System.out.println( jcs.getStats() );
249 
250         // Verify removal
251         // another thread may have inserted since
252         for ( int i = start; i <= end; i++ )
253         {
254             assertNull( "Removed key should be null: " + i + ":key " + "\n stats " + jcs.getStats(), jcs.get( i
255                 + ":key" ) );
256         }
257     }
258 }