1   package org.apache.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  
26  import org.apache.jcs.JCS;
27  
28  /***
29   * Test which exercises the indexed disk cache. This one uses three different regions for thre
30   * threads.
31   */
32  public class HSQLDiskCacheConcurrentUnitTest
33      extends TestCase
34  {
35      /***
36       * Number of items to cache, twice the configured maxObjects for the memory cache regions.
37       */
38      private static int items = 100;
39  
40      /***
41       * Constructor for the TestDiskCache object.
42       * @param testName
43       */
44      public HSQLDiskCacheConcurrentUnitTest( 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       */
54      public static void main( String args[] )
55      {
56          String[] testCaseName = { HSQLDiskCacheConcurrentUnitTest.class.getName() };
57          junit.textui.TestRunner.main( testCaseName );
58      }
59  
60      /***
61       * A unit test suite for JUnit. Uses ActiveTestSuite to run multiple tests concurrently.
62       * <p>
63       * @return The test suite
64       */
65      public static Test suite()
66      {
67          ActiveTestSuite suite = new ActiveTestSuite();
68  
69          suite.addTest( new HSQLDiskCacheConcurrentUnitTest( "testHSQLDiskCache1" )
70          {
71              public void runTest()
72                  throws Exception
73              {
74                  this.runTestForRegion( "indexedRegion1" );
75              }
76          } );
77  
78          suite.addTest( new HSQLDiskCacheConcurrentUnitTest( "testHSQLDiskCache2" )
79          {
80              public void runTest()
81                  throws Exception
82              {
83                  this.runTestForRegion( "indexedRegion2" );
84              }
85          } );
86  
87          suite.addTest( new HSQLDiskCacheConcurrentUnitTest( "testHSQLDiskCache3" )
88          {
89              public void runTest()
90                  throws Exception
91              {
92                  this.runTestForRegion( "indexedRegion3" );
93              }
94          } );
95  
96          return suite;
97      }
98  
99      /***
100      * Test setup
101      */
102     public void setUp()
103     {
104         JCS.setConfigFilename( "/TestHSQLDiskCacheConcurrent.ccf" );
105     }
106 
107     /***
108      * Adds items to cache, gets them, and removes them. The item count is more than the size of the
109      * memory cache, so items should spool to disk.
110      * <p>
111      * @param region Name of the region to access
112      * @exception Exception If an error occurs
113      */
114     public void runTestForRegion( String region )
115         throws Exception
116     {
117         JCS jcs = JCS.getInstance( region );
118 
119         // Add items to cache
120 
121         for ( int i = 0; i <= items; i++ )
122         {
123             jcs.put( i + ":key", region + " data " + i );
124         }
125 
126         System.out.println( jcs.getStats() );
127 
128         // Thread.sleep( 1000 );
129 
130         // System.out.println( jcs.getStats() );
131 
132         // Test that all items are in cache
133 
134         for ( int i = 0; i <= items; i++ )
135         {
136             String value = (String) jcs.get( i + ":key" );
137 
138             assertEquals( "key = [" + i + ":key] value = [" + value + "]", region + " data " + i, value );
139         }
140 
141         // Remove all the items
142 
143         for ( int i = 0; i <= items; i++ )
144         {
145             jcs.remove( i + ":key" );
146         }
147 
148         // Verify removal
149 
150         for ( int i = 0; i <= items; i++ )
151         {
152             assertNull( "Removed key should be null: " + i + ":key", jcs.get( i + ":key" ) );
153         }
154     }
155 }