KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > scheduler > test > SchedulerServiceTest


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.services.scheduler.test;
19
20 import org.sape.carbon.core.component.Component;
21 import org.sape.carbon.core.component.FunctionalInterface;
22 import org.sape.carbon.core.component.Lookup;
23 import org.sape.carbon.core.component.lifecycle.StateTransitionException;
24 import org.sape.carbon.core.config.Config;
25 import org.sape.carbon.core.config.interceptor.ConfigurationInterceptor;
26 import org.sape.carbon.core.util.calendar.DayOfWeekEnum;
27 import org.sape.carbon.core.util.calendar.MonthEnum;
28 import org.sape.carbon.services.scheduler.FixedDelayTaskConfiguration;
29 import org.sape.carbon.services.scheduler.FixedRateTaskConfiguration;
30 import org.sape.carbon.services.scheduler.SchedulerServiceConfiguration;
31 import org.sape.carbon.services.threadpool.ThreadPool;
32
33 import junit.extensions.ActiveTestSuite;
34 import junit.framework.Test;
35 import junit.framework.TestCase;
36 import junit.framework.TestSuite;
37
38 /**
39  * Template for junit test harness. Change this description to reflect what this class is testing.
40  *
41  * <br>Copyright 2002 Sapient
42  *
43  * @since carbon 1.0
44  * @author Douglas Voet, March 2002
45  * @version $Revision: 1.11 $($Author: dvoet $ / $Date: 2003/11/20 21:46:15 $)
46  */

