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 junit.framework.TestCase;
23  
24  import java.sql.Connection;
25  import java.sql.DriverManager;
26  import java.util.HashSet;
27  import java.util.Map;
28  import java.util.Properties;
29  import java.util.Set;
30  
31  import org.apache.commons.jcs3.JCS;
32  import org.apache.commons.jcs3.access.CacheAccess;
33  import org.apache.commons.jcs3.engine.behavior.ICacheElement;
34  
35  /**
36   * Runs basic tests for the JDBC disk cache using a shared connection pool.
37   */
38  public class JDBCDiskCacheSharedPoolUnitTest
39      extends TestCase
40  {
41      /** Test setup */
42      @Override
43      public void setUp()
44      {
45          JCS.setConfigFilename( "/TestJDBCDiskCacheSharedPool.ccf" );
46      }
47  
48      /**
49       * Test the basic JDBC disk cache functionality with a hsql backing.
50       * @throws Exception
51       */
52      public void testSimpleJDBCPutGetWithHSQL()
53          throws Exception
54      {
55          System.setProperty( "hsqldb.cache_scale", "8" );
56  
57          final String rafroot = "target";
58          final Properties p = new Properties();
59          final String driver = p.getProperty( "driver", "org.hsqldb.jdbcDriver" );
60          final String url = p.getProperty( "url", "jdbc:hsqldb:" );
61          final String database = p.getProperty( "database", rafroot + "/cache_hsql_db_sharedpool" );
62          final String user = p.getProperty( "user", "sa" );
63          final String password = p.getProperty( "password", "" );
64  
65          new org.hsqldb.jdbcDriver();
66          Class.forName( driver ).newInstance();
67          final Connection cConn = DriverManager.getConnection( url + database, user, password );
68  
69          HsqlSetupTableUtil.setupTABLE( cConn, "JCS_STORE_0" );
70  
71          HsqlSetupTableUtil.setupTABLE( cConn, "JCS_STORE_1" );
72  
73          runTestForRegion( "testCache1", 200 );
74      }
75  
76      /**
77       * Adds items to cache, gets them, and removes them. The item count is more than the size of the
78       * memory cache, so items should spool to disk.
79       * <p>
80       * @param region Name of the region to access
81       * @param items
82       * @throws Exception If an error occurs
83       */
84      public void runTestForRegion( final String region, final int items )
85          throws Exception
86      {
87          final CacheAccess<String, String> jcs = JCS.getInstance( region );
88  
89  //        System.out.println( "BEFORE PUT \n" + jcs.getStats() );
90  
91          // Add items to cache
92  
93          for ( int i = 0; i <= items; i++ )
94          {
95              jcs.put( i + ":key", region + " data " + i );
96          }
97  
98  //        System.out.println( jcs.getStats() );
99  
100         Thread.sleep( 1000 );
101 
102 //        System.out.println( jcs.getStats() );
103 
104         // Test that all items are in cache
105 
106         for ( int i = 0; i <= items; i++ )
107         {
108             final String value = jcs.get( i + ":key" );
109 
110             assertEquals( "key = [" + i + ":key] value = [" + value + "]", region + " data " + i, value );
111         }
112 
113         // Test that getElements returns all the expected values
114         final Set<String> keys = new HashSet<>();
115         for ( int i = 0; i <= items; i++ )
116         {
117             keys.add( i + ":key" );
118         }
119 
120         final Map<String, ICacheElement<String, String>> elements = jcs.getCacheElements( keys );
121         for ( int i = 0; i <= items; i++ )
122         {
123             final ICacheElement<String, String> element = elements.get( i + ":key" );
124             assertNotNull( "element " + i + ":key is missing", element );
125             assertEquals( "value " + i + ":key", region + " data " + i, element.getVal() );
126         }
127 
128         // Remove all the items
129         for ( int i = 0; i <= items; i++ )
130         {
131             jcs.remove( i + ":key" );
132         }
133 
134         // Verify removal
135         for ( int i = 0; i <= items; i++ )
136         {
137             assertNull( "Removed key should be null: " + i + ":key", jcs.get( i + ":key" ) );
138         }
139     }
140 }