View Javadoc
1   package org.apache.commons.jcs.auxiliary.disk.jdbc.mysql;
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 static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertNull;
25  
26  import java.sql.Connection;
27  import java.sql.DriverManager;
28  import java.util.HashSet;
29  import java.util.Map;
30  import java.util.Set;
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.HsqlSetupTableUtil;
35  import org.apache.commons.jcs.engine.behavior.ICacheElement;
36  import org.junit.Before;
37  import org.junit.BeforeClass;
38  import org.junit.Test;
39  
40  /**
41   * Runs basic tests for the JDBC disk cache.
42   * @author Aaron Smuts
43   */
44  public class MySQLDiskCacheHsqlBackedUnitTest
45  {
46      /**
47       * Creates the DB
48       * <p>
49       * @throws Exception
50       */
51      @BeforeClass
52      public static void setupDatabase() throws Exception
53      {
54          System.setProperty( "hsqldb.cache_scale", "8" );
55  
56          String rafroot = "target";
57          String url = "jdbc:hsqldb:";
58          String database = rafroot + "/MySQLDiskCacheHsqlBackedUnitTest";
59          String user = "sa";
60          String password = "";
61  
62          new org.hsqldb.jdbcDriver();
63          Connection cConn = DriverManager.getConnection( url + database, user, password );
64  
65          HsqlSetupTableUtil.setupTABLE( cConn, "JCS_STORE_MYSQL" );
66      }
67  
68      /**
69       * Test setup
70       */
71      @Before
72      public void setUp()
73      {
74          JCS.setConfigFilename( "/TestMySQLDiskCache.ccf" );
75      }
76  
77      /**
78       * Test the basic JDBC disk cache functionality with a hsql backing.
79       * @throws Exception
80       */
81      @Test
82      public void testSimpleJDBCPutGetWithHSQL()
83          throws Exception
84      {
85          runTestForRegion( "testCache1", 200 );
86      }
87  
88      /**
89       * Adds items to cache, gets them, and removes them. The item count is more than the size of the
90       * memory cache, so items should spool to disk.
91       * <p>
92       * @param region Name of the region to access
93       * @param items
94       * @throws Exception If an error occurs
95       */
96      public void runTestForRegion( String region, int items )
97          throws Exception
98      {
99          CacheAccess<String, String> jcs = JCS.getInstance( region );
100         //System.out.println( "BEFORE PUT \n" + jcs.getStats() );
101 
102         // Add items to cache
103         for ( int i = 0; i < items; i++ )
104         {
105             jcs.put( i + ":key", region + " data " + i );
106         }
107 
108         //System.out.println( jcs.getStats() );
109         Thread.sleep( 1000 );
110         //System.out.println( jcs.getStats() );
111 
112         // Test that all items are in cache
113         for ( int i = 0; i < items; i++ )
114         {
115             String value = jcs.get( i + ":key" );
116 
117             assertEquals( "key = [" + i + ":key] value = [" + value + "]", region + " data " + i, value );
118         }
119 
120         // Test that getElements returns all the expected values
121         Set<String> keys = new HashSet<String>();
122         for ( int i = 0; i < items; i++ )
123         {
124             keys.add( i + ":key" );
125         }
126 
127         Map<String, ICacheElement<String, String>> elements = jcs.getCacheElements( keys );
128         for ( int i = 0; i < items; i++ )
129         {
130             ICacheElement<String, String> element = elements.get( i + ":key" );
131             assertNotNull( "element " + i + ":key is missing", element );
132             assertEquals( "value " + i + ":key", region + " data " + i, element.getVal() );
133         }
134 
135         // Remove all the items
136         for ( int i = 0; i < items; i++ )
137         {
138             jcs.remove( i + ":key" );
139         }
140 
141         // Verify removal
142 
143         for ( int i = 0; i < items; i++ )
144         {
145             assertNull( "Removed key should be null: " + i + ":key", jcs.get( i + ":key" ) );
146         }
147     }
148 
149     /**
150      * Test the basic JDBC disk cache functionality with a hsql backing.
151      * <p>
152      * @throws Exception
153      */
154     @Test
155     public void testPutGetMatchingWithHSQL()
156         throws Exception
157     {
158         // SETUP
159         int items = 200;
160         String region = "testCache2";
161         CacheAccess<String, String> jcs = JCS.getInstance( region );
162 //        System.out.println( "BEFORE PUT \n" + jcs.getStats() );
163 
164         // DO WORK
165         for ( int i = 0; i < items; i++ )
166         {
167             jcs.put( i + ":key", region + " data " + i );
168         }
169         Thread.sleep( 1000 );
170 
171         Map<String, ICacheElement<String, String>> matchingResults = jcs.getMatchingCacheElements( "1.8.+" );
172 
173         // VERIFY
174         assertEquals( "Wrong number returned", 10, matchingResults.size() );
175 //        System.out.println( "matchingResults.keySet() " + matchingResults.keySet() );
176 //        System.out.println( "\nAFTER TEST \n" + jcs.getStats() );
177     }
178 }