1 16 package org.quartz; 17 18 import java.util.ArrayList ; 19 import java.util.Arrays ; 20 import java.util.Calendar ; 21 import java.util.Date ; 22 import java.util.HashSet ; 23 import java.util.TimeZone ; 24 25 26 29 public class SimpleTriggerTest extends SerializationTestSupport { 30 private static final String [] VERSIONS = new String [] {"1.5.2"}; 31 32 private static final TimeZone 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 50 protected Object 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 73 protected String [] getVersions() { 74 return VERSIONS; 75 } 76 77 81 protected void verifyMatch(Object target, Object 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 currentTime = new Date (); 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 (0)); 127 simpleTrigger.setRepeatInterval(10); 128 simpleTrigger.setRepeatCount(4); 129 130 Date fireTimeAfter = simpleTrigger.getFireTimeAfter(new Date (34)); 131 assertEquals(40, fireTimeAfter.getTime()); 132 } 133 134 public void testAddTriggerListener() { 135 String [] listenerNames = new String [] {"X", "A", "B"}; 136 137 HashSet hashSet = new HashSet (Arrays.asList(listenerNames)); 140 assertFalse(Arrays.asList(listenerNames).equals(new ArrayList (hashSet))); 141 142 SimpleTrigger simpleTrigger = new SimpleTrigger(); 143 for (int i = 0; i < listenerNames.length; i++) { 144 simpleTrigger.addTriggerListener(listenerNames[i]); 145 } 146 147 assertEquals(Arrays.asList(listenerNames), 149 Arrays.asList(simpleTrigger.getTriggerListenerNames())); 150 151 for (int i = 0; i < listenerNames.length; i++) { 153 try { 154 simpleTrigger.addTriggerListener(listenerNames[i]); 155 fail(); 156 } catch (IllegalArgumentException e) { 157 } 158 } 159 } 160 161 public void testClone() { 162 SimpleTrigger simpleTrigger = new SimpleTrigger(); 163 164 Trigger clone = (Trigger)simpleTrigger.clone(); 166 assertEquals(0, clone.getTriggerListenerNames().length); 167 assertEquals(0, clone.getJobDataMap().size()); 168 169 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 [] {"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 clone.removeTriggerListener("L2"); 184 assertEquals(1, clone.getTriggerListenerNames().length); 185 assertEquals(Arrays.asList(new String [] {"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 [] {"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 |