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