1 4 package org.oddjob.schedules.schedules; 5 6 import java.text.ParseException ; 7 import java.util.Date ; 8 import java.util.Locale ; 9 10 import junit.framework.TestCase; 11 12 import org.oddjob.schedules.Interval; 13 import org.oddjob.schedules.ScheduleContext; 14 import org.oddjob.util.DateHelper; 15 16 20 public class DayOfWeekScheduleTest extends TestCase { 21 22 public void testFromAndTo() throws ParseException { 23 DayOfWeekSchedule schedule = new DayOfWeekSchedule(); 24 schedule.setFrom("TUE"); 25 schedule.setTo("WED"); 26 27 Date now1 = DateHelper.parseDateTime("10-feb-2004 12:30"); 29 Date expectedStart 30 = DateHelper.parseDateTime("10-feb-2004 00:00:00:000"); 31 Date actualStart1 = schedule.getStartDate( 32 new ScheduleContext(now1)); 33 assertTrue("Start [" + actualStart1 + "] wrong.", 34 expectedStart.equals(actualStart1)); 35 Date expectedEnd 36 = DateHelper.parseDateTime("11-feb-2004 23:59:59:999"); 37 Date actualEnd1 = schedule.getEndDate( 38 new ScheduleContext(now1)); 39 assertTrue("End [" + actualEnd1 + "] wrong.", 40 expectedEnd.equals(actualEnd1)); 41 } 42 43 public void testAfter() throws ParseException { 44 DayOfWeekSchedule schedule = new DayOfWeekSchedule(); 45 schedule.setFrom("TUE"); 46 schedule.setTo("WED"); 47 48 Date now1 = DateHelper.parseDateTime("14-feb-2004 12:30"); 49 Date expectedStart 50 = DateHelper.parseDateTime("17-feb-2004 00:00:00:000"); 51 Date actualStart1 = schedule.getStartDate( 52 new ScheduleContext(now1)); 53 assertTrue("Start [" + actualStart1 + "] wrong.", 54 expectedStart.equals(actualStart1)); 55 Date expectedEnd 56 = DateHelper.parseDateTime("18-feb-2004 23:59:59:999"); 57 Date actualEnd1 = schedule.getEndDate( 58 new ScheduleContext(now1)); 59 assertTrue("End [" + actualEnd1 + "] wrong.", 60 expectedEnd.equals(actualEnd1)); 61 } 62 63 public void testOverBoundry() throws ParseException { 64 DayOfWeekSchedule schedule = new DayOfWeekSchedule(); 65 schedule.setFrom("FRI"); 66 schedule.setTo("MON"); 67 68 Date now1 = DateHelper.parseDateTime("11-feb-2004 12:30"); 69 Date expectedStart 70 = DateHelper.parseDateTime("13-feb-2004 00:00:00:000"); 71 Date actualStart1 = schedule.getStartDate( 72 new ScheduleContext(now1)); 73 assertTrue("Start [" + actualStart1 + "] wrong.", 74 expectedStart.equals(actualStart1)); 75 Date expectedEnd 76 = DateHelper.parseDateTime("16-feb-2004 23:59:59:999"); 77 Date actualEnd1 = schedule.getEndDate( 78 new ScheduleContext(now1)); 79 assertTrue("End [" + actualEnd1 + "] wrong.", 80 expectedEnd.equals(actualEnd1)); 81 82 Date now2 = DateHelper.parseDateTime("14-feb-2004 12:30"); 83 Date actualStart2 = schedule.getStartDate( 84 new ScheduleContext(now2)); 85 assertTrue("Start [" + actualStart2 + "] wrong.", 86 expectedStart.equals(actualStart2)); 87 Date actualEnd2 = schedule.getEndDate( 88 new ScheduleContext(now2)); 89 assertTrue("End [" + actualEnd2 + "] wrong.", 90 expectedEnd.equals(actualEnd2)); 91 92 Date now3 = DateHelper.parseDateTime("16-feb-2004 12:30"); 93 Date actualStart3 = schedule.getStartDate( 94 new ScheduleContext(now3)); 95 assertTrue("Start [" + actualStart3 + "] wrong.", 96 expectedStart.equals(actualStart3)); 97 Date actualEnd3 = schedule.getEndDate( 98 new ScheduleContext(now3)); 99 assertTrue("End [" + expectedEnd + "] wrong.", 100 expectedEnd.equals(actualEnd3)); 101 } 102 103 public void testWithTime() throws ParseException { 104 DayOfWeekSchedule schedule = new DayOfWeekSchedule(); 105 schedule.setOn("FRI"); 106 TimeSchedule time = new TimeSchedule(); 107 time.setOn("10:00"); 108 schedule.addValueSchedule(time); 109 110 ScheduleContext context = new ScheduleContext( 111 DateHelper.parseDate("1-nov-05")); 112 113 Interval nextDue = schedule.nextDue(context); 114 Date expected = DateHelper.parseDateTime("4-nov-05 10:00"); 115 assertEquals(expected, 116 nextDue.getFromDate()); 117 assertEquals(expected, 118 nextDue.getToDate()); 119 } 120 121 public void testDefaultFrom() throws ParseException { 122 Locale.setDefault(Locale.ENGLISH); 123 124 DayOfWeekSchedule schedule = new DayOfWeekSchedule(); 125 schedule.setTo("TUE"); 126 127 Interval result = schedule.nextDue( 128 new ScheduleContext(DateHelper.parseDate("3-mar-06"))); 129 130 assertEquals(new Interval( 131 DateHelper.parseDateTime("5-mar-06 00:00"), 132 DateHelper.parseDateTime("7-mar-06 23:59:59:999")), 133 result); 134 } 135 136 public void testDefaultTo() throws ParseException { 137 Locale.setDefault(Locale.ENGLISH); 138 139 DayOfWeekSchedule schedule = new DayOfWeekSchedule(); 140 schedule.setFrom("TUE"); 141 142 Interval result = schedule.nextDue( 143 new ScheduleContext(DateHelper.parseDate("8-mar-06"))); 144 145 assertEquals(new Interval( 146 DateHelper.parseDateTime("7-mar-06 00:00"), 147 DateHelper.parseDateTime("11-mar-06 23:59:59:999")), 148 result); 149 } 150 151 public void testInclusive() throws ParseException { 152 Locale.setDefault(Locale.ENGLISH); 153 154 DayOfWeekSchedule schedule = new DayOfWeekSchedule(); 155 schedule.setTo("TUE"); 156 157 Interval result = schedule.nextDue( 158 new ScheduleContext(DateHelper.parseDate("7-mar-06 10:15"))); 159 160 assertEquals(new Interval( 161 DateHelper.parseDateTime("5-mar-06 00:00"), 162 DateHelper.parseDateTime("7-mar-06 23:59:59:999")), 163 result); 164 165 } 166 } 167 | Popular Tags |