1 4 package org.oddjob.schedules.schedules; 5 6 import java.text.DateFormat ; 7 import java.text.ParseException ; 8 import java.text.SimpleDateFormat ; 9 import java.util.Date ; 10 11 import junit.framework.TestCase; 12 13 import org.oddjob.schedules.Interval; 14 import org.oddjob.schedules.ScheduleContext; 15 import org.oddjob.util.DateHelper; 16 17 21 public class DayOfMonthScheduleTest extends TestCase { 22 24 DateFormat checkFormat; 25 DateFormat inputFormat; 26 27 protected void setUp() { 28 checkFormat = new SimpleDateFormat ("dd-MMM-yy HH:mm:ss:SSS"); 29 inputFormat = new SimpleDateFormat ("dd-MMM-yy HH:mm"); 30 } 31 32 36 public void testFromAndTo() throws ParseException { 37 DayOfMonthSchedule schedule = new DayOfMonthSchedule(); 38 schedule.setFrom("5"); 39 schedule.setTo("25"); 40 41 Date now1 = inputFormat.parse("10-feb-2003 12:30"); 42 Date expectedStart 43 = checkFormat.parse("05-feb-2003 00:00:00:000"); 44 Date actualStart1 = schedule.getStartDate( 45 new ScheduleContext(now1)); 46 assertEquals(expectedStart, actualStart1); 47 Date expectedEnd 48 = checkFormat.parse("25-feb-2003 23:59:59:999"); 49 Date actualEnd1 = schedule.getEndDate( 50 new ScheduleContext(now1)); 51 assertEquals(expectedEnd, actualEnd1); 52 } 53 54 58 public void testAfter() throws ParseException { 59 DayOfMonthSchedule schedule = new DayOfMonthSchedule(); 60 schedule.setFrom("5"); 61 schedule.setTo("25"); 62 63 Date now1 = inputFormat.parse("26-feb-2003 12:30"); 64 Date expectedStart 65 = checkFormat.parse("05-mar-2003 00:00:00:000"); 66 Date actualStart1 = schedule.getStartDate( 67 new ScheduleContext(now1)); 68 assertTrue("Start [" + actualStart1 + "] wrong.", 69 expectedStart.equals(actualStart1)); 70 Date expectedEnd 71 = checkFormat.parse("25-mar-2003 23:59:59:999"); 72 Date actualEnd1 = schedule.getEndDate( 73 new ScheduleContext(now1)); 74 assertTrue("End [" + actualEnd1 + "] wrong.", 75 expectedEnd.equals(actualEnd1)); 76 } 77 78 82 public void testOverBoundry() throws ParseException { 83 DayOfMonthSchedule schedule = new DayOfMonthSchedule(); 84 schedule.setFrom("25"); 85 schedule.setTo("5"); 86 87 Date now1 = inputFormat.parse("26-feb-2003 12:30"); 88 Date expectedStart 89 = checkFormat.parse("25-feb-2003 00:00:00:000"); 90 Date actualStart1 = schedule.getStartDate( 91 new ScheduleContext(now1)); 92 assertTrue("Start [" + actualStart1 + "] wrong.", 93 expectedStart.equals(actualStart1)); 94 Date expectedEnd 95 = checkFormat.parse("5-mar-2003 23:59:59:999"); 96 Date actualEnd1 = schedule.getEndDate( 97 new ScheduleContext(now1)); 98 assertTrue("End [" + actualEnd1 + "] wrong.", 99 expectedEnd.equals(actualEnd1)); 100 101 Date now2 = inputFormat.parse("24-feb-2003 12:30"); 102 Date actualStart2 = schedule.getStartDate( 103 new ScheduleContext(now2)); 104 assertTrue("Start [" + actualStart2 + "] wrong.", 105 expectedStart.equals(actualStart2)); 106 Date actualEnd2 = schedule.getEndDate( 107 new ScheduleContext(now2)); 108 assertTrue("End [" + actualEnd2 + "] wrong.", 109 expectedEnd.equals(actualEnd2)); 110 111 Date now3 = inputFormat.parse("1-mar-2003 12:30"); 112 Date actualStart3 = schedule.getStartDate( 113 new ScheduleContext(now3)); 114 assertTrue("Start [" + actualStart3 + "] wrong.", 115 expectedStart.equals(actualStart3)); 116 Date actualEnd3 = schedule.getEndDate( 117 new ScheduleContext(now3)); 118 assertTrue("End [" + expectedEnd + "] wrong.", 119 expectedEnd.equals(actualEnd3)); 120 } 121 122 125 public void testLastDay() throws ParseException { 126 DayOfMonthSchedule schedule = new DayOfMonthSchedule(); 127 schedule.setFrom("5"); 128 schedule.setTo("0"); 129 130 Date now1 = inputFormat.parse("02-mar-2003 12:30"); 131 Interval interval1 = schedule.nextDue( 132 new ScheduleContext(now1)); 133 Interval expected1 = new Interval( 134 checkFormat.parse("05-mar-2003 00:00:00:000"), 135 checkFormat.parse("31-mar-2003 23:59:59:999")); 136 System.out.println("interval1: " + interval1); 137 assertTrue("interval as expected", interval1.equals(expected1)); 138 139 schedule.setTo("-1"); 140 Interval interval2 = schedule.nextDue( 141 new ScheduleContext(now1)); 142 Interval expected2 = new Interval( 143 checkFormat.parse("05-mar-2003 00:00:00:000"), 144 checkFormat.parse("30-mar-2003 23:59:59:999")); 145 System.out.println("interval2: " + interval2); 146 assertTrue("interval as expected", interval2.equals(expected2)); 147 } 148 149 public void testDefaultFrom() throws ParseException { 150 DayOfMonthSchedule schedule = new DayOfMonthSchedule(); 151 schedule.setTo("25"); 152 153 Interval result = schedule.nextDue( 154 new ScheduleContext(DateHelper.parseDate("26-mar-06"))); 155 156 assertEquals(new Interval( 157 DateHelper.parseDateTime("1-apr-06 00:00"), 158 DateHelper.parseDateTime("25-apr-06 23:59:59:999")), 159 result); 160 } 161 162 public void testDefaultTo() throws ParseException { 163 DayOfMonthSchedule schedule = new DayOfMonthSchedule(); 164 schedule.setFrom("5"); 165 166 Interval result = schedule.nextDue( 167 new ScheduleContext(DateHelper.parseDate("26-mar-06"))); 168 169 assertEquals(new Interval( 170 DateHelper.parseDateTime("5-mar-06 00:00"), 171 DateHelper.parseDateTime("31-mar-06 23:59:59:999")), 172 result); 173 } 174 175 public void testInclusive() throws ParseException { 176 DayOfMonthSchedule schedule = new DayOfMonthSchedule(); 177 schedule.setTo("25"); 178 179 Interval result = schedule.nextDue( 180 new ScheduleContext(DateHelper.parseDate("25-mar-06 10:15"))); 181 182 assertEquals(new Interval( 183 DateHelper.parseDateTime("1-mar-06 00:00"), 184 DateHelper.parseDateTime("25-mar-06 23:59:59:999")), 185 result); 186 187 } 188 } 189 | Popular Tags |