View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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 org.kuali.rice.krad.service;
17  
18  import org.junit.Test;
19  import org.kuali.rice.core.api.CoreApiServiceLocator;
20  import org.kuali.test.KRADTestCase;
21  
22  import java.text.ParseException;
23  import java.text.SimpleDateFormat;
24  import java.util.Calendar;
25  import java.util.Date;
26  
27  import static org.junit.Assert.*;
28  
29  /**
30   * DateTimeServiceTest tests {@link org.kuali.rice.core.api.datetime.DateTimeService}
31   */
32  public class DateTimeServiceTest extends KRADTestCase {
33  
34      /**
35       * tests that DateTimeService returns a correct current date
36       *
37       * @see org.kuali.rice.core.api.datetime.DateTimeService#getCurrentDate()
38       */
39      @Test public void testGetCurrentDate() {
40          Date beforeServiceDate = new Date();
41          Date serviceDate = CoreApiServiceLocator.getDateTimeService().getCurrentDate();
42          Date afterServiceDate = new Date();
43  
44          assertTrue("beforeServiceDate not <= serviceDate", beforeServiceDate.before(serviceDate) || beforeServiceDate.equals(serviceDate));
45          assertTrue("afterServiceDate not >= serviceDate", afterServiceDate.after(serviceDate) || afterServiceDate.equals(serviceDate));
46      }
47  
48      /**
49       * tests that DateTimeService returns a correct current SQL date
50       *
51       * @see org.kuali.rice.core.api.datetime.DateTimeService#getCurrentSqlDate()
52       */
53      @Test public void testGetCurrentSqlDate() {
54          java.sql.Date serviceDate = CoreApiServiceLocator.getDateTimeService().getCurrentSqlDate();
55  
56          java.sql.Date beforeServiceDate = new java.sql.Date(serviceDate.getTime() - 100);
57          java.sql.Date afterServiceDate = new java.sql.Date(serviceDate.getTime() + 100);
58  
59          assertTrue("beforeServiceDate not <= serviceDate", beforeServiceDate.before(serviceDate) || beforeServiceDate.equals(serviceDate));
60          assertTrue("afterServiceDate not >= serviceDate", afterServiceDate.after(serviceDate) || afterServiceDate.equals(serviceDate));
61      }
62  
63      /**
64       * tests {@link org.kuali.rice.core.api.datetime.DateTimeService#getCurrentSqlDateMidnight()}
65       * @throws InterruptedException
66       */
67      @SuppressWarnings("deprecation")
68      @Test public void testGetCurrentSqlDateMidnight() throws InterruptedException {
69          // this test is invalid within 1 second of midnight, so wait for it
70          waitForMidnightIfWithinOneSecond();
71          java.sql.Date before = CoreApiServiceLocator.getDateTimeService().getCurrentSqlDateMidnight();
72          java.util.Date checkBefore = new java.util.Date();
73          Thread.sleep(500); // makes sure the clock has time to tick
74          java.util.Date checkAfter = new java.util.Date();
75          java.sql.Date after = CoreApiServiceLocator.getDateTimeService().getCurrentSqlDateMidnight();
76          assertTrue(checkBefore.before(checkAfter)); // make sure the clock did tick
77          assertEquals(before.getTime(), after.getTime());
78          java.util.Date afterUtil = new java.util.Date(after.getTime());
79          // these methods in java.sql.Date are not just deprecated; they throw IllegalArgumentException.
80          assertEquals(0, afterUtil.getHours());
81          assertEquals(0, afterUtil.getMinutes());
82          assertEquals(0, afterUtil.getSeconds());
83      }
84  
85      /**
86       * {@link #testGetCurrentSqlDateMidnight()} is invalid within 1 second of midnight, so wait for it
87       *
88       * @throws InterruptedException
89       */
90      @SuppressWarnings("deprecation")
91      private static void waitForMidnightIfWithinOneSecond() throws InterruptedException {
92          java.util.Date now = new java.util.Date();
93          java.util.Date then = new java.util.Date(now.getTime() + 1000);
94          if (now.getDay() != then.getDay()) {
95              Thread.sleep(1000);
96          }
97      }
98  
99      /**
100      * tests {@link org.kuali.rice.core.api.datetime.DateTimeService#getCurrentCalendar()}
101      */
102     @Test public void testGetCurrentCalendar() {
103         Date beforeServiceDate = new Date();
104         Calendar serviceCalendar = CoreApiServiceLocator.getDateTimeService().getCurrentCalendar();
105         Date afterServiceDate = new Date();
106 
107         // extract the calendar's Date, for easier testing
108         Date serviceDate = serviceCalendar.getTime();
109 
110         assertTrue("beforeServiceDate not <= serviceDate", beforeServiceDate.before(serviceDate) || beforeServiceDate.equals(serviceDate));
111         assertTrue("afterServiceDate not >= serviceDate", afterServiceDate.after(serviceDate) || afterServiceDate.equals(serviceDate));
112     }
113 
114     /**
115      * tests {@link org.kuali.rice.core.api.datetime.DateTimeService#convertToSqlTimestamp(String)} with a null value
116      *
117      * @throws ParseException
118      */
119     @Test public void testConvertToSqlTimestamp_blankTimeString() throws ParseException {
120         assertNull(CoreApiServiceLocator.getDateTimeService().convertToSqlTimestamp(null));
121     }
122 
123     /**
124      * tests {@link org.kuali.rice.core.api.datetime.DateTimeService#convertToSqlTimestamp(String)} with an invalid time value
125      */
126     @Test public void testConvertToSqlTimestamp_invalidTimeString() {
127         boolean failedAsExpected = false;
128         try {
129             CoreApiServiceLocator.getDateTimeService().convertToSqlTimestamp("foo");
130         }
131         catch (ParseException e) {
132             failedAsExpected = true;
133         }
134         assertTrue("invalid timeString failed to fail", failedAsExpected);
135     }
136 
137     /**
138      * tests {@link org.kuali.rice.core.api.datetime.DateTimeService#convertToSqlTimestamp(String)} with a valid time string
139      *
140      * @throws ParseException
141      */
142     @Test public void testConvertToSqlTimestamp_validTimeString() throws ParseException {
143         java.sql.Timestamp serviceTimestamp = CoreApiServiceLocator.getDateTimeService().convertToSqlTimestamp("05/01/1966 02:41 PM");
144         Calendar serviceCalendar = Calendar.getInstance();
145         serviceCalendar.setTime(serviceTimestamp);
146         assertEquals("unexpected year", 1966, serviceCalendar.get(Calendar.YEAR));
147         assertEquals("unexpected month", 5, serviceCalendar.get(Calendar.MONTH) + 1);
148         assertEquals("unexpected day", 1, serviceCalendar.get(Calendar.DAY_OF_MONTH));
149         assertEquals("unexpected hours", 14, serviceCalendar.get(Calendar.HOUR_OF_DAY));
150         assertEquals("unexpected minutes", 41, serviceCalendar.get(Calendar.MINUTE));
151         assertEquals("unexpected seconds", 0, serviceCalendar.get(Calendar.SECOND));
152         assertEquals("unexpected milliseconds", serviceTimestamp.getNanos(), 0);
153     }
154 
155     /**
156      * tests {@link org.kuali.rice.core.api.datetime.DateTimeService#convertToSqlDate(String)} with a blank value
157      *
158      * @throws ParseException
159      */
160     @Test public void testConvertToSqlDate_blankDateString() throws ParseException {
161         boolean failedAsExpected = false;
162 
163         try {
164             CoreApiServiceLocator.getDateTimeService().convertToSqlDate("");
165         }
166         catch (IllegalArgumentException e) {
167             failedAsExpected = true;
168         }
169 
170         assertTrue("blank dateString failed to fail", failedAsExpected);
171     }
172 
173     /**
174      * tests {@link org.kuali.rice.core.api.datetime.DateTimeService#convertToSqlDate(String)} with an invalid date string
175      */
176     @Test public void testConvertToSqlDate_invalidDateString() {
177         boolean failedAsExpected = false;
178 
179         try {
180             CoreApiServiceLocator.getDateTimeService().convertToSqlDate("foo");
181         }
182         catch (ParseException e) {
183             failedAsExpected = true;
184         }
185 
186         assertTrue("invalid dateString failed to fail", failedAsExpected);
187     }
188 
189     /**
190      * tests {@link org.kuali.rice.core.api.datetime.DateTimeService#convertToSqlDate(String)} with a valid date string
191      */
192     @Test public void testConvertToSqlDate_validDateString() throws ParseException {
193         java.sql.Date serviceDate = CoreApiServiceLocator.getDateTimeService().convertToSqlDate("05/01/1966");
194 
195         Calendar serviceCalendar = Calendar.getInstance();
196         serviceCalendar.setTime(serviceDate);
197 
198         assertEquals("unexpected year", 1966, serviceCalendar.get(Calendar.YEAR));
199         assertEquals("unexpected month", 5, serviceCalendar.get(Calendar.MONTH) + 1);
200         assertEquals("unexpected day", 1, serviceCalendar.get(Calendar.DAY_OF_MONTH));
201         assertEquals("unexpected hours", 0, serviceCalendar.get(Calendar.HOUR_OF_DAY));
202         assertEquals("unexpected minutes", 0, serviceCalendar.get(Calendar.MINUTE));
203         assertEquals("unexpected seconds", 0, serviceCalendar.get(Calendar.SECOND));
204     }
205 
206     /**
207      * tests {@link org.kuali.rice.core.api.datetime.DateTimeService#dateDiff(java.util.Date, java.util.Date, boolean)}
208      *
209      * @throws ParseException
210      */
211     @Test public void testDateDiff() throws ParseException {
212         SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
213 
214         // regular 1 year period
215         Date date1 = sdf.parse("01/01/2006");
216         Date date2 = sdf.parse("12/31/2006");
217 
218         assertEquals("365", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, true)));
219         assertEquals("364", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, false)));
220 
221         // one year period w/ leap year
222         date1 = sdf.parse("01/01/2008");
223         date2 = sdf.parse("12/31/2008");
224 
225         assertEquals("366", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, true)));
226         assertEquals("365", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, false)));
227 
228         assertEquals("366", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, true)));
229         assertEquals("365", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, false)));
230 
231         // one year period w/ leap year, beginning in the middle of the year
232         date1 = sdf.parse("07/01/2007");
233         date2 = sdf.parse("06/30/2008");
234 
235         assertEquals("366", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, true)));
236         assertEquals("365", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, false)));
237 
238         // one year period, start and end in middle of the year
239         date1 = sdf.parse("07/01/2006");
240         date2 = sdf.parse("06/30/2007");
241 
242         assertEquals("365", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, true)));
243         assertEquals("364", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, false)));
244 
245         // one month period
246         date1 = sdf.parse("01/01/2006");
247         date2 = sdf.parse("01/31/2006");
248 
249         assertEquals("31", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, true)));
250         assertEquals("30", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, false)));
251 
252         // another one month period
253         date1 = sdf.parse("04/14/2006");
254         date2 = sdf.parse("05/13/2006");
255 
256         assertEquals("30", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, true)));
257         assertEquals("29", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, false)));
258 
259         // one day period
260         date1 = sdf.parse("01/01/2006");
261         date2 = sdf.parse("01/02/2006");
262 
263         assertEquals("2", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, true)));
264         assertEquals("1", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, false)));
265 
266         // arbitrary dates
267         date1 = sdf.parse("01/01/2006");
268         date2 = sdf.parse("06/30/2006");
269 
270         assertEquals("181", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, true)));
271 
272         date1 = sdf.parse("07/01/2006");
273         date2 = sdf.parse("12/31/2006");
274 
275         assertEquals("184", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, true)));
276 
277         // within same month
278         date1 = sdf.parse("07/01/2006");
279         date2 = sdf.parse("07/20/2006");
280 
281         assertEquals("19", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, false)));
282 
283         // same day
284         date1 = sdf.parse("07/20/2006");
285         date2 = sdf.parse("07/20/2006");
286 
287         assertEquals("0", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, false)));
288 
289         // end date is prior to start date
290         date1 = sdf.parse("07/25/2006");
291         date2 = sdf.parse("07/20/2006");
292 
293         assertEquals("-5", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, false)));
294     }
295 }