View Javadoc
1   package org.apache.commons.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.framework.TestCase;
23  import org.apache.commons.jcs.JCS;
24  import org.apache.commons.jcs.access.CacheAccess;
25  import org.apache.commons.jcs.access.TestCacheAccess;
26  
27  /**
28   * This is used by other tests to generate a random load on the disk cache.
29   */
30  public class IndexedDiskCacheRandomConcurrentTestUtil
31      extends TestCase
32  {
33  
34      /**
35       * Constructor for the TestDiskCache object.
36       *
37       * @param testName
38       */
39      public IndexedDiskCacheRandomConcurrentTestUtil( String testName )
40      {
41          super( testName );
42      }
43  
44      /**
45       * Randomly adds items to cache, gets them, and removes them. The range
46       * count is more than the size of the memory cache, so items should spool to
47       * disk.
48       *
49       * @param region
50       *            Name of the region to access
51       * @param range
52       * @param numOps
53       * @param testNum
54       *
55       * @throws Exception
56       *                If an error occurs
57       */
58      public void runTestForRegion( String region, int range, int numOps, int testNum )
59          throws Exception
60      {
61          // run a rondom operation test to detect deadlocks
62          TestCacheAccess tca = new TestCacheAccess( "/TestDiskCacheCon.ccf" );
63          tca.setRegion( region );
64          tca.random( range, numOps );
65  
66          // make sure a simple put then get works
67          // this may fail if the other tests are flooding the disk cache
68          CacheAccess<String, String> jcs = JCS.getInstance( region );
69          String key = "testKey" + testNum;
70          String data = "testData" + testNum;
71          jcs.put( key, data );
72          String value = jcs.get( key );
73          assertEquals( data, value );
74  
75      }
76  
77      /**
78       * Test setup
79       */
80      @Override
81      public void setUp()
82      {
83          JCS.setConfigFilename( "/TestDiskCacheCon.ccf" );
84      }
85  
86  }