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