1 package org.apache.jcs.engine.control.event;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import junit.framework.Test;
23 import junit.framework.TestCase;
24 import junit.framework.TestSuite;
25
26 import org.apache.jcs.JCS;
27 import org.apache.jcs.engine.behavior.IElementAttributes;
28 import org.apache.jcs.engine.control.event.behavior.IElementEvent;
29 import org.apache.jcs.engine.control.event.behavior.IElementEventConstants;
30 import org.apache.jcs.engine.control.event.behavior.IElementEventHandler;
31
32 /***
33 * This test suite verifies that the basic ElementEvent are called as they
34 * should be.
35 *
36 *
37 * @version $Id: TestSimpleEventHandling.java,v 1.1 2005/02/01 00:01:59 asmuts
38 * Exp $
39 */
40 public class SimpleEventHandlingUnitTest
41 extends TestCase
42 {
43
44 private static int items = 20000;
45
46 /***
47 * Constructor for test case.
48 *
49 * @param testName
50 * Description of the Parameter
51 */
52 public SimpleEventHandlingUnitTest( String testName )
53 {
54 super( testName );
55 }
56
57 /***
58 * Run at command line.
59 *
60 * @param args
61 * Description of the Parameter
62 */
63 public static void main( String args[] )
64 {
65 String[] testCaseName = { SimpleEventHandlingUnitTest.class.getName() };
66 junit.textui.TestRunner.main( testCaseName );
67 }
68
69 /***
70 * A unit test suite for JUnit
71 *
72 * @return The test suite
73 */
74 public static Test suite()
75 {
76 return new TestSuite( SimpleEventHandlingUnitTest.class );
77 }
78
79 /***
80 * Test setup with expected configuration parameters.
81 *
82 */
83 public void setUp()
84 {
85 JCS.setConfigFilename( "/TestSimpleEventHandling.ccf" );
86 }
87
88 /***
89 * Verify that the spooled event is called as expected.
90 *
91 *
92 * @exception Exception
93 * Description of the Exception
94 */
95 public void testSpoolEvent()
96 throws Exception
97 {
98 MyEventHandler meh = new MyEventHandler();
99
100 JCS jcs = JCS.getInstance( "WithDisk" );
101
102 IElementAttributes attributes = jcs.getDefaultElementAttributes();
103 attributes.addElementEventHandler( meh );
104 jcs.setDefaultElementAttributes( attributes );
105
106
107 for ( int i = 0; i <= items; i++ )
108 {
109 jcs.put( i + ":key", "data" + i );
110 }
111
112
113 Thread.sleep( items / 20 );
114
115
116 assertTrue( "The number of ELEMENT_EVENT_SPOOLED_DISK_AVAILABLE events [" + meh.getSpoolCount()
117 + "] does not equal the number expected [" + items + "]", meh.getSpoolCount() >= items );
118
119 }
120
121 /***
122 * Test overflow with no disk configured for the region.
123 *
124 * @throws Exception
125 */
126 public void testSpoolNoDiskEvent()
127 throws Exception
128 {
129 JCS jcs = JCS.getInstance( "NoDisk" );
130
131 MyEventHandler meh = new MyEventHandler();
132
133
134 IElementAttributes attributes = jcs.getDefaultElementAttributes();
135 attributes.addElementEventHandler( meh );
136 jcs.setDefaultElementAttributes( attributes );
137
138
139 for ( int i = 0; i <= items; i++ )
140 {
141 jcs.put( i + ":key", "data" + i );
142 }
143
144
145 Thread.sleep( items / 20 );
146
147
148 assertTrue( "The number of ELEMENT_EVENT_SPOOLED_DISK_NOT_AVAILABLE events [" + meh.getSpoolNoDiskCount()
149 + "] does not equal the number expected.", meh.getSpoolNoDiskCount() >= items );
150
151 }
152
153 /***
154 * Test the ELEMENT_EVENT_SPOOLED_NOT_ALLOWED event.
155 *
156 * @throws Exception
157 */
158 public void testSpoolNotAllowedEvent()
159 throws Exception
160 {
161 MyEventHandler meh = new MyEventHandler();
162
163 JCS jcs = JCS.getInstance( "DiskButNotAllowed" );
164
165 IElementAttributes attributes = jcs.getDefaultElementAttributes();
166 attributes.addElementEventHandler( meh );
167 jcs.setDefaultElementAttributes( attributes );
168
169
170 for ( int i = 0; i <= items; i++ )
171 {
172 jcs.put( i + ":key", "data" + i );
173 }
174
175
176 Thread.sleep( items / 20 );
177
178
179 assertTrue( "The number of ELEMENT_EVENT_SPOOLED_NOT_ALLOWED events [" + meh.getSpoolNotAllowedCount()
180 + "] does not equal the number expected.", meh.getSpoolNotAllowedCount() >= items );
181
182 }
183
184
185 /***
186 * Test the ELEMENT_EVENT_SPOOLED_NOT_ALLOWED event.
187 *
188 * @throws Exception
189 */
190 public void testSpoolNotAllowedEventOnItem()
191 throws Exception
192 {
193 MyEventHandler meh = new MyEventHandler();
194
195 JCS jcs = JCS.getInstance( "DiskButNotAllowed" );
196
197
198
199
200
201
202 for ( int i = 0; i <= items; i++ )
203 {
204 IElementAttributes attributes = jcs.getDefaultElementAttributes();
205 attributes.addElementEventHandler( meh );
206 jcs.put( i + ":key", "data" + i, attributes );
207 }
208
209
210 Thread.sleep( items / 20 );
211
212
213 assertTrue( "The number of ELEMENT_EVENT_SPOOLED_NOT_ALLOWED events [" + meh.getSpoolNotAllowedCount()
214 + "] does not equal the number expected.", meh.getSpoolNotAllowedCount() >= items );
215
216 }
217 /***
218 * Simple event counter used to verify test results.
219 *
220 * @author aaronsm
221 *
222 */
223 public class MyEventHandler
224 implements IElementEventHandler
225 {
226
227 private int spoolCount = 0;
228
229 private int spoolNotAllowedCount = 0;
230
231 private int spoolNoDiskCount = 0;
232
233
234
235
236
237
238 public synchronized void handleElementEvent( IElementEvent event )
239 {
240
241
242
243 if ( event.getElementEvent() == IElementEventConstants.ELEMENT_EVENT_SPOOLED_DISK_AVAILABLE )
244 {
245
246
247 setSpoolCount( getSpoolCount() + 1 );
248 }
249 else if ( event.getElementEvent() == IElementEventConstants.ELEMENT_EVENT_SPOOLED_NOT_ALLOWED )
250 {
251 setSpoolNotAllowedCount( getSpoolNotAllowedCount() + 1 );
252 }
253 else if ( event.getElementEvent() == IElementEventConstants.ELEMENT_EVENT_SPOOLED_DISK_NOT_AVAILABLE )
254 {
255 setSpoolNoDiskCount( getSpoolNoDiskCount() + 1 );
256 }
257 }
258
259 /***
260 * @param spoolCount
261 * The spoolCount to set.
262 */
263 protected void setSpoolCount( int spoolCount )
264 {
265 this.spoolCount = spoolCount;
266 }
267
268 /***
269 * @return Returns the spoolCount.
270 */
271 protected int getSpoolCount()
272 {
273 return spoolCount;
274 }
275
276 /***
277 * @param spoolNotAllowedCount
278 * The spoolNotAllowedCount to set.
279 */
280 protected void setSpoolNotAllowedCount( int spoolNotAllowedCount )
281 {
282 this.spoolNotAllowedCount = spoolNotAllowedCount;
283 }
284
285 /***
286 * @return Returns the spoolNotAllowedCount.
287 */
288 protected int getSpoolNotAllowedCount()
289 {
290 return spoolNotAllowedCount;
291 }
292
293 /***
294 * @param spoolNoDiskCount
295 * The spoolNoDiskCount to set.
296 */
297 protected void setSpoolNoDiskCount( int spoolNoDiskCount )
298 {
299 this.spoolNoDiskCount = spoolNoDiskCount;
300 }
301
302 /***
303 * @return Returns the spoolNoDiskCount.
304 */
305 protected int getSpoolNoDiskCount()
306 {
307 return spoolNoDiskCount;
308 }
309
310 }
311
312 }