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 java.sql.Connection;
23  import java.sql.DriverManager;
24  import java.util.HashSet;
25  import java.util.Map;
26  import java.util.Properties;
27  import java.util.Set;
28  import java.util.concurrent.Executors;
29  
30  import junit.framework.TestCase;
31  
32  import org.apache.commons.jcs.JCS;
33  import org.apache.commons.jcs.access.CacheAccess;
34  import org.apache.commons.jcs.auxiliary.disk.jdbc.dsfactory.DataSourceFactory;
35  import org.apache.commons.jcs.engine.behavior.ICacheElement;
36  import org.apache.commons.jcs.engine.control.MockCompositeCacheManager;
37  import org.apache.commons.jcs.utils.serialization.StandardSerializer;
38  import org.apache.commons.jcs.utils.threadpool.DaemonThreadFactory;
39  
40  /**
41   * Runs basic tests for the JDBC disk cache.
42   *<p>
43   * @author Aaron Smuts
44   */
45  public class JDBCDiskCacheUnitTest
46      extends TestCase
47  {
48      /** Test setup */
49      @Override
50      public void setUp()
51      {
52          JCS.setConfigFilename( "/TestJDBCDiskCache.ccf" );
53      }
54  
55      /**
56       * Test the basic JDBC disk cache functionality with a hsql backing.
57       * @throws Exception
58       */
59      public void testSimpleJDBCPutGetWithHSQL()
60          throws Exception
61      {
62          System.setProperty( "hsqldb.cache_scale", "8" );
63  
64          String rafroot = "target";
65          Properties p = new Properties();
66          String driver = p.getProperty( "driver", "org.hsqldb.jdbcDriver" );
67          String url = p.getProperty( "url", "jdbc:hsqldb:" );
68          String database = p.getProperty( "database", rafroot + "/cache_hsql_db" );
69          String user = p.getProperty( "user", "sa" );
70          String password = p.getProperty( "password", "" );
71  
72          new org.hsqldb.jdbcDriver();
73          Class.forName( driver ).newInstance();
74          Connection cConn = DriverManager.getConnection( url + database, user, password );
75  
76          HsqlSetupTableUtil.setupTABLE( cConn, "JCS_STORE2" );
77  
78          runTestForRegion( "testCache1", 200 );
79      }
80  
81      /**
82       * Adds items to cache, gets them, and removes them. The item count is more than the size of the
83       * memory cache, so items should spool to disk.
84       * <p>
85       * @param region Name of the region to access
86       * @param items
87       * @throws Exception If an error occurs
88       */
89      public void runTestForRegion( String region, int items )
90          throws Exception
91      {
92          CacheAccess<String, String> jcs = JCS.getInstance( region );
93  
94  //        System.out.println( "BEFORE PUT \n" + jcs.getStats() );
95  
96          // Add items to cache
97  
98          for ( int i = 0; i <= items; i++ )
99          {
100             jcs.put( i + ":key", region + " data " + i );
101         }
102 
103 //        System.out.println( jcs.getStats() );
104 
105         Thread.sleep( 1000 );
106 
107 //        System.out.println( jcs.getStats() );
108 
109         // Test that all items are in cache
110 
111         for ( int i = 0; i <= items; i++ )
112         {
113             String value = jcs.get( i + ":key" );
114 
115             assertEquals( "key = [" + i + ":key] value = [" + value + "]", region + " data " + i, value );
116         }
117 
118         // Test that getElements returns all the expected values
119         Set<String> keys = new HashSet<String>();
120         for ( int i = 0; i <= items; i++ )
121         {
122             keys.add( i + ":key" );
123         }
124 
125         Map<String, ICacheElement<String, String>> elements = jcs.getCacheElements( keys );
126         for ( int i = 0; i <= items; i++ )
127         {
128             ICacheElement<String, String> element = elements.get( i + ":key" );
129             assertNotNull( "element " + i + ":key is missing", element );
130             assertEquals( "value " + i + ":key", region + " data " + i, element.getVal() );
131         }
132 
133         // Remove all the items
134         for ( int i = 0; i <= items; i++ )
135         {
136             jcs.remove( i + ":key" );
137         }
138 
139         // Verify removal
140         for ( int i = 0; i <= items; i++ )
141         {
142             assertNull( "Removed key should be null: " + i + ":key", jcs.get( i + ":key" ) );
143         }
144     }
145 
146     /**
147      * Verfiy that it uses the pool access manager config.
148      * <p>
149      * @throws Exception
150      */
151     public void testInitializePoolAccess_withPoolName()
152         throws Exception
153     {
154         // SETUP
155         String poolName = "testInitializePoolAccess_withPoolName";
156 
157         String url = "jdbc:hsqldb:";
158         String userName = "sa";
159         String password = "";
160         int maxActive = 10;
161         String driverClassName = "org.hsqldb.jdbcDriver";
162 
163         Properties props = new Properties();
164         String prefix = JDBCDiskCacheFactory.POOL_CONFIGURATION_PREFIX
165     		+ poolName
166             + JDBCDiskCacheFactory.ATTRIBUTE_PREFIX;
167         props.put( prefix + ".url", url );
168         props.put( prefix + ".userName", userName );
169         props.put( prefix + ".password", password );
170         props.put( prefix + ".maxActive", String.valueOf( maxActive ) );
171         props.put( prefix + ".driverClassName", driverClassName );
172 
173         JDBCDiskCacheAttributes cattr = new JDBCDiskCacheAttributes();
174         cattr.setConnectionPoolName( poolName );
175         cattr.setTableName("JCSTESTTABLE_InitializePoolAccess");
176 
177         MockCompositeCacheManager compositeCacheManager = new MockCompositeCacheManager();
178         compositeCacheManager.setConfigurationProperties( props );
179         JDBCDiskCacheFactory dcFactory = new JDBCDiskCacheFactory();
180         dcFactory.initialize();
181         dcFactory.setScheduledExecutorService(Executors.newScheduledThreadPool(2,
182         	new DaemonThreadFactory("JCS-JDBCDiskCacheManager-", Thread.MIN_PRIORITY)));
183 
184         JDBCDiskCache<String, String> diskCache = dcFactory.createCache( cattr, compositeCacheManager, null, new StandardSerializer() );
185         assertNotNull( "Should have a cache instance", diskCache );
186 
187         // DO WORK
188         DataSourceFactory result = dcFactory.getDataSourceFactory(cattr, props);
189 
190         // VERIFY
191         assertNotNull( "Should have a data source factory class", result );
192         assertEquals( "wrong name", poolName, result.getName() );
193 
194         System.setProperty( "hsqldb.cache_scale", "8" );
195 
196         String rafroot = "target";
197         String database = rafroot + "/cache_hsql_db";
198 
199         new org.hsqldb.jdbcDriver();
200         Class.forName( driverClassName ).newInstance();
201         Connection cConn = DriverManager.getConnection( url + database, userName, password );
202 
203         HsqlSetupTableUtil.setupTABLE( cConn, "JCSTESTTABLE_InitializePoolAccess" );
204 
205     }
206 }