You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jcs-dev@jakarta.apache.org by as...@apache.org on 2006/03/16 20:05:37 UTC

svn commit: r386420 - in /jakarta/jcs/trunk/src: test-conf/TestJDBCDiskCache.ccf test/org/apache/jcs/auxiliary/disk/jdbc/JDBCDiskCacheUnitTest.java

Author: asmuts
Date: Thu Mar 16 11:05:09 2006
New Revision: 386420

URL: http://svn.apache.org/viewcvs?rev=386420&view=rev
Log:
added unit test for jdbc disk cache using an hsql db backend.
i will replae the hsql disk cache with something that extends the basic jdbc version.
added new test config file for jdbc.
fixed bug in remote cache server that causes an error message when an item is put while the same listener is being disposed.

Added:
    jakarta/jcs/trunk/src/test-conf/TestJDBCDiskCache.ccf
    jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/jdbc/JDBCDiskCacheUnitTest.java

Added: jakarta/jcs/trunk/src/test-conf/TestJDBCDiskCache.ccf
URL: http://svn.apache.org/viewcvs/jakarta/jcs/trunk/src/test-conf/TestJDBCDiskCache.ccf?rev=386420&view=auto
==============================================================================
--- jakarta/jcs/trunk/src/test-conf/TestJDBCDiskCache.ccf (added)
+++ jakarta/jcs/trunk/src/test-conf/TestJDBCDiskCache.ccf Thu Mar 16 11:05:09 2006
@@ -0,0 +1,45 @@
+# Cache configuration for the 'TestHSQLDiskCache' test. The memory cache has a
+# a maximum of 100 objects, so objects should get pushed into the disk cache
+
+jcs.default=JDBC
+jcs.default.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes
+jcs.default.cacheattributes.MaxObjects=100
+jcs.default.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache
+jcs.default.cacheattributes.UseMemoryShrinker=false
+jcs.default.cacheattributes.MaxMemoryIdleTimeSeconds=3600
+jcs.default.cacheattributes.ShrinkerIntervalSeconds=60
+jcs.default.elementattributes=org.apache.jcs.engine.ElementAttributes
+jcs.default.elementattributes.IsEternal=false
+jcs.default.elementattributes.MaxLifeSeconds=700
+jcs.default.elementattributes.IdleTime=1800
+jcs.default.elementattributes.IsSpool=true
+jcs.default.elementattributes.IsRemote=true
+jcs.default.elementattributes.IsLateral=true
+
+##############################################################
+################## AUXILIARY CACHES AVAILABLE ################
+# JDBC disk cache
+jcs.auxiliary.JDBC=org.apache.jcs.auxiliary.disk.jdbc.JDBCDiskCacheFactory
+jcs.auxiliary.JDBC.attributes=org.apache.jcs.auxiliary.disk.jdbc.JDBCDiskCacheAttributes
+jcs.auxiliary.JDBC.attributes.userName=sa
+jcs.auxiliary.JDBC.attributes.password=
+jcs.auxiliary.JDBC.attributes.url=jdbc:hsqldb:target/cache_hsql_db
+jcs.auxiliary.JDBC.attributes.driverClassName=org.hsqldb.jdbcDriver
+jcs.auxiliary.JDBC.attributes.tableName=JCS_STORE2
+jcs.auxiliary.JDBC.attributes.testBeforeInsert=false
+jcs.auxiliary.JDBC.attributes.maxActive=15
+jcs.auxiliary.JDBC.attributes.MaxPurgatorySize=10000000
+jcs.auxiliary.JDBC.attributes.EventQueueType=POOLED
+jcs.auxiliary.JDBC.attributes.EventQueuePoolName=disk_cache_event_queue
+
+
+##############################################################
+################## OPTIONAL THREAD POOL CONFIGURATION #########
+# Disk Cache pool
+thread_pool.disk_cache_event_queue.useBoundary=false
+thread_pool.disk_cache_event_queue.boundarySize=500
+thread_pool.disk_cache_event_queue.maximumPoolSize=15
+thread_pool.disk_cache_event_queue.minimumPoolSize=10
+thread_pool.disk_cache_event_queue.keepAliveTime=3500
+thread_pool.disk_cache_event_queue.whenBlockedPolicy=RUN
+thread_pool.disk_cache_event_queue.startUpSize=10

