View Javadoc
1   package org.apache.commons.jcs3.auxiliary.disk.jdbc.hsql;
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 indexed disk cache. This one uses three different regions for thre
36   * threads.
37   */
38  public class HSQLDiskCacheConcurrentUnitTest
39      extends TestCase
40  {
41      /**
42       * Number of items to cache, twice the configured maxObjects for the memory cache regions.
43       */
44      private static final int items = 100;
45  
46      /**
47       * Constructor for the TestDiskCache object.
48       * @param testName
49       */
50      public HSQLDiskCacheConcurrentUnitTest( final String testName )
51      {
52          super( testName );
53      }
54  
55      /**
56       * A unit test suite for JUnit. Uses ActiveTestSuite to run multiple tests concurrently.
57       * <p>
58       * @return The test suite
59       */
60      public static Test suite()
61      {
62          final ActiveTestSuite suite = new ActiveTestSuite();
63  
64          suite.addTest( new HSQLDiskCacheConcurrentUnitTest( "testHSQLDiskCache1" )
65          {
66              @Override
67              public void runTest()
68                  throws Exception
69              {
70                  this.runTestForRegion( "indexedRegion1" );
71              }
72          } );
73  
74          suite.addTest( new HSQLDiskCacheConcurrentUnitTest( "testHSQLDiskCache2" )
75          {
76              @Override
77              public void runTest()
78                  throws Exception
79              {
80                  this.runTestForRegion( "indexedRegion2" );
81              }
82          } );
83  
84          suite.addTest( new HSQLDiskCacheConcurrentUnitTest( "testHSQLDiskCache3" )
85          {
86              @Override
87              public void runTest()
88                  throws Exception
89              {
90                  this.runTestForRegion( "indexedRegion3" );
91              }
92          } );
93  
94          return suite;
95      }
96  
97      /**
98       * Test setup
99       */
100     @Override
101     public void setUp()
102     {
103         JCS.setConfigFilename( "/TestHSQLDiskCacheConcurrent.ccf" );
104     }
105 
106     /**
107      * Adds items to cache, gets them, and removes them. The item count is more than the size of the
108      * memory cache, so items should spool to disk.
109      * <p>
110      * @param region Name of the region to access
111      * @throws Exception If an error occurs
112      */
113     public void runTestForRegion( final String region )
114         throws Exception
115     {
116         final CacheAccess<String, String> jcs = JCS.getInstance( region );
117 
118         // Add items to cache
119         for ( int i = 0; i <= items; i++ )
120         {
121             jcs.put( i + ":key", region + " data " + i );
122         }
123 
124 //        System.out.println( jcs.getStats() );
125 
126         // Test that all items are in cache
127         for ( int i = 0; i <= items; i++ )
128         {
129             final String value = jcs.get( i + ":key" );
130 
131             assertEquals( "key = [" + i + ":key] value = [" + value + "]", region + " data " + i, value );
132         }
133 
134         // Test that getElements returns all the expected values
135         final Set<String> keys = new HashSet<>();
136         for ( int i = 0; i <= items; i++ )
137         {
138             keys.add( i + ":key" );
139         }
140 
141         final Map<String, ICacheElement<String, String>> elements = jcs.getCacheElements( keys );
142         for ( int i = 0; i <= items; i++ )
143         {
144             final ICacheElement<String, String> element = elements.get( i + ":key" );
145             assertNotNull( "element " + i + ":key is missing", element );
146             assertEquals( "value " + i + ":key", region + " data " + i, element.getVal() );
147         }
148 
149         // Remove all the items
150         for ( int i = 0; i <= items; i++ )
151         {
152             jcs.remove( i + ":key" );
153         }
154 
155         // Verify removal
156         for ( int i = 0; i <= items; i++ )
157         {
158             assertNull( "Removed key should be null: " + i + ":key", jcs.get( i + ":key" ) );
159         }
160     }
161 }