View Javadoc
1   package org.apache.commons.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  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 block disk cache. Runs three threads against the same region.
35   */
36  public class BlockDiskCacheSameRegionConcurrentUnitTest
37      extends TestCase
38  {
39      /**
40       * Constructor for the TestDiskCache object.
41       * <p>
42       * @param testName
43       */
44      public BlockDiskCacheSameRegionConcurrentUnitTest( String testName )
45      {
46          super( testName );
47      }
48  
49      /**
50       * Main method passes this test to the text test runner.
51       * <p>
52       * @param args
53       * @throws InterruptedException
54       */
55      public static void main( String args[] ) throws InterruptedException
56      {
57          String[] testCaseName = { BlockDiskCacheSameRegionConcurrentUnitTest.class.getName() };
58          junit.textui.TestRunner.main( testCaseName );
59  
60          // Give test threads some time to finish
61          Thread.sleep(2000);
62      }
63  
64      /**
65       * A unit test suite for JUnit
66       * @return The test suite
67       */
68      public static Test suite()
69      {
70          ActiveTestSuite suite = new ActiveTestSuite();
71  
72          suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache1" )
73          {
74              @Override
75              public void runTest()
76                  throws Exception
77              {
78                  this.runTestForRegion( "blockRegion4", 0, 200 );
79              }
80          } );
81  
82          suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache2" )
83          {
84              @Override
85              public void runTest()
86                  throws Exception
87              {
88                  this.runTestForRegion( "blockRegion4", 1000, 1200 );
89              }
90          } );
91  
92          suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache3" )
93          {
94              @Override
95              public void runTest()
96                  throws Exception
97              {
98                  this.runTestForRegion( "blockRegion4", 2000, 2200 );
99              }
100         } );
101 
102         suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache4" )
103         {
104             @Override
105             public void runTest()
106                 throws Exception
107             {
108                 this.runTestForRegion( "blockRegion4", 2200, 5200 );
109             }
110         } );
111 
112         return suite;
113     }
114 
115     /**
116      * Test setup.  Sets the config name and clears the region.
117      * <p>
118      * @throws Exception
119      */
120     @Override
121     public void setUp()
122         throws Exception
123     {
124         JCS.setConfigFilename( "/TestBlockDiskCacheCon.ccf" );
125     }
126 
127     /**
128      * Adds items to cache, gets them, and removes them. The item count is more than the size of the
129      * memory cache, so items should spool to disk.
130      * @param region Name of the region to access
131      * @param start
132      * @param end
133      * @throws Exception If an error occurs
134      */
135     public void runTestForRegion( String region, int start, int end )
136         throws Exception
137     {
138         CacheAccess<String, String> jcs = JCS.getInstance( region );
139 
140         // Add items to cache
141 
142         for ( int i = start; i <= end; i++ )
143         {
144             jcs.put( i + ":key", region + " data " + i + "-" + region );
145         }
146 
147         // Test that all items are in cache
148 
149         for ( int i = start; i <= end; i++ )
150         {
151             String key = i + ":key";
152             String value = jcs.get( key );
153 
154             assertEquals( "Wrong value for key [" + key + "]", region + " data " + i + "-" + region, value );
155         }
156 
157         // Test that getElements returns all the expected values
158         Set<String> keys = new HashSet<String>();
159         for ( int i = start; i <= end; i++ )
160         {
161             keys.add( i + ":key" );
162         }
163 
164         Map<String, ICacheElement<String, String>> elements = jcs.getCacheElements( keys );
165         for ( int i = start; i <= end; i++ )
166         {
167             ICacheElement<String, String> element = elements.get( i + ":key" );
168             assertNotNull( "element " + i + ":key is missing", element );
169             assertEquals( "value " + i + ":key", region + " data " + i + "-" + region, element.getVal() );
170         }
171     }
172 }