Added: jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/jdbc/JDBCDiskCacheUnitTest.java
URL: http://svn.apache.org/viewcvs/jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/jdbc/JDBCDiskCacheUnitTest.java?rev=386420&view=auto
==============================================================================
--- jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/jdbc/JDBCDiskCacheUnitTest.java (added)
+++ jakarta/jcs/trunk/src/test/org/apache/jcs/auxiliary/disk/jdbc/JDBCDiskCacheUnitTest.java Thu Mar 16 11:05:09 2006
@@ -0,0 +1,181 @@
+package org.apache.jcs.auxiliary.disk.jdbc;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Properties;
+
+import junit.framework.TestCase;
+
+import org.apache.jcs.JCS;
+
+/**
+ * Runs basic tests for the JDBC disk cache.
+ * 
+ * @author Aaron Smuts
+ * 
+ */
+public class JDBCDiskCacheUnitTest
+    extends TestCase
+{
+
+    /**
+     * Test setup
+     */
+    public void setUp()
+    {
+        JCS.setConfigFilename( "/TestJDBCDiskCache.ccf" );
+    }
+
+    /**
+     * Test the basic JDBC disk cache functionality with a hsql backing.
+     * 
+     * @throws Exception
+     */
+    public void testSimpleJDBCPutGetWithHSQL()
+        throws Exception
+    {
+        System.setProperty( "hsqldb.cache_scale", "8" );
+        
+        String rafroot = "target";
+        Properties p = new Properties();
+        String driver = p.getProperty( "driver", "org.hsqldb.jdbcDriver" );
+        String url = p.getProperty( "url", "jdbc:hsqldb:" );
+        String database = p.getProperty( "database", rafroot + "/cache_hsql_db" );
+        String user = p.getProperty( "user", "sa" );
+        String password = p.getProperty( "password", "" );
+
+        new org.hsqldb.jdbcDriver();
+        Class.forName( driver ).newInstance();
+        Connection cConn = DriverManager.getConnection( url + database, user, password );
+
+        setupTABLE( cConn );
+
+        runTestForRegion( "testCache1", 200 );
+    }
+
+    /**
+     * Adds items to cache, gets them, and removes them. The item count is more
+     * than the size of the memory cache, so items should spool to disk.
+     * 
+     * @param region
+     *            Name of the region to access
+     * @param items
+     * 
+     * @exception Exception
+     *                If an error occurs
+     */
+    public void runTestForRegion( String region, int items )
+        throws Exception
+    {
+        JCS jcs = JCS.getInstance( region );
+
+        System.out.println( "BEFORE PUT \n" + jcs.getStats() );
+        
+        // Add items to cache
+
+        for ( int i = 0; i <= items; i++ )
+        {
+            jcs.put( i + ":key", region + " data " + i );
+        }
+
+        System.out.println( jcs.getStats() );
+
+        Thread.sleep( 1000 );
+
+        System.out.println( jcs.getStats() );
+
+        // Test that all items are in cache
+
+        for ( int i = 0; i <= items; i++ )
+        {
+            String value = (String) jcs.get( i + ":key" );
+
+            assertEquals( "key = [" + i + ":key] value = [" + value + "]", region + " data " + i, value );
+        }
+
+        // Remove all the items
+
+        for ( int i = 0; i <= items; i++ )
+        {
+            jcs.remove( i + ":key" );
+        }
+
+        // Verify removal
+
+        for ( int i = 0; i <= items; i++ )
+        {
+            assertNull( "Removed key should be null: " + i + ":key", jcs.get( i + ":key" ) );
+        }
+    }
+
+    /**
+     * SETUP TABLE FOR CACHE
+     * 
+     * @param cConn
+     */
+    void setupTABLE( Connection cConn )
+    {
+        boolean newT = true;
+
+        StringBuffer createSql = new StringBuffer();
+        createSql.append( "CREATE CACHED TABLE JCS_STORE2 " );
+        createSql.append( "( " );
+        createSql.append( "CACHE_KEY             VARCHAR(250)          NOT NULL, " );
+        createSql.append( "REGION                VARCHAR(250)          NOT NULL, " );
+        createSql.append( "ELEMENT               BINARY, " );
+        createSql.append( "CREATE_TIME           DATE, " );
+        createSql.append( "CREATE_TIME_SECONDS   BIGINT, " );
+        createSql.append( "MAX_LIFE_SECONDS      BIGINT, " );
+        createSql.append( "IS_ETERNAL            CHAR(1), " );
+        createSql.append( "PRIMARY KEY (CACHE_KEY, REGION) " );
+        createSql.append( ");" );
+
+        Statement sStatement = null;
+        try
+        {
+            sStatement = cConn.createStatement();
+        }
+        catch ( SQLException e )
+        {
+            e.printStackTrace();
+        }
+
+        try
+        {
+            sStatement.executeQuery( createSql.toString() );
+            sStatement.close();
+        }
+        catch ( SQLException e )
+        {
+            if ( e.toString().indexOf( "already exists" ) != -1 )
+            {
+                newT = false;
+            }
+            else
+            {
+                // TODO figure out if it exists prior to trying to create it.
+                // log.error( "Problem creating table.", e );
+                e.printStackTrace();
+            }
+        }
+
+        String setupData[] = { "create index iKEY on JCS_STORE (CACHE_KEY, REGION)" };
+
+        if ( newT )
+        {
+            for ( int i = 1; i < setupData.length; i++ )
+            {
+                try
+                {
+                    sStatement.executeQuery( setupData[i] );
+                }
+                catch ( SQLException e )
+                {
+                    System.out.println( "Exception: " + e );
+                }
+            }
+        } // end ifnew
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: jcs-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jcs-dev-help@jakarta.apache.org