View Javadoc
1   package org.apache.commons.jcs.auxiliary.disk.jdbc;
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 static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNull;
24  
25  import java.sql.Connection;
26  import java.sql.DriverManager;
27  import java.util.Properties;
28  
29  import org.apache.commons.jcs.JCS;
30  import org.apache.commons.jcs.access.CacheAccess;
31  import org.apache.commons.jcs.access.exception.CacheException;
32  import org.apache.commons.jcs.utils.timing.SleepUtil;
33  import org.junit.Before;
34  import org.junit.BeforeClass;
35  import org.junit.Test;
36  
37  /**
38   * Runs basic tests for the JDBC disk cache.
39   * <p>
40   * @author Aaron Smuts
41   */
42  public class JDBCDiskCacheShrinkUnitTest
43  {
44      /**
45       * Creates the DB
46       * <p>
47       * @throws Exception
48       */
49      @BeforeClass
50      public static void setupDatabase() throws Exception
51      {
52          System.setProperty( "hsqldb.cache_scale", "8" );
53  
54          String rafroot = "target";
55          Properties p = new Properties();
56          String driver = p.getProperty( "driver", "org.hsqldb.jdbcDriver" );
57          String url = p.getProperty( "url", "jdbc:hsqldb:" );
58          String database = p.getProperty( "database", rafroot + "/JDBCDiskCacheShrinkUnitTest" );
59          String user = p.getProperty( "user", "sa" );
60          String password = p.getProperty( "password", "" );
61  
62          new org.hsqldb.jdbcDriver();
63          Class.forName( driver ).newInstance();
64          Connection cConn = DriverManager.getConnection( url + database, user, password );
65  
66          HsqlSetupTableUtil.setupTABLE( cConn, "JCS_STORE_SHRINK" );
67      }
68  
69      /**
70       * Test setup
71       */
72      @Before
73      public void setUp()
74      {
75          JCS.setConfigFilename( "/TestJDBCDiskCacheShrink.ccf" );
76      }
77  
78      /**
79       * Test the basic JDBC disk cache functionality with a hsql backing. Verify that items
80       * configured to expire after 1 second actually expire.
81       * <p>
82       * @throws Exception
83       */
84      @Test
85      public void testExpireInBackground()
86          throws Exception
87      {
88          String regionExpire = "expire1Second";
89          int items = 200;
90  
91          CacheAccess<String, String> jcsExpire = JCS.getInstance( regionExpire );
92  
93  //        System.out.println( "BEFORE PUT \n" + jcsExpire.getStats() );
94  
95          // Add items to cache
96  
97          for ( int i = 0; i <= items; i++ )
98          {
99              jcsExpire.put( i + ":key", regionExpire + " data " + i );
100         }
101 
102 //        System.out.println( jcsExpire.getStats() );
103 
104         // the shrinker is supposed to run every second
105         SleepUtil.sleepAtLeast( 3000 );
106 
107 //        System.out.println( jcsExpire.getStats() );
108 
109         // Test that all items have been removed from the cache
110         for ( int i = 0; i <= items; i++ )
111         {
112             assertNull( "Removed key should be null: " + i + ":key", jcsExpire.get( i + ":key" ) );
113         }
114     }
115 
116     /**
117      * Verify that those not scheduled to expire do not expire.
118      * <p>
119      * @throws CacheException
120      * @throws InterruptedException
121      */
122     @Test
123     public void testDidNotExpire()
124         throws CacheException, InterruptedException
125     {
126         String region = "expire100Second";
127         int items = 200;
128 
129         CacheAccess<String, String> jcs = JCS.getInstance( region );
130 
131 //        System.out.println( "BEFORE PUT \n" + jcs.getStats() );
132 
133         // Add items to cache
134 
135         for ( int i = 0; i <= items; i++ )
136         {
137             jcs.put( i + ":key", region + " data " + i );
138         }
139 
140 //        System.out.println( jcs.getStats() );
141 
142         SleepUtil.sleepAtLeast( 1000 );
143 
144 //        System.out.println( jcs.getStats() );
145 
146         // Test that all items are in cache
147 
148         for ( int i = 0; i <= items; i++ )
149         {
150             String value = jcs.get( i + ":key" );
151 
152             assertEquals( "key = [" + i + ":key] value = [" + value + "]", region + " data " + i, value );
153         }
154 
155         // Remove all the items
156 
157         for ( int i = 0; i <= items; i++ )
158         {
159             jcs.remove( i + ":key" );
160         }
161 
162         // Verify removal
163 
164         for ( int i = 0; i <= items; i++ )
165         {
166             assertNull( "Removed key should be null: " + i + ":key", jcs.get( i + ":key" ) );
167         }
168     }
169 
170     /**
171      * Verify that eternal trumps max life.
172      * @throws CacheException
173      * @throws InterruptedException
174      */
175     @Test
176     public void testDidNotExpireEternal()
177         throws CacheException, InterruptedException
178     {
179         String region = "eternal";
180         int items = 200;
181 
182         CacheAccess<String, String> jcs = JCS.getInstance( region );
183 
184 //        System.out.println( "BEFORE PUT \n" + jcs.getStats() );
185 
186         // Add items to cache
187 
188         for ( int i = 0; i <= items; i++ )
189         {
190             jcs.put( i + ":key", region + " data " + i );
191         }
192 
193 //        System.out.println( jcs.getStats() );
194 
195         SleepUtil.sleepAtLeast( 1000 );
196 
197 //        System.out.println( jcs.getStats() );
198 
199         // Test that all items are in cache
200 
201         for ( int i = 0; i <= items; i++ )
202         {
203             String value = jcs.get( i + ":key" );
204 
205             assertEquals( "key = [" + i + ":key] value = [" + value + "]", region + " data " + i, value );
206         }
207 
208         // Remove all the items
209 
210         for ( int i = 0; i <= items; i++ )
211         {
212             jcs.remove( i + ":key" );
213         }
214 
215         // Verify removal
216 
217         for ( int i = 0; i <= items; i++ )
218         {
219             assertNull( "Removed key should be null: " + i + ":key", jcs.get( i + ":key" ) );
220         }
221     }
222 }