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  /*** Tests for the removal functionality. */
33  public class JDBCDiskCacheRemovalUnitTest
34      extends TestCase
35  {
36      /*** db name -- set in system props */
37      private String databaseName = "JCS_STORE_REMOVAL";
38  
39      /***
40       * Test setup
41       */
42      public void setUp()
43      {
44          System.setProperty( "DATABASE_NAME", databaseName );
45          JCS.setConfigFilename( "/TestJDBCDiskCacheRemoval.ccf" );
46      }
47  
48      /***
49       * Verify the fix for BUG JCS-20
50       * <p>
51       * Setup an hsql db. Add an item. Remove using partial key.
52       * @throws Exception 
53       */
54      public void testPartialKeyRemoval_Good()
55          throws Exception
56      {
57          // SETUP
58          setupDatabase();
59  
60          String keyPart1 = "part1";
61          String keyPart2 = "part2";
62          String region = "testCache1";
63          String data = "adfadsfasfddsafasasd";
64  
65          JCS jcs = JCS.getInstance( region );
66  
67          // DO WORK
68          jcs.put( keyPart1 + ":" + keyPart2, data );
69          Thread.sleep( 1000 );
70  
71          // VERIFY
72          String resultBeforeRemove = (String) jcs.get( keyPart1 + ":" + keyPart2 );
73          assertEquals( "Wrong result", data, resultBeforeRemove );
74  
75          jcs.remove( keyPart1 + ":" );
76          String resultAfterRemove = (String) jcs.get( keyPart1 + ":" + keyPart2 );
77          assertNull( "Should not have a result after removal.", resultAfterRemove );
78  
79          System.out.println( jcs.getStats() );
80      }
81  
82      /***
83       * Create the database.
84       * @throws InstantiationException 
85       * @throws IllegalAccessException 
86       * @throws ClassNotFoundException 
87       * @throws SQLException
88       */
89      private void setupDatabase()
90          throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException
91      {
92          System.setProperty( "hsqldb.cache_scale", "8" );
93  
94          String rafroot = "target";
95          Properties p = new Properties();
96          String driver = p.getProperty( "driver", "org.hsqldb.jdbcDriver" );
97          String url = p.getProperty( "url", "jdbc:hsqldb:" );
98          String database = p.getProperty( "database", rafroot + "/JDBCDiskCacheRemovalUnitTest" );
99          String user = p.getProperty( "user", "sa" );
100         String password = p.getProperty( "password", "" );
101 
102         new org.hsqldb.jdbcDriver();
103         Class.forName( driver ).newInstance();
104         Connection cConn = DriverManager.getConnection( url + database, user, password );
105 
106         setupTABLE( cConn );
107     }
108 
109     /***
110      * SETUP TABLE FOR CACHE
111      * <p>
112      * @param cConn
113      */
114     private void setupTABLE( Connection cConn )
115     {
116         boolean newT = true;
117 
118         StringBuffer createSql = new StringBuffer();
119         createSql.append( "CREATE CACHED TABLE " + databaseName );
120         createSql.append( "( " );
121         createSql.append( "CACHE_KEY             VARCHAR(250)          NOT NULL, " );
122         createSql.append( "REGION                VARCHAR(250)          NOT NULL, " );
123         createSql.append( "ELEMENT               BINARY, " );
124         createSql.append( "CREATE_TIME           DATE, " );
125         createSql.append( "CREATE_TIME_SECONDS   BIGINT, " );
126         createSql.append( "MAX_LIFE_SECONDS      BIGINT, " );
127         createSql.append( "SYSTEM_EXPIRE_TIME_SECONDS      BIGINT, " );
128         createSql.append( "IS_ETERNAL            CHAR(1), " );
129         createSql.append( "PRIMARY KEY (CACHE_KEY, REGION) " );
130         createSql.append( ");" );
131 
132         Statement sStatement = null;
133         try
134         {
135             sStatement = cConn.createStatement();
136         }
137         catch ( SQLException e )
138         {
139             e.printStackTrace();
140         }
141 
142         try
143         {
144             sStatement.executeQuery( createSql.toString() );
145             sStatement.close();
146         }
147         catch ( SQLException e )
148         {
149             if ( e.toString().indexOf( "already exists" ) != -1 )
150             {
151                 newT = false;
152             }
153             else
154             {
155                 // TODO figure out if it exists prior to trying to create it.
156                 // log.error( "Problem creating table.", e );
157                 e.printStackTrace();
158             }
159         }
160 
161         String setupData[] = { "create index iKEY on " + databaseName + " (CACHE_KEY, REGION)" };
162 
163         if ( newT )
164         {
165             for ( int i = 1; i < setupData.length; i++ )
166             {
167                 try
168                 {
169                     sStatement.executeQuery( setupData[i] );
170                 }
171                 catch ( SQLException e )
172                 {
173                     System.out.println( "Exception: " + e );
174                 }
175             }
176         } // end ifnew
177     }
178 }