View Javadoc
1   package org.apache.commons.jcs3.auxiliary.disk.block;
2   
3   import java.util.HashSet;
4   import java.util.Map;
5   import java.util.Set;
6   
7   import org.apache.commons.jcs3.JCS;
8   import org.apache.commons.jcs3.access.CacheAccess;
9   import org.apache.commons.jcs3.engine.behavior.ICacheElement;
10  
11  /*
12   * Licensed to the Apache Software Foundation (ASF) under one
13   * or more contributor license agreements.  See the NOTICE file
14   * distributed with this work for additional information
15   * regarding copyright ownership.  The ASF licenses this file
16   * to you under the Apache License, Version 2.0 (the
17   * "License"); you may not use this file except in compliance
18   * with the License.  You may obtain a copy of the License at
19   *
20   *   http://www.apache.org/licenses/LICENSE-2.0
21   *
22   * Unless required by applicable law or agreed to in writing,
23   * software distributed under the License is distributed on an
24   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
25   * KIND, either express or implied.  See the License for the
26   * specific language governing permissions and limitations
27   * under the License.
28   */
29  
30  import junit.extensions.ActiveTestSuite;
31  import junit.framework.Test;
32  import junit.framework.TestCase;
33  
34  /**
35   * Test which exercises the block disk cache. Runs three threads against the same region.
36   */
37  public class BlockDiskCacheSameRegionConcurrentUnitTest
38      extends TestCase
39  {
40      /**
41       * Constructor for the TestDiskCache object.
42       * <p>
43       * @param testName
44       */
45      public BlockDiskCacheSameRegionConcurrentUnitTest( final String testName )
46      {
47          super( testName );
48      }
49  
50      /**
51       * A unit test suite for JUnit
52       * @return The test suite
53       */
54      public static Test suite()
55      {
56          final ActiveTestSuite suite = new ActiveTestSuite();
57  
58          suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache1" )
59          {
60              @Override
61              public void runTest()
62                  throws Exception
63              {
64                  this.runTestForRegion( "blockRegion4", 0, 200 );
65              }
66          } );
67  
68          suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache2" )
69          {
70              @Override
71              public void runTest()
72                  throws Exception
73              {
74                  this.runTestForRegion( "blockRegion4", 1000, 1200 );
75              }
76          } );
77  
78          suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache3" )
79          {
80              @Override
81              public void runTest()
82                  throws Exception
83              {
84                  this.runTestForRegion( "blockRegion4", 2000, 2200 );
85              }
86          } );
87  
88          suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache4" )
89          {
90              @Override
91              public void runTest()
92                  throws Exception
93              {
94                  this.runTestForRegion( "blockRegion4", 2200, 5200 );
95              }
96          } );
97  
98          return suite;
99      }
100 
101     /**
102      * Test setup.  Sets the config name and clears the region.
103      * <p>
104      * @throws Exception
105      */
106     @Override
107     public void setUp()
108         throws Exception
109     {
110         JCS.setConfigFilename( "/TestBlockDiskCacheCon.ccf" );
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      * @throws Exception If an error occurs
120      */
121     public void runTestForRegion( final String region, final int start, final int end )
122         throws Exception
123     {
124         final CacheAccess<String, String> 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             final String key = i + ":key";
138             final String value = jcs.get( key );
139 
140             assertEquals( "Wrong value for key [" + key + "]", region + " data " + i + "-" + region, value );
141         }
142 
143         // Test that getElements returns all the expected values
144         final Set<String> keys = new HashSet<>();
145         for ( int i = start; i <= end; i++ )
146         {
147             keys.add( i + ":key" );
148         }
149 
150         final Map<String, ICacheElement<String, String>> elements = jcs.getCacheElements( keys );
151         for ( int i = start; i <= end; i++ )
152         {
153             final ICacheElement<String, String> element = elements.get( i + ":key" );
154             assertNotNull( "element " + i + ":key is missing", element );
155             assertEquals( "value " + i + ":key", region + " data " + i + "-" + region, element.getVal() );
156         }
157     }
158 }