1 package org.apache.commons.jcs.auxiliary.disk.jdbc.mysql;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
42
43
44 public class MySQLDiskCacheHsqlBackedUnitTest
45 {
46
47
48
49
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
70
71 @Before
72 public void setUp()
73 {
74 JCS.setConfigFilename( "/TestMySQLDiskCache.ccf" );
75 }
76
77
78
79
80
81 @Test
82 public void testSimpleJDBCPutGetWithHSQL()
83 throws Exception
84 {
85 runTestForRegion( "testCache1", 200 );
86 }
87
88
89
90
91
92
93
94
95
96 public void runTestForRegion( String region, int items )
97 throws Exception
98 {
99 CacheAccess<String, String> jcs = JCS.getInstance( region );
100
101
102
103 for ( int i = 0; i < items; i++ )
104 {
105 jcs.put( i + ":key", region + " data " + i );
106 }
107
108
109 Thread.sleep( 1000 );
110
111
112
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
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
136 for ( int i = 0; i < items; i++ )
137 {
138 jcs.remove( i + ":key" );
139 }
140
141
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
151
152
153
154 @Test
155 public void testPutGetMatchingWithHSQL()
156 throws Exception
157 {
158
159 int items = 200;
160 String region = "testCache2";
161 CacheAccess<String, String> jcs = JCS.getInstance( region );
162
163
164
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
174 assertEquals( "Wrong number returned", 10, matchingResults.size() );
175
176
177 }
178 }