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