1   package org.apache.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.sql.SQLException;
25  import java.sql.Statement;
26  import java.util.Properties;
27  
28  import junit.framework.TestCase;
29  
30  import org.apache.jcs.JCS;
31  
32  /***
33   * Runs basic tests for the JDBC disk cache.
34   *
35   * @author Aaron Smuts
36   *
37   */
38  public class JDBCDiskCacheUnitTest
39      extends TestCase
40  {
41  
42      /***
43       * Test setup
44       */
45      public void setUp()
46      {
47          JCS.setConfigFilename( "/TestJDBCDiskCache.ccf" );
48      }
49  
50      /***
51       * Test the basic JDBC disk cache functionality with a hsql backing.
52       *
53       * @throws Exception
54       */
55      public void testSimpleJDBCPutGetWithHSQL()
56          throws Exception
57      {
58          System.setProperty( "hsqldb.cache_scale", "8" );
59  
60          String rafroot = "target";
61          Properties p = new Properties();
62          String driver = p.getProperty( "driver", "org.hsqldb.jdbcDriver" );
63          String url = p.getProperty( "url", "jdbc:hsqldb:" );
64          String database = p.getProperty( "database", rafroot + "/cache_hsql_db" );
65          String user = p.getProperty( "user", "sa" );
66          String password = p.getProperty( "password", "" );
67  
68          new org.hsqldb.jdbcDriver();
69          Class.forName( driver ).newInstance();
70          Connection cConn = DriverManager.getConnection( url + database, user, password );
71  
72          setupTABLE( cConn );
73  
74          runTestForRegion( "testCache1", 200 );
75      }
76  
77      /***
78       * Adds items to cache, gets them, and removes them. The item count is more
79       * than the size of the memory cache, so items should spool to disk.
80       *
81       * @param region
82       *            Name of the region to access
83       * @param items
84       *
85       * @exception Exception
86       *                If an error occurs
87       */
88      public void runTestForRegion( String region, int items )
89          throws Exception
90      {
91          JCS jcs = JCS.getInstance( region );
92  
93          System.out.println( "BEFORE PUT \n" + jcs.getStats() );
94  
95          // Add items to cache
96  
97          for ( int i = 0; i <= items; i++ )
98          {
99              jcs.put( i + ":key", region + " data " + i );
100         }
101 
102         System.out.println( jcs.getStats() );
103 
104         Thread.sleep( 1000 );
105 
106         System.out.println( jcs.getStats() );
107 
108         // Test that all items are in cache
109 
110         for ( int i = 0; i <= items; i++ )
111         {
112             String value = (String) jcs.get( i + ":key" );
113 
114             assertEquals( "key = [" + i + ":key] value = [" + value + "]", region + " data " + i, value );
115         }
116 
117         // Remove all the items
118 
119         for ( int i = 0; i <= items; i++ )
120         {
121             jcs.remove( i + ":key" );
122         }
123 
124         // Verify removal
125 
126         for ( int i = 0; i <= items; i++ )
127         {
128             assertNull( "Removed key should be null: " + i + ":key", jcs.get( i + ":key" ) );
129         }
130     }
131 
132     /***
133      * SETUP TABLE FOR CACHE
134      *
135      * @param cConn
136      */
137     void setupTABLE( Connection cConn )
138     {
139         boolean newT = true;
140 
141         StringBuffer createSql = new StringBuffer();
142         createSql.append( "CREATE CACHED TABLE JCS_STORE2 " );
143         createSql.append( "( " );
144         createSql.append( "CACHE_KEY             VARCHAR(250)          NOT NULL, " );
145         createSql.append( "REGION                VARCHAR(250)          NOT NULL, " );
146         createSql.append( "ELEMENT               BINARY, " );
147         createSql.append( "CREATE_TIME           DATE, " );
148         createSql.append( "CREATE_TIME_SECONDS   BIGINT, " );
149         createSql.append( "MAX_LIFE_SECONDS      BIGINT, " );
150         createSql.append( "SYSTEM_EXPIRE_TIME_SECONDS      BIGINT, " );
151         createSql.append( "IS_ETERNAL            CHAR(1), " );
152         createSql.append( "PRIMARY KEY (CACHE_KEY, REGION) " );
153         createSql.append( ");" );
154 
155         Statement sStatement = null;
156         try
157         {
158             sStatement = cConn.createStatement();
159         }
160         catch ( SQLException e )
161         {
162             e.printStackTrace();
163         }
164 
165         try
166         {
167             sStatement.executeQuery( createSql.toString() );
168             sStatement.close();
169         }
170         catch ( SQLException e )
171         {
172             if ( e.toString().indexOf( "already exists" ) != -1 )
173             {
174                 newT = false;
175             }
176             else
177             {
178                 // TODO figure out if it exists prior to trying to create it.
179                 // log.error( "Problem creating table.", e );
180                 e.printStackTrace();
181             }
182         }
183 
184         String setupData[] = { "create index iKEY on JCS_STORE2 (CACHE_KEY, REGION)" };
185 
186         if ( newT )
187         {
188             for ( int i = 1; i < setupData.length; i++ )
189             {
190                 try
191                 {
192                     sStatement.executeQuery( setupData[i] );
193                 }
194                 catch ( SQLException e )
195                 {
196                     System.out.println( "Exception: " + e );
197                 }
198             }
199         } // end ifnew
200     }
201 }