1 package org.apache.commons.jcs3.auxiliary.disk.jdbc;
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.assertNull;
24
25 import java.sql.Connection;
26 import java.sql.DriverManager;
27 import java.util.Properties;
28
29 import org.apache.commons.jcs3.utils.timing.SleepUtil;
30 import org.apache.commons.jcs3.JCS;
31 import org.apache.commons.jcs3.access.CacheAccess;
32 import org.apache.commons.jcs3.access.exception.CacheException;
33 import org.junit.Before;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36
37
38
39
40 public class JDBCDiskCacheShrinkUnitTest
41 {
42
43
44
45
46
47 @BeforeClass
48 public static void setupDatabase() throws Exception
49 {
50 System.setProperty( "hsqldb.cache_scale", "8" );
51
52 final String rafroot = "target";
53 final Properties p = new Properties();
54 final String driver = p.getProperty( "driver", "org.hsqldb.jdbcDriver" );
55 final String url = p.getProperty( "url", "jdbc:hsqldb:" );
56 final String database = p.getProperty( "database", rafroot + "/JDBCDiskCacheShrinkUnitTest" );
57 final String user = p.getProperty( "user", "sa" );
58 final String password = p.getProperty( "password", "" );
59
60 new org.hsqldb.jdbcDriver();
61 Class.forName( driver ).newInstance();
62 final Connection cConn = DriverManager.getConnection( url + database, user, password );
63
64 HsqlSetupTableUtil.setupTABLE( cConn, "JCS_STORE_SHRINK" );
65 }
66
67
68
69
70 @Before
71 public void setUp()
72 {
73 JCS.setConfigFilename( "/TestJDBCDiskCacheShrink.ccf" );
74 }
75
76
77
78
79
80
81
82 @Test
83 public void testExpireInBackground()
84 throws Exception
85 {
86 final String regionExpire = "expire1Second";
87 final int items = 200;
88
89 final CacheAccess<String, String> jcsExpire = JCS.getInstance( regionExpire );
90
91
92
93
94
95 for ( int i = 0; i <= items; i++ )
96 {
97 jcsExpire.put( i + ":key", regionExpire + " data " + i );
98 }
99
100
101
102
103 SleepUtil.sleepAtLeast( 3000 );
104
105
106
107
108 for ( int i = 0; i <= items; i++ )
109 {
110 assertNull( "Removed key should be null: " + i + ":key", jcsExpire.get( i + ":key" ) );
111 }
112 }
113
114
115
116
117
118
119
120 @Test
121 public void testDidNotExpire()
122 throws CacheException, InterruptedException
123 {
124 final String region = "expire100Second";
125 final int items = 200;
126
127 final CacheAccess<String, String> jcs = JCS.getInstance( region );
128
129
130
131
132
133 for ( int i = 0; i <= items; i++ )
134 {
135 jcs.put( i + ":key", region + " data " + i );
136 }
137
138
139
140 SleepUtil.sleepAtLeast( 1000 );
141
142
143
144
145
146 for ( int i = 0; i <= items; i++ )
147 {
148 final String value = jcs.get( i + ":key" );
149
150 assertEquals( "key = [" + i + ":key] value = [" + value + "]", region + " data " + i, value );
151 }
152
153
154
155 for ( int i = 0; i <= items; i++ )
156 {
157 jcs.remove( i + ":key" );
158 }
159
160
161
162 for ( int i = 0; i <= items; i++ )
163 {
164 assertNull( "Removed key should be null: " + i + ":key", jcs.get( i + ":key" ) );
165 }
166 }
167
168
169
170
171
172
173 @Test
174 public void testDidNotExpireEternal()
175 throws CacheException, InterruptedException
176 {
177 final String region = "eternal";
178 final int items = 200;
179
180 final CacheAccess<String, String> jcs = JCS.getInstance( region );
181
182
183
184
185
186 for ( int i = 0; i <= items; i++ )
187 {
188 jcs.put( i + ":key", region + " data " + i );
189 }
190
191
192
193 SleepUtil.sleepAtLeast( 1000 );
194
195
196
197
198
199 for ( int i = 0; i <= items; i++ )
200 {
201 final String value = jcs.get( i + ":key" );
202
203 assertEquals( "key = [" + i + ":key] value = [" + value + "]", region + " data " + i, value );
204 }
205
206
207
208 for ( int i = 0; i <= items; i++ )
209 {
210 jcs.remove( i + ":key" );
211 }
212
213
214
215 for ( int i = 0; i <= items; i++ )
216 {
217 assertNull( "Removed key should be null: " + i + ":key", jcs.get( i + ":key" ) );
218 }
219 }
220 }