Project Report: fawkez

Packagesummary org.jcoderz.commons.types

org.jcoderz.commons.types.PeriodTest

LineHitsNoteSource
1  /*
2   * $Id: PeriodTest.java 1011 2008-06-16 17:57:36Z amandel $
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   */
33  package org.jcoderz.commons.types;
34  
35  import java.text.ParseException;
36  import java.util.Calendar;
37  import junit.framework.TestCase;
38  import 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   */
47100 public 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        {
78100          Period.createDayPeriod(Date.OLD_DATE, Date.FUTURE_DATE);
79        }
800       catch (Exception ex)
81        {
820          fail("Unexpected exception occured!");
830(1)         ex.printStackTrace();
84100       }
85100    }
86  
87     /**
88      * Test the constructor argument combinations and milli-second resolution.
89      */
90     public void testFactoryMethods ()
91     {
92100       badFactory(null, null);
93100       final Date date = new Date(System.currentTimeMillis());
94100       badFactory(null, date);
95100       badFactory(date, null);
96100       final Date later = Date.nowPlus(1);
97100       badFactory(later, date);
98  
99        try
100        {
101100          checkStartEnd(Date.now());
102100          checkStartEnd(Date.fromString("2004-09-03T12:11:33.785Z"));
103        }
1040       catch (ParseException e)
105        {
1060          fail("Testcase internal error. Caught a ParseException "
107                 + e.getMessage());
108100       }
109100    }
110  
111     /**
112      * Test the constructor argument combinations and milli-second resolution.
113      */
114     public void testDayFactoryMethod ()
115     {
116100       badDayFactory(null, null);
117100       final Date date = new Date(System.currentTimeMillis());
118100       badDayFactory(null, date);
119100       badDayFactory(date, null);
120100       final Date later = Date.nowPlus(ONE_DAY_IN_MSEC);
121100       badDayFactory(later, date);
122100    }
123  
124     /**
125      * Tests the method {@link Period#createDayPeriod(Date)}.
126      */
127     public void testCreateDayPeriod ()
128     {
129100       final Period a = Period.createDayPeriod(Date.now(), Date.now());
130100       final Period b = Period.createDayPeriod(Date.now());
131100       assertEquals("createDayPeriod(Date, Date)=" + a + " should be equal to "
132              + " createDayPeriod(Date)=" + b, a, b);
133100    }
134  
135     /**
136      * Tests the method {@link Period#createMonthPeriod(Date)}.
137      */
138     public void testCreateMonthPeriod ()
139     {
140100       final Date date = dateFromString("2005-10-06T11:11:11.111Z");
141100       final Period shouldBePeriod = Period.createPeriod(
142              dateFromString("2005-10-01T00:00:00.000Z"),
143              dateFromString("2005-10-31T23:59:59.999Z"));
144100       final Period p = Period.createMonthPeriod(date);
145100(2)      assertEquals("createMonthPeriod: " + p + ", should be " + shouldBePeriod
146              + ", date " + date, p, shouldBePeriod);
147100    }
148  
149     /**
150      * Test the union method of the Period class in milli-second resolution.
151      */
152     public void testPeriodUnion ()
153     {
154100       final Date now = Date.now();
155  
156        // a1 |-----| b1
157        // a2 |-----| b2 --> a1 |--------| b2
158100       Date a1 = now;
159100       Date b1 = new Date(now.getTime() + TWENTY_MSEC);
160100       Date a2 = new Date(now.getTime() + TEN_MSEC);
161100       Date b2 = new Date(now.getTime() + THIRTY_MSEC);
162100       Period p = createUnion(a1, b1, a2, b2);
163100       assertNotNull(UNEXPECTED_EMPTY_UNION, p);
164100       assertPeriode(a1, b2, p);
165  
166        // a1 |-----| b1
167        // a2 |-----| b2 --> a1 |----------| b2
168100       a1 = now;
169100       a2 = new Date(now.getTime() + TEN_MSEC);
170100       b1 = new Date(now.getTime() + TEN_MSEC);
171100       b2 = new Date(now.getTime() + THIRTY_MSEC);
172100       p = createUnion(a1, b1, a2, b2);
173100       assertNotNull(UNEXPECTED_EMPTY_UNION, p);
174100       assertPeriode(a1, b2, p);
175  
176        // a1 |-----| b1 --> a2 |--------| b1
177        // a2 |-----| b2
178100       a1 = new Date(now.getTime() + TEN_MSEC);
179100       b1 = new Date(now.getTime() + THIRTY_MSEC);
180100       a2 = now;
181100       b2 = new Date(now.getTime() + TWENTY_MSEC);
182100       p = createUnion(a1, b1, a2, b2);
183100       assertNotNull(UNEXPECTED_EMPTY_UNION, p);
184100       assertPeriode(a2, b1, p);
185  
186        // a1 |-----| b1 --> a2 |----------| b1
187        // a2 |-----| b2
188100       a1 = new Date(now.getTime() + TEN_MSEC);
189100       b1 = new Date(now.getTime() + THIRTY_MSEC);
190100       a2 = now;
191100       b2 = new Date(now.getTime() + TEN_MSEC);
192100       p = createUnion(a1, b1, a2, b2);
193100       assertNotNull(UNEXPECTED_EMPTY_UNION, p);
194100       assertPeriode(a2, b1, p);
195100    }
196  
197     /**
198      * Test the union method of the Period class in milli-second resolution.
199      */
200     public void testEmptyPeriodUnion ()
201     {
202100       final Date now = Date.now();
203  
204        // a1 |-----| b1
205        // a2 |-----| b2 --> null
206100       Date a1 = now;
207100       Date b1 = new Date(now.getTime() + TEN_MSEC);
208100       Date a2 = new Date(now.getTime() + TWENTY_MSEC);
209100       Date b2 = new Date(now.getTime() + THIRTY_MSEC);
210100       Period p = createUnion(a1, b1, a2, b2);
211100       assertNull(EXPECTED_EMPTY_UNION, p);
212  
213        // a1 |-----| b1 --> null
214        // a2 |-----| b2
215100       a2 = now;
216100       a1 = new Date(now.getTime() + TWENTY_MSEC);
217100       b1 = new Date(now.getTime() + THIRTY_MSEC);
218100       b2 = new Date(now.getTime() + TEN_MSEC);
219100       p = createUnion(a1, b1, a2, b2);
220100       assertNull(EXPECTED_EMPTY_UNION, p);
221100    }
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
230100       Date a1 = Date.now();
231100       Date b1 = new Date(a1.getTime() + TEN_MSEC);
232100       Date a2 = new Date(a1.getTime() + ONE_DAY_IN_MSEC + TWENTY_MSEC);
233100       Date b2 = new Date(a1.getTime() + ONE_DAY_IN_MSEC + THIRTY_MSEC);
234100       Period p = createUnion(a1, b1, a2, b2);
235100       assertNull(EXPECTED_EMPTY_UNION, p);
236  
237        // a1 |-----| b1
238        // a2 |-----| b2 --> a1 |--------| b2
239100       a1 = Date.now();
240100       b1 = new Date(a1.getTime() + ONE_DAY_IN_MSEC + TWENTY_MSEC);
241100       a2 = new Date(a1.getTime() + ONE_DAY_IN_MSEC + TEN_MSEC);
242100       b2 = new Date(a1.getTime() + ONE_DAY_IN_MSEC + THIRTY_MSEC);
243100       p = createDayUnion(a1, b1, a2, b2);
244100       assertNotNull(UNEXPECTED_EMPTY_UNION, p);
245100       assertPeriode(getMinDayPeriod(a1), getMaxDayPeriod(b2), p);
246  
247        // a1 |-----| b1
248        // a2 |-----| b2 --> a1 |----------| b2
249100       a1 = Date.now();
250100       a2 = new Date(a1.getTime() + ONE_DAY_IN_MSEC + TEN_MSEC);
251100       b1 = new Date(a1.getTime() + ONE_DAY_IN_MSEC + TEN_MSEC);
252100       b2 = new Date(a1.getTime() + ONE_DAY_IN_MSEC + THIRTY_MSEC);
253100       p = createDayUnion(a1, b1, a2, b2);
254100       assertNotNull(UNEXPECTED_EMPTY_UNION, p);
255100       assertPeriode(getMinDayPeriod(a1), getMaxDayPeriod(b2), p);
256  
257        // a1 |-----| b1 --> a2 |--------| b1
258        // a2 |-----| b2
259100       a2 = Date.now();
260100       a1 = new Date(a2.getTime() + ONE_DAY_IN_MSEC + TEN_MSEC);
261100       b2 = new Date(a2.getTime() + ONE_DAY_IN_MSEC + TWENTY_MSEC);
262100       b1 = new Date(a2.getTime() + ONE_DAY_IN_MSEC + THIRTY_MSEC);
263100       p = createDayUnion(a1, b1, a2, b2);
264100       assertNotNull(UNEXPECTED_EMPTY_UNION, p);
265100       assertPeriode(getMinDayPeriod(a2), getMaxDayPeriod(b1), p);
266  
267        // a1 |-----| b1 --> a2 |----------| b1
268        // a2 |-----| b2
269100       b2 = new Date(a2.getTime() + ONE_DAY_IN_MSEC + TEN_MSEC);
270100       p = createDayUnion(a1, b1, a2, b2);
271100       assertNotNull(UNEXPECTED_EMPTY_UNION, p);
272100       assertPeriode(getMinDayPeriod(a2), getMaxDayPeriod(b1), p);
273  
274        // a1 |-----| b1 --> null
275        // a2 |-----| b2
276100       a1 = new Date(a2.getTime() + ONE_DAY_IN_MSEC + TWENTY_MSEC);
277100       b2 = new Date(a2.getTime() + TWENTY_MSEC);
278100       p = createDayUnion(a1, b1, a2, b2);
279100       assertNull(EXPECTED_EMPTY_UNION, p);
280100    }
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
290100       Date a1 = Date.now();
291100       Date b1 = new Date(a1.getTime() + TEN_MSEC);
292100       Date a2 = new Date(a1.getTime() + TWENTY_MSEC);
293100       Date b2 = new Date(a1.getTime() + THIRTY_MSEC);
294100       Period p1 = Period.createPeriod(a1, b1);
295100       Period p2 = Period.createPeriod(a2, b2);
296  
297100       Period p = p1.intersection(p2);
298100       assertNull(EXPECTED_EMPTY_INTERSECTION, p);
299  
300        // a1 |-----| b1
301        // a2 |-----| b2 --> a2 |--| b1
302100       a2 = new Date(a1.getTime() + TEN_MSEC);
303100       b1 = new Date(a1.getTime() + TWENTY_MSEC);
304100       p1 = Period.createPeriod(a1, b1);
305100       p2 = Period.createPeriod(a2, b2);
306  
307100       p = p1.intersection(p2);
308100       assertNotNull("Periods do intersect!" + p1 + ", " + p2, p);
309100       assertPeriode(a2, b1, p);
310100       assertNotNull("Period does intersect." + p2 + ", " + p2,
311              p2.intersection(p2));
312  
313        // a1 |-----| b1
314        // a2 |-----| b2 --> a2 | b1
315100       a1 = Date.now();
316100       b1 = new Date(a1.getTime() + TEN_MSEC);
317100       b2 = new Date(a1.getTime() + THIRTY_MSEC);
318100       p1 = Period.createPeriod(a1, b1);
319100       p2 = Period.createPeriod(a2, b2);
320  
321100       p = p1.intersection(p2);
322100       assertNotNull(UNEXPECTED_EMPTY_INTERSECTION, p);
323100       assertPeriode(a2, b1, p);
324  
325        // a1 |-----| b1 --> a1 |--| b2
326        // a2 |-----| b2
327100       a2 = Date.now();
328100       a1 = new Date(a2.getTime() + TEN_MSEC);
329100       b2 = new Date(a2.getTime() + TWENTY_MSEC);
330100       b1 = new Date(a2.getTime() + THIRTY_MSEC);
331100       p1 = Period.createPeriod(a1, b1);
332100       p2 = Period.createPeriod(a2, b2);
333  
334100       p = p1.intersection(p2);
335100       assertNotNull(UNEXPECTED_EMPTY_INTERSECTION, p);
336100       assertPeriode(a1, b2, p);
337  
338        // a1 |-----| b1 --> a1 | b2
339        // a2 |-----| b2
340100       b2 = new Date(a2.getTime() + TEN_MSEC);
341100       p1 = Period.createPeriod(a1, b1);
342100       p2 = Period.createPeriod(a2, b2);
343  
344100       p = p1.intersection(p2);
345100       assertNotNull(UNEXPECTED_EMPTY_INTERSECTION, p);
346100       assertPeriode(a1, b2, p);
347  
348        // a1 |-----| b1 --> null
349        // a2 |-----| b2
350100       a2 = Date.now();
351100       b2 = new Date(a2.getTime() + TEN_MSEC);
352100       a1 = new Date(a2.getTime() + TWENTY_MSEC);
353100       b1 = new Date(a2.getTime() + THIRTY_MSEC);
354100       p1 = Period.createPeriod(a1, b1);
355100       p2 = Period.createPeriod(a2, b2);
356  
357100       p = p1.intersection(p2);
358100       assertNull(EXPECTED_EMPTY_INTERSECTION, p);
359100    }
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
368100       Date a1 = Date.now();
369100       Date b1 = new Date(a1.getTime() + TEN_MSEC);
370100       Date a2 = new Date(a1.getTime() + ONE_DAY_IN_MSEC + TWENTY_MSEC);
371100       Date b2 = new Date(a1.getTime() + ONE_DAY_IN_MSEC + THIRTY_MSEC);
372100       Period p1 = Period.createDayPeriod(a1, b1);
373100       Period p2 = Period.createDayPeriod(a2, b2);
374  
375100       Period p = p1.intersection(p2);
376100       assertNull(EXPECTED_EMPTY_INTERSECTION, p);
377  
378        // a1 |-----| b1
379        // a2 |-----| b2 --> a2 |--| b1
380100       a2 = new Date(a1.getTime() + TEN_MSEC);
381100       b1 = new Date(a1.getTime() + TWENTY_MSEC);
382100       p1 = Period.createDayPeriod(a1, b1);
383100       p2 = Period.createDayPeriod(a2, b2);
384  
385100       p = p1.intersection(p2);
386100       assertNotNull(UNEXPECTED_EMPTY_INTERSECTION, p);
387100       assertPeriode(getMinDayPeriod(a2), getMaxDayPeriod(b1), p);
388  
389        // a1 |-----| b1
390        // a2 |-----| b2 --> a2 | b1
391100       a1 = Date.now();
392100       b1 = new Date(a1.getTime() + TEN_MSEC);
393100       b2 = new Date(a1.getTime() + THIRTY_MSEC);
394100       p1 = Period.createDayPeriod(a1, b1);
395100       p2 = Period.createDayPeriod(a2, b2);
396  
397100       p = p1.intersection(p2);
398100       assertNotNull(UNEXPECTED_EMPTY_INTERSECTION, p);
399100       assertPeriode(getMinDayPeriod(a2), getMaxDayPeriod(b1), p);
400  
401        // a1 |-----| b1 --> a1 |--| b2
402        // a2 |-----| b2
403100       a2 = Date.now();
404100       a1 = new Date(a2.getTime() + TEN_MSEC);
405100       b2 = new Date(a2.getTime() + TWENTY_MSEC);
406100       b1 = new Date(a2.getTime() + THIRTY_MSEC);
407100       p1 = Period.createDayPeriod(a1, b1);
408100       p2 = Period.createDayPeriod(a2, b2);
409  
410100       p = p1.intersection(p2);
411100       assertNotNull(UNEXPECTED_EMPTY_INTERSECTION, p);
412100       assertPeriode(getMinDayPeriod(a1), getMaxDayPeriod(b2), p);
413  
414        // a1 |-----| b1 --> a1 | b2
415        // a2 |-----| b2
416100       b2 = new Date(a2.getTime() + TEN_MSEC);
417100       p1 = Period.createDayPeriod(a1, b1);
418100       p2 = Period.createDayPeriod(a2, b2);
419  
420100       p = p1.intersection(p2);
421100       assertNotNull(UNEXPECTED_EMPTY_INTERSECTION, p);
422100       assertPeriode(getMinDayPeriod(a1), getMaxDayPeriod(b2), p);
423  
424        // a1 |-----| b1 --> null
425        // a2 |-----| b2
426100       a2 = Date.now();
427100       b2 = new Date(a2.getTime() + TEN_MSEC);
428100       a1 = new Date(a2.getTime() + ONE_DAY_IN_MSEC + TWENTY_MSEC);
429100       b1 = new Date(a2.getTime() + ONE_DAY_IN_MSEC + THIRTY_MSEC);
430100       p1 = Period.createDayPeriod(a1, b1);
431100       p2 = Period.createDayPeriod(a2, b2);
432  
433100       p = p1.intersection(p2);
434100       assertNull(EXPECTED_EMPTY_INTERSECTION, p);
435100    }
436  
437     /**
438      * Test the isIncluded() method of the Period class in milli-second
439      * resolution.
440      */
441     public void testIsIncludedPeriod ()
442     {
443100       final Date a1 = Date.now();
444100       final Date b1 = new Date(a1.getTime() + THIRTY_MSEC);
445100       final Date c = new Date(a1.getTime() + TEN_MSEC);
446100       final Period p = Period.createDayPeriod(a1, b1);
447100       assertTrue("The timestamp '"
448              + c + "' does not fall into the period:" + p, p.isIncluded(c));
449100    }
450  
451     /**
452      * Test the isIncluded() method of the Period class in day resolution.
453      */
454     public void testIsIncludedDayPeriod ()
455     {
456100       final Date a1 = Date.now();
457100       final Date b1 = new Date(a1.getTime() + THIRTY_MSEC);
458100       final Date c = new Date(a1.getTime() + TEN_MSEC);
459100       final Period p = Period.createDayPeriod(a1, b1);
460100       assertTrue("The timestamp '"
461              + c + "' does not fall into the period:" + p, p.isIncluded(c));
462100    }
463  
464     /**
465      * Test the {@link Period#getNextPeriodStartTime()} and
466      * {@link Period#getPrevPeriodEndTime()} methods.
467      */
468     public void testGetNextPrevPeriodTime ()
469     {
470100       final Date start = Date.now();
471100       final Date end = start.plus(Date.MILLIS_PER_DAY);
472100       Date shouldBeNext = end.plus(1);
473100       Date shouldBePrev = start.minus(1);
474  
475        // Regular period
476100       Period p = Period.createPeriod(start, end);
477  
478100       assertEquals("StartTime of the next period is not correct.",
479              shouldBeNext, p.getNextPeriodStartTime());
480100       assertEquals("EndTime of the previous period is not correct.",
481              shouldBePrev, p.getPrevPeriodEndTime());
482  
483        // Day based period
484100       p = Period.createDayPeriod(start, end);
485  
486100       shouldBeNext = p.getEndTime().plus(1);
487100       shouldBePrev = p.getStartTime().minus(1);
488  
489100       assertEquals("StartTime of the next period is not correct.",
490              shouldBeNext, p.getNextPeriodStartTime());
491100       assertEquals("EndTime of the previous period is not correct.",
492              shouldBePrev, p.getPrevPeriodEndTime());
493100    }
494  
495     /**
496      * Tests the method {@link Period#next()}.
497      */
498     public void testNext ()
499     {
500100       final Period org = Period.createPeriod(Date.now(),
501              Date.nowPlus(Date.MILLIS_PER_DAY));
502100       final Period next = org.next();
503100       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()));
507100       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()));
512100    }
513  
514     /**
515      * Tests the method {@link Period#previous()}.
516      */
517     public void testPrevious ()
518     {
519100       final Period org = Period.createPeriod(Date.now(),
520              Date.nowPlus(Date.MILLIS_PER_DAY));
521100       final Period previous = org.previous();
522100       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()));
526100       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()));
531100    }
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
542100(3)         Date timeDate = Date.fromString("2004-09-04T10:04:22.788Z");
543100          Period shouldBePeriod = Period.createPeriod(
544                 Date.fromString("2004-09-04T11:00:00Z"),
545                 Date.fromString("2004-09-04T11:59:59.999Z"));
546100          checkNextHour(timeDate, shouldBePeriod);
547  
548           // end of a day
549100          timeDate = Date.fromString("2005-10-05T23:04:22.788Z");
550100          shouldBePeriod = Period.createPeriod(
551                 Date.fromString("2005-10-06T00:00:00.000Z"),
552                 Date.fromString("2005-10-06T00:59:59.999Z"));
553100          checkNextHour(timeDate, shouldBePeriod);
554  
555           // end of a month
556100          timeDate = Date.fromString("2005-10-31T23:04:22.788Z");
557100          shouldBePeriod = Period.createPeriod(
558                 Date.fromString("2005-11-01T00:00:00.000Z"),
559                 Date.fromString("2005-11-01T00:59:59.999Z"));
560100          checkNextHour(timeDate, shouldBePeriod);
561        }
5620       catch (ParseException e)
563        {
5640(4)         fail("Testcase internal error. Got a ParseException "
565                 + e.getMessage());
566100       }
567100    }
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
578100          Date timeDate = Date.fromString("2004-09-04T10:04:22.788Z");
579100          Period shouldBePeriod = Period.createPeriod(
580                 Date.fromString("2004-09-04T09:00:00Z"),
581                 Date.fromString("2004-09-04T09:59:59.999Z"));
582100          checkPreviousHour(timeDate, shouldBePeriod);
583  
584           // first hour of a day
585100          timeDate = Date.fromString("2005-10-05T00:04:22.788Z");
586100          shouldBePeriod = Period.createPeriod(
587                 Date.fromString("2005-10-04T23:00:00.000Z"),
588                 Date.fromString("2005-10-04T23:59:59.999Z"));
589100          checkPreviousHour(timeDate, shouldBePeriod);
590  
591           // first day of a month
592100          timeDate = Date.fromString("2005-11-01T00:04:22.788Z");
593100          shouldBePeriod = Period.createPeriod(
594                 Date.fromString("2005-10-31T23:00:00.000Z"),
595                 Date.fromString("2005-10-31T23:59:59.999Z"));
596100          checkPreviousHour(timeDate, shouldBePeriod);
597        }
5980       catch (ParseException e)
599        {
6000          fail("Testcase internal error. Got a ParseException "
601                 + e.getMessage());
602100       }
603100    }
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
614100          Date timeDate = Date.fromString("2004-09-04T10:04:22.788Z");
615100          Period shouldBePeriod = Period.createPeriod(
616                 Date.fromString("2004-09-05T00:00:00Z"),
617                 Date.fromString("2004-09-05T23:59:59.999Z"));
618100          checkNextDay(timeDate, shouldBePeriod);
619  
620           // end of a month
621100          timeDate = Date.fromString("2005-10-31T22:04:22.788Z");
622100          shouldBePeriod = Period.createPeriod(
623                 Date.fromString("2005-11-01T00:00:00.000Z"),
624                 Date.fromString("2005-11-01T23:59:59.999Z"));
625100          checkNextDay(timeDate, shouldBePeriod);
626        }
6270       catch (ParseException e)
628        {
6290          fail("Testcase internal error. Got a ParseException "
630                 + e.getMessage());
631100       }
632100    }
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
643100          Date timeDate = Date.fromString("2004-09-04T10:04:22.788Z");
644100          Period shouldBePeriod = Period.createPeriod(
645                 Date.fromString("2004-09-03T00:00:00Z"),
646                 Date.fromString("2004-09-03T23:59:59.999Z"));
647100          checkPreviousDay(timeDate, shouldBePeriod);
648  
649           // first day of a month
650100          timeDate = Date.fromString("2005-11-01T11:04:22.788Z");
651100          shouldBePeriod = Period.createPeriod(
652                 Date.fromString("2005-10-31T00:00:00.000Z"),
653                 Date.fromString("2005-10-31T23:59:59.999Z"));
654100          checkPreviousDay(timeDate, shouldBePeriod);
655        }
6560       catch (ParseException e)
657        {
6580          fail("Testcase internal error. Got a ParseException "
659                 + e.getMessage());
660100       }
661100    }
662  
663     /**
664      * Tests the methods {@link Period#nextMonth()} and
665      * {@link Period#nextMonth(Date)}.
666      */
667     public void testNextMonth ()
668     {
669        // regular
670100       Date date = dateFromString("2005-10-06T11:11:11.111Z");
671100       Period shouldBePeriod = Period.createPeriod(
672              dateFromString("2005-11-01T00:00:00.000Z"),
673              dateFromString("2005-11-30T23:59:59.999Z"));
674100       checkNextMonth(date, shouldBePeriod);
675  
676        // leap day
677100       date = dateFromString("2008-01-06T11:11:11.111Z");
678100       shouldBePeriod = Period.createPeriod(
679              dateFromString("2008-02-01T00:00:00.000Z"),
680              dateFromString("2008-02-29T23:59:59.999Z"));
681100       checkNextMonth(date, shouldBePeriod);
682  
683        // leap day
684100       date = dateFromString("2007-01-06T11:11:11.111Z");
685100       shouldBePeriod = Period.createPeriod(
686              dateFromString("2007-02-01T00:00:00.000Z"),
687              dateFromString("2007-02-28T23:59:59.999Z"));
688100       checkNextMonth(date, shouldBePeriod);
689  
690        // last month
691100       date = dateFromString("2005-12-06T11:11:11.111Z");
692100       shouldBePeriod = Period.createPeriod(
693              dateFromString("2006-01-01T00:00:00.000Z"),
694              dateFromString("2006-01-31T23:59:59.999Z"));
695100       checkNextMonth(date, shouldBePeriod);
696100    }
697  
698     /**
699      * Tests the methods {@link Period#previousMonth()} and
700      * {@link Period#previousDay(Date)}.
701      */
702     public void testPreviousMonth ()
703     {
704        // regular
705100       Date date = dateFromString("2005-10-06T11:11:11.111Z");
706100       Period shouldBePeriod = Period.createPeriod(
707              dateFromString("2005-09-01T00:00:00.000Z"),
708              dateFromString("2005-09-30T23:59:59.999Z"));
709100       checkPreviousMonth(date, shouldBePeriod);
710  
711        // leap day
712100       date = dateFromString("2008-03-06T11:11:11.111Z");
713100       shouldBePeriod = Period.createPeriod(
714              dateFromString("2008-02-01T00:00:00.000Z"),
715              dateFromString("2008-02-29T23:59:59.999Z"));
716100       checkPreviousMonth(date, shouldBePeriod);
717  
718        // leap day
719100       date = dateFromString("2007-03-06T11:11:11.111Z");
720100       shouldBePeriod = Period.createPeriod(
721              dateFromString("2007-02-01T00:00:00.000Z"),
722              dateFromString("2007-02-28T23:59:59.999Z"));
723100       checkPreviousMonth(date, shouldBePeriod);
724  
725        // first month
726100       date = dateFromString("2007-01-06T11:11:11.111Z");
727100       shouldBePeriod = Period.createPeriod(
728              dateFromString("2006-12-01T00:00:00.000Z"),
729              dateFromString("2006-12-31T23:59:59.999Z"));
730100       checkPreviousMonth(date, shouldBePeriod);
731100    }
732  
733     private void checkNextMonth (final Date date, final Period shouldBePeriod)
734     {
735100       final Period current = Period.createPeriod(
736              date.minus(Date.MILLIS_PER_WEEK), date);
737100       Period next = current.nextMonth();
738100       assertEquals("current period: " + current
739              + ", current.nextMonth() period: " + next + ", should be "
740              + shouldBePeriod, next, shouldBePeriod);
741100       next = Period.nextMonth(date);
742100       assertEquals("date: " + date + ", Period.nextMonth(date): " + next
743              + ", should be " + shouldBePeriod, next, shouldBePeriod);
744100    }
745  
746     private void checkPreviousMonth (final Date date,
747           final Period shouldBePeriod)
748     {
749100       final Period current = Period.createPeriod(
750              date, date.plus(Date.MILLIS_PER_WEEK));
751100       Period prev = current.previousMonth();
752100       assertEquals("current period: " + current
753              + ", current.previousMonth() period: " + prev + ", should be "
754              + shouldBePeriod, prev, shouldBePeriod);
755100       prev = Period.previousMonth(date);
756100       assertEquals("date: " + date + ", Period.previousMonth(date): " + prev
757              + ", should be " + shouldBePeriod, prev, shouldBePeriod);
758100    }
759  
760     private void checkNextHour (Date timeDate, Period shouldBePeriod)
761     {
762100       final Period nextHour = Period.nextHour(timeDate);
763100       final Period timePeriod = Period.createPeriod(
764              timeDate.minus(Date.MILLIS_PER_HOUR), timeDate);
765100(5)(6)      assertTrue("The next hour period from " + timeDate + " should be "
766 (7)            + shouldBePeriod + ", got period " + nextHour,
767                 nextHour.equals(shouldBePeriod));
768100       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 (8)            + ", result " + timePeriod.nextHour(),
773                 timePeriod.nextHour().equals(nextHour));
774100    }
775  
776     private void checkPreviousHour (Date timeDate, Period shouldBePeriod)
777     {
778100       final Period prevHour = Period.previousHour(timeDate);
779100       final Period timePeriod = Period.createPeriod(
780              timeDate, timeDate.plus(Date.MILLIS_PER_HOUR));
781  
782100(9)      assertTrue("The previous hour period from " + timeDate + " should be "
783              + shouldBePeriod + ", got period " + prevHour,
784              prevHour.equals(shouldBePeriod));
785100       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));
791100    }
792  
793     private void checkNextDay (Date timeDate, Period shouldBePeriod)
794     {
795100       final Period nextDay = Period.nextDay(timeDate);
796100       final Period timePeriod = Period.createPeriod(
797              timeDate.minus(Date.MILLIS_PER_HOUR), timeDate);
798100(10)      assertTrue("The next day period from " + timeDate + " should be "
799              + shouldBePeriod + ", got period " + nextDay,
800                 nextDay.equals(shouldBePeriod));
801100       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));
807100    }
808  
809     private void checkPreviousDay (Date timeDate, Period shouldBePeriod)
810     {
811100       final Period prevDay = Period.previousDay(timeDate);
812100       final Period timePeriod = Period.createPeriod(
813              timeDate, timeDate.plus(Date.MILLIS_PER_HOUR));
814  
815100(11)      assertTrue("The previous day period from " + timeDate + " should be "
816              + shouldBePeriod + ", got period " + prevDay,
817              prevDay.equals(shouldBePeriod));
818100       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));
824100    }
825  
826     private void assertPeriode (Date expectedStart, Date expectedEnd, Period p)
827     {
828100       assertEquals("StartTime of period is not correct.",
829              expectedStart, p.getStartTime());
830100       assertEquals("EndTime of period is not correct.",
831              expectedEnd, p.getEndTime());
832100    }
833  
834     private Period createDayUnion (Date a1, Date b1, Date a2, Date b2)
835     {
836100       final Period p1 = Period.createDayPeriod(a1, b1);
837100       final Period p2 = Period.createDayPeriod(a2, b2);
838100       return p1.union(p2);
839     }
840  
841     private void badFactory (Date start, Date end)
842     {
843        try
844        {
8450          Period.createPeriod(start, end);
8460          fail("Period should not accept the parameters start = '" + start
847                 + "' and end = '" + end + "'.");
848        }
849100       catch (ArgumentMalformedException ex)
850        {
851           // this is correct
8520       }
853100    }
854     private void badDayFactory (Date start, Date end)
855     {
856        try
857        {
8580          Period.createDayPeriod(start, end);
8590          fail("Period should not accept the parameters start = '" + start
860                 + "' and end = '" + end + "'.");
861        }
862100       catch (ArgumentMalformedException ex)
863        {
864           // this is correct
8650       }
866100    }
867  
868     private Period createUnion (Date a1, Date b1, Date a2, Date b2)
869     {
870100       final Period p1 = Period.createPeriod(a1, b1);
871100       final Period p2 = Period.createPeriod(a2, b2);
872100       return p1.union(p2);
873     }
874  
875     private Date getMinDayPeriod (Date time)
876     {
877100       final Calendar s = Period.getCalendarInstance(time);
878100       final int year = s.get(Calendar.YEAR);
879100       final int month = s.get(Calendar.MONTH);
880100       final int day = s.get(Calendar.DAY_OF_MONTH);
881100       s.set(year, month, day, s.getMinimum(Calendar.HOUR_OF_DAY),
882              s.getMinimum(Calendar.MINUTE), s.getMinimum(Calendar.SECOND));
883100       s.set(Calendar.MILLISECOND, s.getMinimum(Calendar.MILLISECOND));
884100       return new Date(s.getTimeInMillis());
885     }
886  
887     private Date getMaxDayPeriod (Date time)
888     {
889100       final Calendar e = Period.getCalendarInstance(time);
890100       final int year = e.get(Calendar.YEAR);
891100       final int month = e.get(Calendar.MONTH);
892100       final int day = e.get(Calendar.DAY_OF_MONTH);
893100       e.set(year, month, day, e.getMaximum(Calendar.HOUR_OF_DAY),
894              e.getMaximum(Calendar.MINUTE), e.getMaximum(Calendar.SECOND));
895100       e.set(Calendar.MILLISECOND, e.getMaximum(Calendar.MILLISECOND));
896100       return new Date(e.getTimeInMillis());
897     }
898  
899     private void checkStartEnd (Date s)
900     {
901100       checkStartEnd(s, s.plus(Date.MILLIS_PER_SECOND));
902100       checkStartEnd(s, s.plus(Date.MILLIS_PER_MINUTE));
903100       checkStartEnd(s, s.plus(Date.MILLIS_PER_HOUR));
904100       checkStartEnd(s, s.plus(Date.MILLIS_PER_DAY));
905100       checkStartEnd(s, s.plus(Date.MILLIS_PER_WEEK));
906100    }
907  
908  
909     private void checkStartEnd (Date s, Date e)
910     {
911100       final Period p = Period.createPeriod(s, e);
912100(12)      assertTrue("Period's start time (" + p.getStartTime()
913              + ")should be equals to " + s, s.equals(p.getStartTime()));
914100(13)      assertTrue("Period's start time (" + p.getEndTime()
915              + ")should be equals to " + e, e.equals(p.getEndTime()));
916100    }
917  
918     private Date dateFromString (final String s)
919     {
920100       Date result = null;
921        try
922        {
923100          result = Date.fromString(s);
924        }
9250       catch (ParseException e)
926        {
9270(14)         e.printStackTrace();
9280          fail("Testcase internal error, invalid date '" + s
929                 + "', got a ParseException " + e.getMessage());
930100       }
931100       return result;
932     }
933  }

