1 package org.apache.commons.jcs.auxiliary.disk.jdbc.hsql;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import junit.framework.TestCase;
23 import org.apache.commons.jcs.JCS;
24 import org.apache.commons.jcs.access.CacheAccess;
25 import org.apache.commons.jcs.access.exception.CacheException;
26 import org.apache.commons.jcs.engine.behavior.ICacheElement;
27
28 import java.util.HashSet;
29 import java.util.Map;
30 import java.util.Set;
31
32
33
34
35
36 public class HSQLDiskCacheUnitTest
37 extends TestCase
38 {
39
40
41
42 @Override
43 public void setUp()
44 {
45 JCS.setConfigFilename( "/TestHSQLDiskCache.ccf" );
46 }
47
48
49
50
51
52
53
54 public void testBasicPutRemove()
55 throws Exception
56 {
57 int items = 20;
58
59 String region = "testBasicPutRemove";
60
61 CacheAccess<String, String> jcs = JCS.getInstance( region );
62
63
64
65 for ( int i = 0; i <= items; i++ )
66 {
67 jcs.put( i + ":key", region + " data " + i );
68 }
69
70
71
72
73
74
75
76 for ( int i = 0; i <= items; i++ )
77 {
78 String value = jcs.get( i + ":key" );
79
80 assertEquals( "key = [" + i + ":key] value = [" + value + "]", region + " data " + i, value );
81 }
82
83
84 Set<String> keys = new HashSet<String>();
85 for ( int i = 0; i <= items; i++ )
86 {
87 keys.add( i + ":key" );
88 }
89
90 Map<String, ICacheElement<String, String>> elements = jcs.getCacheElements( keys );
91 for ( int i = 0; i <= items; i++ )
92 {
93 ICacheElement<String, String> element = elements.get( i + ":key" );
94 assertNotNull( "element " + i + ":key is missing", element );
95 assertEquals( "value " + i + ":key", region + " data " + i, element.getVal() );
96 }
97
98
99
100
101 for ( int i = 0; i <= items; i++ )
102 {
103 jcs.remove( i + ":key" );
104 }
105
106
107
108 for ( int i = 0; i <= items; i++ )
109 {
110 assertNull( "Removed key should be null: " + i + ":key", jcs.get( i + ":key" ) );
111 }
112 }
113
114
115
116
117
118
119
120 public void testRemoveAll()
121 throws CacheException, InterruptedException
122 {
123 String region = "removeAllAllowed";
124 CacheAccess<String, String> jcs = JCS.getInstance( region );
125
126 int items = 20;
127
128
129
130 for ( int i = 0; i <= items; i++ )
131 {
132 jcs.put( i + ":key", region + " data " + i );
133 }
134
135
136
137
138
139
140
141
142
143
144 jcs.clear();
145
146 for ( int i = 0; i <= items; i++ )
147 {
148 String value = jcs.get( i + ":key" );
149
150 assertNull( "value should be null key = [" + i + ":key] value = [" + value + "]", value );
151 }
152 }
153
154
155
156
157
158
159
160 public void testRemoveAllProhibition()
161 throws CacheException, InterruptedException
162 {
163 String region = "noRemoveAll";
164 CacheAccess<String, String> jcs = JCS.getInstance( region );
165
166 int items = 20;
167
168
169
170 for ( int i = 0; i <= items; i++ )
171 {
172 jcs.put( i + ":key", region + " data " + i );
173 }
174
175
176
177
178
179
180 jcs.clear();
181
182 for ( int i = 0; i <= items; i++ )
183 {
184 String value = jcs.get( i + ":key" );
185
186 assertEquals( "key = [" + i + ":key] value = [" + value + "]", region + " data " + i, value );
187 }
188 }
189 }