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