001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.core.service;
017:
018: import static org.kuali.test.util.KualiTestAssertionUtils.assertEquality;
019:
020: import java.text.ParseException;
021: import java.text.SimpleDateFormat;
022: import java.util.Calendar;
023: import java.util.Date;
024:
025: import org.kuali.kfs.context.KualiTestBase;
026: import org.kuali.kfs.context.SpringContext;
027: import org.kuali.test.ConfigureContext;
028:
029: /**
030: * This class tests the DateTime service.
031: */
032: @ConfigureContext
033: public class DateTimeServiceTest extends KualiTestBase {
034:
035: public final void testGetCurrentDate() {
036: Date beforeServiceDate = new Date();
037: Date serviceDate = SpringContext.getBean(DateTimeService.class)
038: .getCurrentDate();
039: Date afterServiceDate = new Date();
040:
041: assertTrue("beforeServiceDate not <= serviceDate",
042: beforeServiceDate.before(serviceDate)
043: || beforeServiceDate.equals(serviceDate));
044: assertTrue("afterServiceDate not >= serviceDate",
045: afterServiceDate.after(serviceDate)
046: || afterServiceDate.equals(serviceDate));
047: }
048:
049: public final void testGetCurrentSqlDate() {
050: java.sql.Date serviceDate = SpringContext.getBean(
051: DateTimeService.class).getCurrentSqlDate();
052:
053: java.sql.Date beforeServiceDate = new java.sql.Date(serviceDate
054: .getTime() - 100);
055: java.sql.Date afterServiceDate = new java.sql.Date(serviceDate
056: .getTime() + 100);
057:
058: assertTrue("beforeServiceDate not <= serviceDate",
059: beforeServiceDate.before(serviceDate)
060: || beforeServiceDate.equals(serviceDate));
061: assertTrue("afterServiceDate not >= serviceDate",
062: afterServiceDate.after(serviceDate)
063: || afterServiceDate.equals(serviceDate));
064: }
065:
066: @SuppressWarnings("deprecation")
067: public final void testGetCurrentSqlDateMidnight()
068: throws InterruptedException {
069: // this test is invalid within 1 second of midnight, so wait for it
070: waitForMidnightIfWithinOneSecond();
071: java.sql.Date before = SpringContext.getBean(
072: DateTimeService.class).getCurrentSqlDateMidnight();
073: java.util.Date checkBefore = new java.util.Date();
074: Thread.sleep(500); // makes sure the clock has time to tick
075: java.util.Date checkAfter = new java.util.Date();
076: java.sql.Date after = SpringContext.getBean(
077: DateTimeService.class).getCurrentSqlDateMidnight();
078: assertTrue(checkBefore.before(checkAfter)); // make sure the clock did tick
079: assertEquals(before.getTime(), after.getTime());
080: java.util.Date afterUtil = new java.util.Date(after.getTime());
081: // these methods in java.sql.Date are not just deprecated; they throw IllegalArgumentException.
082: assertEquals(0, afterUtil.getHours());
083: assertEquals(0, afterUtil.getMinutes());
084: assertEquals(0, afterUtil.getSeconds());
085: }
086:
087: @SuppressWarnings("deprecation")
088: private static void waitForMidnightIfWithinOneSecond()
089: throws InterruptedException {
090: java.util.Date now = new java.util.Date();
091: java.util.Date then = new java.util.Date(now.getTime() + 1000);
092: if (now.getDay() != then.getDay()) {
093: Thread.sleep(1000);
094: }
095: }
096:
097: public final void testGetCurrentCalendar() {
098: Date beforeServiceDate = new Date();
099: Calendar serviceCalendar = SpringContext.getBean(
100: DateTimeService.class).getCurrentCalendar();
101: Date afterServiceDate = new Date();
102:
103: // extract the calendar's Date, for easier testing
104: Date serviceDate = serviceCalendar.getTime();
105:
106: assertTrue("beforeServiceDate not <= serviceDate",
107: beforeServiceDate.before(serviceDate)
108: || beforeServiceDate.equals(serviceDate));
109: assertTrue("afterServiceDate not >= serviceDate",
110: afterServiceDate.after(serviceDate)
111: || afterServiceDate.equals(serviceDate));
112: }
113:
114: public final void testConvertToSqlTimestamp_blankTimeString()
115: throws ParseException {
116: assertNull(SpringContext.getBean(DateTimeService.class)
117: .convertToSqlTimestamp(null));
118: }
119:
120: public final void testConvertToSqlTimestamp_invalidTimeString() {
121: boolean failedAsExpected = false;
122: try {
123: SpringContext.getBean(DateTimeService.class)
124: .convertToSqlTimestamp("foo");
125: } catch (ParseException e) {
126: failedAsExpected = true;
127: }
128: assertTrue("invalid timeString failed to fail",
129: failedAsExpected);
130: }
131:
132: public final void testConvertToSqlTimestamp_validTimeString()
133: throws ParseException {
134: java.sql.Timestamp serviceTimestamp = SpringContext.getBean(
135: DateTimeService.class).convertToSqlTimestamp(
136: "05/01/1966 02:41 PM");
137: Calendar serviceCalendar = Calendar.getInstance();
138: serviceCalendar.setTime(serviceTimestamp);
139: assertEquals("unexpected year", 1966, serviceCalendar
140: .get(Calendar.YEAR));
141: assertEquals("unexpected month", 5, serviceCalendar
142: .get(Calendar.MONTH) + 1);
143: assertEquals("unexpected day", 1, serviceCalendar
144: .get(Calendar.DAY_OF_MONTH));
145: assertEquals("unexpected hours", 14, serviceCalendar
146: .get(Calendar.HOUR_OF_DAY));
147: assertEquals("unexpected minutes", 41, serviceCalendar
148: .get(Calendar.MINUTE));
149: assertEquals("unexpected seconds", 0, serviceCalendar
150: .get(Calendar.SECOND));
151: assertEquals("unexpected milliseconds", serviceTimestamp
152: .getNanos(), 0);
153: }
154:
155: public final void testConvertToSqlDate_blankDateString()
156: throws ParseException {
157: boolean failedAsExpected = false;
158:
159: try {
160: SpringContext.getBean(DateTimeService.class)
161: .convertToSqlDate("");
162: } catch (IllegalArgumentException e) {
163: failedAsExpected = true;
164: }
165:
166: assertTrue("blank dateString failed to fail", failedAsExpected);
167: }
168:
169: public final void testConvertToSqlDate_invalidDateString() {
170: boolean failedAsExpected = false;
171:
172: try {
173: SpringContext.getBean(DateTimeService.class)
174: .convertToSqlDate("foo");
175: } catch (ParseException e) {
176: failedAsExpected = true;
177: }
178:
179: assertTrue("invalid dateString failed to fail",
180: failedAsExpected);
181: }
182:
183: public final void testConvertToSqlDate_validDateeString()
184: throws ParseException {
185: java.sql.Date serviceDate = SpringContext.getBean(
186: DateTimeService.class).convertToSqlDate("1966-05-01");
187:
188: Calendar serviceCalendar = Calendar.getInstance();
189: serviceCalendar.setTime(serviceDate);
190:
191: assertEquals("unexpected year", 1966, serviceCalendar
192: .get(Calendar.YEAR));
193: assertEquals("unexpected month", 5, serviceCalendar
194: .get(Calendar.MONTH) + 1);
195: assertEquals("unexpected day", 1, serviceCalendar
196: .get(Calendar.DAY_OF_MONTH));
197: assertEquals("unexpected hours", 0, serviceCalendar
198: .get(Calendar.HOUR_OF_DAY));
199: assertEquals("unexpected minutes", 0, serviceCalendar
200: .get(Calendar.MINUTE));
201: assertEquals("unexpected seconds", 0, serviceCalendar
202: .get(Calendar.SECOND));
203: }
204:
205: public void testDateDiff() throws ParseException {
206: SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
207:
208: // regular 1 year period
209: Date date1 = sdf.parse("01/01/2006");
210: Date date2 = sdf.parse("12/31/2006");
211:
212: assertEquals("365", Integer.toString(SpringContext.getBean(
213: DateTimeService.class).dateDiff(date1, date2, true)));
214: assertEquals("364", Integer.toString(SpringContext.getBean(
215: DateTimeService.class).dateDiff(date1, date2, false)));
216:
217: // one year period w/ leap year
218: date1 = sdf.parse("01/01/2008");
219: date2 = sdf.parse("12/31/2008");
220:
221: assertEquals("366", Integer.toString(SpringContext.getBean(
222: DateTimeService.class).dateDiff(date1, date2, true)));
223: assertEquals("365", Integer.toString(SpringContext.getBean(
224: DateTimeService.class).dateDiff(date1, date2, false)));
225:
226: assertEquals("366", Integer.toString(SpringContext.getBean(
227: DateTimeService.class).dateDiff(date1, date2, true)));
228: assertEquals("365", Integer.toString(SpringContext.getBean(
229: DateTimeService.class).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(SpringContext.getBean(
236: DateTimeService.class).dateDiff(date1, date2, true)));
237: assertEquals("365", Integer.toString(SpringContext.getBean(
238: DateTimeService.class).dateDiff(date1, date2, false)));
239:
240: // one year period, start and end in middle of the year
241: date1 = sdf.parse("07/01/2006");
242: date2 = sdf.parse("06/30/2007");
243:
244: assertEquals("365", Integer.toString(SpringContext.getBean(
245: DateTimeService.class).dateDiff(date1, date2, true)));
246: assertEquals("364", Integer.toString(SpringContext.getBean(
247: DateTimeService.class).dateDiff(date1, date2, false)));
248:
249: // one month period
250: date1 = sdf.parse("01/01/2006");
251: date2 = sdf.parse("01/31/2006");
252:
253: assertEquals("31", Integer.toString(SpringContext.getBean(
254: DateTimeService.class).dateDiff(date1, date2, true)));
255: assertEquals("30", Integer.toString(SpringContext.getBean(
256: DateTimeService.class).dateDiff(date1, date2, false)));
257:
258: // another one month period
259: date1 = sdf.parse("04/14/2006");
260: date2 = sdf.parse("05/13/2006");
261:
262: assertEquals("30", Integer.toString(SpringContext.getBean(
263: DateTimeService.class).dateDiff(date1, date2, true)));
264: assertEquals("29", Integer.toString(SpringContext.getBean(
265: DateTimeService.class).dateDiff(date1, date2, false)));
266:
267: // one day period
268: date1 = sdf.parse("01/01/2006");
269: date2 = sdf.parse("01/02/2006");
270:
271: assertEquals("2", Integer.toString(SpringContext.getBean(
272: DateTimeService.class).dateDiff(date1, date2, true)));
273: assertEquals("1", Integer.toString(SpringContext.getBean(
274: DateTimeService.class).dateDiff(date1, date2, false)));
275:
276: // arbitrary dates
277: date1 = sdf.parse("01/01/2006");
278: date2 = sdf.parse("06/30/2006");
279:
280: assertEquality("181", Integer.toString(SpringContext.getBean(
281: DateTimeService.class).dateDiff(date1, date2, true)));
282:
283: date1 = sdf.parse("07/01/2006");
284: date2 = sdf.parse("12/31/2006");
285:
286: assertEquality("184", Integer.toString(SpringContext.getBean(
287: DateTimeService.class).dateDiff(date1, date2, true)));
288:
289: // within same month
290: date1 = sdf.parse("07/01/2006");
291: date2 = sdf.parse("07/20/2006");
292:
293: assertEquality("19", Integer.toString(SpringContext.getBean(
294: DateTimeService.class).dateDiff(date1, date2, false)));
295:
296: // same day
297: date1 = sdf.parse("07/20/2006");
298: date2 = sdf.parse("07/20/2006");
299:
300: assertEquality("0", Integer.toString(SpringContext.getBean(
301: DateTimeService.class).dateDiff(date1, date2, false)));
302:
303: // end date is prior to start date
304: date1 = sdf.parse("07/25/2006");
305: date2 = sdf.parse("07/20/2006");
306:
307: assertEquality("-5", Integer.toString(SpringContext.getBean(
308: DateTimeService.class).dateDiff(date1, date2, false)));
309: }
310: }
|