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