1 package org.apache.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
24 import org.apache.jcs.JCS;
25 import org.apache.jcs.access.exception.CacheException;
26
27 /***
28 * Test which exercises the indexed disk cache. This one uses three different regions for thre
29 * threads.
30 */
31 public class HSQLDiskCacheUnitTest
32 extends TestCase
33 {
34 /***
35 * Test setup
36 */
37 public void setUp()
38 {
39 JCS.setConfigFilename( "/TestHSQLDiskCache.ccf" );
40 }
41
42 /***
43 * Adds items to cache, gets them, and removes them. The item count is more than the size of the
44 * memory cache, so items should spool to disk.
45 * <p>
46 * @param region Name of the region to access
47 * @exception Exception If an error occurs
48 */
49 public void testBasicPutRemove()
50 throws Exception
51 {
52 int items = 20;
53
54 String region = "testBasicPutRemove";
55
56 JCS jcs = JCS.getInstance( region );
57
58
59
60 for ( int i = 0; i <= items; i++ )
61 {
62 jcs.put( i + ":key", region + " data " + i );
63 }
64
65
66
67 System.out.println( jcs.getStats() );
68
69
70
71 for ( int i = 0; i <= items; i++ )
72 {
73 String value = (String) jcs.get( i + ":key" );
74
75 assertEquals( "key = [" + i + ":key] value = [" + value + "]", region + " data " + i, value );
76 }
77
78
79
80 for ( int i = 0; i <= items; i++ )
81 {
82 jcs.remove( i + ":key" );
83 }
84
85
86
87 for ( int i = 0; i <= items; i++ )
88 {
89 assertNull( "Removed key should be null: " + i + ":key", jcs.get( i + ":key" ) );
90 }
91 }
92
93 /***
94 * Verify that remove all work son a region where it is not prohibited.
95 * <p>
96 * @throws CacheException
97 * @throws InterruptedException
98 */
99 public void testRemoveAll()
100 throws CacheException, InterruptedException
101 {
102 String region = "removeAllAllowed";
103 JCS jcs = JCS.getInstance( region );
104
105 int items = 20;
106
107
108
109 for ( int i = 0; i <= items; i++ )
110 {
111 jcs.put( i + ":key", region + " data " + i );
112 }
113
114
115
116
117
118
119
120
121 System.out.println( jcs.getStats() );
122
123 jcs.clear();
124
125 for ( int i = 0; i <= items; i++ )
126 {
127 String value = (String) jcs.get( i + ":key" );
128
129 assertNull( "value should be null key = [" + i + ":key] value = [" + value + "]", value );
130 }
131 }
132
133 /***
134 * Verify that remove all does not work on a region where it is prohibited.
135 * <p>
136 * @throws CacheException
137 * @throws InterruptedException
138 */
139 public void testRemoveAllProhibition()
140 throws CacheException, InterruptedException
141 {
142 String region = "noRemoveAll";
143 JCS jcs = JCS.getInstance( region );
144
145 int items = 20;
146
147
148
149 for ( int i = 0; i <= items; i++ )
150 {
151 jcs.put( i + ":key", region + " data " + i );
152 }
153
154
155
156
157 System.out.println( jcs.getStats() );
158
159 jcs.clear();
160
161 for ( int i = 0; i <= items; i++ )
162 {
163 String value = (String) jcs.get( i + ":key" );
164
165 assertEquals( "key = [" + i + ":key] value = [" + value + "]", region + " data " + i, value );
166 }
167 }
168 }