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.Schedule; 15 import org.oddjob.schedules.ScheduleContext; 16 17 21 public class OccurenceScheduleTest extends TestCase { 22 23 static DateFormat checkFormat = new SimpleDateFormat ("dd-MMM-yy HH:mm:ss:SSS"); 24 static DateFormat inputFormat = new SimpleDateFormat ("dd-MMM-yy HH:mm"); 25 26 public void testBasic() throws ParseException { 27 OccurrenceSchedule schedule = new OccurrenceSchedule(); 28 schedule.setOccurrence("2"); 29 30 MockSchedule child = new MockSchedule(); 31 child.setResults(new Interval[] { 32 new Interval( 33 inputFormat.parse("10-feb-2004 10:00"), 34 inputFormat.parse("10-feb-2004 14:00")), 35 new Interval( 36 inputFormat.parse("11-feb-2004 10:00"), 37 inputFormat.parse("11-feb-2004 14:00")) 38 }); 39 schedule.addValueSchedule(child); 40 Date now1 = inputFormat.parse("10-feb-2004 12:30"); 41 Interval expected = new Interval( 42 inputFormat.parse("11-feb-2004 10:00"), 43 inputFormat.parse("11-feb-2004 14:00")); 44 Interval actual = schedule.nextDue( 45 new ScheduleContext(now1)); 46 assertTrue("[" + actual+ "] wrong.", 47 expected.equals(actual)); 48 } 49 50 public void testOutsideLimits() throws ParseException { 51 OccurrenceSchedule schedule = new OccurrenceSchedule(); 52 schedule.setOccurrence("2"); 53 54 MockSchedule child = new MockSchedule(); 55 child.setResults(new Interval[] { 56 new Interval( 57 inputFormat.parse("10-feb-2004 10:00"), 58 inputFormat.parse("10-feb-2004 14:00")), 59 new Interval( 60 inputFormat.parse("11-feb-2004 10:00"), 61 inputFormat.parse("11-feb-2004 14:00")) 62 }); 63 schedule.addValueSchedule(child); 64 Date now1 = inputFormat.parse("10-feb-2004 12:30"); 65 Interval expected = null; 66 schedule.setLimits(new Interval( 67 inputFormat.parse("10-feb-2004 09:00"), 68 inputFormat.parse("10-feb-2004 15:00"))); 69 Interval actual = schedule.nextDue( 70 new ScheduleContext(now1)); 71 assertTrue("[" + actual+ "] wrong.", 72 actual == expected); 73 } 74 75 private static class MockSchedule implements Schedule { 76 Interval[] results; 77 int next; 78 public void setResults(Interval[] results) { 79 this.results = results; 80 next = 0; 81 } 82 public void setLimits(Interval limits) {} 83 public Interval nextDue(ScheduleContext context) { 84 if (results == null) { 85 return null; 86 } 87 if (next < results.length) { 88 return results[next++]; 89 } 90 return null; 91 } 92 } 93 } 94 | Popular Tags |