1 64 65 package com.jcorporate.expresso.services.crontab.tests; 66 67 import com.jcorporate.expresso.services.crontab.CronException; 68 import com.jcorporate.expresso.services.crontab.CrontabEntry; 69 import com.jcorporate.expresso.services.crontab.CrontabListenerI; 70 import junit.framework.TestCase; 71 import org.apache.log4j.Logger; 72 73 import java.util.Calendar ; 74 import java.util.Date ; 75 76 77 84 public class TestCrontabEntry extends TestCase { 85 86 private static final Logger log = Logger.getLogger(TestCrontabEntry.class); 87 88 CrontabListenerI listener; 89 90 public TestCrontabEntry(String _name) { 91 super(_name); 92 } 93 94 public void setUp() { 95 listener = new CrontabListenerI() { 96 public void handleCrontabEntry(CrontabEntry entry) { 97 log.info("Starting Cron process"); 98 } 99 }; 100 } 101 102 105 public void testConstructor1() { 106 try { 107 Calendar cal = Calendar.getInstance(); 109 cal.add(Calendar.DATE, 1); 110 Date dt = cal.getTime(); 111 CrontabEntry cronID = new CrontabEntry(dt, listener); 112 if (log.isDebugEnabled()) { 113 log.debug("Constructor 1: Created cron: " + cronID.toString() + "now is: " + dt.toString()); 114 } 115 assertTrue("Cron date should be the exact time we originally set", 116 dt.equals(new Date (cronID.getAlarmTime()))); 117 } catch (CronException ex) { 118 ex.printStackTrace(); 119 fail("crontab entry threw an exception"); 120 } 121 122 } 123 124 127 public void testConstructor2() { 128 129 Calendar cal = Calendar.getInstance(); 130 cal.add(Calendar.MINUTE, 20); 131 Date testDate = cal.getTime(); 132 CrontabEntry cronID = new CrontabEntry(20, false, listener); 136 137 assertTrue(Math.abs(testDate.getTime() - cronID.getAlarmTime()) < (1000 * 60)); 141 142 if (log.isDebugEnabled()) { 143 log.debug("Constructor 2: Created cron: " + cronID.toString()); 144 } 145 assertTrue("Cron should not be repetitive", cronID.isIsRepetitive() == false); 146 assertTrue("Cron should be relative", cronID.isIsRelative() == true); 147 148 cal = Calendar.getInstance(); 152 cal.add(Calendar.MINUTE, 90); 153 testDate = cal.getTime(); 154 cronID = new CrontabEntry(90, true, listener); 155 if (log.isDebugEnabled()) { 156 log.debug("Constructor 2: Created cron: " + cronID.toString()); 157 } 158 assertTrue("Cron should be repetitive", cronID.isIsRepetitive() == true); 159 assertTrue("Cron should be relative", cronID.isIsRelative() == true); 160 assertTrue(Math.abs(testDate.getTime() - cronID.getAlarmTime()) < (1000 * 60)); 161 162 163 } 164 165 168 public void testConstructor3() { 169 170 try { 171 CrontabEntry cronID = new CrontabEntry(4, -1, 173 -1, -1, 174 -1, -1, 175 listener); 176 { 177 Calendar now = Calendar.getInstance(); 178 if (now.get(Calendar.MINUTE) > 4) { 179 now.add(Calendar.HOUR, 1); 180 } 181 Calendar cronDate = Calendar.getInstance(); 182 cronDate.setTime(new Date (cronID.getAlarmTime())); 183 if (log.isInfoEnabled()) { 184 System.out.println("Constructor 3: Created cron: " + cronID.toString()); 185 } 186 assertTrue("Cron should execute at minute 4. Got: " + 187 cronDate.get(Calendar.MINUTE) + " instead", cronDate.get(Calendar.MINUTE) == 4); 188 assertTrue("Cron should execute either this hour or next hour", 189 cronDate.get(Calendar.HOUR) == now.get(Calendar.HOUR)); 190 191 assertTrue("Cron should be repetitive", cronID.isIsRepetitive() == true); 192 assertTrue("Cron should not be relative time", cronID.isIsRelative() == false); 193 } 194 195 Calendar curDate = Calendar.getInstance(); 197 curDate.add(Calendar.DATE, 1); 198 cronID = new CrontabEntry(0, 0, 199 -1, -1, 200 -1, -1, 201 listener); 202 203 assertTrue("Cron should be repetitive", cronID.isIsRepetitive() == true); 204 assertTrue("Cron should not be relative time", cronID.isIsRelative() == false); 205 Calendar cal = Calendar.getInstance(); 206 cal.setTime(new Date (cronID.getAlarmTime())); 207 assertTrue("Cron job should execute at midnight", cal.get(Calendar.HOUR) == 0 209 && cal.get(Calendar.MINUTE) == 0); 210 211 212 assertTrue("Cron job should execute tomorrow", 216 cal.get(Calendar.DATE) == curDate.get(Calendar.DATE)); 217 if (log.isInfoEnabled()) { 218 log.info("Constructor 3: Created cron: " + cronID.toString()); 219 } 220 221 cronID = new CrontabEntry(20, -1, 223 -1, -1, 224 1, -1, 225 listener); 226 227 if (log.isInfoEnabled()) { 228 log.info("Constructor 3: Created cron: " + cronID.toString()); 229 } 230 231 curDate = Calendar.getInstance(); 232 assertTrue("Cron should be repetitive", cronID.isIsRepetitive() == true); 233 assertTrue("Cron should not be relative time", cronID.isIsRelative() == false); 234 cal = Calendar.getInstance(); 235 cal.setTime(new Date (cronID.getAlarmTime())); 236 assertTrue("Cron job should execute every sunday at 20 minutes of every hour", cal.get( 238 Calendar.DAY_OF_WEEK) == 1 239 && cal.get(Calendar.MINUTE) == 20); 240 assertTrue("Cron job should execute at 0'th hour of next sunday ", 241 curDate.get(Calendar.DAY_OF_WEEK) == 1 ? true : cal.get(Calendar.HOUR_OF_DAY) == 0); 242 243 244 cronID = new CrontabEntry(0, 0, 246 1, 0, 247 -1, 2024, 248 listener); 249 250 assertTrue("Cron should not be repetitive", cronID.isIsRepetitive() == false); 251 assertTrue("Cron should not be relative time", cronID.isIsRelative() == false); 252 253 cal = Calendar.getInstance(); 254 cal.setTime(new Date (cronID.getAlarmTime())); 255 assertTrue("Cron should execute year 20204", cal.get(Calendar.YEAR) == 2024); 256 assertTrue("Cron should execute in January", cal.get(Calendar.MONTH) == Calendar.JANUARY); 257 assertTrue("Cron should execute the first of the month", cal.get(Calendar.DAY_OF_MONTH) == 1); 258 assertTrue("Cron should execute at midnight", 259 cal.get(Calendar.HOUR) == 0 && cal.get(Calendar.MINUTE) == 0); 260 if (log.isInfoEnabled()) { 261 log.info("Constructor 3: Created cron: " + cronID.toString()); 262 } 263 } catch (CronException ex) { 264 ex.printStackTrace(); 265 fail("crontab entry threw an exception"); 266 } 267 268 } 269 270 274 public void testConstructor4() { 275 final String label = "Test Cron"; 276 try { 277 CrontabEntry cronID = new CrontabEntry(4, -1, 279 -1, -1, 280 -1, -1, label, 281 listener); 282 283 assertTrue(label.equals(cronID.getLabel())); 285 if (log.isInfoEnabled()) { 286 log.info("Constructor 4: Created cron: " + cronID.toString()); 287 } 288 } catch (CronException ex) { 289 ex.printStackTrace(); 290 fail("crontab entry threw an exception"); 291 } 292 293 } 294 295 296 299 public static void main(String [] argv) { 300 String [] testCaseList = {TestCrontabEntry.class.getName()}; 301 junit.textui.TestRunner.main(testCaseList); 302 } 303 } 304 | Popular Tags |