KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > schedules > schedules > DayOfMonthScheduleTest


1 /*
2  * Copyright (c) 2004, Rob Gordon.
3  */

4 package org.oddjob.schedules.schedules;
5
6 import java.text.DateFormat JavaDoc;
7 import java.text.ParseException JavaDoc;
8 import java.text.SimpleDateFormat JavaDoc;
9 import java.util.Date JavaDoc;
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 /**
18  *
19  * @author Rob Gordon.
20  */

21 public class DayOfMonthScheduleTest extends TestCase {
22 // private static Logger logger = Logger.getLogger(DayOfMonthScheduleTest.class);
23

24     DateFormat JavaDoc checkFormat;
25     DateFormat JavaDoc inputFormat;
26     
27     protected void setUp() {
28         checkFormat = new SimpleDateFormat JavaDoc("dd-MMM-yy HH:mm:ss:SSS");
29         inputFormat = new SimpleDateFormat JavaDoc("dd-MMM-yy HH:mm");
30     }
31
32     /**
33      * Test the next due time when a date is between the from
34      * and the to date.
35      */

36     public void testFromAndTo() throws ParseException JavaDoc {
37         DayOfMonthSchedule schedule = new DayOfMonthSchedule();
38         schedule.setFrom("5");
39         schedule.setTo("25");
40
41         Date JavaDoc now1 = inputFormat.parse("10-feb-2003 12:30");
42         Date JavaDoc expectedStart
43                 = checkFormat.parse("05-feb-2003 00:00:00:000");
44         Date JavaDoc actualStart1 = schedule.getStartDate(
45                 new ScheduleContext(now1));
46         assertEquals(expectedStart, actualStart1);
47         Date JavaDoc expectedEnd
48                 = checkFormat.parse("25-feb-2003 23:59:59:999");
49         Date JavaDoc actualEnd1 = schedule.getEndDate(
50                 new ScheduleContext(now1));
51         assertEquals(expectedEnd, actualEnd1);
52     }
53     
54     /**
55      * Test the next due time when a date is after
56      * the to time.
57      */

58     public void testAfter() throws ParseException JavaDoc {
59         DayOfMonthSchedule schedule = new DayOfMonthSchedule();
60         schedule.setFrom("5");
61         schedule.setTo("25");
62         
63         Date JavaDoc now1 = inputFormat.parse("26-feb-2003 12:30");
64         Date JavaDoc expectedStart
65                 = checkFormat.parse("05-mar-2003 00:00:00:000");
66         Date JavaDoc actualStart1 = schedule.getStartDate(
67                 new ScheduleContext(now1));
68         assertTrue("Start [" + actualStart1 + "] wrong.",
69                 expectedStart.equals(actualStart1));
70         Date JavaDoc expectedEnd
71                 = checkFormat.parse("25-mar-2003 23:59:59:999");
72         Date JavaDoc actualEnd1 = schedule.getEndDate(
73                 new ScheduleContext(now1));
74         assertTrue("End [" + actualEnd1 + "] wrong.",
75                 expectedEnd.equals(actualEnd1));
76     }
77     
78     /**
79      * Test the next due when the from and to span
80      * a month boundry.
81      */

82     public void testOverBoundry() throws ParseException JavaDoc {
83         DayOfMonthSchedule schedule = new DayOfMonthSchedule();
84         schedule.setFrom("25");
85         schedule.setTo("5");
86         
87         Date JavaDoc now1 = inputFormat.parse("26-feb-2003 12:30");
88         Date JavaDoc expectedStart
89                 = checkFormat.parse("25-feb-2003 00:00:00:000");
90         Date JavaDoc actualStart1 = schedule.getStartDate(
91                 new ScheduleContext(now1));
92         assertTrue("Start [" + actualStart1 + "] wrong.",
93                 expectedStart.equals(actualStart1));
94         Date JavaDoc expectedEnd
95                 = checkFormat.parse("5-mar-2003 23:59:59:999");
96         Date JavaDoc actualEnd1 = schedule.getEndDate(
97                 new ScheduleContext(now1));
98         assertTrue("End [" + actualEnd1 + "] wrong.",
99                 expectedEnd.equals(actualEnd1));
100
101         Date JavaDoc now2 = inputFormat.parse("24-feb-2003 12:30");
102         Date JavaDoc actualStart2 = schedule.getStartDate(
103                 new ScheduleContext(now2));
104         assertTrue("Start [" + actualStart2 + "] wrong.",
105                 expectedStart.equals(actualStart2));
106         Date JavaDoc actualEnd2 = schedule.getEndDate(
107                 new ScheduleContext(now2));
108         assertTrue("End [" + actualEnd2 + "] wrong.",
109                 expectedEnd.equals(actualEnd2));
110
111         Date JavaDoc now3 = inputFormat.parse("1-mar-2003 12:30");
112         Date JavaDoc actualStart3 = schedule.getStartDate(
113                 new ScheduleContext(now3));
114         assertTrue("Start [" + actualStart3 + "] wrong.",
115                 expectedStart.equals(actualStart3));
116         Date JavaDoc actualEnd3 = schedule.getEndDate(
117                 new ScheduleContext(now3));
118         assertTrue("End [" + expectedEnd + "] wrong.",
119                 expectedEnd.equals(actualEnd3));
120     }
121
122     /**
123      * Test last day of the month.
124      */

125     public void testLastDay() throws ParseException JavaDoc {
126         DayOfMonthSchedule schedule = new DayOfMonthSchedule();
127         schedule.setFrom("5");
128         schedule.setTo("0");
129         
130         Date JavaDoc 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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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