KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > SimpleTriggerTest


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;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Arrays JavaDoc;
20 import java.util.Calendar JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.TimeZone JavaDoc;
24
25
26 /**
27  * Unit test for SimpleTrigger serialization backwards compatibility.
28  */

29 public class SimpleTriggerTest extends SerializationTestSupport {
30     private static final String JavaDoc[] VERSIONS = new String JavaDoc[] {"1.5.2"};
31
32     private static final TimeZone JavaDoc EST_TIME_ZONE = TimeZone.getTimeZone("US/Eastern");
33     private static final Calendar START_TIME = Calendar.getInstance();
34     private static final Calendar END_TIME = Calendar.getInstance();
35     
36     static
37     {
38         START_TIME.clear();
39         START_TIME.set(2006, Calendar.JUNE, 1, 10, 5, 15);
40         START_TIME.setTimeZone(EST_TIME_ZONE);
41         END_TIME.clear();
42         END_TIME.set(2008, Calendar.MAY, 2, 20, 15, 30);
43         END_TIME.setTimeZone(EST_TIME_ZONE);
44     }
45     
46     /**
47      * Get the object to serialize when generating serialized file for future
48      * tests, and against which to validate deserialized object.
49      */

50     protected Object JavaDoc getTargetObject() {
51         JobDataMap jobDataMap = new JobDataMap();
52         jobDataMap.put("A", "B");
53         
54         SimpleTrigger t = new SimpleTrigger("SimpleTrigger", "SimpleGroup",
55                 "JobName", "JobGroup", START_TIME.getTime(),
56                 END_TIME.getTime(), 5, 1000);
57         t.setCalendarName("MyCalendar");
58         t.setDescription("SimpleTriggerDesc");
59         t.setJobDataMap(jobDataMap);
60         t.setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT);
61         t.setVolatility(true);
62
63         t.addTriggerListener("L1");
64         t.addTriggerListener("L2");
65         
66         return t;
67     }
68     
69     /**
70      * Get the Quartz versions for which we should verify
71      * serialization backwards compatibility.
72      */

73     protected String JavaDoc[] getVersions() {
74         return VERSIONS;
75     }
76     
77     /**
78      * Verify that the target object and the object we just deserialized
79      * match.
80      */

