1 package org.apache.commons.jcs;
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.access.CacheAccess;
24
25
26
27
28 public class JCSRemovalSimpleConcurrentTest
29 extends TestCase
30 {
31
32
33
34 public JCSRemovalSimpleConcurrentTest( String testName )
35 {
36 super( testName );
37 }
38
39
40
41
42
43
44 @Override
45 public void setUp()
46 throws Exception
47 {
48 JCS.setConfigFilename( "/TestRemoval.ccf" );
49 JCS.getInstance( "testCache1" );
50 }
51
52
53
54
55
56
57 public static void main( String args[] )
58 {
59 String[] testCaseName = { JCSRemovalSimpleConcurrentTest.class.getName() };
60 junit.textui.TestRunner.main( testCaseName );
61 }
62
63
64
65
66
67
68 public void testTwoDeepRemoval()
69 throws Exception
70 {
71 int count = 500;
72 CacheAccess<String, String> jcs = JCS.getInstance( "testCache1" );
73
74 for ( int i = 0; i <= count; i++ )
75 {
76 jcs.put( "key:" + i + ":anotherpart", "data" + i );
77 }
78
79 for ( int i = count; i >= 0; i-- )
80 {
81 String res = jcs.get( "key:" + i + ":anotherpart" );
82 assertNotNull( "[key:" + i + ":anotherpart] should not be null, " + jcs.getStats(), res );
83 }
84
85 for ( int i = 0; i <= count; i++ )
86 {
87 jcs.remove( "key:" + i + ":" );
88 assertNull( jcs.getStats(), jcs.get( "key:" + i + ":anotherpart" ) );
89 }
90
91 }
92
93
94
95
96
97
98 public void testSingleDepthRemoval()
99 throws Exception
100 {
101
102 int count = 500;
103 CacheAccess<String, String> jcs = JCS.getInstance( "testCache1" );
104
105 for ( int i = 0; i <= count; i++ )
106 {
107 jcs.put( i + ":key", "data" + i );
108 }
109
110 for ( int i = count; i >= 0; i-- )
111 {
112 String res = jcs.get( i + ":key" );
113 assertNotNull( "[" + i + ":key] should not be null", res );
114 }
115
116 for ( int i = 0; i <= count; i++ )
117 {
118 jcs.remove( i + ":" );
119 assertNull( jcs.get( i + ":key" ) );
120 }
121 }
122
123
124
125
126
127
128 public void testClear()
129 throws Exception
130 {
131
132 int count = 500;
133 CacheAccess<String, String> jcs = JCS.getInstance( "testCache1" );
134
135 for ( int i = 0; i <= count; i++ )
136 {
137 jcs.put( i + ":key", "data" + i );
138 }
139
140 for ( int i = count; i >= 0; i-- )
141 {
142 String res = jcs.get( i + ":key" );
143 assertNotNull( "[" + i + ":key] should not be null", res );
144 }
145 jcs.clear();
146
147 for ( int i = count; i >= 0; i-- )
148 {
149 String res = jcs.get( i + ":key" );
150 if ( res != null )
151 {
152 assertNull( "[" + i + ":key] should be null after remvoeall" + jcs.getStats(), res );
153 }
154 }
155 }
156
157
158
159
160
161
162 public void testClearRepeatedlyWithoutError()
163 throws Exception
164 {
165 int count = 500;
166 CacheAccess<String, String> jcs = JCS.getInstance( "testCache1" );
167
168 jcs.clear();
169
170 for ( int i = 0; i <= count; i++ )
171 {
172 jcs.put( i + ":key", "data" + i );
173 }
174
175 for ( int i = count; i >= 0; i-- )
176 {
177 String res = jcs.get( i + ":key" );
178 assertNotNull( "[" + i + ":key] should not be null", res );
179 }
180
181 for ( int i = count; i >= 0; i-- )
182 {
183 jcs.put( i + ":key", "data" + i );
184 jcs.clear();
185 String res = jcs.get( i + ":key" );
186 if ( res != null )
187 {
188 assertNull( "[" + i + ":key] should be null after remvoeall" + jcs.getStats(), res );
189 }
190 }
191 }
192 }