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