1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.rice.krad.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
31
32 public class DateTimeServiceTest extends KRADTestCase {
33
34
35
36
37
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
50
51
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
65
66
67 @SuppressWarnings("deprecation")
68 @Test public void testGetCurrentSqlDateMidnight() throws InterruptedException {
69
70 waitForMidnightIfWithinOneSecond();
71 java.sql.Date before = CoreApiServiceLocator.getDateTimeService().getCurrentSqlDateMidnight();
72 java.util.Date checkBefore = new java.util.Date();
73 Thread.sleep(500);
74 java.util.Date checkAfter = new java.util.Date();
75 java.sql.Date after = CoreApiServiceLocator.getDateTimeService().getCurrentSqlDateMidnight();
76 assertTrue(checkBefore.before(checkAfter));
77 assertEquals(before.getTime(), after.getTime());
78 java.util.Date afterUtil = new java.util.Date(after.getTime());
79
80 assertEquals(0, afterUtil.getHours());
81 assertEquals(0, afterUtil.getMinutes());
82 assertEquals(0, afterUtil.getSeconds());
83 }
84
85
86
87
88
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
101
102 @Test public void testGetCurrentCalendar() {
103 Date beforeServiceDate = new Date();
104 Calendar serviceCalendar = CoreApiServiceLocator.getDateTimeService().getCurrentCalendar();
105 Date afterServiceDate = new Date();
106
107
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
116
117
118
119 @Test public void testConvertToSqlTimestamp_blankTimeString() throws ParseException {
120 assertNull(CoreApiServiceLocator.getDateTimeService().convertToSqlTimestamp(null));
121 }
122
123
124
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
139
140
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
157
158
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
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
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
208
209
210
211 @Test public void testDateDiff() throws ParseException {
212 SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
213
214
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
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
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
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
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
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
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
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
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
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
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 }