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. Runs three threads against the same region.
30   */
31  public class BlockDiskCacheSameRegionConcurrentUnitTest
32      extends TestCase
33  {
34      /***
35       * Constructor for the TestDiskCache object.
36       * <p>
37       * @param testName
38       */
39      public BlockDiskCacheSameRegionConcurrentUnitTest( String testName )
40      {
41          super( testName );
42      }
43  
44      /***
45       * Main method passes this test to the text test runner.
46       * <p>
47       * @param args
48       */
49      public static void main( String args[] )
50      {
51          String[] testCaseName = { BlockDiskCacheSameRegionConcurrentUnitTest.class.getName() };
52          junit.textui.TestRunner.main( testCaseName );
53      }
54  
55      /***
56       * A unit test suite for JUnit
57       * @return The test suite
58       */
59      public static Test suite()
60      {
61          ActiveTestSuite suite = new ActiveTestSuite();
62  
63          suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache1" )
64          {
65              public void runTest()
66                  throws Exception
67              {
68                  this.runTestForRegion( "blockRegion4", 0, 200 );
69              }
70          } );
71  
72          suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache2" )
73          {
74              public void runTest()
75                  throws Exception
76              {
77                  this.runTestForRegion( "blockRegion4", 1000, 1200 );
78              }
79          } );
80  
81          suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache3" )
82          {
83              public void runTest()
84                  throws Exception
85              {
86                  this.runTestForRegion( "blockRegion4", 2000, 2200 );
87              }
88          } );
89  
90          suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache4" )
91          {
92              public void runTest()
93                  throws Exception
94              {
95                  this.runTestForRegion( "blockRegion4", 2200, 5200 );
96              }
97          } );
98  
99          return suite;
100     }
101 
102     /***
103      * Test setup.  Sets the config name and clears the region.
104      * <p>
105      * @throws Exception
106      */
107     public void setUp() throws Exception
108     {
109         JCS.setConfigFilename( "/TestBlockDiskCacheCon.ccf" );
110         JCS.getInstance( "blockRegion4" ).clear();
111     }
112 
113     /***
114      * Adds items to cache, gets them, and removes them. The item count is more than the size of the
115      * memory cache, so items should spool to disk.
116      * @param region Name of the region to access
117      * @param start
118      * @param end
119      * @exception Exception If an error occurs
120      */
121     public void runTestForRegion( String region, int start, int end )
122         throws Exception
123     {
124         JCS jcs = JCS.getInstance( region );
125 
126         // Add items to cache
127 
128         for ( int i = start; i <= end; i++ )
129         {
130             jcs.put( i + ":key", region + " data " + i + "-" + region );
131         }
132 
133         // Test that all items are in cache
134 
135         for ( int i = start; i <= end; i++ )
136         {
137             String key = i + ":key";
138             String value = (String) jcs.get( key );
139 
140             assertEquals( "Wrong value for key [" + key + "]", region + " data " + i + "-" + region, value );
141         }
142     }
143 }