View Javadoc

1   /*
2    * Copyright 2008-2012 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.util.test.util;
17  
18  import java.util.Calendar;
19  import java.util.Date;
20  
21  /**
22   * Calendar utilities for test cases.
23   *
24   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
25   * @version $Revision:$
26   */
27  public final class CalendarTestUtils
28  {
29    // ******************************** Fields ********************************
30  
31    // --- constants ----------------------------------------------------------
32  
33    /**
34     * Some day in March.
35     */
36    private static final int SOME_DAY_IN_MARCH = 23;
37  
38    /**
39     * Last day in November.
40     */
41    private static final int LAST_DAY_OF_NOVEMBER = 31;
42  
43    /**
44     * Year between {@link #LATER_YEAR} and {@link #YEAR}.
45     */
46    private static final int MIDDLE_YEAR = 2007;
47  
48    /**
49     * Latest year for the tests.
50     */
51    private static final int LATER_YEAR = 2008;
52  
53    /**
54     * Earliest year for the tests.
55     */
56    private static final int YEAR = 1960;
57  
58    /**
59     * A constant date that points to 1960, 13rd of April.
60     */
61    public static final Date CONSTANT_DATE_1960_04_23;
62  
63    /**
64     * A constant date that points to 2008, 2nd of June.
65     */
66    public static final Date CONSTANT_DATE_2008_06_02;
67  
68    // --- members ------------------------------------------------------------
69  
70    // ***************************** Initializer ******************************
71  
72    static
73    {
74      final Calendar calendar = Calendar.getInstance();
75      calendar.clear();
76      calendar.set(YEAR, 3, SOME_DAY_IN_MARCH);
77      CONSTANT_DATE_1960_04_23 = calendar.getTime();
78      calendar.clear();
79      calendar.set(LATER_YEAR, 5, 2);
80      CONSTANT_DATE_2008_06_02 = calendar.getTime();
81    }
82  
83    // ***************************** Constructors ******************************
84  
85    /**
86     * Utility class.
87     */
88    private CalendarTestUtils()
89    {
90    }
91  
92    // ***************************** Inner Classes ****************************
93  
94    // ********************************* Methods *******************************
95  
96    // --- init ---------------------------------------------------------------
97  
98    // --- get&set ------------------------------------------------------------
99  
100   // --- business -----------------------------------------------------------
101 
102   /**
103    * Creates a test date.
104    *
105    * @param index adds this number of days to the first day of December, 2007.
106    * @return the date to use in tests.
107    */
108   public static Date createDate(final int index)
109   {
110     final Calendar calendar = Calendar.getInstance();
111     calendar.clear();
112     calendar.set(MIDDLE_YEAR, 11, LAST_DAY_OF_NOVEMBER);
113     calendar.add(Calendar.DATE, index);
114     final Date date = calendar.getTime();
115     return date;
116   }
117 
118   /**
119    * Creates a test date.
120    *
121    * @param day the say of month, starting with <code>1</code>.
122    * @param month the month of the year, starting with <code>1</code>.
123    * @param year the year of the test date.
124    * @return the date to use in tests.
125    */
126   public static Date createDate(final int day, final int month, final int year)
127   {
128     final Calendar calendar = Calendar.getInstance();
129     calendar.clear();
130     calendar.set(year, month - 1, day);
131     final Date date = calendar.getTime();
132     return date;
133   }
134 
135   /**
136    * Creates a date in the future of the given date.
137    *
138    * @param date the base date to create a date in the future.
139    * @return a date after the given date.
140    * @impl The date is the given day (leap years will expect the previous valid
141    *       day) next year.
142    */
143   public static Date createDateInFuture(final Date date)
144   {
145     final Calendar calendar = Calendar.getInstance();
146     calendar.clear();
147     calendar.setTime(date);
148     calendar.add(Calendar.YEAR, 1);
149     final Date futureDate = calendar.getTime();
150     return futureDate;
151   }
152 
153   /**
154    * Creates a date in the future of today.
155    *
156    * @return a date after today.
157    * @impl The date is the current day (leap years will expect the previous
158    *       valid day) next year.
159    */
160   public static Date createDateInFuture()
161   {
162     return createDateInFuture(new Date());
163   }
164 
165   // --- object basics ------------------------------------------------------
166 
167 }