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 testConvertToSqlTime_blankTimeString() throws ParseException {
212 boolean failedAsExpected = false;
213
214 try {
215 CoreApiServiceLocator.getDateTimeService().convertToSqlTime("");
216 }
217 catch (IllegalArgumentException e) {
218 failedAsExpected = true;
219 }
220
221 assertTrue("blank timeString failed to fail", failedAsExpected);
222 }
223
224
225
226
227 @Test public void testConvertToSqlTime_invalidTimeString() {
228 boolean failedAsExpected = false;
229
230 try {
231 CoreApiServiceLocator.getDateTimeService().convertToSqlTime("foo");
232 }
233 catch (ParseException e) {
234 failedAsExpected = true;
235 }
236
237 assertTrue("invalid timeString failed to fail", failedAsExpected);
238 }
239
240
241
242
243 @Test public void testConvertToSqlTime_validTimeString() throws ParseException {
244 java.sql.Time serviceTime = CoreApiServiceLocator.getDateTimeService().convertToSqlTime("10:28 am");
245
246 Calendar serviceCalendar = Calendar.getInstance();
247 serviceCalendar.setTime(serviceTime);
248
249 assertEquals("unexpected hours", 10, serviceCalendar.get(Calendar.HOUR_OF_DAY));
250 assertEquals("unexpected minutes", 28, serviceCalendar.get(Calendar.MINUTE));
251 assertEquals("unexpected seconds", 0, serviceCalendar.get(Calendar.SECOND));
252 }
253
254
255
256
257
258
259 @Test public void testDateDiff() throws ParseException {
260 SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
261
262
263 Date date1 = sdf.parse("01/01/2006");
264 Date date2 = sdf.parse("12/31/2006");
265
266 assertEquals("365", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, true)));
267 assertEquals("364", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, false)));
268
269
270 date1 = sdf.parse("01/01/2008");
271 date2 = sdf.parse("12/31/2008");
272
273 assertEquals("366", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, true)));
274 assertEquals("365", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, false)));
275
276 assertEquals("366", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, true)));
277 assertEquals("365", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, false)));
278
279
280 date1 = sdf.parse("07/01/2007");
281 date2 = sdf.parse("06/30/2008");
282
283 assertEquals("366", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, true)));
284 assertEquals("365", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, false)));
285
286
287 date1 = sdf.parse("07/01/2006");
288 date2 = sdf.parse("06/30/2007");
289
290 assertEquals("365", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, true)));
291 assertEquals("364", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, false)));
292
293
294 date1 = sdf.parse("01/01/2006");
295 date2 = sdf.parse("01/31/2006");
296
297 assertEquals("31", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, true)));
298 assertEquals("30", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, false)));
299
300
301 date1 = sdf.parse("04/14/2006");
302 date2 = sdf.parse("05/13/2006");
303
304 assertEquals("30", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, true)));
305 assertEquals("29", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, false)));
306
307
308 date1 = sdf.parse("01/01/2006");
309 date2 = sdf.parse("01/02/2006");
310
311 assertEquals("2", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, true)));
312 assertEquals("1", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, false)));
313
314
315 date1 = sdf.parse("01/01/2006");
316 date2 = sdf.parse("06/30/2006");
317
318 assertEquals("181", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, true)));
319
320 date1 = sdf.parse("07/01/2006");
321 date2 = sdf.parse("12/31/2006");
322
323 assertEquals("184", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, true)));
324
325
326 date1 = sdf.parse("07/01/2006");
327 date2 = sdf.parse("07/20/2006");
328
329 assertEquals("19", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, false)));
330
331
332 date1 = sdf.parse("07/20/2006");
333 date2 = sdf.parse("07/20/2006");
334
335 assertEquals("0", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, false)));
336
337
338 date1 = sdf.parse("07/25/2006");
339 date2 = sdf.parse("07/20/2006");
340
341 assertEquals("-5", Integer.toString(CoreApiServiceLocator.getDateTimeService().dateDiff(date1, date2, false)));
342 }
343 }