KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > scheduling > quartz > QuartzSupportTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.scheduling.quartz;
18
19 import java.util.Arrays JavaDoc;
20 import java.util.Date JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import junit.framework.TestCase;
26 import org.easymock.MockControl;
27 import org.quartz.CronTrigger;
28 import org.quartz.Job;
29 import org.quartz.JobDetail;
30 import org.quartz.JobExecutionContext;
31 import org.quartz.JobExecutionException;
32 import org.quartz.JobListener;
33 import org.quartz.Scheduler;
34 import org.quartz.SchedulerContext;
35 import org.quartz.SchedulerException;
36 import org.quartz.SchedulerFactory;
37 import org.quartz.SchedulerListener;
38 import org.quartz.SimpleTrigger;
39 import org.quartz.Trigger;
40 import org.quartz.TriggerListener;
41
42 import org.springframework.beans.TestBean;
43 import org.springframework.context.support.ClassPathXmlApplicationContext;
44 import org.springframework.context.support.StaticApplicationContext;
45 import org.springframework.scheduling.TestMethodInvokingTask;
46
47 /**
48  * @author Juergen Hoeller
49  * @author Alef Arendsen
50  * @since 20.02.2004
51  */

52 public class QuartzSupportTests extends TestCase {
53
54     public void testSchedulerFactoryBean() throws Exception JavaDoc {
55         doTestSchedulerFactoryBean(false);
56     }
57
58     public void testSchedulerFactoryBeanWithExplicitJobDetail() throws Exception JavaDoc {
59         doTestSchedulerFactoryBean(true);
60     }
61
62     private void doTestSchedulerFactoryBean(boolean explicitJobDetail) throws Exception JavaDoc {
63         TestBean tb = new TestBean("tb", 99);
64         JobDetailBean jobDetail0 = new JobDetailBean();
65         jobDetail0.setJobClass(Job.class);
66         jobDetail0.setBeanName("myJob0");
67         Map JavaDoc jobData = new HashMap JavaDoc();
68         jobData.put("testBean", tb);
69         jobDetail0.setJobDataAsMap(jobData);
70         jobDetail0.afterPropertiesSet();
71         assertEquals(tb, jobDetail0.getJobDataMap().get("testBean"));
72
73         CronTriggerBean trigger0 = new CronTriggerBean();
74         trigger0.setBeanName("myTrigger0");
75         trigger0.setJobDetail(jobDetail0);
76         trigger0.setCronExpression("0/1 * * * * ?");
77         trigger0.afterPropertiesSet();
78
79         TestMethodInvokingTask task1 = new TestMethodInvokingTask();
80         MethodInvokingJobDetailFactoryBean mijdfb = new MethodInvokingJobDetailFactoryBean();
81         mijdfb.setBeanName("myJob1");
82         mijdfb.setTargetObject(task1);
83         mijdfb.setTargetMethod("doSomething");
84         mijdfb.afterPropertiesSet();
85         JobDetail jobDetail1 = (JobDetail) mijdfb.getObject();
86
87         SimpleTriggerBean trigger1 = new SimpleTriggerBean();
88         trigger1.setBeanName("myTrigger1");
89         trigger1.setJobDetail(jobDetail1);
90         trigger1.setStartDelay(0);
91         trigger1.setRepeatInterval(20);
92         trigger1.afterPropertiesSet();
93
94         MockControl schedulerControl = MockControl.createControl(Scheduler.class);
95         final Scheduler scheduler = (Scheduler) schedulerControl.getMock();
96         scheduler.getContext();
97         schedulerControl.setReturnValue(new SchedulerContext());
98         scheduler.getJobDetail("myJob0", Scheduler.DEFAULT_GROUP);
99         schedulerControl.setReturnValue(null);
100         scheduler.getJobDetail("myJob1", Scheduler.DEFAULT_GROUP);
101         schedulerControl.setReturnValue(null);
102         scheduler.getTrigger("myTrigger0", Scheduler.DEFAULT_GROUP);
103         schedulerControl.setReturnValue(null);
104         scheduler.getTrigger("myTrigger1", Scheduler.DEFAULT_GROUP);
105         schedulerControl.setReturnValue(null);
106         scheduler.addJob(jobDetail0, true);
107         schedulerControl.setVoidCallable();
108         scheduler.scheduleJob(trigger0);
109         schedulerControl.setReturnValue(new Date JavaDoc());
110         scheduler.addJob(jobDetail1, true);
111         schedulerControl.setVoidCallable();
112         scheduler.scheduleJob(trigger1);
113         schedulerControl.setReturnValue(new Date JavaDoc());
114         scheduler.start();
115         schedulerControl.setVoidCallable();
116         scheduler.shutdown(false);
117         schedulerControl.setVoidCallable();
118         schedulerControl.replay();
119
120         SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean() {
121             protected Scheduler createScheduler(SchedulerFactory schedulerFactory, String JavaDoc schedulerName) {
122                 return scheduler;
123             }
124         };
125         Map JavaDoc schedulerContext = new HashMap JavaDoc();
126         schedulerContext.put("otherTestBean", tb);
127         schedulerFactoryBean.setSchedulerContextAsMap(schedulerContext);
128         if (explicitJobDetail) {
129             schedulerFactoryBean.setJobDetails(new JobDetail[] {jobDetail0});
130         }
131         schedulerFactoryBean.setTriggers(new Trigger[] {trigger0, trigger1});
132         try {
133             schedulerFactoryBean.afterPropertiesSet();
134         }
135         finally {
136             schedulerFactoryBean.destroy();
137         }
138
139         schedulerControl.verify();
140     }
141
142     public void testSchedulerFactoryBeanWithListeners() throws Exception JavaDoc {
143         MockControl schedulerControl = MockControl.createControl(Scheduler.class);
144         final Scheduler scheduler = (Scheduler) schedulerControl.getMock();
145
146         SchedulerListener schedulerListener = new TestSchedulerListener();
147         JobListener globalJobListener = new TestJobListener();
148         JobListener jobListener = new TestJobListener();
149         TriggerListener globalTriggerListener = new TestTriggerListener();
150         TriggerListener triggerListener = new TestTriggerListener();
151
152         scheduler.addSchedulerListener(schedulerListener);
153         schedulerControl.setVoidCallable();
154         scheduler.addGlobalJobListener(globalJobListener);
155         schedulerControl.setVoidCallable();
156         scheduler.addJobListener(jobListener);
157         schedulerControl.setVoidCallable();
158         scheduler.addGlobalTriggerListener(globalTriggerListener);
159         schedulerControl.setVoidCallable();
160         scheduler.addTriggerListener(triggerListener);
161         schedulerControl.setVoidCallable();
162         scheduler.start();
163         schedulerControl.setVoidCallable();
164         scheduler.shutdown(false);
165         schedulerControl.setVoidCallable();
166         schedulerControl.replay();
167
168         SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean() {
169             protected Scheduler createScheduler(SchedulerFactory schedulerFactory, String JavaDoc schedulerName) {
170                 return scheduler;
171             }
172         };
173         schedulerFactoryBean.setSchedulerListeners(new SchedulerListener[]{schedulerListener});
174         schedulerFactoryBean.setGlobalJobListeners(new JobListener[]{globalJobListener});
175         schedulerFactoryBean.setJobListeners(new JobListener[]{jobListener});
176         schedulerFactoryBean.setGlobalTriggerListeners(new TriggerListener[]{globalTriggerListener});
177         schedulerFactoryBean.setTriggerListeners(new TriggerListener[]{triggerListener});
178         try {
179             schedulerFactoryBean.afterPropertiesSet();
180         }
181         finally {
182             schedulerFactoryBean.destroy();
183         }
184
185         schedulerControl.verify();
186     }
187
188     /*public void testMethodInvocationWithConcurrency() throws Exception {
189         methodInvokingConcurrency(true);
190     }*/

191     
192     // We can't test both since Quartz somehow seems to keep things in memory
193
// enable both and one of them will fail (order doesn't matter).
194
/*public void testMethodInvocationWithoutConcurrency() throws Exception {
195         methodInvokingConcurrency(false);
196     }*/

197
198     private void methodInvokingConcurrency(boolean concurrent) throws Exception JavaDoc {
199         // Test the concurrency flag.
200
// Method invoking job with two triggers.
201
// If the concurrent flag is false, the triggers are NOT allowed
202
// to interfere with each other.
203

204         TestMethodInvokingTask task1 = new TestMethodInvokingTask();
205         MethodInvokingJobDetailFactoryBean mijdfb = new MethodInvokingJobDetailFactoryBean();
206         // set the concurrency flag!
207
mijdfb.setConcurrent(concurrent);
208         mijdfb.setBeanName("myJob1");
209         mijdfb.setTargetObject(task1);
210         mijdfb.setTargetMethod("doWait");
211         mijdfb.afterPropertiesSet();
212         JobDetail jobDetail1 = (JobDetail) mijdfb.getObject();
213
214         SimpleTriggerBean trigger0 = new SimpleTriggerBean();
215         trigger0.setBeanName("myTrigger1");
216         trigger0.setJobDetail(jobDetail1);
217         trigger0.setStartDelay(0);
218         trigger0.setRepeatInterval(1);
219         trigger0.setRepeatCount(1);
220         trigger0.afterPropertiesSet();
221
222         SimpleTriggerBean trigger1 = new SimpleTriggerBean();
223         trigger1.setBeanName("myTrigger1");
224         trigger1.setJobDetail(jobDetail1);
225         trigger1.setStartDelay(1000L);
226         trigger1.setRepeatInterval(1);
227         trigger1.setRepeatCount(1);
228         trigger1.afterPropertiesSet();
229
230         SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
231         schedulerFactoryBean.setJobDetails(new JobDetail[] {jobDetail1});
232         schedulerFactoryBean.setTriggers(new Trigger[] {trigger1, trigger0});
233         schedulerFactoryBean.afterPropertiesSet();
234         
235         // ok scheduler is set up... let's wait for like 4 seconds
236
try {
237             Thread.sleep(4000);
238         }
239         catch (InterruptedException JavaDoc ex) {
240             // fall through
241
}
242
243         if (concurrent) {
244             assertEquals(2, task1.counter);
245             task1.stop();
246             // we're done, both jobs have ran, let's call it a day
247
return;
248         }
249         else {
250             assertEquals(1, task1.counter);
251             task1.stop();
252             // we need to check whether or not the test succeed with non-concurrent jobs
253
}
254
255         try {
256             Thread.sleep(4000);
257         }
258         catch (InterruptedException JavaDoc ex) {
259             // fall through
260
}
261
262         task1.stop();
263         assertEquals(2, task1.counter);
264         
265         // Although we're destroying the scheduler, it does seem to keep things in memory:
266
// When executing both tests (concurrent and non-concurrent), the second test always
267
// fails.
268
schedulerFactoryBean.destroy();
269     }
270
271     public void testSchedulerFactoryBeanWithPlainQuartzObjects() throws Exception JavaDoc {
272         TestBean tb = new TestBean("tb", 99);
273         JobDetail jobDetail0 = new JobDetail();
274         jobDetail0.setJobClass(Job.class);
275         jobDetail0.setName("myJob0");
276         jobDetail0.setGroup(Scheduler.DEFAULT_GROUP);
277         jobDetail0.getJobDataMap().put("testBean", tb);
278         assertEquals(tb, jobDetail0.getJobDataMap().get("testBean"));
279
280         CronTrigger trigger0 = new CronTrigger();
281         trigger0.setName("myTrigger0");
282         trigger0.setGroup(Scheduler.DEFAULT_GROUP);
283         trigger0.setJobName("myJob0");
284         trigger0.setJobGroup(Scheduler.DEFAULT_GROUP);
285         trigger0.setStartTime(new Date JavaDoc());
286         trigger0.setCronExpression("0/1 * * * * ?");
287
288         TestMethodInvokingTask task1 = new TestMethodInvokingTask();
289         MethodInvokingJobDetailFactoryBean mijdfb = new MethodInvokingJobDetailFactoryBean();
290         mijdfb.setName("myJob1");
291         mijdfb.setGroup(Scheduler.DEFAULT_GROUP);
292         mijdfb.setTargetObject(task1);
293         mijdfb.setTargetMethod("doSomething");
294         mijdfb.afterPropertiesSet();
295         JobDetail jobDetail1 = (JobDetail) mijdfb.getObject();
296
297         SimpleTrigger trigger1 = new SimpleTrigger();
298         trigger1.setName("myTrigger1");
299         trigger1.setGroup(Scheduler.DEFAULT_GROUP);
300         trigger1.setJobName("myJob1");
301         trigger1.setJobGroup(Scheduler.DEFAULT_GROUP);
302         trigger1.setStartTime(new Date JavaDoc());
303         trigger1.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
304         trigger1.setRepeatInterval(20);
305
306         MockControl schedulerControl = MockControl.createControl(Scheduler.class);
307         final Scheduler scheduler = (Scheduler) schedulerControl.getMock();
308         scheduler.getJobDetail("myJob0", Scheduler.DEFAULT_GROUP);
309         schedulerControl.setReturnValue(null);
310         scheduler.getJobDetail("myJob1", Scheduler.DEFAULT_GROUP);
311         schedulerControl.setReturnValue(null);
312         scheduler.getTrigger("myTrigger0", Scheduler.DEFAULT_GROUP);
313         schedulerControl.setReturnValue(null);
314         scheduler.getTrigger("myTrigger1", Scheduler.DEFAULT_GROUP);
315         schedulerControl.setReturnValue(null);
316         scheduler.addJob(jobDetail0, true);
317         schedulerControl.setVoidCallable();
318         scheduler.addJob(jobDetail1, true);
319         schedulerControl.setVoidCallable();
320         scheduler.scheduleJob(trigger0);
321         schedulerControl.setReturnValue(new Date JavaDoc());
322         scheduler.scheduleJob(trigger1);
323         schedulerControl.setReturnValue(new Date JavaDoc());
324         scheduler.start();
325         schedulerControl.setVoidCallable();
326         scheduler.shutdown(false);
327         schedulerControl.setVoidCallable();
328         schedulerControl.replay();
329
330         SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean() {
331             protected Scheduler createScheduler(SchedulerFactory schedulerFactory, String JavaDoc schedulerName) {
332                 return scheduler;
333             }
334         };
335         schedulerFactoryBean.setJobDetails(new JobDetail[] {jobDetail0, jobDetail1});
336         schedulerFactoryBean.setTriggers(new Trigger[] {trigger0, trigger1});
337         try {
338             schedulerFactoryBean.afterPropertiesSet();
339         }
340         finally {
341             schedulerFactoryBean.destroy();
342         }
343
344         schedulerControl.verify();
345     }
346
347     public void testSchedulerFactoryBeanWithApplicationContext() throws Exception JavaDoc {
348         TestBean tb = new TestBean("tb", 99);
349         StaticApplicationContext ac = new StaticApplicationContext();
350
351         MockControl schedulerControl = MockControl.createControl(Scheduler.class);
352         final Scheduler scheduler = (Scheduler) schedulerControl.getMock();
353         SchedulerContext schedulerContext = new SchedulerContext();
354         scheduler.getContext();
355         schedulerControl.setReturnValue(schedulerContext, 4);
356         scheduler.start();
357         schedulerControl.setVoidCallable();
358         scheduler.shutdown(false);
359         schedulerControl.setVoidCallable();
360         schedulerControl.replay();
361
362         SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean() {
363             protected Scheduler createScheduler(SchedulerFactory schedulerFactory, String JavaDoc schedulerName) {
364                 return scheduler;
365             }
366         };
367         Map JavaDoc schedulerContextMap = new HashMap JavaDoc();
368         schedulerContextMap.put("testBean", tb);
369         schedulerFactoryBean.setSchedulerContextAsMap(schedulerContextMap);
370         schedulerFactoryBean.setApplicationContext(ac);
371         schedulerFactoryBean.setApplicationContextSchedulerContextKey("appCtx");
372         try {
373             schedulerFactoryBean.afterPropertiesSet();
374             Scheduler returnedScheduler = (Scheduler) schedulerFactoryBean.getObject();
375             assertEquals(tb, returnedScheduler.getContext().get("testBean"));
376             assertEquals(ac, returnedScheduler.getContext().get("appCtx"));
377         }
378         finally {
379             schedulerFactoryBean.destroy();
380         }
381
382         schedulerControl.verify();
383     }
384
385     public void testJobDetailBeanWithApplicationContext() throws Exception JavaDoc {
386         TestBean tb = new TestBean("tb", 99);
387         StaticApplicationContext ac = new StaticApplicationContext();
388
389         JobDetailBean jobDetail = new JobDetailBean();
390         jobDetail.setJobClass(Job.class);
391         jobDetail.setBeanName("myJob0");
392         Map JavaDoc jobData = new HashMap JavaDoc();
393         jobData.put("testBean", tb);
394         jobDetail.setJobDataAsMap(jobData);
395         jobDetail.setApplicationContext(ac);
396         jobDetail.setApplicationContextJobDataKey("appCtx");
397         jobDetail.afterPropertiesSet();
398
399         assertEquals(tb, jobDetail.getJobDataMap().get("testBean"));
400         assertEquals(ac, jobDetail.getJobDataMap().get("appCtx"));
401     }
402
403     public void testJobDetailBeanWithListenerNames() {
404         JobDetailBean jobDetail = new JobDetailBean();
405         String JavaDoc[] names = new String JavaDoc[]{"test1", "test2"};
406         jobDetail.setJobListenerNames(names);
407         List JavaDoc result = Arrays.asList(jobDetail.getJobListenerNames());
408         assertEquals(Arrays.asList(names), result);
409     }
410
411     public void testCronTriggerBeanWithListenerNames() {
412         CronTriggerBean trigger = new CronTriggerBean();
413         String JavaDoc[] names = new String JavaDoc[]{"test1", "test2"};
414         trigger.setTriggerListenerNames(names);
415         List JavaDoc result = Arrays.asList(trigger.getTriggerListenerNames());
416         assertEquals(Arrays.asList(names), result);
417     }
418
419     public void testSimpleTriggerBeanWithListenerNames() {
420         SimpleTriggerBean trigger = new SimpleTriggerBean();
421         String JavaDoc[] names = new String JavaDoc[]{"test1", "test2"};
422         trigger.setTriggerListenerNames(names);
423         List JavaDoc result = Arrays.asList(trigger.getTriggerListenerNames());
424         assertEquals(Arrays.asList(names), result);
425     }
426
427     /**
428      * Tests the creation of multiple schedulers (SPR-772)
429      */

430     public void testMultipleSchedulers() throws Exception JavaDoc {
431         ClassPathXmlApplicationContext ctx =
432                 new ClassPathXmlApplicationContext("/org/springframework/scheduling/quartz/multipleSchedulers.xml");
433
434         try {
435             Scheduler scheduler1 = (Scheduler) ctx.getBean("scheduler1");
436             Scheduler scheduler2 = (Scheduler) ctx.getBean("scheduler2");
437
438             assertNotSame(scheduler1, scheduler2);
439             assertFalse(scheduler1.getSchedulerName().equals(scheduler2.getSchedulerName()));
440         }
441         finally {
442             ctx.close();
443         }
444     }
445
446
447     private static class TestSchedulerListener implements SchedulerListener {
448
449         public void jobScheduled(Trigger trigger) {
450         }
451
452         public void jobUnscheduled(String JavaDoc triggerName, String JavaDoc triggerGroup) {
453         }
454
455         public void triggerFinalized(Trigger trigger) {
456         }
457
458         public void triggersPaused(String JavaDoc triggerName, String JavaDoc triggerGroup) {
459         }
460
461         public void triggersResumed(String JavaDoc triggerName, String JavaDoc triggerGroup) {
462         }
463
464         public void jobsPaused(String JavaDoc jobName, String JavaDoc jobGroup) {
465         }
466
467         public void jobsResumed(String JavaDoc jobName, String JavaDoc jobGroup) {
468         }
469
470         public void schedulerError(String JavaDoc msg, SchedulerException cause) {
471         }
472
473         public void schedulerShutdown() {
474         }
475     }
476
477
478     private static class TestJobListener implements JobListener {
479
480         public String JavaDoc getName() {
481             return null;
482         }
483
484         public void jobToBeExecuted(JobExecutionContext context) {
485         }
486
487         public void jobExecutionVetoed(JobExecutionContext context) {
488         }
489
490         public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {
491         }
492     }
493
494
495     private static class TestTriggerListener implements TriggerListener {
496
497         public String JavaDoc getName() {
498             return null;
499         }
500
501         public void triggerFired(Trigger trigger, JobExecutionContext context) {
502         }
503
504         public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) {
505             return false;
506         }
507
508         public void triggerMisfired(Trigger trigger) {
509         }
510
511         public void triggerComplete(Trigger trigger, JobExecutionContext context, int triggerInstructionCode) {
512         }
513     }
514
515 }
516
Popular Tags