1 16 17 package org.springframework.scheduling.quartz; 18 19 import java.util.Arrays ; 20 import java.util.Date ; 21 import java.util.HashMap ; 22 import java.util.List ; 23 import java.util.Map ; 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 52 public class QuartzSupportTests extends TestCase { 53 54 public void testSchedulerFactoryBean() throws Exception { 55 doTestSchedulerFactoryBean(false); 56 } 57 58 public void testSchedulerFactoryBeanWithExplicitJobDetail() throws Exception { 59 doTestSchedulerFactoryBean(true); 60 } 61 62 private void doTestSchedulerFactoryBean(boolean explicitJobDetail) throws Exception { 63 TestBean tb = new TestBean("tb", 99); 64 JobDetailBean jobDetail0 = new JobDetailBean(); 65 jobDetail0.setJobClass(Job.class); 66 jobDetail0.setBeanName("myJob0"); 67 Map jobData = new HashMap (); 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 ()); 110 scheduler.addJob(jobDetail1, true); 111 schedulerControl.setVoidCallable(); 112 scheduler.scheduleJob(trigger1); 113 schedulerControl.setReturnValue(new Date ()); 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 schedulerName) { 122 return scheduler; 123 } 124 }; 125 Map schedulerContext = new HashMap (); 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 { 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 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 191 192 197 198 private void methodInvokingConcurrency(boolean concurrent) throws Exception { 199 204 TestMethodInvokingTask task1 = new TestMethodInvokingTask(); 205 MethodInvokingJobDetailFactoryBean mijdfb = new MethodInvokingJobDetailFactoryBean(); 206 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 try { 237 Thread.sleep(4000); 238 } 239 catch (InterruptedException ex) { 240 } 242 243 if (concurrent) { 244 assertEquals(2, task1.counter); 245 task1.stop(); 246 return; 248 } 249 else { 250 assertEquals(1, task1.counter); 251 task1.stop(); 252 } 254 255 try { 256 Thread.sleep(4000); 257 } 258 catch (InterruptedException ex) { 259 } 261 262 task1.stop(); 263 assertEquals(2, task1.counter); 264 265 schedulerFactoryBean.destroy(); 269 } 270 271 public void testSchedulerFactoryBeanWithPlainQuartzObjects() throws Exception { 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 ()); 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 ()); 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 ()); 322 scheduler.scheduleJob(trigger1); 323 schedulerControl.setReturnValue(new Date ()); 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 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 { 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 schedulerName) { 364 return scheduler; 365 } 366 }; 367 Map schedulerContextMap = new HashMap (); 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 { 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 jobData = new HashMap (); 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 [] names = new String []{"test1", "test2"}; 406 jobDetail.setJobListenerNames(names); 407 List result = Arrays.asList(jobDetail.getJobListenerNames()); 408 assertEquals(Arrays.asList(names), result); 409 } 410 411 public void testCronTriggerBeanWithListenerNames() { 412 CronTriggerBean trigger = new CronTriggerBean(); 413 String [] names = new String []{"test1", "test2"}; 414 trigger.setTriggerListenerNames(names); 415 List result = Arrays.asList(trigger.getTriggerListenerNames()); 416 assertEquals(Arrays.asList(names), result); 417 } 418 419 public void testSimpleTriggerBeanWithListenerNames() { 420 SimpleTriggerBean trigger = new SimpleTriggerBean(); 421 String [] names = new String []{"test1", "test2"}; 422 trigger.setTriggerListenerNames(names); 423 List result = Arrays.asList(trigger.getTriggerListenerNames()); 424 assertEquals(Arrays.asList(names), result); 425 } 426 427 430 public void testMultipleSchedulers() throws Exception { 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 triggerName, String triggerGroup) { 453 } 454 455 public void triggerFinalized(Trigger trigger) { 456 } 457 458 public void triggersPaused(String triggerName, String triggerGroup) { 459 } 460 461 public void triggersResumed(String triggerName, String triggerGroup) { 462 } 463 464 public void jobsPaused(String jobName, String jobGroup) { 465 } 466 467 public void jobsResumed(String jobName, String jobGroup) { 468 } 469 470 public void schedulerError(String msg, SchedulerException cause) { 471 } 472 473 public void schedulerShutdown() { 474 } 475 } 476 477 478 private static class TestJobListener implements JobListener { 479 480 public String 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 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 |