81     protected void verifyMatch(Object JavaDoc target, Object JavaDoc deserialized) {
82         SimpleTrigger targetSimpleTrigger = (SimpleTrigger)target;
83         SimpleTrigger deserializedSimpleTrigger = (SimpleTrigger)deserialized;
84         
85         assertNotNull(deserializedSimpleTrigger);
86         assertEquals(targetSimpleTrigger.getName(), deserializedSimpleTrigger.getName());
87         assertEquals(targetSimpleTrigger.getGroup(), deserializedSimpleTrigger.getGroup());
88         assertEquals(targetSimpleTrigger.getJobName(), deserializedSimpleTrigger.getJobName());
89         assertEquals(targetSimpleTrigger.getJobGroup(), deserializedSimpleTrigger.getJobGroup());
90         assertEquals(targetSimpleTrigger.getStartTime(), deserializedSimpleTrigger.getStartTime());
91         assertEquals(targetSimpleTrigger.getEndTime(), deserializedSimpleTrigger.getEndTime());
92         assertEquals(targetSimpleTrigger.getRepeatCount(), deserializedSimpleTrigger.getRepeatCount());
93         assertEquals(targetSimpleTrigger.getRepeatInterval(), deserializedSimpleTrigger.getRepeatInterval());
94         assertEquals(targetSimpleTrigger.getCalendarName(), deserializedSimpleTrigger.getCalendarName());
95         assertEquals(targetSimpleTrigger.getDescription(), deserializedSimpleTrigger.getDescription());
96         assertEquals(targetSimpleTrigger.getJobDataMap(), deserializedSimpleTrigger.getJobDataMap());
97         assertEquals(targetSimpleTrigger.getMisfireInstruction(), deserializedSimpleTrigger.getMisfireInstruction());
98         assertTrue(targetSimpleTrigger.isVolatile());
99         assertEquals(2, deserializedSimpleTrigger.getTriggerListenerNames().length);
100     }
101     
102     public void testUpdateAfterMisfire() {
103         
104         Calendar startTime = Calendar.getInstance();
105         startTime.set(2005, Calendar.JULY, 5, 9, 0, 0);
106         
107         Calendar endTime = Calendar.getInstance();
108         endTime.set(2005, Calendar.JULY, 5, 10, 0, 0);
109         
110         SimpleTrigger simpleTrigger = new SimpleTrigger();
111         simpleTrigger.setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT);
112         simpleTrigger.setRepeatCount(5);
113         simpleTrigger.setStartTime(startTime.getTime());
114         simpleTrigger.setEndTime(endTime.getTime());
115         
116         Date JavaDoc currentTime = new Date JavaDoc();
117         simpleTrigger.updateAfterMisfire(null);
118         assertEquals(startTime.getTime(), simpleTrigger.getStartTime());
119         assertEquals(endTime.getTime(), simpleTrigger.getEndTime());
120         assertNull(simpleTrigger.getNextFireTime());
121     }
122     
123     public void testGetFireTimeAfter() {
124         SimpleTrigger simpleTrigger = new SimpleTrigger();
125
126         simpleTrigger.setStartTime(new Date JavaDoc(0));
127         simpleTrigger.setRepeatInterval(10);
128         simpleTrigger.setRepeatCount(4);
129         
130         Date JavaDoc fireTimeAfter = simpleTrigger.getFireTimeAfter(new Date JavaDoc(34));
131         assertEquals(40, fireTimeAfter.getTime());
132     }
133     
134     public void testAddTriggerListener() {
135         String JavaDoc[] listenerNames = new String JavaDoc[] {"X", "A", "B"};
136         
137         // Verify that a HashSet shuffles order, so we know that order test
138
// below is actually testing something
139
HashSet JavaDoc hashSet = new HashSet JavaDoc(Arrays.asList(listenerNames));
140         assertFalse(Arrays.asList(listenerNames).equals(new ArrayList JavaDoc(hashSet)));
141         
142         SimpleTrigger simpleTrigger = new SimpleTrigger();
143         for (int i = 0; i < listenerNames.length; i++) {
144             simpleTrigger.addTriggerListener(listenerNames[i]);
145         }
146
147         // Make sure order was maintained
148
assertEquals(Arrays.asList(listenerNames),
149                      Arrays.asList(simpleTrigger.getTriggerListenerNames()));
150         
151         // Make sure uniqueness is enforced
152
for (int i = 0; i < listenerNames.length; i++) {
153             try {
154                 simpleTrigger.addTriggerListener(listenerNames[i]);
155                 fail();
156             } catch (IllegalArgumentException JavaDoc e) {
157             }
158         }
159     }
160     
161     public void testClone() {
162         SimpleTrigger simpleTrigger = new SimpleTrigger();
163         
164         // Make sure empty sub-objects are cloned okay
165
Trigger clone = (Trigger)simpleTrigger.clone();
166         assertEquals(0, clone.getTriggerListenerNames().length);
167         assertEquals(0, clone.getJobDataMap().size());
168         
169         // Make sure non-empty sub-objects are cloned okay
170
simpleTrigger.addTriggerListener("L1");
171         simpleTrigger.addTriggerListener("L2");
172         simpleTrigger.getJobDataMap().put("K1", "V1");
173         simpleTrigger.getJobDataMap().put("K2", "V2");
174         clone = (Trigger)simpleTrigger.clone();
175         assertEquals(2, clone.getTriggerListenerNames().length);
176         assertEquals(Arrays.asList(new String JavaDoc[] {"L1", "L2"}), Arrays.asList(clone.getTriggerListenerNames()));
177         assertEquals(2, clone.getJobDataMap().size());
178         assertEquals("V1", clone.getJobDataMap().get("K1"));
179         assertEquals("V2", clone.getJobDataMap().get("K2"));
180         
181         // Make sure sub-object collections have really been cloned by ensuring
182
// their modification does not change the source Trigger
183
clone.removeTriggerListener("L2");
184         assertEquals(1, clone.getTriggerListenerNames().length);
185         assertEquals(Arrays.asList(new String JavaDoc[] {"L1"}), Arrays.asList(clone.getTriggerListenerNames()));
186         clone.getJobDataMap().remove("K1");
187         assertEquals(1, clone.getJobDataMap().size());
188         
189         assertEquals(2, simpleTrigger.getTriggerListenerNames().length);
190         assertEquals(Arrays.asList(new String JavaDoc[] {"L1", "L2"}), Arrays.asList(simpleTrigger.getTriggerListenerNames()));
191         assertEquals(2, simpleTrigger.getJobDataMap().size());
192         assertEquals("V1", simpleTrigger.getJobDataMap().get("K1"));
193         assertEquals("V2", simpleTrigger.getJobDataMap().get("K2"));
194     }
195 }
196
Popular Tags