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