View Javadoc
1   package org.apache.commons.jcs3.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 java.util.HashSet;
23  import java.util.Map;
24  import java.util.Set;
25  
26  import org.apache.commons.jcs3.JCS;
27  import org.apache.commons.jcs3.access.CacheAccess;
28  import org.apache.commons.jcs3.engine.behavior.ICacheElement;
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. Runs three threads against the
36   * same region.
37   */
38  public class IndexedDiskCacheSameRegionConcurrentUnitTest
39      extends TestCase
40  {
41      /**
42       * Constructor for the TestDiskCache object.
43       *
44       * @param testName
45       */
46      public IndexedDiskCacheSameRegionConcurrentUnitTest( final String testName )
47      {
48          super( testName );
49      }
50  
51      /**
52       * A unit test suite for JUnit
53       *
54       * @return The test suite
55       */
56      public static Test suite()
57      {
58          final ActiveTestSuite suite = new ActiveTestSuite();
59  
60          suite.addTest( new IndexedDiskCacheSameRegionConcurrentUnitTest( "testIndexedDiskCache1" )
61          {
62              @Override
63              public void runTest()
64                  throws Exception
65              {
66                  this.runTestForRegion( "indexedRegion4", 0, 200 );
67              }
68          } );
69  
70          suite.addTest( new IndexedDiskCacheSameRegionConcurrentUnitTest( "testIndexedDiskCache2" )
71          {
72              @Override
73              public void runTest()
74                  throws Exception
75              {
76                  this.runTestForRegion( "indexedRegion4", 1000, 1200 );
77              }
78          } );
79  
80          suite.addTest( new IndexedDiskCacheSameRegionConcurrentUnitTest( "testIndexedDiskCache3" )
81          {
82              @Override
83              public void runTest()
84                  throws Exception
85              {
86                  this.runTestForRegion( "indexedRegion4", 2000, 2200 );
87              }
88          } );
89  
90          suite.addTest( new IndexedDiskCacheSameRegionConcurrentUnitTest( "testIndexedDiskCache4" )
91          {
92              @Override
93              public void runTest()
94                  throws Exception
95              {
96                  this.runTestForRegion( "indexedRegion4", 2200, 5200 );
97              }
98          } );
99  
100         suite.addTest( new IndexedDiskCacheSameRegionConcurrentUnitTest( "testIndexedDiskCache5" )
101         {
102             @Override
103             public void runTest()
104                 throws Exception
105             {
106                 this.runTestForRegion( "indexedRegion4", 0, 5100 );
107             }
108         } );
109 
110         return suite;
111     }
112 
113     /**
114      * Test setup
115      */
116     @Override
117     public void setUp()
118     {
119         JCS.setConfigFilename( "/TestDiskCacheCon.ccf" );
120     }
121 
122     // /**
123     // * Tests the region which uses the indexed disk cache
124     // */
125     // public void testIndexedDiskCache()
126     // throws Exception
127     // {
128     // runTestForRegion( "indexedRegion" );
129     // }
130     //
131     // /**
132     // * Tests the region which uses the indexed disk cache
133     // */
134     // public void testIndexedDiskCache2()
135     // throws Exception
136     // {
137     // runTestForRegion( "indexedRegion2" );
138     // }
139 
140     /**
141      * Adds items to cache, gets them, and removes them. The item count is more
142      * than the size of the memory cache, so items should spool to disk.
143      *
144      * @param region
145      *            Name of the region to access
146      * @param start
147      * @param end
148      *
149      * @throws Exception
150      *                If an error occurs
151      */
152     public void runTestForRegion( final String region, final int start, final int end )
153         throws Exception
154     {
155         final CacheAccess<String, String> jcs = JCS.getInstance( region );
156 
157         // Add items to cache
158 
159         for ( int i = start; i <= end; i++ )
160         {
161             jcs.put( i + ":key", region + " data " + i );
162         }
163 
164         // Test that all items are in cache
165 
166         for ( int i = start; i <= end; i++ )
167         {
168             final String key = i + ":key";
169             final String value = jcs.get( key );
170 
171             assertEquals( "Wrong value for key [" + key + "]", region + " data " + i, value );
172         }
173 
174         // Test that getElements returns all the expected values
175         final Set<String> keys = new HashSet<>();
176         for ( int i = start; i <= end; i++ )
177         {
178             keys.add( i + ":key" );
179         }
180 
181         final Map<String, ICacheElement<String, String>> elements = jcs.getCacheElements( keys );
182         for ( int i = start; i <= end; i++ )
183         {
184             final ICacheElement<String, String> element = elements.get( i + ":key" );
185             assertNotNull( "element " + i + ":key is missing", element );
186             assertEquals( "value " + i + ":key", region + " data " + i, element.getVal() );
187         }
188 
189         // you can't remove in one thread and expect them to be in another //
190         //          Remove all the items
191         //
192         //          for ( int i = start; i <= end; i++ ) { jcs.remove( i + ":key" ); } //
193         //          Verify removal
194         //
195         //          for ( int i = start; i <= end; i++ ) { assertNull( "Removed key
196         //          should be null: " + i + ":key", jcs.get( i + ":key" ) ); }
197     }
198 }