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