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. This one uses three different
30   * regions for thre threads.
31   *
32   * @version $Id: TestDiskCache.java 224346 2005-06-04 02:01:59Z asmuts $
33   */
34  public class IndexedDiskCacheConcurrentUnitTest
35      extends TestCase
36  {
37      /***
38       * Number of items to cache, twice the configured maxObjects for the memory
39       * cache regions.
40       */
41      private static int items = 200;
42  
43      /***
44       * Constructor for the TestDiskCache object.
45       *
46       * @param testName
47       */
48      public IndexedDiskCacheConcurrentUnitTest( String testName )
49      {
50          super( testName );
51      }
52  
53      /***
54       * Main method passes this test to the text test runner.
55       *
56       * @param args
57       */
58      public static void main( String args[] )
59      {
60          String[] testCaseName = { IndexedDiskCacheConcurrentUnitTest.class.getName() };
61          junit.textui.TestRunner.main( testCaseName );
62      }
63  
64      /***
65       * A unit test suite for JUnit
66       *
67       * @return The test suite
68       */
69      public static Test suite()
70      {
71          ActiveTestSuite suite = new ActiveTestSuite();
72  
73          suite.addTest( new IndexedDiskCacheConcurrentUnitTest( "testIndexedDiskCache1" )
74          {
75              public void runTest()
76                  throws Exception
77              {
78                  this.runTestForRegion( "indexedRegion1" );
79              }
80          } );
81  
82          suite.addTest( new IndexedDiskCacheConcurrentUnitTest( "testIndexedDiskCache2" )
83          {
84              public void runTest()
85                  throws Exception
86              {
87                  this.runTestForRegion( "indexedRegion2" );
88              }
89          } );
90  
91          suite.addTest( new IndexedDiskCacheConcurrentUnitTest( "testIndexedDiskCache3" )
92          {
93              public void runTest()
94                  throws Exception
95              {
96                  this.runTestForRegion( "indexedRegion3" );
97              }
98          } );
99  
100         suite.addTest( new IndexedDiskCacheConcurrentUnitTest( "testIndexedDiskCache4" )
101         {
102             public void runTest()
103                 throws Exception
104             {
105                 this.runTestForRegionInRange( "indexedRegion3", 300, 600 );
106             }
107         } );
108 
109         return suite;
110     }
111 
112     /***
113      * Test setup
114      */
115     public void setUp()
116     {
117         JCS.setConfigFilename( "/TestDiskCache.ccf" );
118     }
119 
120     /***
121      * Adds items to cache, gets them, and removes them. The item count is more
122      * than the size of the memory cache, so items should spool to disk.
123      *
124      * @param region
125      *            Name of the region to access
126      *
127      * @exception Exception
128      *                If an error occurs
129      */
130     public void runTestForRegion( String region )
131         throws Exception
132     {
133         JCS jcs = JCS.getInstance( region );
134 
135         // Add items to cache
136         for ( int i = 0; i <= items; i++ )
137         {
138             jcs.put( i + ":key", region + " data " + i );
139         }
140 
141         // Test that all items are in cache
142         for ( int i = 0; i <= items; i++ )
143         {
144             String value = (String) jcs.get( i + ":key" );
145 
146             assertEquals( region + " data " + i, value );
147         }
148 
149         // Remove all the items
150         for ( int i = 0; i <= items; i++ )
151         {
152             jcs.remove( i + ":key" );
153         }
154 
155         // Verify removal
156         // another thread may have inserted since
157         for ( int i = 0; i <= items; i++ )
158         {
159             assertNull( "Removed key should be null: " + i + ":key" + "\n stats " + jcs.getStats(), jcs
160                 .get( i + ":key" ) );
161         }
162     }
163 
164     /***
165      * Adds items to cache, gets them, and removes them. The item count is more
166      * than the size of the memory cache, so items should spool to disk.
167      *
168      * @param region
169      *            Name of the region to access
170      * @param start
171      * @param end
172      *
173      * @exception Exception
174      *                If an error occurs
175      */
176     public void runTestForRegionInRange( String region, int start, int end )
177         throws Exception
178     {
179         JCS jcs = JCS.getInstance( region );
180 
181         // Add items to cache
182         for ( int i = start; i <= end; i++ )
183         {
184             jcs.put( i + ":key", region + " data " + i );
185         }
186 
187         // Test that all items are in cache
188         for ( int i = start; i <= end; i++ )
189         {
190             String value = (String) jcs.get( i + ":key" );
191 
192             assertEquals( region + " data " + i, value );
193         }
194 
195         // Remove all the items
196         for ( int i = start; i <= end; i++ )
197         {
198             jcs.remove( i + ":key" );
199         }
200 
201         System.out.println( jcs.getStats() );
202 
203         // Verify removal
204         // another thread may have inserted since
205         for ( int i = start; i <= end; i++ )
206         {
207             assertNull( "Removed key should be null: " + i + ":key " + "\n stats " + jcs.getStats(), jcs.get( i
208                 + ":key" ) );
209         }
210     }
211 }