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.access.exception.CacheException;
10  import org.apache.commons.jcs3.engine.behavior.ICacheElement;
11  
12  /*
13   * Licensed to the Apache Software Foundation (ASF) under one
14   * or more contributor license agreements.  See the NOTICE file
15   * distributed with this work for additional information
16   * regarding copyright ownership.  The ASF licenses this file
17   * to you under the Apache License, Version 2.0 (the
18   * "License"); you may not use this file except in compliance
19   * with the License.  You may obtain a copy of the License at
20   *
21   *   http://www.apache.org/licenses/LICENSE-2.0
22   *
23   * Unless required by applicable law or agreed to in writing,
24   * software distributed under the License is distributed on an
25   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
26   * KIND, either express or implied.  See the License for the
27   * specific language governing permissions and limitations
28   * under the License.
29   */
30  
31  import junit.framework.TestCase;
32  
33  /**
34   * Test which exercises the HSQL cache.
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          final int items = 20;
58  
59          final String region = "testBasicPutRemove";
60  
61          final CacheAccess<String, String> jcs = JCS.getInstance( region );
62  
63          // Add items to cache
64          for ( int i = 0; i <= items; i++ )
65          {
66              jcs.put( i + ":key", region + " data " + i );
67          }
68  
69          // Test that all items are in cache
70          for ( int i = 0; i <= items; i++ )
71          {
72              final String value = jcs.get( i + ":key" );
73              assertEquals( "key = [" + i + ":key] value = [" + value + "]", region + " data " + i, value );
74          }
75  
76          // Test that getElements returns all the expected values
77          final Set<String> keys = new HashSet<>();
78          for ( int i = 0; i <= items; i++ )
79          {
80              keys.add( i + ":key" );
81          }
82  
83          final Map<String, ICacheElement<String, String>> elements = jcs.getCacheElements( keys );
84          for ( int i = 0; i <= items; i++ )
85          {
86              final ICacheElement<String, String> element = elements.get( i + ":key" );
87              assertNotNull( "element " + i + ":key is missing", element );
88              assertEquals( "value " + i + ":key", region + " data " + i, element.getVal() );
89          }
90  
91          // Remove all the items
92          for ( int i = 0; i <= items; i++ )
93          {
94              jcs.remove( i + ":key" );
95          }
96  
97          // Verify removal
98          for ( int i = 0; i <= items; i++ )
99          {
100             assertNull( "Removed key should be null: " + i + ":key", jcs.get( i + ":key" ) );
101         }
102     }
103 
104     /**
105      * Verify that remove all work son a region where it is not prohibited.
106      * <p>
107      * @throws CacheException
108      * @throws InterruptedException
109      */
110     public void testRemoveAll()
111         throws CacheException, InterruptedException
112     {
113         final String region = "removeAllAllowed";
114         final CacheAccess<String, String> jcs = JCS.getInstance( region );
115 
116         final int items = 20;
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         // a db thread could be updating when we call remove all?
125         // there was a race on remove all, an element may be put to disk after it is called even
126         // though the put
127         // was called before clear.
128         // I discovered it and removed it.
129         // Thread.sleep( 500 );
130 
131 //        System.out.println( jcs.getStats() );
132 
133         jcs.clear();
134 
135         for ( int i = 0; i <= items; i++ )
136         {
137             final String value = jcs.get( i + ":key" );
138             assertNull( "value should be null key = [" + i + ":key] value = [" + value + "]", value );
139         }
140     }
141 
142     /**
143      * Verify that remove all does not work on a region where it is prohibited.
144      * <p>
145      * @throws CacheException
146      * @throws InterruptedException
147      */
148     public void testRemoveAllProhibition()
149         throws CacheException, InterruptedException
150     {
151         final String region = "noRemoveAll";
152         final CacheAccess<String, String> jcs = JCS.getInstance( region );
153 
154         final int items = 20;
155 
156         // Add items to cache
157         for ( int i = 0; i <= items; i++ )
158         {
159             jcs.put( i + ":key", region + " data " + i );
160         }
161 
162         // a db thread could be updating the disk when
163         // Thread.sleep( 500 );
164 
165         jcs.clear();
166 
167         for ( int i = 0; i <= items; i++ )
168         {
169             final String value = jcs.get( i + ":key" );
170             assertEquals( "key = [" + i + ":key] value = [" + value + "]", region + " data " + i, value );
171         }
172     }
173 }