001: /*
002: * Copyright 2006-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.util;
017:
018: import java.util.Calendar;
019: import java.util.Date;
020: import java.util.GregorianCalendar;
021:
022: import org.kuali.kfs.context.KualiTestBase;
023:
024: public class DateUtilsTest extends KualiTestBase {
025: Calendar nowCal;
026: Calendar prevCal;
027: Calendar nextCal;
028:
029: Date nowDate;
030: Date prevDate;
031: Date nextDate;
032:
033: @Override
034: protected void setUp() throws Exception {
035: super .setUp();
036:
037: nowCal = GregorianCalendar.getInstance();
038: nowDate = nowCal.getTime();
039:
040: prevCal = new GregorianCalendar();
041: prevCal.setTime(nowDate);
042: prevCal.add(Calendar.DAY_OF_YEAR, -1);
043: prevDate = prevCal.getTime();
044:
045: nextCal = new GregorianCalendar();
046: nextCal.setTime(nowDate);
047: nextCal.add(Calendar.DAY_OF_YEAR, 1);
048: nextDate = nextCal.getTime();
049:
050: assertTrue(prevCal.before(nowCal));
051: assertTrue(nextCal.after(nowCal));
052:
053: assertTrue(prevDate.before(nowDate));
054: assertTrue(nextDate.after(nowDate));
055: }
056:
057: public void testIsSameDayDateDate_prevDate() {
058: assertFalse(DateUtils.isSameDay(prevDate, nowDate));
059: }
060:
061: public void testIsSameDayDateDate_sameDate() {
062: assertTrue(DateUtils.isSameDay(nowDate, nowDate));
063: }
064:
065: public void testIsSameDayDateDate_nextDate() {
066: assertFalse(DateUtils.isSameDay(nowDate, nextDate));
067: }
068:
069: public void testIsSameDayCalendarCalendar_prevCal() {
070: assertFalse(DateUtils.isSameDay(nowCal, prevCal));
071: }
072:
073: public void testIsSameDayCalendarCalendar_sameCal() {
074: assertTrue(DateUtils.isSameDay(nowCal, nowCal));
075: }
076:
077: public void testIsSameDayCalendarCalendar_nextCal() {
078: assertFalse(DateUtils.isSameDay(nextCal, nowCal));
079: }
080:
081: public void testConvertToSqlDate() {
082: java.sql.Date sqlDate = DateUtils.convertToSqlDate(nowDate);
083: assertEquals(sqlDate.getTime(), nowDate.getTime());
084: }
085:
086: public void testClearTimeFieldsDate() {
087: java.util.Date timeless = DateUtils.clearTimeFields(nowDate);
088:
089: assertTimeCleared(timeless.getTime());
090: }
091:
092: public void testClearTimeFieldsSqlDate() {
093: java.sql.Date timeless = DateUtils.clearTimeFields(DateUtils
094: .convertToSqlDate(nowDate));
095:
096: assertTimeCleared(timeless.getTime());
097: }
098:
099: private void assertTimeCleared(long timeInMilliseconds) {
100: Calendar cal = new GregorianCalendar();
101: cal.setTimeInMillis(timeInMilliseconds);
102:
103: assertFalse(0 == cal.get(Calendar.YEAR));
104: // month can legitimately be 0
105: assertFalse(0 == cal.get(Calendar.DATE));
106: assertEquals(0, cal.get(Calendar.HOUR_OF_DAY));
107: assertEquals(0, cal.get(Calendar.MINUTE));
108: assertEquals(0, cal.get(Calendar.SECOND));
109: assertEquals(0, cal.get(Calendar.MILLISECOND));
110: }
111: }
|