1 16 package org.quartz.simpl; 17 18 import java.util.Date ; 19 20 import junit.framework.TestCase; 21 22 import org.quartz.JobDetail; 23 import org.quartz.SimpleTrigger; 24 import org.quartz.Trigger; 25 import org.quartz.jobs.NoOpJob; 26 import org.quartz.spi.JobStore; 27 import org.quartz.spi.SchedulerSignaler; 28 29 33 public class RAMJobStoreTest extends TestCase { 34 private JobStore fJobStore; 35 private JobDetail fJobDetail; 36 private SampleSignaler fSignaler; 37 38 protected void setUp() throws Exception { 39 this.fJobStore = new RAMJobStore(); 40 this.fSignaler = new SampleSignaler(); 41 this.fJobStore.initialize(null, this.fSignaler); 42 43 this.fJobDetail = new JobDetail("job1", "jobGroup1", NoOpJob.class); 44 this.fJobDetail.setDurability(true); 45 this.fJobStore.storeJob(null, this.fJobDetail, false); 46 } 47 48 public void testAcquireNextTrigger() throws Exception { 49 Trigger trigger1 = 50 new SimpleTrigger("trigger1", "triggerGroup1", this.fJobDetail.getName(), 51 this.fJobDetail.getGroup(), new Date (System.currentTimeMillis() + 200000), 52 new Date (System.currentTimeMillis() + 200000), 2, 2000); 53 Trigger trigger2 = 54 new SimpleTrigger("trigger2", "triggerGroup1", this.fJobDetail.getName(), 55 this.fJobDetail.getGroup(), new Date (System.currentTimeMillis() - 100000), 56 new Date (System.currentTimeMillis() + 20000), 2, 2000); 57 Trigger trigger3 = 58 new SimpleTrigger("trigger1", "triggerGroup2", this.fJobDetail.getName(), 59 this.fJobDetail.getGroup(), new Date (System.currentTimeMillis() + 100000), 60 new Date (System.currentTimeMillis() + 200000), 2, 2000); 61 62 trigger1.computeFirstFireTime(null); 63 trigger2.computeFirstFireTime(null); 64 trigger3.computeFirstFireTime(null); 65 this.fJobStore.storeTrigger(null, trigger1, false); 66 this.fJobStore.storeTrigger(null, trigger2, false); 67 this.fJobStore.storeTrigger(null, trigger3, false); 68 69 assertNull(this.fJobStore.acquireNextTrigger(null, 10)); 70 assertEquals( 71 trigger2, 72 this.fJobStore.acquireNextTrigger(null, new Date (trigger1.getNextFireTime().getTime()).getTime() + 10000)); 73 assertEquals( 74 trigger3, 75 this.fJobStore.acquireNextTrigger(null, new Date (trigger1.getNextFireTime().getTime()).getTime() + 10000)); 76 assertEquals( 77 trigger1, 78 this.fJobStore.acquireNextTrigger(null, new Date (trigger1.getNextFireTime().getTime()).getTime() + 10000)); 79 assertNull( 80 this.fJobStore.acquireNextTrigger(null, new Date (trigger1.getNextFireTime().getTime()).getTime() + 10000)); 81 82 assertEquals(1, this.fSignaler.fMisfireCount); 84 85 this.fJobStore.releaseAcquiredTrigger(null, trigger3); 87 assertEquals( 88 trigger3, 89 this.fJobStore.acquireNextTrigger(null, new Date (trigger1.getNextFireTime().getTime()).getTime() + 10000)); 90 } 91 92 public void testTriggerStates() throws Exception { 93 Trigger trigger = 94 new SimpleTrigger("trigger1", "triggerGroup1", this.fJobDetail.getName(), this.fJobDetail.getGroup(), 95 new Date (System.currentTimeMillis() + 100000), new Date (System.currentTimeMillis() + 200000), 2, 2000); 96 trigger.computeFirstFireTime(null); 97 assertEquals(Trigger.STATE_NONE, this.fJobStore.getTriggerState(null, trigger.getName(), trigger.getGroup())); 98 this.fJobStore.storeTrigger(null, trigger, false); 99 assertEquals(Trigger.STATE_NORMAL, this.fJobStore.getTriggerState(null, trigger.getName(), trigger.getGroup())); 100 101 this.fJobStore.pauseTrigger(null, trigger.getName(), trigger.getGroup()); 102 assertEquals(Trigger.STATE_PAUSED, this.fJobStore.getTriggerState(null, trigger.getName(), trigger.getGroup())); 103 104 this.fJobStore.resumeTrigger(null, trigger.getName(), trigger.getGroup()); 105 assertEquals(Trigger.STATE_NORMAL, this.fJobStore.getTriggerState(null, trigger.getName(), trigger.getGroup())); 106 107 trigger = this.fJobStore.acquireNextTrigger(null, 108 new Date (trigger.getNextFireTime().getTime()).getTime() + 10000); 109 assertNotNull(trigger); 110 this.fJobStore.releaseAcquiredTrigger(null, trigger); 111 trigger=this.fJobStore.acquireNextTrigger(null, 112 new Date (trigger.getNextFireTime().getTime()).getTime() + 10000); 113 assertNotNull(trigger); 114 assertNull(this.fJobStore.acquireNextTrigger(null, 115 new Date (trigger.getNextFireTime().getTime()).getTime() + 10000)); 116 } 117 118 public static class SampleSignaler implements SchedulerSignaler { 119 int fMisfireCount = 0; 120 121 public void notifyTriggerListenersMisfired(Trigger trigger) { 122 fMisfireCount++; 123 } 124 125 public void signalSchedulingChange() { 126 } 127 } 128 } 129 | Popular Tags |