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.extensions.ActiveTestSuite;
23 import junit.framework.Test;
24 import junit.framework.TestCase;
25 import org.apache.commons.jcs.JCS;
26 import org.apache.commons.jcs.access.CacheAccess;
27 import org.apache.commons.jcs.engine.behavior.ICacheElement;
28
29 import java.util.HashSet;
30 import java.util.Map;
31 import java.util.Set;
32
33
34
35
36
37 public class HSQLDiskCacheConcurrentUnitTest
38 extends TestCase
39 {
40
41
42
43 private static int items = 100;
44
45
46
47
48
49 public HSQLDiskCacheConcurrentUnitTest( String testName )
50 {
51 super( testName );
52 }
53
54
55
56
57
58
59 public static void main( String args[] )
60 {
61 String[] testCaseName = { HSQLDiskCacheConcurrentUnitTest.class.getName() };
62 junit.textui.TestRunner.main( testCaseName );
63 }
64
65
66
67
68
69
70 public static Test suite()
71 {
72 ActiveTestSuite suite = new ActiveTestSuite();
73
74 suite.addTest( new HSQLDiskCacheConcurrentUnitTest( "testHSQLDiskCache1" )
75 {
76 @Override
77 public void runTest()
78 throws Exception
79 {
80 this.runTestForRegion( "indexedRegion1" );
81 }
82 } );
83
84 suite.addTest( new HSQLDiskCacheConcurrentUnitTest( "testHSQLDiskCache2" )
85 {
86 @Override
87 public void runTest()
88 throws Exception
89 {
90 this.runTestForRegion( "indexedRegion2" );
91 }
92 } );
93
94 suite.addTest( new HSQLDiskCacheConcurrentUnitTest( "testHSQLDiskCache3" )
95 {
96 @Override
97 public void runTest()
98 throws Exception
99 {
100 this.runTestForRegion( "indexedRegion3" );
101 }
102 } );
103
104 return suite;
105 }
106
107
108
109
110 @Override
111 public void setUp()
112 {
113 JCS.setConfigFilename( "/TestHSQLDiskCacheConcurrent.ccf" );
114 }
115
116
117
118
119
120
121
122
123 public void runTestForRegion( String region )
124 throws Exception
125 {
126 CacheAccess<String, String> jcs = JCS.getInstance( region );
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 for ( int i = 0; i <= items; i++ )
140 {
141 String value = jcs.get( i + ":key" );
142
143 assertEquals( "key = [" + i + ":key] value = [" + value + "]", region + " data " + i, value );
144 }
145
146
147 Set<String> keys = new HashSet<String>();
148 for ( int i = 0; i <= items; i++ )
149 {
150 keys.add( i + ":key" );
151 }
152
153 Map<String, ICacheElement<String, String>> elements = jcs.getCacheElements( keys );
154 for ( int i = 0; i <= items; i++ )
155 {
156 ICacheElement<String, String> element = elements.get( i + ":key" );
157 assertNotNull( "element " + i + ":key is missing", element );
158 assertEquals( "value " + i + ":key", region + " data " + i, element.getVal() );
159 }
160
161
162
163 for ( int i = 0; i <= items; i++ )
164 {
165 jcs.remove( i + ":key" );
166 }
167
168
169
170 for ( int i = 0; i <= items; i++ )
171 {
172 assertNull( "Removed key should be null: " + i + ":key", jcs.get( i + ":key" ) );
173 }
174 }
175 }