KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > NthIncludedDayTriggerTest


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.Calendar JavaDoc;
19 import java.util.Date JavaDoc;
20 import java.util.TimeZone JavaDoc;
21
22
23
24 /**
25  * Unit test for NthIncludedDayTrigger serialization backwards compatibility.
26  */

27 public class NthIncludedDayTriggerTest extends SerializationTestSupport {
28     private static final String JavaDoc[] VERSIONS = new String JavaDoc[] {"1.5.2"};
29     
30     public void testGetFireTimeAfter() {
31         Calendar startCalendar = Calendar.getInstance();
32         startCalendar.set(2005, Calendar.JUNE, 1, 9, 30, 17);
33         startCalendar.clear(Calendar.MILLISECOND);
34         
35         // Test yearly
36
NthIncludedDayTrigger yearlyTrigger = new NthIncludedDayTrigger();
37         yearlyTrigger.setIntervalType(NthIncludedDayTrigger.INTERVAL_TYPE_YEARLY);
38         yearlyTrigger.setStartTime(startCalendar.getTime());
39         yearlyTrigger.setN(10);
40         yearlyTrigger.setFireAtTime("14:35:15");
41         
42         Calendar targetCalendar = Calendar.getInstance();
43         targetCalendar.set(2006, Calendar.JANUARY, 10, 14, 35, 15);
44         targetCalendar.clear(Calendar.MILLISECOND);
45         Date JavaDoc nextFireTime = yearlyTrigger.getFireTimeAfter(new Date JavaDoc(startCalendar.getTime().getTime() + 1000));
46         assertEquals(targetCalendar.getTime(), nextFireTime);
47         
48         // Test monthly
49
NthIncludedDayTrigger monthlyTrigger = new NthIncludedDayTrigger();
50         monthlyTrigger.setIntervalType(NthIncludedDayTrigger.INTERVAL_TYPE_MONTHLY);
51         monthlyTrigger.setStartTime(startCalendar.getTime());
52         monthlyTrigger.setN(5);
53         monthlyTrigger.setFireAtTime("14:35:15");
54         
55         targetCalendar.set(2005, Calendar.JUNE, 5, 14, 35, 15);
56         nextFireTime = monthlyTrigger.getFireTimeAfter(new Date JavaDoc(startCalendar.getTime().getTime() + 1000));
57         assertEquals(targetCalendar.getTime(), nextFireTime);
58         
59         // Test weekly
60
NthIncludedDayTrigger weeklyTrigger = new NthIncludedDayTrigger();
61         weeklyTrigger.setIntervalType(NthIncludedDayTrigger.INTERVAL_TYPE_WEEKLY);
62         weeklyTrigger.setStartTime(startCalendar.getTime());
63         weeklyTrigger.setN(3);
64         weeklyTrigger.setFireAtTime("14:35:15");
65
66         targetCalendar.set(2005, Calendar.JUNE, 7, 14, 35, 15);
67         nextFireTime = weeklyTrigger.getFireTimeAfter(new Date JavaDoc(startCalendar.getTime().getTime() + 1000));
68         assertEquals(targetCalendar.getTime(), nextFireTime);
69     }
70     
71     public void testSetGetFireAtTime() {
72         NthIncludedDayTrigger trigger = new NthIncludedDayTrigger();
73         
74         // Make sure a bad fire at time doesn't reset fire time
75
trigger.setFireAtTime("14:30:10");
76         try {
77             trigger.setFireAtTime("blah");
78             fail();
79         } catch (IllegalArgumentException JavaDoc ignore) {
80         }
81         assertEquals("14:30:10", trigger.getFireAtTime());
82         
83         trigger.setFireAtTime("4:03:15");
84         assertEquals("04:03:15", trigger.getFireAtTime());
85         
86         try {
87             trigger.setFireAtTime("4:3");
88             fail();
89         } catch (IllegalArgumentException JavaDoc ignore) {
90         }
91         
92         try {
93             trigger.setFireAtTime("4:3:15");
94             fail();
95         } catch (IllegalArgumentException JavaDoc ignore) {
96         }
97         
98         trigger.setFireAtTime("23:17");
99         assertEquals("23:17:00", trigger.getFireAtTime());
100         
101         try {
102             trigger.setFireAtTime("24:3:15");
103             fail();
104         } catch (IllegalArgumentException JavaDoc ignore) {
105         }
106
107         try {
108             trigger.setFireAtTime("-1:3:15");
109             fail();
110         } catch (IllegalArgumentException JavaDoc ignore) {
111         }
112
113         try {
114             trigger.setFireAtTime("23:60:15");
115             fail();
116         } catch (IllegalArgumentException JavaDoc ignore) {
117         }
118
119         try {
120             trigger.setFireAtTime("23:-1:15");
121             fail();
122         } catch (IllegalArgumentException JavaDoc ignore) {
123         }
124
125         try {
126             trigger.setFireAtTime("23:17:60");
127             fail();
128         } catch (IllegalArgumentException JavaDoc ignore) {
129         }
130         
131         try {
132             trigger.setFireAtTime("23:17:-1");
133             fail();
134         } catch (IllegalArgumentException JavaDoc ignore) {
135         }
136     }
137     
138     public void testTimeZone() throws Exception JavaDoc {
139         
140         TimeZone JavaDoc GMT = TimeZone.getTimeZone("GMT-0:00");
141         TimeZone JavaDoc EST = TimeZone.getTimeZone("GMT-5:00");
142         
143         Calendar startTime = Calendar.getInstance(EST);
144         startTime.set(2006, Calendar.MARCH, 7, 7, 0, 0);
145         
146         // Same timezone, so should just get back 8:00 that day
147
{
148             NthIncludedDayTrigger t = new NthIncludedDayTrigger("name", "group");
149             t.setIntervalType(NthIncludedDayTrigger.INTERVAL_TYPE_WEEKLY);
150             t.setN(3);
151             t.setStartTime(startTime.getTime());
152             t.setFireAtTime("8:00");
153             t.setTimeZone(EST);
154             
155             Date JavaDoc firstTime = t.computeFirstFireTime(null);
156             Calendar firstTimeCal = Calendar.getInstance(EST);
157             firstTimeCal.setTime(firstTime);
158             assertEquals(7, firstTimeCal.get(Calendar.DATE));
159         }
160
161         // Timezone is 5 hours later, so should just get back 8:00 a week later
162
{
163             NthIncludedDayTrigger t = new NthIncludedDayTrigger("name", "group");
164             t.setIntervalType(NthIncludedDayTrigger.INTERVAL_TYPE_WEEKLY);
165             t.setN(3);
166             t.setStartTime(startTime.getTime());
167             t.setFireAtTime("8:00");
168             t.setTimeZone(GMT);
169             
170             Date JavaDoc firstTime = t.computeFirstFireTime(null);
171             Calendar firstTimeCal = Calendar.getInstance(EST);
172             firstTimeCal.setTime(firstTime);
173             assertEquals(14, firstTimeCal.get(Calendar.DATE));
174         }
175     }
176     
177     
178     /**
179      * Get the object to serialize when generating serialized file for future
180      * tests, and against which to validate deserialized object.
181      */

182     protected Object JavaDoc getTargetObject() {
183         Calendar startTime = Calendar.getInstance();
184         startTime.set(2005, 6, 1, 11, 30, 0);
185         
186         NthIncludedDayTrigger t = new NthIncludedDayTrigger("name", "group");
187         t.setIntervalType(NthIncludedDayTrigger.INTERVAL_TYPE_MONTHLY);
188         t.setN(3);
189         t.setStartTime(startTime.getTime());
190         t.setFireAtTime("12:15");
191         t.setNextFireCutoffInterval(13);
192         
193         return t;
194     }
195     
196     /**
197      * Get the Quartz versions for which we should verify
198      * serialization backwards compatibility.
199      */

200     protected String JavaDoc[] getVersions() {
201         return VERSIONS;
202     }
203     
204     /**
205      * Verify that the target object and the object we just deserialized
206      * match.
207      */

208     protected void verifyMatch(Object JavaDoc target, Object JavaDoc deserialized) {
209         NthIncludedDayTrigger targetTrigger = (NthIncludedDayTrigger)target;
210         NthIncludedDayTrigger deserializedTrigger = (NthIncludedDayTrigger)deserialized;
211         
212         assertNotNull(deserializedTrigger);
213         assertEquals(deserializedTrigger.getName(), deserializedTrigger.getName());
214         assertEquals(deserializedTrigger.getGroup(), deserializedTrigger.getGroup());
215         assertEquals(deserializedTrigger.getIntervalType(), deserializedTrigger.getIntervalType());
216         assertEquals(deserializedTrigger.getN(), deserializedTrigger.getN());
217         assertEquals(deserializedTrigger.getStartTime(), deserializedTrigger.getStartTime());
218         assertNull(deserializedTrigger.getEndTime());
219         assertEquals(deserializedTrigger.getFireAtTime(), deserializedTrigger.getFireAtTime());
220         assertEquals(deserializedTrigger.getNextFireCutoffInterval(), deserializedTrigger.getNextFireCutoffInterval());
221         assertEquals(TimeZone.getDefault(), deserializedTrigger.getTimeZone());
222     }
223 }
224
Popular Tags