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