1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.fosstrak.epcis.repository.query;
22
23 import java.util.Calendar;
24 import java.util.GregorianCalendar;
25
26 import junit.framework.TestCase;
27
28 import org.fosstrak.epcis.model.ImplementationException;
29 import org.fosstrak.epcis.model.QuerySchedule;
30 import org.fosstrak.epcis.model.SubscriptionControlsException;
31 import org.fosstrak.epcis.soap.ImplementationExceptionResponse;
32 import org.fosstrak.epcis.soap.SubscriptionControlsExceptionResponse;
33
34
35
36
37
38
39 public class ScheduleTest extends TestCase {
40
41 static {
42
43
44 if (System.getenv("CATALINA_HOME") != null) {
45 System.setProperty("catalina.base", System.getenv("CATALINA_HOME"));
46 }
47 }
48
49
50
51
52
53
54
55
56
57 public void testNextScheduledYear() throws ImplementationExceptionResponse, SubscriptionControlsExceptionResponse {
58
59 QuerySchedule qs = new QuerySchedule();
60 qs.setSecond("0");
61 qs.setMinute("0");
62 qs.setHour("1");
63 qs.setDayOfMonth("1");
64 qs.setMonth("1");
65 Schedule sched = new Schedule(qs);
66
67
68 GregorianCalendar start = new GregorianCalendar(2006, 5, 1, 0, 0, 0);
69
70
71 GregorianCalendar act = sched.nextScheduledTime(start);
72
73
74 GregorianCalendar exp = new GregorianCalendar(2007, 0, 1, 1, 0, 0);
75 assertEquals(exp, act);
76 }
77
78
79
80
81
82
83
84
85
86 public void testNextScheduledHalfHour() throws ImplementationExceptionResponse,
87 SubscriptionControlsExceptionResponse {
88
89
90 QuerySchedule qs = new QuerySchedule();
91 qs.setSecond("0");
92 qs.setMinute("0,30");
93 Schedule sched = new Schedule(qs);
94
95
96 GregorianCalendar start = new GregorianCalendar(2006, 5, 1, 0, 0, 0);
97
98
99 GregorianCalendar act = sched.nextScheduledTime(start);
100
101
102 GregorianCalendar exp = (GregorianCalendar) start.clone();
103 assertEquals(exp, act);
104
105
106
107 start.add(Calendar.SECOND, 1);
108
109
110 act = sched.nextScheduledTime(start);
111
112
113 exp = new GregorianCalendar(2006, 5, 1, 0, 30, 0);
114 assertEquals(exp, act);
115 }
116
117
118
119
120
121
122
123
124
125 public void testNextScheduledDayOfWeek() throws ImplementationExceptionResponse,
126 SubscriptionControlsExceptionResponse {
127
128 QuerySchedule qs = new QuerySchedule();
129 qs.setMonth("7");
130 qs.setDayOfMonth("[1-31],15,20");
131 qs.setDayOfWeek("4");
132 qs.setHour("17");
133 qs.setMinute("15");
134 qs.setSecond("59");
135 Schedule sched = new Schedule(qs);
136
137
138 GregorianCalendar start = new GregorianCalendar(2006, 6, 14, 15, 0, 0);
139
140
141 GregorianCalendar act = sched.nextScheduledTime(start);
142
143
144 GregorianCalendar exp = new GregorianCalendar(2006, 6, 20, 17, 15, 59);
145 assertEquals(exp, act);
146 }
147
148
149
150
151
152
153
154
155
156 public void testNextScheduledLeapYear() throws ImplementationExceptionResponse,
157 SubscriptionControlsExceptionResponse {
158
159 QuerySchedule qs = new QuerySchedule();
160 qs.setMonth("2");
161 qs.setDayOfMonth("29");
162 qs.setHour("23");
163 qs.setMinute("0");
164 qs.setSecond("0");
165 Schedule sched = new Schedule(qs);
166
167
168 GregorianCalendar start = new GregorianCalendar(2001, 0, 1);
169
170
171 GregorianCalendar act = sched.nextScheduledTime(start);
172
173
174 GregorianCalendar exp = new GregorianCalendar(2004, 1, 29, 23, 0, 0);
175 assertEquals(exp, act);
176 }
177
178
179
180
181
182
183
184
185
186 public void testNextScheduledMinute() throws ImplementationExceptionResponse, SubscriptionControlsExceptionResponse {
187
188 QuerySchedule qs = new QuerySchedule();
189 qs.setSecond("0");
190 Schedule sched = new Schedule(qs);
191
192
193 GregorianCalendar start = new GregorianCalendar(2007, 1, 8, 17, 47, 24);
194
195
196 GregorianCalendar act = sched.nextScheduledTime(start);
197
198
199 GregorianCalendar exp = new GregorianCalendar(2007, 1, 8, 17, 48, 00);
200 assertEquals(exp, act);
201 }
202
203
204
205
206
207
208
209
210
211 public void testComplexNextScheduledTime() throws ImplementationExceptionResponse,
212 SubscriptionControlsExceptionResponse {
213
214
215 QuerySchedule qs = new QuerySchedule();
216 qs.setSecond("30");
217 qs.setMinute("50");
218 qs.setHour("[7-11],[13-17],20");
219 qs.setDayOfMonth("1,10,20,30");
220 Schedule sched = new Schedule(qs);
221
222
223 GregorianCalendar start = new GregorianCalendar(2010, 0, 2, 8, 30, 0);
224
225
226 GregorianCalendar act = sched.nextScheduledTime(start);
227
228
229 GregorianCalendar exp = new GregorianCalendar(2010, 0, 10, 7, 50, 30);
230 assertEquals(exp, act);
231
232
233 start = (GregorianCalendar) exp.clone();
234 start.add(Calendar.SECOND, 1);
235
236
237 act = sched.nextScheduledTime(start);
238
239
240 exp = new GregorianCalendar(2010, 0, 10, 8, 50, 30);
241 assertEquals(exp, act);
242
243
244 start = (GregorianCalendar) exp.clone();
245 start.add(Calendar.HOUR, 3);
246 start.add(Calendar.SECOND, 1);
247
248
249 act = sched.nextScheduledTime(start);
250
251
252 exp = new GregorianCalendar(2010, 0, 10, 13, 50, 30);
253 assertEquals(exp, act);
254
255
256 start = (GregorianCalendar) exp.clone();
257 start.add(Calendar.DAY_OF_MONTH, 1);
258
259
260 act = sched.nextScheduledTime(start);
261
262
263 exp = new GregorianCalendar(2010, 0, 20, 07, 50, 30);
264 assertEquals(exp, act);
265 }
266
267
268
269
270
271
272
273
274
275 public void testLeapYearDayOfWeekNextScheduledTime() throws ImplementationExceptionResponse,
276 SubscriptionControlsExceptionResponse {
277
278 QuerySchedule qs = new QuerySchedule();
279 qs.setSecond("30");
280 qs.setMinute("50");
281 qs.setHour("[7-11],[13-17],20");
282 qs.setDayOfMonth("29");
283 qs.setMonth("2");
284 qs.setDayOfWeek("1");
285 Schedule schedule = new Schedule(qs);
286
287 GregorianCalendar start = schedule.nextScheduledTime(new GregorianCalendar(2001, 0, 1, 0, 0, 0));
288 GregorianCalendar result = schedule.nextScheduledTime(start);
289 GregorianCalendar expected = new GregorianCalendar(2016, 1, 29, 7, 50, 30);
290 assertEquals(result, expected);
291 }
292
293
294
295
296
297 public void testInvalidQuerySchedule() {
298
299 QuerySchedule qsImpossibleSchedule = new QuerySchedule();
300 qsImpossibleSchedule.setSecond("0");
301 qsImpossibleSchedule.setMinute("0");
302 qsImpossibleSchedule.setHour("0");
303 qsImpossibleSchedule.setDayOfMonth("31");
304 qsImpossibleSchedule.setMonth("6");
305 try {
306 new Schedule(qsImpossibleSchedule);
307 fail("entering an invalid QuerySchedule should raise a SubscriptionControlsException");
308 } catch (SubscriptionControlsExceptionResponse e) {
309
310 }
311 }
312 }