KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > simpl > RAMJobStoreTest


1 /*
2  * Copyright 2004-2006 OpenSymphony
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  */

16 package org.quartz.simpl;
17
18 import java.util.Date JavaDoc;
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 /**
30  * Unit test for RAMJobStore. These tests were submitted by Johannes Zillmann
31  * as part of issue QUARTZ-306.
32  */

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 JavaDoc {
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 JavaDoc {
49         Trigger trigger1 =
50             new SimpleTrigger("trigger1", "triggerGroup1", this.fJobDetail.getName(),
51                     this.fJobDetail.getGroup(), new Date JavaDoc(System.currentTimeMillis() + 200000),
52                     new Date JavaDoc(System.currentTimeMillis() + 200000), 2, 2000);
53         Trigger trigger2 =
54             new SimpleTrigger("trigger2", "triggerGroup1", this.fJobDetail.getName(),
55                     this.fJobDetail.getGroup(), new Date JavaDoc(System.currentTimeMillis() - 100000),
56                     new Date JavaDoc(System.currentTimeMillis() + 20000), 2, 2000);
57         Trigger trigger3 =
58             new SimpleTrigger("trigger1", "triggerGroup2", this.fJobDetail.getName(),
59                     this.fJobDetail.getGroup(), new Date JavaDoc(System.currentTimeMillis() + 100000),
60                     new Date JavaDoc(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 JavaDoc(trigger1.getNextFireTime().getTime()).getTime() + 10000));
73         assertEquals(
74             trigger3,
75             this.fJobStore.acquireNextTrigger(null, new Date JavaDoc(trigger1.getNextFireTime().getTime()).getTime() + 10000));
76         assertEquals(
77             trigger1,
78             this.fJobStore.acquireNextTrigger(null, new Date JavaDoc(trigger1.getNextFireTime().getTime()).getTime() + 10000));
79         assertNull(
80             this.fJobStore.acquireNextTrigger(null, new Date JavaDoc(trigger1.getNextFireTime().getTime()).getTime() + 10000));
81
82         // because of trigger2
83
assertEquals(1, this.fSignaler.fMisfireCount);
84
85         // release trigger3
86
this.fJobStore.releaseAcquiredTrigger(null, trigger3);
87         assertEquals(
88             trigger3,
89             this.fJobStore.acquireNextTrigger(null, new Date JavaDoc(trigger1.getNextFireTime().getTime()).getTime() + 10000));
90     }
91
92     public void testTriggerStates() throws Exception JavaDoc {
93         Trigger trigger =
94             new SimpleTrigger("trigger1", "triggerGroup1", this.fJobDetail.getName(), this.fJobDetail.getGroup(),
95                     new Date JavaDoc(System.currentTimeMillis() + 100000), new Date JavaDoc(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 JavaDoc(trigger.getNextFireTime().getTime()).getTime() + 10000);
109         assertNotNull(trigger);
110         this.fJobStore.releaseAcquiredTrigger(null, trigger);
111         trigger=this.fJobStore.acquireNextTrigger(null,
112                 new Date JavaDoc(trigger.getNextFireTime().getTime()).getTime() + 10000);
113         assertNotNull(trigger);
114         assertNull(this.fJobStore.acquireNextTrigger(null,
115                 new Date JavaDoc(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