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