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.framework.TestCase;
23  import org.apache.commons.jcs.JCS;
24  import org.apache.commons.jcs.access.CacheAccess;
25  import org.apache.commons.jcs.access.exception.CacheException;
26  import org.apache.commons.jcs.engine.behavior.ICacheElement;
27  
28  import java.util.HashSet;
29  import java.util.Map;
30  import java.util.Set;
31  
32  /**
33   * Test which exercises the indexed disk cache. This one uses three different regions for thre
34   * threads.
35   */
36  public class HSQLDiskCacheUnitTest
37      extends TestCase
38  {
39      /**
40       * Test setup
41       */
42      @Override
43      public void setUp()
44      {
45          JCS.setConfigFilename( "/TestHSQLDiskCache.ccf" );
46      }
47  
48      /**
49       * Adds items to cache, gets them, and removes them. The item count is more than the size of the
50       * memory cache, so items should spool to disk.
51       * <p>
52       * @throws Exception If an error occurs
53       */
54      public void testBasicPutRemove()
55          throws Exception
56      {
57          int items = 20;
58  
59          String region = "testBasicPutRemove";
60  
61          CacheAccess<String, String> jcs = JCS.getInstance( region );
62  
63          // Add items to cache
64  
65          for ( int i = 0; i <= items; i++ )
66          {
67              jcs.put( i + ":key", region + " data " + i );
68          }
69  
70          //SleepUtil.sleepAtLeast( 1000 );
71  
72  //        System.out.println( jcs.getStats() );
73  
74          // Test that all items are in cache
75  
76          for ( int i = 0; i <= items; i++ )
77          {
78              String value = jcs.get( i + ":key" );
79  
80              assertEquals( "key = [" + i + ":key] value = [" + value + "]", region + " data " + i, value );
81          }
82  
83          // Test that getElements returns all the expected values
84          Set<String> keys = new HashSet<String>();
85          for ( int i = 0; i <= items; i++ )
86          {
87              keys.add( i + ":key" );
88          }
89  
90          Map<String, ICacheElement<String, String>> elements = jcs.getCacheElements( keys );
91          for ( int i = 0; i <= items; i++ )
92          {
93              ICacheElement<String, String> element = elements.get( i + ":key" );
94              assertNotNull( "element " + i + ":key is missing", element );
95              assertEquals( "value " + i + ":key", region + " data " + i, element.getVal() );
96          }
97  
98  
99          // Remove all the items
100 
101         for ( int i = 0; i <= items; i++ )
102         {
103             jcs.remove( i + ":key" );
104         }
105 
106         // Verify removal
107 
108         for ( int i = 0; i <= items; i++ )
109         {
110             assertNull( "Removed key should be null: " + i + ":key", jcs.get( i + ":key" ) );
111         }
112     }
113 
114     /**
115      * Verify that remove all work son a region where it is not prohibited.
116      * <p>
117      * @throws CacheException
118      * @throws InterruptedException
119      */
120     public void testRemoveAll()
121         throws CacheException, InterruptedException
122     {
123         String region = "removeAllAllowed";
124         CacheAccess<String, String> jcs = JCS.getInstance( region );
125 
126         int items = 20;
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         // a db thread could be updating when we call remove all?
136         // there was a race on remove all, an element may be put to disk after it is called even
137         // though the put
138         // was called before clear.
139         // I discovered it and removed it.
140         // Thread.sleep( 500 );
141 
142 //        System.out.println( jcs.getStats() );
143 
144         jcs.clear();
145 
146         for ( int i = 0; i <= items; i++ )
147         {
148             String value = jcs.get( i + ":key" );
149 
150             assertNull( "value should be null key = [" + i + ":key] value = [" + value + "]", value );
151         }
152     }
153 
154     /**
155      * Verify that remove all does not work on a region where it is prohibited.
156      * <p>
157      * @throws CacheException
158      * @throws InterruptedException
159      */
160     public void testRemoveAllProhibition()
161         throws CacheException, InterruptedException
162     {
163         String region = "noRemoveAll";
164         CacheAccess<String, String> jcs = JCS.getInstance( region );
165 
166         int items = 20;
167 
168         // Add items to cache
169 
170         for ( int i = 0; i <= items; i++ )
171         {
172             jcs.put( i + ":key", region + " data " + i );
173         }
174 
175         // a db thread could be updating the disk when
176         // Thread.sleep( 500 );
177 
178 //        System.out.println( jcs.getStats() );
179 
180         jcs.clear();
181 
182         for ( int i = 0; i <= items; i++ )
183         {
184             String value = jcs.get( i + ":key" );
185 
186             assertEquals( "key = [" + i + ":key] value = [" + value + "]", region + " data " + i, value );
187         }
188     }
189 }