View Javadoc
1   package org.apache.commons.jcs3.auxiliary.disk.indexed;
2   
3   import java.util.HashSet;
4   import java.util.Map;
5   import java.util.Set;
6   
7   import org.apache.commons.jcs3.JCS;
8   import org.apache.commons.jcs3.access.CacheAccess;
9   import org.apache.commons.jcs3.engine.behavior.ICacheElement;
10  
11  /*
12   * Licensed to the Apache Software Foundation (ASF) under one
13   * or more contributor license agreements.  See the NOTICE file
14   * distributed with this work for additional information
15   * regarding copyright ownership.  The ASF licenses this file
16   * to you under the Apache License, Version 2.0 (the
17   * "License"); you may not use this file except in compliance
18   * with the License.  You may obtain a copy of the License at
19   *
20   *   http://www.apache.org/licenses/LICENSE-2.0
21   *
22   * Unless required by applicable law or agreed to in writing,
23   * software distributed under the License is distributed on an
24   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
25   * KIND, either express or implied.  See the License for the
26   * specific language governing permissions and limitations
27   * under the License.
28   */
29  
30  import junit.extensions.ActiveTestSuite;
31  import junit.framework.Test;
32  import junit.framework.TestCase;
33  
34  /**
35   * Test which exercises the indexed disk cache. This one uses three different
36   * regions for thre threads.
37   */
38  public class IndexedDiskCacheConcurrentUnitTest
39      extends TestCase
40  {
41      /**
42       * Number of items to cache, twice the configured maxObjects for the memory
43       * cache regions.
44       */
45      private static final int items = 200;
46  
47      /**
48       * Constructor for the TestDiskCache object.
49       *
50       * @param testName
51       */
52      public IndexedDiskCacheConcurrentUnitTest( final String testName )
53      {
54          super( testName );
55      }
56  
57      /**
58       * A unit test suite for JUnit
59       *
60       * @return The test suite
61       */
62      public static Test suite()
63      {
64          final ActiveTestSuite suite = new ActiveTestSuite();
65  
66          suite.addTest( new IndexedDiskCacheConcurrentUnitTest( "testIndexedDiskCache1" )
67          {
68              @Override
69              public void runTest()
70                  throws Exception
71              {
72                  this.runTestForRegion( "indexedRegion1" );
73              }
74          } );
75  
76          suite.addTest( new IndexedDiskCacheConcurrentUnitTest( "testIndexedDiskCache2" )
77          {
78              @Override
79              public void runTest()
80                  throws Exception
81              {
82                  this.runTestForRegion( "indexedRegion2" );
83              }
84          } );
85  
86          suite.addTest( new IndexedDiskCacheConcurrentUnitTest( "testIndexedDiskCache3" )
87          {
88              @Override
89              public void runTest()
90                  throws Exception
91              {
92                  this.runTestForRegion( "indexedRegion3" );
93              }
94          } );
95  
96          suite.addTest( new IndexedDiskCacheConcurrentUnitTest( "testIndexedDiskCache4" )
97          {
98              @Override
99              public void runTest()
100                 throws Exception
101             {
102                 this.runTestForRegionInRange( "indexedRegion3", 300, 600 );
103             }
104         } );
105 
106         return suite;
107     }
108 
109     /**
110      * Test setup
111      */
112     @Override
113     public void setUp()
114     {
115         JCS.setConfigFilename( "/TestDiskCache.ccf" );
116     }
117 
118     /**
119      * Adds items to cache, gets them, and removes them. The item count is more
120      * than the size of the memory cache, so items should spool to disk.
121      *
122      * @param region
123      *            Name of the region to access
124      *
125      * @throws Exception
126      *                If an error occurs
127      */
128     public void runTestForRegion( final String region )
129         throws Exception
130     {
131         final CacheAccess<String, String> jcs = JCS.getInstance( region );
132 
133         // Add items to cache
134         for ( int i = 0; i <= items; i++ )
135         {
136             jcs.put( i + ":key", region + " data " + i );
137         }
138 
139         // Test that all items are in cache
140         for ( int i = 0; i <= items; i++ )
141         {
142             final String value = jcs.get( i + ":key" );
143 
144             assertEquals( region + " data " + i, value );
145         }
146 
147         // Test that getElements returns all the expected values
148         final Set<String> keys = new HashSet<>();
149         for ( int i = 0; i <= items; i++ )
150         {
151             keys.add( i + ":key" );
152         }
153 
154         final Map<String, ICacheElement<String, String>> elements = jcs.getCacheElements( keys );
155         for ( int i = 0; i <= items; i++ )
156         {
157             final ICacheElement<String, String> element = elements.get( i + ":key" );
158             assertNotNull( "element " + i + ":key is missing", element );
159             assertEquals( "value " + i + ":key", region + " data " + i, element.getVal() );
160         }
161 
162         // Remove all the items
163         for ( int i = 0; i <= items; i++ )
164         {
165             jcs.remove( i + ":key" );
166         }
167 
168         // Verify removal
169         // another thread may have inserted since
170         for ( int i = 0; i <= items; i++ )
171         {
172             assertNull( "Removed key should be null: " + i + ":key" + "\n stats " + jcs.getStats(), jcs
173                 .get( i + ":key" ) );
174         }
175     }
176 
177     /**
178      * Adds items to cache, gets them, and removes them. The item count is more
179      * than the size of the memory cache, so items should spool to disk.
180      *
181      * @param region
182      *            Name of the region to access
183      * @param start
184      * @param end
185      *
186      * @throws Exception
187      *                If an error occurs
188      */
189     public void runTestForRegionInRange( final String region, final int start, final int end )
190         throws Exception
191     {
192         final CacheAccess<String, String> jcs = JCS.getInstance( region );
193 
194         // Add items to cache
195         for ( int i = start; i <= end; i++ )
196         {
197             jcs.put( i + ":key", region + " data " + i );
198         }
199 
200         // Test that all items are in cache
201         for ( int i = start; i <= end; i++ )
202         {
203             final String value = jcs.get( i + ":key" );
204 
205             assertEquals( region + " data " + i, value );
206         }
207 
208         // Test that getElements returns all the expected values
209         final Set<String> keys = new HashSet<>();
210         for ( int i = start; i <= end; i++ )
211         {
212             keys.add( i + ":key" );
213         }
214 
215         final Map<String, ICacheElement<String, String>> elements = jcs.getCacheElements( keys );
216         for ( int i = start; i <= end; i++ )
217         {
218             final ICacheElement<String, String> element = elements.get( i + ":key" );
219             assertNotNull( "element " + i + ":key is missing", element );
220             assertEquals( "value " + i + ":key", region + " data " + i, element.getVal() );
221         }
222 
223         // Remove all the items
224         for ( int i = start; i <= end; i++ )
225         {
226             jcs.remove( i + ":key" );
227         }
228 
229 //        System.out.println( jcs.getStats() );
230 
231         // Verify removal
232         // another thread may have inserted since
233         for ( int i = start; i <= end; i++ )
234         {
235             assertNull( "Removed key should be null: " + i + ":key " + "\n stats " + jcs.getStats(), jcs.get( i
236                 + ":key" ) );
237         }
238     }
239 }