47 public class SchedulerServiceTest
48     extends TestCase
49     implements SchedulerServiceTestListener {
50
51     private int count = 0;
52     private int secondaryCount = 0;
53     private int lastCount = 0;
54
55     /**
56      * @see SchedulerServiceTestListener#incrementCount()
57      */

58     public synchronized void incrementCount() {
59         count++;
60         notify();
61     }
62
63     public synchronized void incrementSecondaryCount() {
64         secondaryCount++;
65         notify();
66     }
67
68     public SchedulerServiceTest(String JavaDoc name) {
69         super(name);
70     }
71
72     public void testFixedRateTask() throws InterruptedException JavaDoc {
73         SchedulableComponent component =
74             (SchedulableComponent) Lookup.getInstance().fetchComponent(
75                 SCHEDULABLE_SERVICE_NAME);
76
77         component.setListener(this);
78
79         setFixedRateTask(null, null, null, null, null, component, null);
80
81         synchronized (this) {
82             this.lastCount = this.count;
83             // wait for 2 minutes max
84
wait(120000);
85             if (this.count == this.lastCount) {
86                 TestCase.fail(
87                     "Waited 2 minutes and scheduled task was not executed: "
88                         + "last call count ["
89                         + this.lastCount
90                         + "] current call count [ "
91                         + this.count
92                         + "] expected current count != last count");
93             }
94         }
95
96         Lookup.getInstance().getComponentKeeper().destroyComponent(
97             TEST_SCHEDULER_SERVICE_NAME);
98     }
99
100
101     public void testReflectionBasedFixedRateTask() throws InterruptedException JavaDoc {
102         SchedulableComponent component =
103             (SchedulableComponent) Lookup.getInstance().fetchComponent(
104                 SCHEDULABLE_SERVICE_NAME);
105
106         component.setListener(this);
107
108         setFixedRateTask(null, null, null, null, null, component, "doSpecializeTask");
109
110         synchronized (this) {
111             this.lastCount = this.secondaryCount;
112             // wait for 2 minutes max
113
wait(120000);
114             assertTrue(
115                 "Waited 2 minutes and scheduled task was not executed: "
116                     + "last call count ["
117                     + this.lastCount
118                     + "] current call count [ "
119                     + this.secondaryCount
120                     + "] expected current count != last count",
121                 this.secondaryCount != this.lastCount);
122
123         }
124
125         Lookup.getInstance().getComponentKeeper().destroyComponent(
126             TEST_SCHEDULER_SERVICE_NAME);
127     }
128     
129     public void testSchedulerWithThreadPool() throws InterruptedException JavaDoc {
130         Component scheduler =
131             Lookup.getInstance().fetchComponent(TEST_SCHEDULER_SERVICE_NAME);
132             
133         ThreadPool threadPool =
134             (ThreadPool) Lookup.getInstance().fetchComponent(THREAD_POOL);
135             
136         assertTrue(
137             "Thread pool was not empty at start of test",
138             threadPool.getPoolSize().equals(new Integer JavaDoc(0)));
139
140         ((SchedulerServiceConfiguration) scheduler).setThreadPool(threadPool);
141         ((ConfigurationInterceptor) scheduler).applyConfiguration();
142
143         SchedulableComponent component =
144             (SchedulableComponent) Lookup.getInstance().fetchComponent(
145                 SCHEDULABLE_SERVICE_NAME);
146
147         component.setListener(this);
148
149         setFixedRateTask(null, null, null, null, null, component, null);
150
151         synchronized (this) {
152             this.lastCount = this.count;
153             // wait for 2 minutes max
154
wait(120000);
155             if (this.count == this.lastCount) {
156                 TestCase.fail(
157                     "Waited 2 minutes and scheduled task was not executed: "
158                         + "last call count ["
159                         + this.lastCount
160                         + "] current call count [ "
161                         + this.count
162                         + "] expected current count != last count");
163             }
164             
165             assertTrue(
166                 "task was not executed on thread pool",
167                 threadPool.getPoolSize().equals(new Integer JavaDoc(1)));
168         }
169
170         Lookup.getInstance().getComponentKeeper().destroyComponent(
171             TEST_SCHEDULER_SERVICE_NAME);
172     }
173
174
175     public void testFixedDelayTask() throws InterruptedException JavaDoc {
176         SchedulableComponent component =
177             (SchedulableComponent) Lookup.getInstance().fetchComponent(
178                 SCHEDULABLE_SERVICE_NAME);
179
180         component.setListener(this);
181
182         setFixedDelayTask(0, 500, component,null);
183
184         synchronized (this) {
185             this.lastCount = this.count;
186             // wait for 1 sec max
187
wait(1000);
188             if (this.count == this.lastCount) {
189                 TestCase.fail(
190                     "Waited 1 second and scheduled task was not executed: "
191                         + "last call count ["
192                         + this.lastCount
193                         + "] current call count [ "
194                         + this.count
195                         + "] expected current count != last count");
196             }
197         }
198
199         Lookup.getInstance().getComponentKeeper().destroyComponent(
200             TEST_SCHEDULER_SERVICE_NAME);
201     }
202
203     public void testInvalidConfigurations() {
204         SchedulableComponent component =
205             (SchedulableComponent) Lookup.getInstance().fetchComponent(
206                 SCHEDULABLE_SERVICE_NAME);
207
208         testInvalidFixedRateTask(new Integer JavaDoc(0), null, null, null, null, null,null);
209
210         testInvalidFixedRateTask(
211             null,
212             new Integer JavaDoc(0),
213             null,
214             null,
215             null,
216             component,
217             null);
218
219         testInvalidFixedRateTask(
220             new Integer JavaDoc(0),
221             null,
222             new Integer JavaDoc(0),
223             null,
224             null,
225             component,
226             null);
227
228         testInvalidFixedRateTask(
229             new Integer JavaDoc(0),
230             new Integer JavaDoc(0),
231             null,
232             MonthEnum.JANUARY,
233             null,
234             component,
235             null);
236
237         testInvalidFixedRateTask(
238             new Integer JavaDoc(0),
239             new Integer JavaDoc(0),
240             new Integer JavaDoc(1),
241             null,
242             DayOfWeekEnum.MONDAY,
243             component,
244             null);
245
246         testInvalidFixedRateTask(
247             new Integer JavaDoc(0),
248             new Integer JavaDoc(0),
249             null,
250             MonthEnum.JANUARY,
251             DayOfWeekEnum.MONDAY,
252             component,
253             null);
254
255         testInvalidFixedRateTask(
256             null, null, null, null, null,
257             component,
258             "nonExistantMethod");
259
260         testInvalidFixedDelayTask(-1, 100, component);
261         testInvalidFixedDelayTask(0, -1, component);
262     }
263
264     public void testRecoverableExceptions() throws InterruptedException JavaDoc {
265         SchedulableComponent component =
266             (SchedulableComponent) Lookup.getInstance().fetchComponent(
267                 SchedulerServiceTest.RECOVERABLE_SCHEDULABLE_SERVICE_NAME);
268
269         component.setListener(this);
270
271         setFixedDelayTask(0, 500, component, null);
272
273         synchronized (this) {
274             this.lastCount = this.count;
275             // wait twice for 1 sec to make sure that the task runs more than
276
// once
277
wait(1000);
278             wait(1000);
279             if (this.count <= this.lastCount + 1) {
280                 TestCase.fail(
281                     "Recoverable exception caused timer to stop: "
282                         + "last call count ["
283                         + this.lastCount
284                         + "] current call count [ "
285                         + this.count
286                         + "] expected <= last count + 1");
287             }
288         }
289
290         Lookup.getInstance().getComponentKeeper().destroyComponent(
291             TEST_SCHEDULER_SERVICE_NAME);
292     }
293
294     public void testUnrecoverableExceptions() throws InterruptedException JavaDoc {
295         SchedulableComponent component =
296             (SchedulableComponent) Lookup.getInstance().fetchComponent(
297                 SchedulerServiceTest.UNRECOVERABLE_SCHEDULABLE_SERVICE_NAME);
298
299         component.setListener(this);
300
301         setFixedDelayTask(0, 500, component, null);
302
303         synchronized (this) {
304             this.lastCount = this.count;
305             // wait twice for 1 sec to make sure that the task could run more
306
// than once (if it does, it is a failure)
307
wait(1000);
308             wait(1000);
309             if (this.count != this.lastCount + 1) {
310                 TestCase.fail(
311                     "Unrecoverable exception did not caused task to be canceled: "
312                         + "last call count ["
313                         + this.lastCount
314                         + "] current call count [ "
315                         + this.count
316                         + "] expected current count == last count + 1");
317             }
318         }
319
320         Lookup.getInstance().getComponentKeeper().destroyComponent(
321             TEST_SCHEDULER_SERVICE_NAME);
322     }
323
324     private void testInvalidFixedRateTask(
325         Integer JavaDoc minute,
326         Integer JavaDoc hour,
327         Integer JavaDoc dayOfMonth,
328         MonthEnum month,
329         DayOfWeekEnum dayOfWeek,
330         FunctionalInterface component,
331         String JavaDoc method) {
332
333         try {
334             setFixedRateTask(
335                 minute,
336                 hour,
337                 dayOfMonth,
338                 month,
339                 dayOfWeek,
340                 component,
341                 method);
342             fail(
343                 "Did not catch expected StateTransitionException for "
344                     + "configuration values: Minute ["
345                     + minute
346                     + "], Hour ["
347                     + hour
348                     + "], DayOfMonth ["
349                     + dayOfMonth
350                     + "], Month ["
351                     + month
352                     + "], DayOfWeek ["
353                     + dayOfWeek
354                     + "], SchedulableComponent ["
355                     + component
356                     + "]");
357
358         } catch (StateTransitionException ste) {
359             // expected
360
}
361
362     }
363
364     private void testInvalidFixedDelayTask(
365         long delay,
366         long period,
367         FunctionalInterface component) {
368
369         try {
370             setFixedDelayTask(delay, period, component, null);
371             fail(
372                 "Did catch expected StateTransitionException for "
373                     + "configuration values: InitialDelay ["
374                     + delay
375                     + "], Period ["
376                     + period
377                     + "], SchedulableComponent ["
378                     + component
379                     + "]");
380
381         } catch (StateTransitionException ste) {
382             // expected
383
}
384
385     }
386
387     private void setFixedRateTask(
388         Integer JavaDoc minute,
389         Integer JavaDoc hour,
390         Integer JavaDoc dayOfMonth,
391         MonthEnum month,
392         DayOfWeekEnum dayOfWeek,
393         FunctionalInterface component,
394         String JavaDoc methodName) {
395
396         Component scheduler =
397             Lookup.getInstance().fetchComponent(TEST_SCHEDULER_SERVICE_NAME);
398
399         FixedRateTaskConfiguration newConfig =
400             (FixedRateTaskConfiguration) Config
401                 .getInstance()
402                 .createConfiguration(
403                 FixedRateTaskConfiguration.class);
404         newConfig.setSchedulableComponent((FunctionalInterface) component);
405
406         newConfig.setMinute(minute);
407         newConfig.setHour(hour);
408         newConfig.setDayOfMonth(dayOfMonth);
409         newConfig.setMonth(month);
410         newConfig.setDayOfWeek(dayOfWeek);
411         if (methodName != null)
412             newConfig.setScheduledMethod(methodName);
413
414         ((SchedulerServiceConfiguration) scheduler).setFixedRateTask(
415             new FixedRateTaskConfiguration[] { newConfig });
416         ((SchedulerServiceConfiguration) scheduler).setFixedDelayTask(null);
417         ((ConfigurationInterceptor) scheduler).applyConfiguration();
418     }
419
420     private void setFixedDelayTask(
421         long delay,
422         long period,
423         FunctionalInterface component,
424         String JavaDoc methodName) {
425
426         Component scheduler =
427             Lookup.getInstance().fetchComponent(TEST_SCHEDULER_SERVICE_NAME);
428
429         FixedDelayTaskConfiguration newConfig =
430             (FixedDelayTaskConfiguration) Config
431                 .getInstance()
432                 .createConfiguration(
433                 FixedDelayTaskConfiguration.class);
434         newConfig.setSchedulableComponent((FunctionalInterface) component);
435
436         if (methodName != null)
437             newConfig.setScheduledMethod(methodName);
438
439         newConfig.setInitialDelay(delay);
440         newConfig.setPeriod(period);
441
442         ((SchedulerServiceConfiguration) scheduler).setFixedRateTask(null);
443         ((SchedulerServiceConfiguration) scheduler).setFixedDelayTask(
444             new FixedDelayTaskConfiguration[] { newConfig });
445         ((ConfigurationInterceptor) scheduler).applyConfiguration();
446     }
447
448     /*
449      * write your test methods here following these examples:
450      *
451      * public void testFunction1() {
452      * test something
453      * }
454      *
455      * public void testFunction2() {
456      * test something else
457      * }
458      */

459
460     private static final String JavaDoc SCHEDULABLE_SERVICE_NAME =
461         "/scheduler/test/TestSchedulableComponent";
462     private static final String JavaDoc UNRECOVERABLE_SCHEDULABLE_SERVICE_NAME =
463         "/scheduler/test/TestUnrecoverableSchedulableComponent";
464     private static final String JavaDoc RECOVERABLE_SCHEDULABLE_SERVICE_NAME =
465         "/scheduler/test/TestRecoverableSchedulableComponent";
466     private static final String JavaDoc TEST_SCHEDULER_SERVICE_NAME =
467         "/scheduler/test/TestScheduler";
468     private static final String JavaDoc THREAD_POOL =
469         "/scheduler/test/SchedulerThreadPool";
470
471     /**
472      * Method called by jUnit to get all the tests in this test case.
473      * @return Test the suite of tests in this test case
474      */

475     public static Test suite() {
476         TestSuite masterSuite = new TestSuite();
477         // add single threaded tests
478
Test singleThreadedTests = getSingleThreadedTests();
479         if (singleThreadedTests != null) {
480             masterSuite.addTest(singleThreadedTests);
481         }
482         // add multi threaded tests
483
Test multiThreadedTests = getMultiThreadedTests();
484         if (multiThreadedTests != null) {
485             masterSuite.addTest(multiThreadedTests);
486         }
487         return masterSuite;
488     }
489
490     /**
491      * This method is used within the suite method to get all of the single threaded tests.
492      * Add all your single threaded tests in this method with a line like:
493      * suite.addTest(new SchedulerServiceTest("testFunction1"));
494      * @return Test the suite of single threaded tests in this test case
495      */

496     private static Test getSingleThreadedTests() {
497         TestSuite suite = new TestSuite();
498
499         suite.addTest(new SchedulerServiceTest("testFixedRateTask"));
500         suite.addTest(new SchedulerServiceTest("testReflectionBasedFixedRateTask"));
501         suite.addTest(new SchedulerServiceTest("testSchedulerWithThreadPool"));
502         suite.addTest(new SchedulerServiceTest("testFixedDelayTask"));
503         suite.addTest(new SchedulerServiceTest("testRecoverableExceptions"));
504         suite.addTest(new SchedulerServiceTest("testUnrecoverableExceptions"));
505         suite.addTest(new SchedulerServiceTest("testInvalidConfigurations"));
506
507         return suite;
508     }
509
510     /**
511      * This method is used within the suite method to get all of the multi threaded tests.
512      * Add all your multi threaded tests in this method with a line like: addTest(suite, "testFunction1", 5);
513      * @return Test the suite of multi-threaded tests in this test case
514      */

515     private static Test getMultiThreadedTests() {
516         TestSuite suite = new ActiveTestSuite();
517
518         /*
519          * add your tests here following these examples:
520          *
521          * addTest(suite, "testFunction1", 5);
522          * addTest(suite, "testFunction2", 10);
523          */

524
525         return suite;
526     }
527
528     /**
529      * This method will add the give test to the give suite the specified
530      * number of times. This is best used for multi-threaded tests where
531      * suite is an instance of ActiveTestSuite and you want to run the same test in multiple threads.
532      * @param suite the suite to add the test to.
533      * @param testName the name of the test to add.
534      * @param number the number of times to add the test to the suite
535      */

536     private static void addTest(TestSuite suite, String JavaDoc testName, int number) {
537         for (int count = 0; count < number; count++) {
538             suite.addTest(new SchedulerServiceTest(testName));
539         }
540     }
541 }
Popular Tags