1 16 package org.quartz; 17 18 import java.util.Calendar ; 19 import java.util.Date ; 20 import java.util.TimeZone ; 21 22 23 24 27 public class NthIncludedDayTriggerTest extends SerializationTestSupport { 28 private static final String [] VERSIONS = new String [] {"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 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 nextFireTime = yearlyTrigger.getFireTimeAfter(new Date (startCalendar.getTime().getTime() + 1000)); 46 assertEquals(targetCalendar.getTime(), nextFireTime); 47 48 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 (startCalendar.getTime().getTime() + 1000)); 57 assertEquals(targetCalendar.getTime(), nextFireTime); 58 59 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 (startCalendar.getTime().getTime() + 1000)); 68 assertEquals(targetCalendar.getTime(), nextFireTime); 69 } 70 71 public void testSetGetFireAtTime() { 72 NthIncludedDayTrigger trigger = new NthIncludedDayTrigger(); 73 74 trigger.setFireAtTime("14:30:10"); 76 try { 77 trigger.setFireAtTime("blah"); 78 fail(); 79 } catch (IllegalArgumentException 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 ignore) { 90 } 91 92 try { 93 trigger.setFireAtTime("4:3:15"); 94 fail(); 95 } catch (IllegalArgumentException 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 ignore) { 105 } 106 107 try { 108 trigger.setFireAtTime("-1:3:15"); 109 fail(); 110 } catch (IllegalArgumentException ignore) { 111 } 112 113 try { 114 trigger.setFireAtTime("23:60:15"); 115 fail(); 116 } catch (IllegalArgumentException ignore) { 117 } 118 119 try { 120 trigger.setFireAtTime("23:-1:15"); 121 fail(); 122 } catch (IllegalArgumentException ignore) { 123 } 124 125 try { 126 trigger.setFireAtTime("23:17:60"); 127 fail(); 128 } catch (IllegalArgumentException ignore) { 129 } 130 131 try { 132 trigger.setFireAtTime("23:17:-1"); 133 fail(); 134 } catch (IllegalArgumentException ignore) { 135 } 136 } 137 138 public void testTimeZone() throws Exception { 139 140 TimeZone GMT = TimeZone.getTimeZone("GMT-0:00"); 141 TimeZone 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 { 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 firstTime = t.computeFirstFireTime(null); 156 Calendar firstTimeCal = Calendar.getInstance(EST); 157 firstTimeCal.setTime(firstTime); 158 assertEquals(7, firstTimeCal.get(Calendar.DATE)); 159 } 160 161 { 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 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 182 protected Object 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 200 protected String [] getVersions() { 201 return VERSIONS; 202 } 203 204 208 protected void verifyMatch(Object target, Object 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 |