Findings in this File

d (1) 83 : 10 Avoid printStackTrace(); use a logger call instead.
i (2) 145 : 48 The String literal ", should be " appears 5 times in this file; the first occurrence is on line 145 (test code)
i (3) 542 : 42 The String literal "2004-09-04T10:04:22.788Z" appears 4 times in this file; the first occurrence is on line 542 (test code)
i (4) 564 : 15 The String literal "Testcase internal error. Got a ParseException " appears 4 times in this file; the first occurrence is on line 564 (test code)
i (5) 765 : 60 The String literal " should be " appears 4 times in this file; the first occurrence is on line 765 (test code)
c (6) 765 : 7 Use assertEquals(x, y) instead of assertTrue(x.equals(y))
i (7) 766 : 32 The String literal ", got period " appears 4 times in this file; the first occurrence is on line 766 (test code)
i (8) 772 : 15 The String literal ", result " appears 4 times in this file; the first occurrence is on line 772 (test code)
c (9) 782 : 7 Use assertEquals(x, y) instead of assertTrue(x.equals(y))
c (10) 798 : 7 Use assertEquals(x, y) instead of assertTrue(x.equals(y))
c (11) 815 : 7 Use assertEquals(x, y) instead of assertTrue(x.equals(y))
c (12) 912 : 7 Use assertEquals(x, y) instead of assertTrue(x.equals(y))
c (13) 914 : 7 Use assertEquals(x, y) instead of assertTrue(x.equals(y))
d (14) 927 : 10 Avoid printStackTrace(); use a logger call instead.