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.sql.SQLException;
25  import java.util.Properties;
26  
27  import org.apache.commons.jcs.JCS;
28  import org.apache.commons.jcs.access.CacheAccess;
29  
30  import junit.framework.TestCase;
31  
32  /** Tests for the removal functionality. */
33  public class JDBCDiskCacheRemovalUnitTest
34      extends TestCase
35  {
36      /** db name -- set in system props */
37      private final String databaseName = "JCS_STORE_REMOVAL";
38  
39      /**
40       * Test setup
41       */
42      @Override
43      public void setUp()
44      {
45          System.setProperty( "DATABASE_NAME", databaseName );
46          JCS.setConfigFilename( "/TestJDBCDiskCacheRemoval.ccf" );
47      }
48  
49      /**
50       * Verify the fix for BUG JCS-20
51       * <p>
52       * Setup an hsql db. Add an item. Remove using partial key.
53       * @throws Exception
54       */
55      public void testPartialKeyRemoval_Good()
56          throws Exception
57      {
58          // SETUP
59          setupDatabase();
60  
61          String keyPart1 = "part1";
62          String keyPart2 = "part2";
63          String region = "testCache1";
64          String data = "adfadsfasfddsafasasd";
65  
66          CacheAccess<String, String> jcs = JCS.getInstance( region );
67  
68          // DO WORK
69          jcs.put( keyPart1 + ":" + keyPart2, data );
70          Thread.sleep( 1000 );
71  
72          // VERIFY
73          String resultBeforeRemove = jcs.get( keyPart1 + ":" + keyPart2 );
74          assertEquals( "Wrong result", data, resultBeforeRemove );
75  
76          jcs.remove( keyPart1 + ":" );
77          String resultAfterRemove = jcs.get( keyPart1 + ":" + keyPart2 );
78          assertNull( "Should not have a result after removal.", resultAfterRemove );
79  
80  //        System.out.println( jcs.getStats() );
81      }
82  
83      /**
84       * Create the database.
85       * @throws InstantiationException
86       * @throws IllegalAccessException
87       * @throws ClassNotFoundException
88       * @throws SQLException
89       */
90      private void setupDatabase()
91          throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException
92      {
93          System.setProperty( "hsqldb.cache_scale", "8" );
94  
95          String rafroot = "target";
96          Properties p = new Properties();
97          String driver = p.getProperty( "driver", "org.hsqldb.jdbcDriver" );
98          String url = p.getProperty( "url", "jdbc:hsqldb:" );
99          String database = p.getProperty( "database", rafroot + "/JDBCDiskCacheRemovalUnitTest" );
100         String user = p.getProperty( "user", "sa" );
101         String password = p.getProperty( "password", "" );
102 
103         new org.hsqldb.jdbcDriver();
104         Class.forName( driver ).newInstance();
105         Connection cConn = DriverManager.getConnection( url + database, user, password );
106 
107         HsqlSetupTableUtil.setupTABLE( cConn, databaseName );
108     }
109 }