View Javadoc

1   /*
2    * Copyright (C) 2007 ETH Zurich
3    *
4    * This file is part of Fosstrak (www.fosstrak.org).
5    *
6    * Fosstrak is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License version 2.1, as published by the Free Software Foundation.
9    *
10   * Fosstrak is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with Fosstrak; if not, write to the Free
17   * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18   * Boston, MA  02110-1301  USA
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   * Tests for class Schedule.
36   * 
37   * @author Arthur van Dorp
38   */
39  public class ScheduleTest extends TestCase {
40  
41      static {
42          // provide the catalina.base property which is not available when the
43          // application is not deployed, i.e., when running tests
44          if (System.getenv("CATALINA_HOME") != null) {
45              System.setProperty("catalina.base", System.getenv("CATALINA_HOME"));
46          }
47      }
48  
49      /**
50       * Test for next scheduled year.
51       * 
52       * @throws ImplementationException
53       *             If an error in the implementation occurred.
54       * @throws SubscriptionControlsException
55       *             If an error in the schedule occurred.
56       */
57      public void testNextScheduledYear() throws ImplementationExceptionResponse, SubscriptionControlsExceptionResponse {
58          // scheduled time is 1.1. 01:00.00
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          // current time is 1.6.2006 00:00.00
68          GregorianCalendar start = new GregorianCalendar(2006, 5, 1, 0, 0, 0);
69  
70          // get next scheduled time
71          GregorianCalendar act = sched.nextScheduledTime(start);
72  
73          // expected time is 1.1.2007 01:00.00
74          GregorianCalendar exp = new GregorianCalendar(2007, 0, 1, 1, 0, 0);
75          assertEquals(exp, act);
76      }
77  
78      /**
79       * Test for next scheduled half hour.
80       * 
81       * @throws ImplementationException
82       *             If an error in the implementation occurred.
83       * @throws SubscriptionControlsException
84       *             If an error in the schedule occurred.
85       */
86      public void testNextScheduledHalfHour() throws ImplementationExceptionResponse,
87              SubscriptionControlsExceptionResponse {
88          // scheduled time is every half an hour
89          // always at the top and the bottom of every hour
90          QuerySchedule qs = new QuerySchedule();
91          qs.setSecond("0");
92          qs.setMinute("0,30");
93          Schedule sched = new Schedule(qs);
94  
95          // current time is 1.6.2006 00:00.00
96          GregorianCalendar start = new GregorianCalendar(2006, 5, 1, 0, 0, 0);
97  
98          // get next scheduled time
99          GregorianCalendar act = sched.nextScheduledTime(start);
100 
101         // this is already a valid time!
102         GregorianCalendar exp = (GregorianCalendar) start.clone();
103         assertEquals(exp, act);
104 
105         // add a second to current time
106         // current time is 1.6.2006 00:00.01
107         start.add(Calendar.SECOND, 1);
108 
109         // get next scheduled time
110         act = sched.nextScheduledTime(start);
111 
112         // expected time is 1.6.2006 00:30.00
113         exp = new GregorianCalendar(2006, 5, 1, 0, 30, 0);
114         assertEquals(exp, act);
115     }
116 
117     /**
118      * Test for next scheduled day of the week.
119      * 
120      * @throws ImplementationException
121      *             If an error in the implementation occurred.
122      * @throws SubscriptionControlsException
123      *             If an error in the schedule occurred.
124      */
125     public void testNextScheduledDayOfWeek() throws ImplementationExceptionResponse,
126             SubscriptionControlsExceptionResponse {
127         // scheduled time is every July, at a Thursday, 17:15.59
128         QuerySchedule qs = new QuerySchedule();
129         qs.setMonth("7");
130         qs.setDayOfMonth("[1-31],15,20"); // test duplicates!!
131         qs.setDayOfWeek("4");
132         qs.setHour("17");
133         qs.setMinute("15");
134         qs.setSecond("59");
135         Schedule sched = new Schedule(qs);
136 
137         // current time is 14.7.2006 15:00.00
138         GregorianCalendar start = new GregorianCalendar(2006, 6, 14, 15, 0, 0);
139 
140         // get next scheduled time
141         GregorianCalendar act = sched.nextScheduledTime(start);
142 
143         // expected time is 20.7.2006 17:15.59
144         GregorianCalendar exp = new GregorianCalendar(2006, 6, 20, 17, 15, 59);
145         assertEquals(exp, act);
146     }
147 
148     /**
149      * Test for next scheduled leap year (Schaltjahr).
150      * 
151      * @throws ImplementationException
152      *             If an error in the implementation occurred.
153      * @throws SubscriptionControlsException
154      *             If an error in the schedule occurred.
155      */
156     public void testNextScheduledLeapYear() throws ImplementationExceptionResponse,
157             SubscriptionControlsExceptionResponse {
158         // scheduled time is 29.2. 23:00.00 -> must be a leap year
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         // current time is 1.1.2001
168         GregorianCalendar start = new GregorianCalendar(2001, 0, 1);
169 
170         // get next scheduled time
171         GregorianCalendar act = sched.nextScheduledTime(start);
172 
173         // expected time is 29.2.2004 23:00.00
174         GregorianCalendar exp = new GregorianCalendar(2004, 1, 29, 23, 0, 0);
175         assertEquals(exp, act);
176     }
177 
178     /**
179      * Test for next scheduled minute (used in the query tests!).
180      * 
181      * @throws ImplementationException
182      *             If an error in the implementation occurred.
183      * @throws SubscriptionControlsException
184      *             If an error in the schedule occurred.
185      */
186     public void testNextScheduledMinute() throws ImplementationExceptionResponse, SubscriptionControlsExceptionResponse {
187         // scheduled time is always at top of a minute
188         QuerySchedule qs = new QuerySchedule();
189         qs.setSecond("0");
190         Schedule sched = new Schedule(qs);
191 
192         // current time is 8.2.2007 17:47.24
193         GregorianCalendar start = new GregorianCalendar(2007, 1, 8, 17, 47, 24);
194 
195         // get next scheduled time
196         GregorianCalendar act = sched.nextScheduledTime(start);
197 
198         // expected time is 8.2.2007 17:48.00
199         GregorianCalendar exp = new GregorianCalendar(2007, 1, 8, 17, 48, 00);
200         assertEquals(exp, act);
201     }
202 
203     /**
204      * Test for a complex next scheduled time.
205      * 
206      * @throws ImplementationException
207      *             If an error in the implementation occurred.
208      * @throws SubscriptionControlsException
209      *             If an error in the schedule occurred.
210      */
211     public void testComplexNextScheduledTime() throws ImplementationExceptionResponse,
212             SubscriptionControlsExceptionResponse {
213         // scheduled time is 1., 10., 20., or 30. of a month,
214         // at 07-11, 13-17, or 20 hours, 50.30 minutes
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         // current time is 2.1.2010 08:30.00
223         GregorianCalendar start = new GregorianCalendar(2010, 0, 2, 8, 30, 0);
224 
225         // get next scheduled time
226         GregorianCalendar act = sched.nextScheduledTime(start);
227 
228         // expected time is 10.1. 07:50.30
229         GregorianCalendar exp = new GregorianCalendar(2010, 0, 10, 7, 50, 30);
230         assertEquals(exp, act);
231 
232         // add one second to scheduled time
233         start = (GregorianCalendar) exp.clone();
234         start.add(Calendar.SECOND, 1);
235 
236         // get next scheduled time
237         act = sched.nextScheduledTime(start);
238 
239         // expected time is 10.1. 08:50.30
240         exp = new GregorianCalendar(2010, 0, 10, 8, 50, 30);
241         assertEquals(exp, act);
242 
243         // add some more time to scheduled time
244         start = (GregorianCalendar) exp.clone();
245         start.add(Calendar.HOUR, 3);
246         start.add(Calendar.SECOND, 1);
247 
248         // get next scheduled time
249         act = sched.nextScheduledTime(start);
250 
251         // expected time is 10.1. 13:50.30
252         exp = new GregorianCalendar(2010, 0, 10, 13, 50, 30);
253         assertEquals(exp, act);
254 
255         // add some more time to scheduled time
256         start = (GregorianCalendar) exp.clone();
257         start.add(Calendar.DAY_OF_MONTH, 1);
258 
259         // get next scheduled time
260         act = sched.nextScheduledTime(start);
261 
262         // expected time is 20.1. 07:50.30
263         exp = new GregorianCalendar(2010, 0, 20, 07, 50, 30);
264         assertEquals(exp, act);
265     }
266 
267     /**
268      * Test for another complex next scheduled time.
269      * 
270      * @throws ImplementationException
271      *             If an error in the implementation occurred.
272      * @throws SubscriptionControlsException
273      *             If an error in the schedule occurred.
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      * Test whether constructor throws an exception in case of an invalid
295      * QuerySchedule.
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             // success
310         }
311     }
312 }