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