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