root/trunk/test/java/org/jcoderz/commons/types/PeriodTest.java

Revision 1011, 33.3 kB (checked in by amandel, 4 years ago)

Aligned svn keyword settings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 *
4 * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 *    * Redistributions of source code must retain the above copyright
11 *      notice, this list of conditions and the following disclaimer.
12 *    * Redistributions in binary form must reproduce the above
13 *      copyright notice, this list of conditions and the following
14 *      disclaimer in the documentation and/or other materials
15 *      provided with the distribution.
16 *    * Neither the name of the jCoderZ.org Project nor the names of
17 *      its contributors may be used to endorse or promote products
18 *      derived from this software without specific prior written
19 *      permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33package org.jcoderz.commons.types;
34
35import java.text.ParseException;
36import java.util.Calendar;
37import junit.framework.TestCase;
38import org.jcoderz.commons.ArgumentMalformedException;
39
40
41/**
42 * Implements test-cases for the {@link org.jcoderz.commons.types.Period}
43 * class.
44 *
45 * @author Michael Rumpf
46 */
47public class PeriodTest
48      extends TestCase
49{
50   /** One day in milli-seconds: 24*60*60*1000. */
51   public static final long ONE_DAY_IN_MSEC = Date.MILLIS_PER_DAY;
52
53   /** A constant for 10 msec. */
54   public static final int TEN_MSEC = 10;
55   /** A constant for 20 msec. */
56   public static final int TWENTY_MSEC = 20;
57   /** A constant for 30 msec. */
58   public static final int THIRTY_MSEC = 30;
59
60   private static final String UNEXPECTED_EMPTY_UNION
61         = "The defined periods should not return an empty union set!";
62   private static final String UNEXPECTED_EMPTY_INTERSECTION
63         = "The defined periods should not return an empty "
64            + "intersection set!";
65   private static final String EXPECTED_EMPTY_UNION
66         = "The defined periods should return an empty union set!";
67   private static final String EXPECTED_EMPTY_INTERSECTION
68         = "The defined periods should return an empty intersection set!";
69
70   /**
71    * Tries to create a period with the largest possible
72    * start and end dates.
73    */
74   public void testLargestPeriod ()
75   {
76      try
77      {
78         Period.createDayPeriod(Date.OLD_DATE, Date.FUTURE_DATE);
79      }
80      catch (Exception ex)
81      {
82         fail("Unexpected exception occured!");
83         ex.printStackTrace();
84      }
85   }
86
87   /**
88    * Test the constructor argument combinations and milli-second resolution.
89    */
90   public void testFactoryMethods ()
91   {
92      badFactory(null, null);
93      final Date date = new Date(System.currentTimeMillis());
94      badFactory(null, date);
95      badFactory(date, null);
96      final Date later = Date.nowPlus(1);
97      badFactory(later, date);
98
99      try
100      {
101         checkStartEnd(Date.now());
102         checkStartEnd(Date.fromString("2004-09-03T12:11:33.785Z"));
103      }
104      catch (ParseException e)
105      {
106         fail("Testcase internal error. Caught a ParseException "
107               + e.getMessage());
108      }
109   }
110
111   /**
112    * Test the constructor argument combinations and milli-second resolution.
113    */
114   public void testDayFactoryMethod ()
115   {
116      badDayFactory(null, null);
117      final Date date = new Date(System.currentTimeMillis());
118      badDayFactory(null, date);
119      badDayFactory(date, null);
120      final Date later = Date.nowPlus(ONE_DAY_IN_MSEC);
121      badDayFactory(later, date);
122   }
123
124   /**
125    * Tests the method {@link Period#createDayPeriod(Date)}.
126    */
127   public void testCreateDayPeriod ()
128   {
129      final Period a = Period.createDayPeriod(Date.now(), Date.now());
130      final Period b = Period.createDayPeriod(Date.now());
131      assertEquals("createDayPeriod(Date, Date)=" + a + " should be equal to "
132            + " createDayPeriod(Date)=" + b, a, b);
133   }
134
135   /**
136    * Tests the method {@link Period#createMonthPeriod(Date)}.
137    */
138   public void testCreateMonthPeriod ()
139   {
140      final Date date = dateFromString("2005-10-06T11:11:11.111Z");
141      final Period shouldBePeriod = Period.createPeriod(
142            dateFromString("2005-10-01T00:00:00.000Z"),
143            dateFromString("2005-10-31T23:59:59.999Z"));
144      final Period p = Period.createMonthPeriod(date);
145      assertEquals("createMonthPeriod: " + p + ", should be " + shouldBePeriod
146            + ", date " + date, p, shouldBePeriod);
147   }
148
149   /**
150    * Test the union method of the Period class in milli-second resolution.
151    */
152   public void testPeriodUnion ()
153   {
154      final Date now = Date.now();
155
156      //  a1 |-----| b1
157      //     a2 |-----| b2    -->   a1 |--------| b2
158      Date a1 = now;
159      Date b1 = new Date(now.getTime() + TWENTY_MSEC);
160      Date a2 = new Date(now.getTime() + TEN_MSEC);
161      Date b2 = new Date(now.getTime() + THIRTY_MSEC);
162      Period p = createUnion(a1, b1, a2, b2);
163      assertNotNull(UNEXPECTED_EMPTY_UNION, p);
164      assertPeriode(a1, b2, p);
165
166      //  a1 |-----| b1
167      //        a2 |-----| b2    -->   a1 |----------| b2
168      a1 = now;
169      a2 = new Date(now.getTime() + TEN_MSEC);
170      b1 = new Date(now.getTime() + TEN_MSEC);
171      b2 = new Date(now.getTime() + THIRTY_MSEC);
172      p = createUnion(a1, b1, a2, b2);
173      assertNotNull(UNEXPECTED_EMPTY_UNION, p);
174      assertPeriode(a1, b2, p);
175
176      //     a1 |-----| b1    -->   a2 |--------| b1
177      //  a2 |-----| b2
178      a1 = new Date(now.getTime() + TEN_MSEC);
179      b1 = new Date(now.getTime() + THIRTY_MSEC);
180      a2 = now;
181      b2 = new Date(now.getTime() + TWENTY_MSEC);
182      p = createUnion(a1, b1, a2, b2);
183      assertNotNull(UNEXPECTED_EMPTY_UNION, p);
184      assertPeriode(a2, b1, p);
185
186      //        a1 |-----| b1    -->   a2 |----------| b1
187      //  a2 |-----| b2
188      a1 = new Date(now.getTime() + TEN_MSEC);
189      b1 = new Date(now.getTime() + THIRTY_MSEC);
190      a2 = now;
191      b2 = new Date(now.getTime() + TEN_MSEC);
192      p = createUnion(a1, b1, a2, b2);
193      assertNotNull(UNEXPECTED_EMPTY_UNION, p);
194      assertPeriode(a2, b1, p);
195   }
196
197   /**
198    * Test the union method of the Period class in milli-second resolution.
199    */
200   public void testEmptyPeriodUnion ()
201   {
202      final Date now = Date.now();
203
204      //  a1 |-----| b1
205      //             a2 |-----| b2    -->   null
206      Date a1 = now;
207      Date b1 = new Date(now.getTime() + TEN_MSEC);
208      Date a2 = new Date(now.getTime() + TWENTY_MSEC);
209      Date b2 = new Date(now.getTime() + THIRTY_MSEC);
210      Period p = createUnion(a1, b1, a2, b2);
211      assertNull(EXPECTED_EMPTY_UNION, p);
212
213      //             a1 |-----| b1    -->   null
214      //  a2 |-----| b2
215      a2 = now;
216      a1 = new Date(now.getTime() + TWENTY_MSEC);
217      b1 = new Date(now.getTime() + THIRTY_MSEC);
218      b2 = new Date(now.getTime() + TEN_MSEC);
219      p = createUnion(a1, b1, a2, b2);
220      assertNull(EXPECTED_EMPTY_UNION, p);
221   }
222
223   /**
224    * Test the union method of the Period class in day resolution.
225    */
226   public void testDayPeriodUnion ()
227   {
228      //  a1 |-----| b1
229      //             a2 |-----| b2    -->   null
230      Date a1 = Date.now();
231      Date b1 = new Date(a1.getTime() + TEN_MSEC);
232      Date a2 = new Date(a1.getTime() + ONE_DAY_IN_MSEC + TWENTY_MSEC);
233      Date b2 = new Date(a1.getTime() + ONE_DAY_IN_MSEC + THIRTY_MSEC);
234      Period p = createUnion(a1, b1, a2, b2);
235      assertNull(EXPECTED_EMPTY_UNION, p);
236
237      //  a1 |-----| b1
238      //     a2 |-----| b2    -->   a1 |--------| b2
239      a1 = Date.now();
240      b1 = new Date(a1.getTime() + ONE_DAY_IN_MSEC + TWENTY_MSEC);
241      a2 = new Date(a1.getTime() + ONE_DAY_IN_MSEC + TEN_MSEC);
242      b2 = new Date(a1.getTime() + ONE_DAY_IN_MSEC + THIRTY_MSEC);
243      p = createDayUnion(a1, b1, a2, b2);
244      assertNotNull(UNEXPECTED_EMPTY_UNION, p);
245      assertPeriode(getMinDayPeriod(a1), getMaxDayPeriod(b2), p);
246
247      //  a1 |-----| b1
248      //        a2 |-----| b2    -->   a1 |----------| b2
249      a1 = Date.now();
250      a2 = new Date(a1.getTime() + ONE_DAY_IN_MSEC + TEN_MSEC);
251      b1 = new Date(a1.getTime() + ONE_DAY_IN_MSEC + TEN_MSEC);
252      b2 = new Date(a1.getTime() + ONE_DAY_IN_MSEC + THIRTY_MSEC);
253      p = createDayUnion(a1, b1, a2, b2);
254      assertNotNull(UNEXPECTED_EMPTY_UNION, p);
255      assertPeriode(getMinDayPeriod(a1), getMaxDayPeriod(b2), p);
256
257      //     a1 |-----| b1    -->   a2 |--------| b1
258      //  a2 |-----| b2
259      a2 = Date.now();
260      a1 = new Date(a2.getTime() + ONE_DAY_IN_MSEC + TEN_MSEC);
261      b2 = new Date(a2.getTime() + ONE_DAY_IN_MSEC + TWENTY_MSEC);
262      b1 = new Date(a2.getTime() + ONE_DAY_IN_MSEC + THIRTY_MSEC);
263      p = createDayUnion(a1, b1, a2, b2);
264      assertNotNull(UNEXPECTED_EMPTY_UNION, p);
265      assertPeriode(getMinDayPeriod(a2), getMaxDayPeriod(b1), p);
266
267      //        a1 |-----| b1    -->   a2 |----------| b1
268      //  a2 |-----| b2
269      b2 = new Date(a2.getTime() + ONE_DAY_IN_MSEC + TEN_MSEC);
270      p = createDayUnion(a1, b1, a2, b2);
271      assertNotNull(UNEXPECTED_EMPTY_UNION, p);
272      assertPeriode(getMinDayPeriod(a2), getMaxDayPeriod(b1), p);
273
274      //             a1 |-----| b1    -->   null
275      //  a2 |-----| b2
276      a1 = new Date(a2.getTime() + ONE_DAY_IN_MSEC + TWENTY_MSEC);
277      b2 = new Date(a2.getTime() + TWENTY_MSEC);
278      p = createDayUnion(a1, b1, a2, b2);
279      assertNull(EXPECTED_EMPTY_UNION, p);
280   }
281
282   /**
283    * Test the intersection method of the Period class in milli-second
284    * resolution.
285    */
286   public void testPeriodIntersection ()
287   {
288      //  a1 |-----| b1
289      //             a2 |-----| b2    -->   null
290      Date a1 = Date.now();
291      Date b1 = new Date(a1.getTime() + TEN_MSEC);
292      Date a2 = new Date(a1.getTime() + TWENTY_MSEC);
293      Date b2 = new Date(a1.getTime() + THIRTY_MSEC);
294      Period p1 = Period.createPeriod(a1, b1);
295      Period p2 = Period.createPeriod(a2, b2);
296
297      Period p = p1.intersection(p2);
298      assertNull(EXPECTED_EMPTY_INTERSECTION, p);
299
300      //  a1 |-----| b1
301      //     a2 |-----| b2    -->   a2 |--| b1
302      a2 = new Date(a1.getTime() + TEN_MSEC);
303      b1 = new Date(a1.getTime() + TWENTY_MSEC);
304      p1 = Period.createPeriod(a1, b1);
305      p2 = Period.createPeriod(a2, b2);
306
307      p = p1.intersection(p2);
308      assertNotNull("Periods do intersect!" + p1 + ", " + p2, p);
309      assertPeriode(a2, b1, p);
310      assertNotNull("Period does intersect." + p2 + ", " + p2,
311            p2.intersection(p2));
312
313      //  a1 |-----| b1
314      //        a2 |-----| b2    -->   a2 | b1
315      a1 = Date.now();
316      b1 = new Date(a1.getTime() + TEN_MSEC);
317      b2 = new Date(a1.getTime() + THIRTY_MSEC);
318      p1 = Period.createPeriod(a1, b1);
319      p2 = Period.createPeriod(a2, b2);
320
321      p = p1.intersection(p2);
322      assertNotNull(UNEXPECTED_EMPTY_INTERSECTION, p);
323      assertPeriode(a2, b1, p);
324
325      //     a1 |-----| b1    -->   a1 |--| b2
326      //  a2 |-----| b2
327      a2 = Date.now();
328      a1 = new Date(a2.getTime() + TEN_MSEC);
329      b2 = new Date(a2.getTime() + TWENTY_MSEC);
330      b1 = new Date(a2.getTime() + THIRTY_MSEC);
331      p1 = Period.createPeriod(a1, b1);
332      p2 = Period.createPeriod(a2, b2);
333
334      p = p1.intersection(p2);
335      assertNotNull(UNEXPECTED_EMPTY_INTERSECTION, p);
336      assertPeriode(a1, b2, p);
337
338      //        a1 |-----| b1    -->   a1 | b2
339      //  a2 |-----| b2
340      b2 = new Date(a2.getTime() + TEN_MSEC);
341      p1 = Period.createPeriod(a1, b1);
342      p2 = Period.createPeriod(a2, b2);
343
344      p = p1.intersection(p2);
345      assertNotNull(UNEXPECTED_EMPTY_INTERSECTION, p);
346      assertPeriode(a1, b2, p);
347
348      //             a1 |-----| b1    -->   null
349      //  a2 |-----| b2
350      a2 = Date.now();
351      b2 = new Date(a2.getTime() + TEN_MSEC);
352      a1 = new Date(a2.getTime() + TWENTY_MSEC);
353      b1 = new Date(a2.getTime() + THIRTY_MSEC);
354      p1 = Period.createPeriod(a1, b1);
355      p2 = Period.createPeriod(a2, b2);
356
357      p = p1.intersection(p2);
358      assertNull(EXPECTED_EMPTY_INTERSECTION, p);
359   }
360
361   /**
362    * Test the intersection method of the Period class in day resolution.
363    */
364   public void testDayPeriodIntersection ()
365   {
366      //  a1 |-----| b1
367      //             a2 |-----| b2    -->   null
368      Date a1 = Date.now();
369      Date b1 = new Date(a1.getTime() + TEN_MSEC);
370      Date a2 = new Date(a1.getTime() + ONE_DAY_IN_MSEC + TWENTY_MSEC);
371      Date b2 = new Date(a1.getTime() + ONE_DAY_IN_MSEC + THIRTY_MSEC);
372      Period p1 = Period.createDayPeriod(a1, b1);
373      Period p2 = Period.createDayPeriod(a2, b2);
374
375      Period p = p1.intersection(p2);
376      assertNull(EXPECTED_EMPTY_INTERSECTION, p);
377
378      //  a1 |-----| b1
379      //     a2 |-----| b2    -->   a2 |--| b1
380      a2 = new Date(a1.getTime() + TEN_MSEC);
381      b1 = new Date(a1.getTime() + TWENTY_MSEC);
382      p1 = Period.createDayPeriod(a1, b1);
383      p2 = Period.createDayPeriod(a2, b2);
384
385      p = p1.intersection(p2);
386      assertNotNull(UNEXPECTED_EMPTY_INTERSECTION, p);
387      assertPeriode(getMinDayPeriod(a2), getMaxDayPeriod(b1), p);
388
389      //  a1 |-----| b1
390      //        a2 |-----| b2    -->   a2 | b1
391      a1 = Date.now();
392      b1 = new Date(a1.getTime() + TEN_MSEC);
393      b2 = new Date(a1.getTime() + THIRTY_MSEC);
394      p1 = Period.createDayPeriod(a1, b1);
395      p2 = Period.createDayPeriod(a2, b2);
396
397      p = p1.intersection(p2);
398      assertNotNull(UNEXPECTED_EMPTY_INTERSECTION, p);
399      assertPeriode(getMinDayPeriod(a2), getMaxDayPeriod(b1), p);
400
401      //     a1 |-----| b1    -->   a1 |--| b2
402      //  a2 |-----| b2
403      a2 = Date.now();
404      a1 = new Date(a2.getTime() + TEN_MSEC);
405      b2 = new Date(a2.getTime() + TWENTY_MSEC);
406      b1 = new Date(a2.getTime() + THIRTY_MSEC);
407      p1 = Period.createDayPeriod(a1, b1);
408      p2 = Period.createDayPeriod(a2, b2);
409
410      p = p1.intersection(p2);
411      assertNotNull(UNEXPECTED_EMPTY_INTERSECTION, p);
412      assertPeriode(getMinDayPeriod(a1), getMaxDayPeriod(b2), p);
413
414      //        a1 |-----| b1    -->   a1 | b2
415      //  a2 |-----| b2
416      b2 = new Date(a2.getTime() + TEN_MSEC);
417      p1 = Period.createDayPeriod(a1, b1);
418      p2 = Period.createDayPeriod(a2, b2);
419
420      p = p1.intersection(p2);
421      assertNotNull(UNEXPECTED_EMPTY_INTERSECTION, p);
422      assertPeriode(getMinDayPeriod(a1), getMaxDayPeriod(b2), p);
423
424      //             a1 |-----| b1    -->   null
425      //  a2 |-----| b2
426      a2 = Date.now();
427      b2 = new Date(a2.getTime() + TEN_MSEC);
428      a1 = new Date(a2.getTime() + ONE_DAY_IN_MSEC + TWENTY_MSEC);
429      b1 = new Date(a2.getTime() + ONE_DAY_IN_MSEC + THIRTY_MSEC);
430      p1 = Period.createDayPeriod(a1, b1);
431      p2 = Period.createDayPeriod(a2, b2);
432
433      p = p1.intersection(p2);
434      assertNull(EXPECTED_EMPTY_INTERSECTION, p);
435   }
436
437   /**
438    * Test the isIncluded() method of the Period class in milli-second
439    * resolution.
440    */
441   public void testIsIncludedPeriod ()
442   {
443      final Date a1 = Date.now();
444      final Date b1 = new Date(a1.getTime() + THIRTY_MSEC);
445      final Date c  = new Date(a1.getTime() + TEN_MSEC);
446      final Period p = Period.createDayPeriod(a1, b1);
447      assertTrue("The timestamp '"
448            + c + "' does not fall into the period:" + p, p.isIncluded(c));
449   }
450
451   /**
452    * Test the isIncluded() method of the Period class in day resolution.
453    */
454   public void testIsIncludedDayPeriod ()
455   {
456      final Date a1 = Date.now();
457      final Date b1 = new Date(a1.getTime() + THIRTY_MSEC);
458      final Date c  = new Date(a1.getTime() + TEN_MSEC);
459      final Period p = Period.createDayPeriod(a1, b1);
460      assertTrue("The timestamp '"
461            + c + "' does not fall into the period:" + p, p.isIncluded(c));
462   }
463
464   /**
465    * Test the {@link Period#getNextPeriodStartTime()} and
466    * {@link Period#getPrevPeriodEndTime()} methods.
467    */
468   public void testGetNextPrevPeriodTime ()
469   {
470      final Date start = Date.now();
471      final Date end = start.plus(Date.MILLIS_PER_DAY);
472      Date shouldBeNext = end.plus(1);
473      Date shouldBePrev = start.minus(1);
474
475      // Regular period
476      Period p = Period.createPeriod(start, end);
477
478      assertEquals("StartTime of the next period is not correct.",
479            shouldBeNext, p.getNextPeriodStartTime());
480      assertEquals("EndTime of the previous period is not correct.",
481            shouldBePrev, p.getPrevPeriodEndTime());
482
483      // Day based period
484      p = Period.createDayPeriod(start, end);
485
486      shouldBeNext = p.getEndTime().plus(1);
487      shouldBePrev = p.getStartTime().minus(1);
488
489      assertEquals("StartTime of the next period is not correct.",
490            shouldBeNext, p.getNextPeriodStartTime());
491      assertEquals("EndTime of the previous period is not correct.",
492            shouldBePrev, p.getPrevPeriodEndTime());
493   }
494
495   /**
496    * Tests the method {@link Period#next()}.
497    */
498   public void testNext ()
499   {
500      final Period org = Period.createPeriod(Date.now(),
501            Date.nowPlus(Date.MILLIS_PER_DAY));
502      final Period next = org.next();
503      assertTrue("Start time of the next period " + next.getStartTime()
504            + " should be equal to the NextPeriodStartTime "
505            + org.getNextPeriodStartTime(),
506            org.getNextPeriodStartTime().equals(next.getStartTime()));
507      assertTrue("End time of the next period " + next.getStartTime()
508            + " should be equal to the NextPeriodStartTime + duration "
509            + org.getNextPeriodStartTime().plus(org.duration()),
510            org.getNextPeriodStartTime().plus(org.duration()).equals(
511                  next.getEndTime()));
512   }
513
514   /**
515    * Tests the method {@link Period#previous()}.
516    */
517   public void testPrevious ()
518   {
519      final Period org = Period.createPeriod(Date.now(),
520            Date.nowPlus(Date.MILLIS_PER_DAY));
521      final Period previous = org.previous();
522      assertTrue("End time of the previous period " + previous.getEndTime()
523            + " should be equal to the PrevPeriodEndTime "
524            + org.getPrevPeriodEndTime(),
525            org.getPrevPeriodEndTime().equals(previous.getEndTime()));
526      assertTrue("Start time of the previous period " + previous.getStartTime()
527            + " should be equal to the PrevPeriodEndTime - duration "
528            + org.getPrevPeriodEndTime().minus(org.duration()),
529            org.getPrevPeriodEndTime().minus(org.duration()).equals(
530                  previous.getStartTime()));
531   }
532
533   /**
534    * Tests the methods {@link Period#nextHour()} and
535    * {@link Period#nextHour(Date)}.
536    */
537   public void testNextHour ()
538   {
539      try
540      {
541         // regular
542         Date timeDate = Date.fromString("2004-09-04T10:04:22.788Z");
543         Period shouldBePeriod = Period.createPeriod(
544               Date.fromString("2004-09-04T11:00:00Z"),
545               Date.fromString("2004-09-04T11:59:59.999Z"));
546         checkNextHour(timeDate, shouldBePeriod);
547
548         // end of a day
549         timeDate = Date.fromString("2005-10-05T23:04:22.788Z");
550         shouldBePeriod = Period.createPeriod(
551               Date.fromString("2005-10-06T00:00:00.000Z"),
552               Date.fromString("2005-10-06T00:59:59.999Z"));
553         checkNextHour(timeDate, shouldBePeriod);
554
555         // end of a month
556         timeDate = Date.fromString("2005-10-31T23:04:22.788Z");
557         shouldBePeriod = Period.createPeriod(
558               Date.fromString("2005-11-01T00:00:00.000Z"),
559               Date.fromString("2005-11-01T00:59:59.999Z"));
560         checkNextHour(timeDate, shouldBePeriod);
561      }
562      catch (ParseException e)
563      {
564         fail("Testcase internal error. Got a ParseException "
565               + e.getMessage());
566      }
567   }
568
569   /**
570    * Tests the methods {@link Period#previousHour()} and
571    * {@link Period#previousHour(Date)}.
572    */
573   public void testPreviousHour ()
574   {
575      try
576      {
577         // regular
578         Date timeDate = Date.fromString("2004-09-04T10:04:22.788Z");
579         Period shouldBePeriod = Period.createPeriod(
580               Date.fromString("2004-09-04T09:00:00Z"),
581               Date.fromString("2004-09-04T09:59:59.999Z"));
582         checkPreviousHour(timeDate, shouldBePeriod);
583
584         // first hour of a day
585         timeDate = Date.fromString("2005-10-05T00:04:22.788Z");
586         shouldBePeriod = Period.createPeriod(
587               Date.fromString("2005-10-04T23:00:00.000Z"),
588               Date.fromString("2005-10-04T23:59:59.999Z"));
589         checkPreviousHour(timeDate, shouldBePeriod);
590
591         // first day of a month
592         timeDate = Date.fromString("2005-11-01T00:04:22.788Z");
593         shouldBePeriod = Period.createPeriod(
594               Date.fromString("2005-10-31T23:00:00.000Z"),
595               Date.fromString("2005-10-31T23:59:59.999Z"));
596         checkPreviousHour(timeDate, shouldBePeriod);
597      }
598      catch (ParseException e)
599      {
600         fail("Testcase internal error. Got a ParseException "
601               + e.getMessage());
602      }
603   }
604
605   /**
606    * Tests the methods {@link Period#nextDay()} and
607    * {@link Period#nextDay(Date)}.
608    */
609   public void testNextDay ()
610   {
611      try
612      {
613         // regular
614         Date timeDate = Date.fromString("2004-09-04T10:04:22.788Z");
615         Period shouldBePeriod = Period.createPeriod(
616               Date.fromString("2004-09-05T00:00:00Z"),
617               Date.fromString("2004-09-05T23:59:59.999Z"));
618         checkNextDay(timeDate, shouldBePeriod);
619
620         // end of a month
621         timeDate = Date.fromString("2005-10-31T22:04:22.788Z");
622         shouldBePeriod = Period.createPeriod(
623               Date.fromString("2005-11-01T00:00:00.000Z"),
624               Date.fromString("2005-11-01T23:59:59.999Z"));
625         checkNextDay(timeDate, shouldBePeriod);
626      }
627      catch (ParseException e)
628      {
629         fail("Testcase internal error. Got a ParseException "
630               + e.getMessage());
631      }
632   }
633
634   /**
635    * Tests the methods {@link Period#previousDay()} and
636    * {@link Period#previousDay(Date)}.
637    */
638   public void testPreviousDay ()
639   {
640      try
641      {
642         // regular
643         Date timeDate = Date.fromString("2004-09-04T10:04:22.788Z");
644         Period shouldBePeriod = Period.createPeriod(
645               Date.fromString("2004-09-03T00:00:00Z"),
646               Date.fromString("2004-09-03T23:59:59.999Z"));
647         checkPreviousDay(timeDate, shouldBePeriod);
648
649         // first day of a month
650         timeDate = Date.fromString("2005-11-01T11:04:22.788Z");
651         shouldBePeriod = Period.createPeriod(
652               Date.fromString("2005-10-31T00:00:00.000Z"),
653               Date.fromString("2005-10-31T23:59:59.999Z"));
654         checkPreviousDay(timeDate, shouldBePeriod);
655      }
656      catch (ParseException e)
657      {
658         fail("Testcase internal error. Got a ParseException "
659               + e.getMessage());
660      }
661   }
662
663   /**
664    * Tests the methods {@link Period#nextMonth()} and
665    * {@link Period#nextMonth(Date)}.
666    */
667   public void testNextMonth ()
668   {
669      // regular
670      Date date = dateFromString("2005-10-06T11:11:11.111Z");
671      Period shouldBePeriod = Period.createPeriod(
672            dateFromString("2005-11-01T00:00:00.000Z"),
673            dateFromString("2005-11-30T23:59:59.999Z"));
674      checkNextMonth(date, shouldBePeriod);
675
676      // leap day
677      date = dateFromString("2008-01-06T11:11:11.111Z");
678      shouldBePeriod = Period.createPeriod(
679            dateFromString("2008-02-01T00:00:00.000Z"),
680            dateFromString("2008-02-29T23:59:59.999Z"));
681      checkNextMonth(date, shouldBePeriod);
682
683      // leap day
684      date = dateFromString("2007-01-06T11:11:11.111Z");
685      shouldBePeriod = Period.createPeriod(
686            dateFromString("2007-02-01T00:00:00.000Z"),
687            dateFromString("2007-02-28T23:59:59.999Z"));
688      checkNextMonth(date, shouldBePeriod);
689
690      // last month
691      date = dateFromString("2005-12-06T11:11:11.111Z");
692      shouldBePeriod = Period.createPeriod(
693            dateFromString("2006-01-01T00:00:00.000Z"),
694            dateFromString("2006-01-31T23:59:59.999Z"));
695      checkNextMonth(date, shouldBePeriod);
696   }
697
698   /**
699    * Tests the methods {@link Period#previousMonth()} and
700    * {@link Period#previousDay(Date)}.
701    */
702   public void testPreviousMonth ()
703   {
704      // regular
705      Date date = dateFromString("2005-10-06T11:11:11.111Z");
706      Period shouldBePeriod = Period.createPeriod(
707            dateFromString("2005-09-01T00:00:00.000Z"),
708            dateFromString("2005-09-30T23:59:59.999Z"));
709      checkPreviousMonth(date, shouldBePeriod);
710
711      // leap day
712      date = dateFromString("2008-03-06T11:11:11.111Z");
713      shouldBePeriod = Period.createPeriod(
714            dateFromString("2008-02-01T00:00:00.000Z"),
715            dateFromString("2008-02-29T23:59:59.999Z"));
716      checkPreviousMonth(date, shouldBePeriod);
717
718      // leap day
719      date = dateFromString("2007-03-06T11:11:11.111Z");
720      shouldBePeriod = Period.createPeriod(
721            dateFromString("2007-02-01T00:00:00.000Z"),
722            dateFromString("2007-02-28T23:59:59.999Z"));
723      checkPreviousMonth(date, shouldBePeriod);
724
725      // first month
726      date = dateFromString("2007-01-06T11:11:11.111Z");
727      shouldBePeriod = Period.createPeriod(
728            dateFromString("2006-12-01T00:00:00.000Z"),
729            dateFromString("2006-12-31T23:59:59.999Z"));
730      checkPreviousMonth(date, shouldBePeriod);
731   }
732
733   private void checkNextMonth (final Date date, final Period shouldBePeriod)
734   {
735      final Period current = Period.createPeriod(
736            date.minus(Date.MILLIS_PER_WEEK), date);
737      Period next = current.nextMonth();
738      assertEquals("current period: " + current
739            + ", current.nextMonth() period: " + next + ", should be "
740            + shouldBePeriod, next, shouldBePeriod);
741      next = Period.nextMonth(date);
742      assertEquals("date: " + date  + ", Period.nextMonth(date): " + next
743            + ", should be " + shouldBePeriod, next, shouldBePeriod);
744   }
745
746   private void checkPreviousMonth (final Date date,
747         final Period shouldBePeriod)
748   {
749      final Period current = Period.createPeriod(
750            date, date.plus(Date.MILLIS_PER_WEEK));
751      Period prev = current.previousMonth();
752      assertEquals("current period: " + current
753            + ", current.previousMonth() period: " + prev + ", should be "
754            + shouldBePeriod, prev, shouldBePeriod);
755      prev = Period.previousMonth(date);
756      assertEquals("date: " + date  + ", Period.previousMonth(date): " + prev
757            + ", should be " + shouldBePeriod, prev, shouldBePeriod);
758   }
759
760   private void checkNextHour (Date timeDate, Period shouldBePeriod)
761   {
762      final Period nextHour = Period.nextHour(timeDate);
763      final Period timePeriod = Period.createPeriod(
764            timeDate.minus(Date.MILLIS_PER_HOUR), timeDate);
765      assertTrue("The next hour period from " + timeDate + " should be "
766            + shouldBePeriod + ", got period " + nextHour,
767               nextHour.equals(shouldBePeriod));
768      assertTrue("Period.nextHour(Date) should return the same "
769            + "period as period.nextHour(), Date='" + timeDate
770            + "', Period.nextHour(Date)='" + nextHour.toString()
771            + ", period.nextHour(), period " + timePeriod.toString()
772            + ", result " + timePeriod.nextHour(),
773               timePeriod.nextHour().equals(nextHour));
774   }
775
776   private void checkPreviousHour (Date timeDate, Period shouldBePeriod)
777   {
778      final Period prevHour = Period.previousHour(timeDate);
779      final Period timePeriod = Period.createPeriod(
780            timeDate, timeDate.plus(Date.MILLIS_PER_HOUR));
781
782      assertTrue("The previous hour period from " + timeDate + " should be "
783            + shouldBePeriod + ", got period " + prevHour,
784            prevHour.equals(shouldBePeriod));
785      assertTrue("Period.previousHour(Date) should return the same "
786            + "period as period.previousHour(), Date='" + timeDate
787            + "', Period.previousHour(Date)='" + prevHour.toString()
788            + ", period.previousHour(), period " + timePeriod.toString()
789            + ", result " + timePeriod.previousHour(),
790               timePeriod.previousHour().equals(prevHour));
791   }
792
793   private void checkNextDay (Date timeDate, Period shouldBePeriod)
794   {
795      final Period nextDay = Period.nextDay(timeDate);
796      final Period timePeriod = Period.createPeriod(
797            timeDate.minus(Date.MILLIS_PER_HOUR), timeDate);
798      assertTrue("The next day period from " + timeDate + " should be "
799            + shouldBePeriod + ", got period " + nextDay,
800               nextDay.equals(shouldBePeriod));
801      assertTrue("Period.nextDay(Date) should return the same "
802            + "period as period.nextDay(), Date='" + timeDate
803            + "', Period.nextDay(Date)='" + nextDay.toString()
804            + ", period.nextDay(), period " + timePeriod.toString()
805            + ", result " + timePeriod.nextDay(),
806               timePeriod.nextDay().equals(nextDay));
807   }
808
809   private void checkPreviousDay (Date timeDate, Period shouldBePeriod)
810   {
811      final Period prevDay = Period.previousDay(timeDate);
812      final Period timePeriod = Period.createPeriod(
813            timeDate, timeDate.plus(Date.MILLIS_PER_HOUR));
814
815      assertTrue("The previous day period from " + timeDate + " should be "
816            + shouldBePeriod + ", got period " + prevDay,
817            prevDay.equals(shouldBePeriod));
818      assertTrue("Period.previousDay(Date) should return the same "
819            + "period as period.previousDay(), Date='" + timeDate
820            + "', Period.previousDay(Date)='" + prevDay.toString()
821            + ", period.previousDay(), period " + timePeriod.toString()
822            + ", result " + timePeriod.previousDay(),
823               timePeriod.previousDay().equals(prevDay));
824   }
825
826   private void assertPeriode (Date expectedStart, Date expectedEnd, Period p)
827   {
828      assertEquals("StartTime of period is not correct.",
829            expectedStart, p.getStartTime());
830      assertEquals("EndTime of period is not correct.",
831            expectedEnd, p.getEndTime());
832   }
833
834   private Period createDayUnion (Date a1, Date b1, Date a2, Date b2)
835   {
836      final Period p1 = Period.createDayPeriod(a1, b1);
837      final Period p2 = Period.createDayPeriod(a2, b2);
838      return p1.union(p2);
839   }
840
841   private void badFactory (Date start, Date end)
842   {
843      try
844      {
845         Period.createPeriod(start, end);
846         fail("Period should not accept the parameters start = '" + start
847               + "' and end = '" + end + "'.");
848      }
849      catch (ArgumentMalformedException ex)
850      {
851         // this is correct
852      }
853   }
854   private void badDayFactory (Date start, Date end)
855   {
856      try
857      {
858         Period.createDayPeriod(start, end);
859         fail("Period should not accept the parameters start = '" + start
860               + "' and end = '" + end + "'.");
861      }
862      catch (ArgumentMalformedException ex)
863      {
864         // this is correct
865      }
866   }
867
868   private Period createUnion (Date a1, Date b1, Date a2, Date b2)
869   {
870      final Period p1 = Period.createPeriod(a1, b1);
871      final Period p2 = Period.createPeriod(a2, b2);
872      return p1.union(p2);
873   }
874
875   private Date getMinDayPeriod (Date time)
876   {
877      final Calendar s = Period.getCalendarInstance(time);
878      final int year  = s.get(Calendar.YEAR);
879      final int month = s.get(Calendar.MONTH);
880      final int day   = s.get(Calendar.DAY_OF_MONTH);
881      s.set(year, month, day, s.getMinimum(Calendar.HOUR_OF_DAY),
882            s.getMinimum(Calendar.MINUTE), s.getMinimum(Calendar.SECOND));
883      s.set(Calendar.MILLISECOND, s.getMinimum(Calendar.MILLISECOND));
884      return new Date(s.getTimeInMillis());
885   }
886
887   private Date getMaxDayPeriod (Date time)
888   {
889      final Calendar e = Period.getCalendarInstance(time);
890      final int year  = e.get(Calendar.YEAR);
891      final int month = e.get(Calendar.MONTH);
892      final int day   = e.get(Calendar.DAY_OF_MONTH);
893      e.set(year, month, day, e.getMaximum(Calendar.HOUR_OF_DAY),
894            e.getMaximum(Calendar.MINUTE), e.getMaximum(Calendar.SECOND));
895      e.set(Calendar.MILLISECOND, e.getMaximum(Calendar.MILLISECOND));
896      return new Date(e.getTimeInMillis());
897   }
898
899   private void checkStartEnd (Date s)
900   {
901      checkStartEnd(s, s.plus(Date.MILLIS_PER_SECOND));
902      checkStartEnd(s, s.plus(Date.MILLIS_PER_MINUTE));
903      checkStartEnd(s, s.plus(Date.MILLIS_PER_HOUR));
904      checkStartEnd(s, s.plus(Date.MILLIS_PER_DAY));
905      checkStartEnd(s, s.plus(Date.MILLIS_PER_WEEK));
906   }
907
908
909   private void checkStartEnd (Date s, Date e)
910   {
911      final Period p = Period.createPeriod(s, e);
912      assertTrue("Period's start time (" + p.getStartTime()
913            + ")should be equals to " + s, s.equals(p.getStartTime()));
914      assertTrue("Period's start time (" + p.getEndTime()
915            + ")should be equals to " + e, e.equals(p.getEndTime()));
916   }
917
918   private Date dateFromString (final String s)
919   {
920      Date result = null;
921      try
922      {
923         result = Date.fromString(s);
924      }
925      catch (ParseException e)
926      {
927         e.printStackTrace();
928         fail("Testcase internal error, invalid date '" + s
929               + "', got a ParseException " + e.getMessage());
930      }
931      return result;
932   }
933}
Note: See TracBrowser for help on using the browser.