From b43136cb1c9c2220210a2766a9175d133877b2fe Mon Sep 17 00:00:00 2001 From: Sebastian Bazley Date: Sat, 14 Mar 2009 14:05:13 +0000 Subject: [PATCH] Tab police git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@753662 13f79535-47bb-0310-9956-ffa450edef68 --- src/pending/CalendarUtilsTest.java | 4 +- .../lang/time/DateUtilsFragmentTest.java | 816 +++++------ .../lang/time/DateUtilsRoundingTest.java | 1250 ++++++++--------- .../commons/lang/time/DateUtilsTest.java | 236 ++-- 4 files changed, 1153 insertions(+), 1153 deletions(-) diff --git a/src/pending/CalendarUtilsTest.java b/src/pending/CalendarUtilsTest.java index a25474d9e..e658b3536 100644 --- a/src/pending/CalendarUtilsTest.java +++ b/src/pending/CalendarUtilsTest.java @@ -46,8 +46,8 @@ public class CalendarUtilsTest extends TestCase { } public static Test suite() { - TestSuite suite = new TestSuite(CalendarUtilsTest.class); - suite.setName("CalendarUtilsTest Tests"); + TestSuite suite = new TestSuite(CalendarUtilsTest.class); + suite.setName("CalendarUtilsTest Tests"); return suite; } diff --git a/src/test/org/apache/commons/lang/time/DateUtilsFragmentTest.java b/src/test/org/apache/commons/lang/time/DateUtilsFragmentTest.java index 0b099c52c..9e217d184 100644 --- a/src/test/org/apache/commons/lang/time/DateUtilsFragmentTest.java +++ b/src/test/org/apache/commons/lang/time/DateUtilsFragmentTest.java @@ -34,475 +34,475 @@ public class DateUtilsFragmentTest extends TestCase { return suite; } - private static final int months = 7; // second final prime before 12 - private static final int days = 23; // second final prime before 31 (and valid) - private static final int hours = 19; // second final prime before 24 - private static final int minutes = 53; // second final prime before 60 - private static final int seconds = 47; // third final prime before 60 - private static final int millis = 991; // second final prime before 1000 + private static final int months = 7; // second final prime before 12 + private static final int days = 23; // second final prime before 31 (and valid) + private static final int hours = 19; // second final prime before 24 + private static final int minutes = 53; // second final prime before 60 + private static final int seconds = 47; // third final prime before 60 + private static final int millis = 991; // second final prime before 1000 - private Date aDate; - private Calendar aCalendar; + private Date aDate; + private Calendar aCalendar; - @Override + @Override protected void setUp() { - aCalendar = Calendar.getInstance(); - aCalendar.set(2005, months, days, hours, minutes, seconds); - aCalendar.set(Calendar.MILLISECOND, millis); - aDate = aCalendar.getTime(); - } - - public void testNullDate() { - try { - DateUtils.getFragmentInMilliseconds((Date) null, Calendar.MILLISECOND); - fail(); - } catch(IllegalArgumentException iae) {} + aCalendar = Calendar.getInstance(); + aCalendar.set(2005, months, days, hours, minutes, seconds); + aCalendar.set(Calendar.MILLISECOND, millis); + aDate = aCalendar.getTime(); + } + + public void testNullDate() { + try { + DateUtils.getFragmentInMilliseconds((Date) null, Calendar.MILLISECOND); + fail(); + } catch(IllegalArgumentException iae) {} - try { - DateUtils.getFragmentInSeconds((Date) null, Calendar.MILLISECOND); - fail(); - } catch(IllegalArgumentException iae) {} + try { + DateUtils.getFragmentInSeconds((Date) null, Calendar.MILLISECOND); + fail(); + } catch(IllegalArgumentException iae) {} - try { - DateUtils.getFragmentInMinutes((Date) null, Calendar.MILLISECOND); - fail(); - } catch(IllegalArgumentException iae) {} + try { + DateUtils.getFragmentInMinutes((Date) null, Calendar.MILLISECOND); + fail(); + } catch(IllegalArgumentException iae) {} - try { - DateUtils.getFragmentInHours((Date) null, Calendar.MILLISECOND); - fail(); - } catch(IllegalArgumentException iae) {} + try { + DateUtils.getFragmentInHours((Date) null, Calendar.MILLISECOND); + fail(); + } catch(IllegalArgumentException iae) {} - try { - DateUtils.getFragmentInDays((Date) null, Calendar.MILLISECOND); - fail(); - } catch(IllegalArgumentException iae) {} - } + try { + DateUtils.getFragmentInDays((Date) null, Calendar.MILLISECOND); + fail(); + } catch(IllegalArgumentException iae) {} + } - public void testNullCalendar() { - try { - DateUtils.getFragmentInMilliseconds((Calendar) null, Calendar.MILLISECOND); - fail(); - } catch(IllegalArgumentException iae) {} + public void testNullCalendar() { + try { + DateUtils.getFragmentInMilliseconds((Calendar) null, Calendar.MILLISECOND); + fail(); + } catch(IllegalArgumentException iae) {} - try { - DateUtils.getFragmentInSeconds((Calendar) null, Calendar.MILLISECOND); - fail(); - } catch(IllegalArgumentException iae) {} + try { + DateUtils.getFragmentInSeconds((Calendar) null, Calendar.MILLISECOND); + fail(); + } catch(IllegalArgumentException iae) {} - try { - DateUtils.getFragmentInMinutes((Calendar) null, Calendar.MILLISECOND); - fail(); - } catch(IllegalArgumentException iae) {} + try { + DateUtils.getFragmentInMinutes((Calendar) null, Calendar.MILLISECOND); + fail(); + } catch(IllegalArgumentException iae) {} - try { - DateUtils.getFragmentInHours((Calendar) null, Calendar.MILLISECOND); - fail(); - } catch(IllegalArgumentException iae) {} + try { + DateUtils.getFragmentInHours((Calendar) null, Calendar.MILLISECOND); + fail(); + } catch(IllegalArgumentException iae) {} - try { - DateUtils.getFragmentInDays((Calendar) null, Calendar.MILLISECOND); - fail(); - } catch(IllegalArgumentException iae) {} - } - - public void testInvalidFragmentWithDate() { - try { - DateUtils.getFragmentInMilliseconds(aDate, 0); - fail(); - } catch(IllegalArgumentException iae) {} + try { + DateUtils.getFragmentInDays((Calendar) null, Calendar.MILLISECOND); + fail(); + } catch(IllegalArgumentException iae) {} + } + + public void testInvalidFragmentWithDate() { + try { + DateUtils.getFragmentInMilliseconds(aDate, 0); + fail(); + } catch(IllegalArgumentException iae) {} - try { - DateUtils.getFragmentInSeconds(aDate, 0); - fail(); - } catch(IllegalArgumentException iae) {} + try { + DateUtils.getFragmentInSeconds(aDate, 0); + fail(); + } catch(IllegalArgumentException iae) {} - try { - DateUtils.getFragmentInMinutes(aDate, 0); - fail(); - } catch(IllegalArgumentException iae) {} + try { + DateUtils.getFragmentInMinutes(aDate, 0); + fail(); + } catch(IllegalArgumentException iae) {} - try { - DateUtils.getFragmentInHours(aDate, 0); - fail(); - } catch(IllegalArgumentException iae) {} + try { + DateUtils.getFragmentInHours(aDate, 0); + fail(); + } catch(IllegalArgumentException iae) {} - try { - DateUtils.getFragmentInDays(aDate, 0); - fail(); - } catch(IllegalArgumentException iae) {} - } + try { + DateUtils.getFragmentInDays(aDate, 0); + fail(); + } catch(IllegalArgumentException iae) {} + } - public void testInvalidFragmentWithCalendar() { - try { - DateUtils.getFragmentInMilliseconds(aCalendar, 0); - fail(); - } catch(IllegalArgumentException iae) {} + public void testInvalidFragmentWithCalendar() { + try { + DateUtils.getFragmentInMilliseconds(aCalendar, 0); + fail(); + } catch(IllegalArgumentException iae) {} - try { - DateUtils.getFragmentInSeconds(aCalendar, 0); - fail(); - } catch(IllegalArgumentException iae) {} + try { + DateUtils.getFragmentInSeconds(aCalendar, 0); + fail(); + } catch(IllegalArgumentException iae) {} - try { - DateUtils.getFragmentInMinutes(aCalendar, 0); - fail(); - } catch(IllegalArgumentException iae) {} + try { + DateUtils.getFragmentInMinutes(aCalendar, 0); + fail(); + } catch(IllegalArgumentException iae) {} - try { - DateUtils.getFragmentInHours(aCalendar, 0); - fail(); - } catch(IllegalArgumentException iae) {} + try { + DateUtils.getFragmentInHours(aCalendar, 0); + fail(); + } catch(IllegalArgumentException iae) {} - try { - DateUtils.getFragmentInDays(aCalendar, 0); - fail(); - } catch(IllegalArgumentException iae) {} - } + try { + DateUtils.getFragmentInDays(aCalendar, 0); + fail(); + } catch(IllegalArgumentException iae) {} + } - public void testMillisecondFragmentInLargerUnitWithDate() { - assertEquals(0, DateUtils.getFragmentInMilliseconds(aDate, Calendar.MILLISECOND)); - assertEquals(0, DateUtils.getFragmentInSeconds(aDate, Calendar.MILLISECOND)); - assertEquals(0, DateUtils.getFragmentInMinutes(aDate, Calendar.MILLISECOND)); - assertEquals(0, DateUtils.getFragmentInHours(aDate, Calendar.MILLISECOND)); - assertEquals(0, DateUtils.getFragmentInDays(aDate, Calendar.MILLISECOND)); - } + public void testMillisecondFragmentInLargerUnitWithDate() { + assertEquals(0, DateUtils.getFragmentInMilliseconds(aDate, Calendar.MILLISECOND)); + assertEquals(0, DateUtils.getFragmentInSeconds(aDate, Calendar.MILLISECOND)); + assertEquals(0, DateUtils.getFragmentInMinutes(aDate, Calendar.MILLISECOND)); + assertEquals(0, DateUtils.getFragmentInHours(aDate, Calendar.MILLISECOND)); + assertEquals(0, DateUtils.getFragmentInDays(aDate, Calendar.MILLISECOND)); + } - public void testMillisecondFragmentInLargerUnitWithCalendar() { - assertEquals(0, DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.MILLISECOND)); - assertEquals(0, DateUtils.getFragmentInSeconds(aCalendar, Calendar.MILLISECOND)); - assertEquals(0, DateUtils.getFragmentInMinutes(aCalendar, Calendar.MILLISECOND)); - assertEquals(0, DateUtils.getFragmentInHours(aCalendar, Calendar.MILLISECOND)); - assertEquals(0, DateUtils.getFragmentInDays(aCalendar, Calendar.MILLISECOND)); - } - - public void testSecondFragmentInLargerUnitWithDate() { - assertEquals(0, DateUtils.getFragmentInSeconds(aDate, Calendar.SECOND)); - assertEquals(0, DateUtils.getFragmentInMinutes(aDate, Calendar.SECOND)); - assertEquals(0, DateUtils.getFragmentInHours(aDate, Calendar.SECOND)); - assertEquals(0, DateUtils.getFragmentInDays(aDate, Calendar.SECOND)); - } + public void testMillisecondFragmentInLargerUnitWithCalendar() { + assertEquals(0, DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.MILLISECOND)); + assertEquals(0, DateUtils.getFragmentInSeconds(aCalendar, Calendar.MILLISECOND)); + assertEquals(0, DateUtils.getFragmentInMinutes(aCalendar, Calendar.MILLISECOND)); + assertEquals(0, DateUtils.getFragmentInHours(aCalendar, Calendar.MILLISECOND)); + assertEquals(0, DateUtils.getFragmentInDays(aCalendar, Calendar.MILLISECOND)); + } + + public void testSecondFragmentInLargerUnitWithDate() { + assertEquals(0, DateUtils.getFragmentInSeconds(aDate, Calendar.SECOND)); + assertEquals(0, DateUtils.getFragmentInMinutes(aDate, Calendar.SECOND)); + assertEquals(0, DateUtils.getFragmentInHours(aDate, Calendar.SECOND)); + assertEquals(0, DateUtils.getFragmentInDays(aDate, Calendar.SECOND)); + } - public void testSecondFragmentInLargerUnitWithCalendar() { - assertEquals(0, DateUtils.getFragmentInSeconds(aCalendar, Calendar.SECOND)); - assertEquals(0, DateUtils.getFragmentInMinutes(aCalendar, Calendar.SECOND)); - assertEquals(0, DateUtils.getFragmentInHours(aCalendar, Calendar.SECOND)); - assertEquals(0, DateUtils.getFragmentInDays(aCalendar, Calendar.SECOND)); - } - - public void testMinuteFragmentInLargerUnitWithDate() { - assertEquals(0, DateUtils.getFragmentInMinutes(aDate, Calendar.MINUTE)); - assertEquals(0, DateUtils.getFragmentInHours(aDate, Calendar.MINUTE)); - assertEquals(0, DateUtils.getFragmentInDays(aDate, Calendar.MINUTE)); - } + public void testSecondFragmentInLargerUnitWithCalendar() { + assertEquals(0, DateUtils.getFragmentInSeconds(aCalendar, Calendar.SECOND)); + assertEquals(0, DateUtils.getFragmentInMinutes(aCalendar, Calendar.SECOND)); + assertEquals(0, DateUtils.getFragmentInHours(aCalendar, Calendar.SECOND)); + assertEquals(0, DateUtils.getFragmentInDays(aCalendar, Calendar.SECOND)); + } + + public void testMinuteFragmentInLargerUnitWithDate() { + assertEquals(0, DateUtils.getFragmentInMinutes(aDate, Calendar.MINUTE)); + assertEquals(0, DateUtils.getFragmentInHours(aDate, Calendar.MINUTE)); + assertEquals(0, DateUtils.getFragmentInDays(aDate, Calendar.MINUTE)); + } - public void testMinuteFragmentInLargerUnitWithCalendar() { - assertEquals(0, DateUtils.getFragmentInMinutes(aCalendar, Calendar.MINUTE)); - assertEquals(0, DateUtils.getFragmentInHours(aCalendar, Calendar.MINUTE)); - assertEquals(0, DateUtils.getFragmentInDays(aCalendar, Calendar.MINUTE)); - } + public void testMinuteFragmentInLargerUnitWithCalendar() { + assertEquals(0, DateUtils.getFragmentInMinutes(aCalendar, Calendar.MINUTE)); + assertEquals(0, DateUtils.getFragmentInHours(aCalendar, Calendar.MINUTE)); + assertEquals(0, DateUtils.getFragmentInDays(aCalendar, Calendar.MINUTE)); + } - public void testHourOfDayFragmentInLargerUnitWithDate() { - assertEquals(0, DateUtils.getFragmentInHours(aDate, Calendar.HOUR_OF_DAY)); - assertEquals(0, DateUtils.getFragmentInDays(aDate, Calendar.HOUR_OF_DAY)); - } + public void testHourOfDayFragmentInLargerUnitWithDate() { + assertEquals(0, DateUtils.getFragmentInHours(aDate, Calendar.HOUR_OF_DAY)); + assertEquals(0, DateUtils.getFragmentInDays(aDate, Calendar.HOUR_OF_DAY)); + } - public void testHourOfDayFragmentInLargerUnitWithCalendar() { - assertEquals(0, DateUtils.getFragmentInHours(aCalendar, Calendar.HOUR_OF_DAY)); - assertEquals(0, DateUtils.getFragmentInDays(aCalendar, Calendar.HOUR_OF_DAY)); - } + public void testHourOfDayFragmentInLargerUnitWithCalendar() { + assertEquals(0, DateUtils.getFragmentInHours(aCalendar, Calendar.HOUR_OF_DAY)); + assertEquals(0, DateUtils.getFragmentInDays(aCalendar, Calendar.HOUR_OF_DAY)); + } - public void testDayOfYearFragmentInLargerUnitWithDate() { - assertEquals(0, DateUtils.getFragmentInDays(aDate, Calendar.DAY_OF_YEAR)); - } + public void testDayOfYearFragmentInLargerUnitWithDate() { + assertEquals(0, DateUtils.getFragmentInDays(aDate, Calendar.DAY_OF_YEAR)); + } - public void testDayOfYearFragmentInLargerUnitWithCalendar() { - assertEquals(0, DateUtils.getFragmentInDays(aCalendar, Calendar.DAY_OF_YEAR)); - } + public void testDayOfYearFragmentInLargerUnitWithCalendar() { + assertEquals(0, DateUtils.getFragmentInDays(aCalendar, Calendar.DAY_OF_YEAR)); + } - public void testDateFragmentInLargerUnitWithDate() { - assertEquals(0, DateUtils.getFragmentInDays(aDate, Calendar.DATE)); - } + public void testDateFragmentInLargerUnitWithDate() { + assertEquals(0, DateUtils.getFragmentInDays(aDate, Calendar.DATE)); + } - public void testDateFragmentInLargerUnitWithCalendar() { - assertEquals(0, DateUtils.getFragmentInDays(aCalendar, Calendar.DATE)); - } + public void testDateFragmentInLargerUnitWithCalendar() { + assertEquals(0, DateUtils.getFragmentInDays(aCalendar, Calendar.DATE)); + } - //Calendar.SECOND as useful fragment - - public void testMillisecondsOfSecondWithDate() { - long testResult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.SECOND); - assertEquals(millis, testResult); - } + //Calendar.SECOND as useful fragment + + public void testMillisecondsOfSecondWithDate() { + long testResult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.SECOND); + assertEquals(millis, testResult); + } - public void testMillisecondsOfSecondWithCalendar() { - long testResult = DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.SECOND); - assertEquals(millis, testResult); - assertEquals(aCalendar.get(Calendar.MILLISECOND), testResult); - } + public void testMillisecondsOfSecondWithCalendar() { + long testResult = DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.SECOND); + assertEquals(millis, testResult); + assertEquals(aCalendar.get(Calendar.MILLISECOND), testResult); + } - //Calendar.MINUTE as useful fragment + //Calendar.MINUTE as useful fragment - public void testMillisecondsOfMinuteWithDate() { - long testResult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.MINUTE); - assertEquals(millis + (seconds * DateUtils.MILLIS_PER_SECOND), testResult); - } + public void testMillisecondsOfMinuteWithDate() { + long testResult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.MINUTE); + assertEquals(millis + (seconds * DateUtils.MILLIS_PER_SECOND), testResult); + } - public void testMillisecondsOfMinuteWithCalender() { - long testResult = DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.MINUTE); - assertEquals(millis + (seconds * DateUtils.MILLIS_PER_SECOND), testResult); - } + public void testMillisecondsOfMinuteWithCalender() { + long testResult = DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.MINUTE); + assertEquals(millis + (seconds * DateUtils.MILLIS_PER_SECOND), testResult); + } - public void testSecondsofMinuteWithDate() { - long testResult = DateUtils.getFragmentInSeconds(aDate, Calendar.MINUTE); - assertEquals(seconds, testResult); - } + public void testSecondsofMinuteWithDate() { + long testResult = DateUtils.getFragmentInSeconds(aDate, Calendar.MINUTE); + assertEquals(seconds, testResult); + } - public void testSecondsofMinuteWithCalendar() { - long testResult = DateUtils.getFragmentInSeconds(aCalendar, Calendar.MINUTE); - assertEquals(seconds, testResult); - assertEquals(aCalendar.get(Calendar.SECOND), testResult); - } + public void testSecondsofMinuteWithCalendar() { + long testResult = DateUtils.getFragmentInSeconds(aCalendar, Calendar.MINUTE); + assertEquals(seconds, testResult); + assertEquals(aCalendar.get(Calendar.SECOND), testResult); + } - //Calendar.HOUR_OF_DAY as useful fragment - - public void testMillisecondsOfHourWithDate() { - long testResult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.HOUR_OF_DAY); - assertEquals(millis + (seconds * DateUtils.MILLIS_PER_SECOND) + (minutes * DateUtils.MILLIS_PER_MINUTE), testResult); - } - - public void testMillisecondsOfHourWithCalendar() { - long testResult = DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.HOUR_OF_DAY); - assertEquals(millis + (seconds * DateUtils.MILLIS_PER_SECOND) + (minutes * DateUtils.MILLIS_PER_MINUTE), testResult); - } + //Calendar.HOUR_OF_DAY as useful fragment + + public void testMillisecondsOfHourWithDate() { + long testResult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.HOUR_OF_DAY); + assertEquals(millis + (seconds * DateUtils.MILLIS_PER_SECOND) + (minutes * DateUtils.MILLIS_PER_MINUTE), testResult); + } + + public void testMillisecondsOfHourWithCalendar() { + long testResult = DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.HOUR_OF_DAY); + assertEquals(millis + (seconds * DateUtils.MILLIS_PER_SECOND) + (minutes * DateUtils.MILLIS_PER_MINUTE), testResult); + } - public void testSecondsofHourWithDate() { - long testResult = DateUtils.getFragmentInSeconds(aDate, Calendar.HOUR_OF_DAY); - assertEquals( - seconds - + (minutes - * DateUtils.MILLIS_PER_MINUTE / DateUtils.MILLIS_PER_SECOND), - testResult); - } + public void testSecondsofHourWithDate() { + long testResult = DateUtils.getFragmentInSeconds(aDate, Calendar.HOUR_OF_DAY); + assertEquals( + seconds + + (minutes + * DateUtils.MILLIS_PER_MINUTE / DateUtils.MILLIS_PER_SECOND), + testResult); + } - public void testSecondsofHourWithCalendar() { - long testResult = DateUtils.getFragmentInSeconds(aCalendar, Calendar.HOUR_OF_DAY); - assertEquals( - seconds - + (minutes - * DateUtils.MILLIS_PER_MINUTE / DateUtils.MILLIS_PER_SECOND), - testResult); - } + public void testSecondsofHourWithCalendar() { + long testResult = DateUtils.getFragmentInSeconds(aCalendar, Calendar.HOUR_OF_DAY); + assertEquals( + seconds + + (minutes + * DateUtils.MILLIS_PER_MINUTE / DateUtils.MILLIS_PER_SECOND), + testResult); + } - public void testMinutesOfHourWithDate() { - long testResult = DateUtils.getFragmentInMinutes(aDate, Calendar.HOUR_OF_DAY); - assertEquals(minutes, testResult); - } + public void testMinutesOfHourWithDate() { + long testResult = DateUtils.getFragmentInMinutes(aDate, Calendar.HOUR_OF_DAY); + assertEquals(minutes, testResult); + } - public void testMinutesOfHourWithCalendar() { - long testResult = DateUtils.getFragmentInMinutes(aCalendar, Calendar.HOUR_OF_DAY); - assertEquals(minutes, testResult); - } + public void testMinutesOfHourWithCalendar() { + long testResult = DateUtils.getFragmentInMinutes(aCalendar, Calendar.HOUR_OF_DAY); + assertEquals(minutes, testResult); + } - //Calendar.DATE and Calendar.DAY_OF_YEAR as useful fragment - public void testMillisecondsOfDayWithDate() { - long testresult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.DATE); - long expectedValue = millis + (seconds * DateUtils.MILLIS_PER_SECOND) + (minutes * DateUtils.MILLIS_PER_MINUTE) + (hours * DateUtils.MILLIS_PER_HOUR); - assertEquals(expectedValue, testresult); - testresult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.DAY_OF_YEAR); - assertEquals(expectedValue, testresult); - } - - public void testMillisecondsOfDayWithCalendar() { - long testresult = DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.DATE); - long expectedValue = millis + (seconds * DateUtils.MILLIS_PER_SECOND) + (minutes * DateUtils.MILLIS_PER_MINUTE) + (hours * DateUtils.MILLIS_PER_HOUR); - assertEquals(expectedValue, testresult); - testresult = DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.DAY_OF_YEAR); - assertEquals(expectedValue, testresult); - } + //Calendar.DATE and Calendar.DAY_OF_YEAR as useful fragment + public void testMillisecondsOfDayWithDate() { + long testresult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.DATE); + long expectedValue = millis + (seconds * DateUtils.MILLIS_PER_SECOND) + (minutes * DateUtils.MILLIS_PER_MINUTE) + (hours * DateUtils.MILLIS_PER_HOUR); + assertEquals(expectedValue, testresult); + testresult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.DAY_OF_YEAR); + assertEquals(expectedValue, testresult); + } + + public void testMillisecondsOfDayWithCalendar() { + long testresult = DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.DATE); + long expectedValue = millis + (seconds * DateUtils.MILLIS_PER_SECOND) + (minutes * DateUtils.MILLIS_PER_MINUTE) + (hours * DateUtils.MILLIS_PER_HOUR); + assertEquals(expectedValue, testresult); + testresult = DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.DAY_OF_YEAR); + assertEquals(expectedValue, testresult); + } - public void testSecondsOfDayWithDate() { - long testresult = DateUtils.getFragmentInSeconds(aDate, Calendar.DATE); - long expectedValue = seconds + ((minutes * DateUtils.MILLIS_PER_MINUTE) + (hours * DateUtils.MILLIS_PER_HOUR))/ DateUtils.MILLIS_PER_SECOND; - assertEquals(expectedValue, testresult); - testresult = DateUtils.getFragmentInSeconds(aDate, Calendar.DAY_OF_YEAR); - assertEquals(expectedValue, testresult); - } + public void testSecondsOfDayWithDate() { + long testresult = DateUtils.getFragmentInSeconds(aDate, Calendar.DATE); + long expectedValue = seconds + ((minutes * DateUtils.MILLIS_PER_MINUTE) + (hours * DateUtils.MILLIS_PER_HOUR))/ DateUtils.MILLIS_PER_SECOND; + assertEquals(expectedValue, testresult); + testresult = DateUtils.getFragmentInSeconds(aDate, Calendar.DAY_OF_YEAR); + assertEquals(expectedValue, testresult); + } - public void testSecondsOfDayWithCalendar() { - long testresult = DateUtils.getFragmentInSeconds(aCalendar, Calendar.DATE); - long expectedValue = seconds + ((minutes * DateUtils.MILLIS_PER_MINUTE) + (hours * DateUtils.MILLIS_PER_HOUR))/ DateUtils.MILLIS_PER_SECOND; - assertEquals(expectedValue, testresult); - testresult = DateUtils.getFragmentInSeconds(aCalendar, Calendar.DAY_OF_YEAR); - assertEquals(expectedValue, testresult); - } + public void testSecondsOfDayWithCalendar() { + long testresult = DateUtils.getFragmentInSeconds(aCalendar, Calendar.DATE); + long expectedValue = seconds + ((minutes * DateUtils.MILLIS_PER_MINUTE) + (hours * DateUtils.MILLIS_PER_HOUR))/ DateUtils.MILLIS_PER_SECOND; + assertEquals(expectedValue, testresult); + testresult = DateUtils.getFragmentInSeconds(aCalendar, Calendar.DAY_OF_YEAR); + assertEquals(expectedValue, testresult); + } - public void testMinutesOfDayWithDate() { - long testResult = DateUtils.getFragmentInMinutes(aDate, Calendar.DATE); - long expectedValue = minutes + ((hours * DateUtils.MILLIS_PER_HOUR))/ DateUtils.MILLIS_PER_MINUTE; - assertEquals(expectedValue,testResult); - testResult = DateUtils.getFragmentInMinutes(aDate, Calendar.DAY_OF_YEAR); - assertEquals(expectedValue,testResult); - } + public void testMinutesOfDayWithDate() { + long testResult = DateUtils.getFragmentInMinutes(aDate, Calendar.DATE); + long expectedValue = minutes + ((hours * DateUtils.MILLIS_PER_HOUR))/ DateUtils.MILLIS_PER_MINUTE; + assertEquals(expectedValue,testResult); + testResult = DateUtils.getFragmentInMinutes(aDate, Calendar.DAY_OF_YEAR); + assertEquals(expectedValue,testResult); + } - public void testMinutesOfDayWithCalendar() { - long testResult = DateUtils.getFragmentInMinutes(aCalendar, Calendar.DATE); - long expectedValue = minutes + ((hours * DateUtils.MILLIS_PER_HOUR))/ DateUtils.MILLIS_PER_MINUTE; - assertEquals(expectedValue, testResult); - testResult = DateUtils.getFragmentInMinutes(aCalendar, Calendar.DAY_OF_YEAR); - assertEquals(expectedValue, testResult); - } - - public void testHoursOfDayWithDate() { - long testResult = DateUtils.getFragmentInHours(aDate, Calendar.DATE); - long expectedValue = hours; - assertEquals(expectedValue,testResult); - testResult = DateUtils.getFragmentInHours(aDate, Calendar.DAY_OF_YEAR); - assertEquals(expectedValue,testResult); - } + public void testMinutesOfDayWithCalendar() { + long testResult = DateUtils.getFragmentInMinutes(aCalendar, Calendar.DATE); + long expectedValue = minutes + ((hours * DateUtils.MILLIS_PER_HOUR))/ DateUtils.MILLIS_PER_MINUTE; + assertEquals(expectedValue, testResult); + testResult = DateUtils.getFragmentInMinutes(aCalendar, Calendar.DAY_OF_YEAR); + assertEquals(expectedValue, testResult); + } + + public void testHoursOfDayWithDate() { + long testResult = DateUtils.getFragmentInHours(aDate, Calendar.DATE); + long expectedValue = hours; + assertEquals(expectedValue,testResult); + testResult = DateUtils.getFragmentInHours(aDate, Calendar.DAY_OF_YEAR); + assertEquals(expectedValue,testResult); + } - public void testHoursOfDayWithCalendar() { - long testResult = DateUtils.getFragmentInHours(aCalendar, Calendar.DATE); - long expectedValue = hours; - assertEquals(expectedValue, testResult); - testResult = DateUtils.getFragmentInHours(aCalendar, Calendar.DAY_OF_YEAR); - assertEquals(expectedValue, testResult); - } - - - //Calendar.MONTH as useful fragment - public void testMillisecondsOfMonthWithDate() { - long testResult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.MONTH); - assertEquals(millis + (seconds * DateUtils.MILLIS_PER_SECOND) + (minutes * DateUtils.MILLIS_PER_MINUTE) - + (hours * DateUtils.MILLIS_PER_HOUR) + (days * DateUtils.MILLIS_PER_DAY), - testResult); - } + public void testHoursOfDayWithCalendar() { + long testResult = DateUtils.getFragmentInHours(aCalendar, Calendar.DATE); + long expectedValue = hours; + assertEquals(expectedValue, testResult); + testResult = DateUtils.getFragmentInHours(aCalendar, Calendar.DAY_OF_YEAR); + assertEquals(expectedValue, testResult); + } + + + //Calendar.MONTH as useful fragment + public void testMillisecondsOfMonthWithDate() { + long testResult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.MONTH); + assertEquals(millis + (seconds * DateUtils.MILLIS_PER_SECOND) + (minutes * DateUtils.MILLIS_PER_MINUTE) + + (hours * DateUtils.MILLIS_PER_HOUR) + (days * DateUtils.MILLIS_PER_DAY), + testResult); + } - public void testMillisecondsOfMonthWithCalendar() { - long testResult = DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.MONTH); - assertEquals(millis + (seconds * DateUtils.MILLIS_PER_SECOND) + (minutes * DateUtils.MILLIS_PER_MINUTE) - + (hours * DateUtils.MILLIS_PER_HOUR) + (days * DateUtils.MILLIS_PER_DAY), + public void testMillisecondsOfMonthWithCalendar() { + long testResult = DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.MONTH); + assertEquals(millis + (seconds * DateUtils.MILLIS_PER_SECOND) + (minutes * DateUtils.MILLIS_PER_MINUTE) + + (hours * DateUtils.MILLIS_PER_HOUR) + (days * DateUtils.MILLIS_PER_DAY), testResult); - } - - public void testSecondsOfMonthWithDate() { - long testResult = DateUtils.getFragmentInSeconds(aDate, Calendar.MONTH); - assertEquals( - seconds - + ((minutes * DateUtils.MILLIS_PER_MINUTE) - + (hours * DateUtils.MILLIS_PER_HOUR) + (days * DateUtils.MILLIS_PER_DAY)) - / DateUtils.MILLIS_PER_SECOND, - testResult); - } + } + + public void testSecondsOfMonthWithDate() { + long testResult = DateUtils.getFragmentInSeconds(aDate, Calendar.MONTH); + assertEquals( + seconds + + ((minutes * DateUtils.MILLIS_PER_MINUTE) + + (hours * DateUtils.MILLIS_PER_HOUR) + (days * DateUtils.MILLIS_PER_DAY)) + / DateUtils.MILLIS_PER_SECOND, + testResult); + } - public void testSecondsOfMonthWithCalendar() { - long testResult = DateUtils.getFragmentInSeconds(aCalendar, Calendar.MONTH); - assertEquals( - seconds - + ((minutes * DateUtils.MILLIS_PER_MINUTE) - + (hours * DateUtils.MILLIS_PER_HOUR) + (days * DateUtils.MILLIS_PER_DAY)) - / DateUtils.MILLIS_PER_SECOND, - testResult); - } + public void testSecondsOfMonthWithCalendar() { + long testResult = DateUtils.getFragmentInSeconds(aCalendar, Calendar.MONTH); + assertEquals( + seconds + + ((minutes * DateUtils.MILLIS_PER_MINUTE) + + (hours * DateUtils.MILLIS_PER_HOUR) + (days * DateUtils.MILLIS_PER_DAY)) + / DateUtils.MILLIS_PER_SECOND, + testResult); + } - public void testMinutesOfMonthWithDate() { - long testResult = DateUtils.getFragmentInMinutes(aDate, Calendar.MONTH); - assertEquals(minutes - + ((hours * DateUtils.MILLIS_PER_HOUR) + (days * DateUtils.MILLIS_PER_DAY)) - / DateUtils.MILLIS_PER_MINUTE, - testResult); - } + public void testMinutesOfMonthWithDate() { + long testResult = DateUtils.getFragmentInMinutes(aDate, Calendar.MONTH); + assertEquals(minutes + + ((hours * DateUtils.MILLIS_PER_HOUR) + (days * DateUtils.MILLIS_PER_DAY)) + / DateUtils.MILLIS_PER_MINUTE, + testResult); + } - public void testMinutesOfMonthWithCalendar() { - long testResult = DateUtils.getFragmentInMinutes(aCalendar, Calendar.MONTH); - assertEquals( minutes +((hours * DateUtils.MILLIS_PER_HOUR) + (days * DateUtils.MILLIS_PER_DAY)) - / DateUtils.MILLIS_PER_MINUTE, - testResult); - } + public void testMinutesOfMonthWithCalendar() { + long testResult = DateUtils.getFragmentInMinutes(aCalendar, Calendar.MONTH); + assertEquals( minutes +((hours * DateUtils.MILLIS_PER_HOUR) + (days * DateUtils.MILLIS_PER_DAY)) + / DateUtils.MILLIS_PER_MINUTE, + testResult); + } - public void testHoursOfMonthWithDate() { - long testResult = DateUtils.getFragmentInHours(aDate, Calendar.MONTH); - assertEquals(hours + ((days * DateUtils.MILLIS_PER_DAY)) - / DateUtils.MILLIS_PER_HOUR, - testResult); - } + public void testHoursOfMonthWithDate() { + long testResult = DateUtils.getFragmentInHours(aDate, Calendar.MONTH); + assertEquals(hours + ((days * DateUtils.MILLIS_PER_DAY)) + / DateUtils.MILLIS_PER_HOUR, + testResult); + } - public void testHoursOfMonthWithCalendar() { - long testResult = DateUtils.getFragmentInHours(aCalendar, Calendar.MONTH); - assertEquals( hours +((days * DateUtils.MILLIS_PER_DAY)) - / DateUtils.MILLIS_PER_HOUR, - testResult); - } - - //Calendar.YEAR as useful fragment - public void testMillisecondsOfYearWithDate() { - long testResult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.YEAR); - Calendar cal = Calendar.getInstance(); - cal.setTime(aDate); - assertEquals(millis + (seconds * DateUtils.MILLIS_PER_SECOND) + (minutes * DateUtils.MILLIS_PER_MINUTE) - + (hours * DateUtils.MILLIS_PER_HOUR) + (cal.get(Calendar.DAY_OF_YEAR) * DateUtils.MILLIS_PER_DAY), - testResult); - } + public void testHoursOfMonthWithCalendar() { + long testResult = DateUtils.getFragmentInHours(aCalendar, Calendar.MONTH); + assertEquals( hours +((days * DateUtils.MILLIS_PER_DAY)) + / DateUtils.MILLIS_PER_HOUR, + testResult); + } + + //Calendar.YEAR as useful fragment + public void testMillisecondsOfYearWithDate() { + long testResult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.YEAR); + Calendar cal = Calendar.getInstance(); + cal.setTime(aDate); + assertEquals(millis + (seconds * DateUtils.MILLIS_PER_SECOND) + (minutes * DateUtils.MILLIS_PER_MINUTE) + + (hours * DateUtils.MILLIS_PER_HOUR) + (cal.get(Calendar.DAY_OF_YEAR) * DateUtils.MILLIS_PER_DAY), + testResult); + } - public void testMillisecondsOfYearWithCalendar() { - long testResult = DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.YEAR); - assertEquals(millis + (seconds * DateUtils.MILLIS_PER_SECOND) + (minutes * DateUtils.MILLIS_PER_MINUTE) - + (hours * DateUtils.MILLIS_PER_HOUR) + (aCalendar.get(Calendar.DAY_OF_YEAR) * DateUtils.MILLIS_PER_DAY), + public void testMillisecondsOfYearWithCalendar() { + long testResult = DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.YEAR); + assertEquals(millis + (seconds * DateUtils.MILLIS_PER_SECOND) + (minutes * DateUtils.MILLIS_PER_MINUTE) + + (hours * DateUtils.MILLIS_PER_HOUR) + (aCalendar.get(Calendar.DAY_OF_YEAR) * DateUtils.MILLIS_PER_DAY), testResult); - } - - public void testSecondsOfYearWithDate() { - long testResult = DateUtils.getFragmentInSeconds(aDate, Calendar.YEAR); - Calendar cal = Calendar.getInstance(); - cal.setTime(aDate); - assertEquals( - seconds - + ((minutes * DateUtils.MILLIS_PER_MINUTE) - + (hours * DateUtils.MILLIS_PER_HOUR) + (cal.get(Calendar.DAY_OF_YEAR) * DateUtils.MILLIS_PER_DAY)) - / DateUtils.MILLIS_PER_SECOND, - testResult); - } + } + + public void testSecondsOfYearWithDate() { + long testResult = DateUtils.getFragmentInSeconds(aDate, Calendar.YEAR); + Calendar cal = Calendar.getInstance(); + cal.setTime(aDate); + assertEquals( + seconds + + ((minutes * DateUtils.MILLIS_PER_MINUTE) + + (hours * DateUtils.MILLIS_PER_HOUR) + (cal.get(Calendar.DAY_OF_YEAR) * DateUtils.MILLIS_PER_DAY)) + / DateUtils.MILLIS_PER_SECOND, + testResult); + } - public void testSecondsOfYearWithCalendar() { - long testResult = DateUtils.getFragmentInSeconds(aCalendar, Calendar.YEAR); - assertEquals( - seconds - + ((minutes * DateUtils.MILLIS_PER_MINUTE) - + (hours * DateUtils.MILLIS_PER_HOUR) + (aCalendar.get(Calendar.DAY_OF_YEAR) * DateUtils.MILLIS_PER_DAY)) - / DateUtils.MILLIS_PER_SECOND, - testResult); - } + public void testSecondsOfYearWithCalendar() { + long testResult = DateUtils.getFragmentInSeconds(aCalendar, Calendar.YEAR); + assertEquals( + seconds + + ((minutes * DateUtils.MILLIS_PER_MINUTE) + + (hours * DateUtils.MILLIS_PER_HOUR) + (aCalendar.get(Calendar.DAY_OF_YEAR) * DateUtils.MILLIS_PER_DAY)) + / DateUtils.MILLIS_PER_SECOND, + testResult); + } - public void testMinutesOfYearWithDate() { - long testResult = DateUtils.getFragmentInMinutes(aDate, Calendar.YEAR); - Calendar cal = Calendar.getInstance(); - cal.setTime(aDate); - assertEquals(minutes - + ((hours * DateUtils.MILLIS_PER_HOUR) + (cal.get(Calendar.DAY_OF_YEAR) * DateUtils.MILLIS_PER_DAY)) - / DateUtils.MILLIS_PER_MINUTE, - testResult); - } + public void testMinutesOfYearWithDate() { + long testResult = DateUtils.getFragmentInMinutes(aDate, Calendar.YEAR); + Calendar cal = Calendar.getInstance(); + cal.setTime(aDate); + assertEquals(minutes + + ((hours * DateUtils.MILLIS_PER_HOUR) + (cal.get(Calendar.DAY_OF_YEAR) * DateUtils.MILLIS_PER_DAY)) + / DateUtils.MILLIS_PER_MINUTE, + testResult); + } - public void testMinutesOfYearWithCalendar() { - long testResult = DateUtils.getFragmentInMinutes(aCalendar, Calendar.YEAR); - assertEquals( minutes +((hours * DateUtils.MILLIS_PER_HOUR) + (aCalendar.get(Calendar.DAY_OF_YEAR) * DateUtils.MILLIS_PER_DAY)) - / DateUtils.MILLIS_PER_MINUTE, - testResult); - } + public void testMinutesOfYearWithCalendar() { + long testResult = DateUtils.getFragmentInMinutes(aCalendar, Calendar.YEAR); + assertEquals( minutes +((hours * DateUtils.MILLIS_PER_HOUR) + (aCalendar.get(Calendar.DAY_OF_YEAR) * DateUtils.MILLIS_PER_DAY)) + / DateUtils.MILLIS_PER_MINUTE, + testResult); + } - public void testHoursOfYearWithDate() { - long testResult = DateUtils.getFragmentInHours(aDate, Calendar.YEAR); - Calendar cal = Calendar.getInstance(); - cal.setTime(aDate); - assertEquals(hours + ((cal.get(Calendar.DAY_OF_YEAR) * DateUtils.MILLIS_PER_DAY)) - / DateUtils.MILLIS_PER_HOUR, - testResult); - } + public void testHoursOfYearWithDate() { + long testResult = DateUtils.getFragmentInHours(aDate, Calendar.YEAR); + Calendar cal = Calendar.getInstance(); + cal.setTime(aDate); + assertEquals(hours + ((cal.get(Calendar.DAY_OF_YEAR) * DateUtils.MILLIS_PER_DAY)) + / DateUtils.MILLIS_PER_HOUR, + testResult); + } - public void testHoursOfYearWithCalendar() { - long testResult = DateUtils.getFragmentInHours(aCalendar, Calendar.YEAR); - assertEquals( hours +((aCalendar.get(Calendar.DAY_OF_YEAR) * DateUtils.MILLIS_PER_DAY)) - / DateUtils.MILLIS_PER_HOUR, - testResult); - } + public void testHoursOfYearWithCalendar() { + long testResult = DateUtils.getFragmentInHours(aCalendar, Calendar.YEAR); + assertEquals( hours +((aCalendar.get(Calendar.DAY_OF_YEAR) * DateUtils.MILLIS_PER_DAY)) + / DateUtils.MILLIS_PER_HOUR, + testResult); + } } diff --git a/src/test/org/apache/commons/lang/time/DateUtilsRoundingTest.java b/src/test/org/apache/commons/lang/time/DateUtilsRoundingTest.java index 44d5a19c8..8bc9e33ff 100644 --- a/src/test/org/apache/commons/lang/time/DateUtilsRoundingTest.java +++ b/src/test/org/apache/commons/lang/time/DateUtilsRoundingTest.java @@ -50,665 +50,665 @@ public class DateUtilsRoundingTest extends TestCase { return suite; } - DateFormat dateTimeParser; - - Date januaryOneDate; - Date targetYearDate; - //No targetMonths, these must be tested for every type of month(28-31 days) - Date targetDateDate, targetDayOfMonthDate, targetAmDate, targetPmDate; - Date targetHourOfDayDate, targetHourDate; - Date targetMinuteDate; - Date targetSecondDate; - Date targetMilliSecondDate; + DateFormat dateTimeParser; + + Date januaryOneDate; + Date targetYearDate; + //No targetMonths, these must be tested for every type of month(28-31 days) + Date targetDateDate, targetDayOfMonthDate, targetAmDate, targetPmDate; + Date targetHourOfDayDate, targetHourDate; + Date targetMinuteDate; + Date targetSecondDate; + Date targetMilliSecondDate; - Calendar januaryOneCalendar; - FastDateFormat fdf = DateFormatUtils.ISO_DATETIME_FORMAT; + Calendar januaryOneCalendar; + FastDateFormat fdf = DateFormatUtils.ISO_DATETIME_FORMAT; - @Override + @Override protected void setUp() throws Exception { - super.setUp(); - dateTimeParser = new SimpleDateFormat("MMM dd, yyyy H:mm:ss.SSS", Locale.ENGLISH); - - targetYearDate = dateTimeParser.parse("January 1, 2007 0:00:00.000"); - targetDateDate = targetDayOfMonthDate = dateTimeParser.parse("June 1, 2008 0:00:00.000"); - targetAmDate = dateTimeParser.parse("June 1, 2008 0:00:00.000"); - targetPmDate = dateTimeParser.parse("June 1, 2008 12:00:00.000"); - targetHourDate = dateTimeParser.parse("June 1, 2008 8:00:00.000"); - targetHourOfDayDate = dateTimeParser.parse("June 1, 2008 8:00:00.000"); - targetMinuteDate = dateTimeParser.parse("June 1, 2008 8:15:00.000"); - targetSecondDate = dateTimeParser.parse("June 1, 2008 8:15:14.000"); - targetMilliSecondDate = dateTimeParser.parse("June 1, 2008 8:15:14.231"); - - januaryOneDate = dateTimeParser.parse("January 1, 2008 0:00:00.000"); - januaryOneCalendar = Calendar.getInstance(); - januaryOneCalendar.setTime(januaryOneDate); - } + super.setUp(); + dateTimeParser = new SimpleDateFormat("MMM dd, yyyy H:mm:ss.SSS", Locale.ENGLISH); + + targetYearDate = dateTimeParser.parse("January 1, 2007 0:00:00.000"); + targetDateDate = targetDayOfMonthDate = dateTimeParser.parse("June 1, 2008 0:00:00.000"); + targetAmDate = dateTimeParser.parse("June 1, 2008 0:00:00.000"); + targetPmDate = dateTimeParser.parse("June 1, 2008 12:00:00.000"); + targetHourDate = dateTimeParser.parse("June 1, 2008 8:00:00.000"); + targetHourOfDayDate = dateTimeParser.parse("June 1, 2008 8:00:00.000"); + targetMinuteDate = dateTimeParser.parse("June 1, 2008 8:15:00.000"); + targetSecondDate = dateTimeParser.parse("June 1, 2008 8:15:14.000"); + targetMilliSecondDate = dateTimeParser.parse("June 1, 2008 8:15:14.231"); + + januaryOneDate = dateTimeParser.parse("January 1, 2008 0:00:00.000"); + januaryOneCalendar = Calendar.getInstance(); + januaryOneCalendar.setTime(januaryOneDate); + } - /** - * Tests DateUtils.round()-method with Calendar.Year - * - * @throws Exception - * @since 3.0 - */ - public void testRoundYear() throws Exception { - final int calendarField = Calendar.YEAR; - Date roundedUpDate = dateTimeParser.parse("January 1, 2008 0:00:00.000"); - Date roundedDownDate = targetYearDate; - Date lastRoundedDownDate = dateTimeParser.parse("June 30, 2007 23:59:59.999"); - baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); - } - - /** - * Tests DateUtils.round()-method with Calendar.MONTH - * Includes rounding months with 28, 29, 30 and 31 days - * Includes rounding to January 1 - * - * @throws Exception - * @since 3.0 - */ - public void testRoundMonth() throws Exception { - final int calendarField = Calendar.MONTH; - Date roundedUpDate, roundedDownDate, lastRoundedDownDate; - Date minDate, maxDate; - - //month with 28 days - roundedUpDate = dateTimeParser.parse("March 1, 2007 0:00:00.000"); - roundedDownDate = dateTimeParser.parse("February 1, 2007 0:00:00.000"); - lastRoundedDownDate = dateTimeParser.parse("February 14, 2007 23:59:59.999"); - baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); + /** + * Tests DateUtils.round()-method with Calendar.Year + * + * @throws Exception + * @since 3.0 + */ + public void testRoundYear() throws Exception { + final int calendarField = Calendar.YEAR; + Date roundedUpDate = dateTimeParser.parse("January 1, 2008 0:00:00.000"); + Date roundedDownDate = targetYearDate; + Date lastRoundedDownDate = dateTimeParser.parse("June 30, 2007 23:59:59.999"); + baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); + } + + /** + * Tests DateUtils.round()-method with Calendar.MONTH + * Includes rounding months with 28, 29, 30 and 31 days + * Includes rounding to January 1 + * + * @throws Exception + * @since 3.0 + */ + public void testRoundMonth() throws Exception { + final int calendarField = Calendar.MONTH; + Date roundedUpDate, roundedDownDate, lastRoundedDownDate; + Date minDate, maxDate; + + //month with 28 days + roundedUpDate = dateTimeParser.parse("March 1, 2007 0:00:00.000"); + roundedDownDate = dateTimeParser.parse("February 1, 2007 0:00:00.000"); + lastRoundedDownDate = dateTimeParser.parse("February 14, 2007 23:59:59.999"); + baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); - //month with 29 days - roundedUpDate = dateTimeParser.parse("March 1, 2008 0:00:00.000"); - roundedDownDate = dateTimeParser.parse("February 1, 2008 0:00:00.000"); - lastRoundedDownDate = dateTimeParser.parse("February 15, 2008 23:59:59.999"); - baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); - - //month with 30 days - roundedUpDate = dateTimeParser.parse("May 1, 2008 0:00:00.000"); - roundedDownDate = dateTimeParser.parse("April 1, 2008 0:00:00.000"); - lastRoundedDownDate = dateTimeParser.parse("April 15, 2008 23:59:59.999"); - baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); - - //month with 31 days - roundedUpDate = dateTimeParser.parse("June 1, 2008 0:00:00.000"); - roundedDownDate = dateTimeParser.parse("May 1, 2008 0:00:00.000"); - lastRoundedDownDate = dateTimeParser.parse("May 16, 2008 23:59:59.999"); - baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); - - //round to January 1 - minDate = dateTimeParser.parse("December 17, 2007 00:00:00.000"); - maxDate = dateTimeParser.parse("January 16, 2008 23:59:59.999"); - roundToJanuaryFirst(minDate, maxDate, calendarField); - } - - /** - * Tests DateUtils.round()-method with DateUtils.SEMI_MONTH - * Includes rounding months with 28, 29, 30 and 31 days, each with first and second half - * Includes rounding to January 1 - * - * @throws Exception - * @since 3.0 - */ - public void testRoundSemiMonth() throws Exception { - final int calendarField = DateUtils.SEMI_MONTH; - Date roundedUpDate, roundedDownDate, lastRoundedDownDate; - Date minDate, maxDate; - - //month with 28 days (1) - roundedUpDate = dateTimeParser.parse("February 16, 2007 0:00:00.000"); - roundedDownDate = dateTimeParser.parse("February 1, 2007 0:00:00.000"); - lastRoundedDownDate = dateTimeParser.parse("February 8, 2007 23:59:59.999"); - baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); + //month with 29 days + roundedUpDate = dateTimeParser.parse("March 1, 2008 0:00:00.000"); + roundedDownDate = dateTimeParser.parse("February 1, 2008 0:00:00.000"); + lastRoundedDownDate = dateTimeParser.parse("February 15, 2008 23:59:59.999"); + baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); + + //month with 30 days + roundedUpDate = dateTimeParser.parse("May 1, 2008 0:00:00.000"); + roundedDownDate = dateTimeParser.parse("April 1, 2008 0:00:00.000"); + lastRoundedDownDate = dateTimeParser.parse("April 15, 2008 23:59:59.999"); + baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); + + //month with 31 days + roundedUpDate = dateTimeParser.parse("June 1, 2008 0:00:00.000"); + roundedDownDate = dateTimeParser.parse("May 1, 2008 0:00:00.000"); + lastRoundedDownDate = dateTimeParser.parse("May 16, 2008 23:59:59.999"); + baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); + + //round to January 1 + minDate = dateTimeParser.parse("December 17, 2007 00:00:00.000"); + maxDate = dateTimeParser.parse("January 16, 2008 23:59:59.999"); + roundToJanuaryFirst(minDate, maxDate, calendarField); + } + + /** + * Tests DateUtils.round()-method with DateUtils.SEMI_MONTH + * Includes rounding months with 28, 29, 30 and 31 days, each with first and second half + * Includes rounding to January 1 + * + * @throws Exception + * @since 3.0 + */ + public void testRoundSemiMonth() throws Exception { + final int calendarField = DateUtils.SEMI_MONTH; + Date roundedUpDate, roundedDownDate, lastRoundedDownDate; + Date minDate, maxDate; + + //month with 28 days (1) + roundedUpDate = dateTimeParser.parse("February 16, 2007 0:00:00.000"); + roundedDownDate = dateTimeParser.parse("February 1, 2007 0:00:00.000"); + lastRoundedDownDate = dateTimeParser.parse("February 8, 2007 23:59:59.999"); + baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); - //month with 28 days (2) - roundedUpDate = dateTimeParser.parse("March 1, 2007 0:00:00.000"); - roundedDownDate = dateTimeParser.parse("February 16, 2007 0:00:00.000"); - lastRoundedDownDate = dateTimeParser.parse("February 23, 2007 23:59:59.999"); - baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); + //month with 28 days (2) + roundedUpDate = dateTimeParser.parse("March 1, 2007 0:00:00.000"); + roundedDownDate = dateTimeParser.parse("February 16, 2007 0:00:00.000"); + lastRoundedDownDate = dateTimeParser.parse("February 23, 2007 23:59:59.999"); + baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); - //month with 29 days (1) - roundedUpDate = dateTimeParser.parse("February 16, 2008 0:00:00.000"); - roundedDownDate = dateTimeParser.parse("February 1, 2008 0:00:00.000"); - lastRoundedDownDate = dateTimeParser.parse("February 8, 2008 23:59:59.999"); - baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); - - //month with 29 days (2) - roundedUpDate = dateTimeParser.parse("March 1, 2008 0:00:00.000"); - roundedDownDate = dateTimeParser.parse("February 16, 2008 0:00:00.000"); - lastRoundedDownDate = dateTimeParser.parse("February 23, 2008 23:59:59.999"); - baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); + //month with 29 days (1) + roundedUpDate = dateTimeParser.parse("February 16, 2008 0:00:00.000"); + roundedDownDate = dateTimeParser.parse("February 1, 2008 0:00:00.000"); + lastRoundedDownDate = dateTimeParser.parse("February 8, 2008 23:59:59.999"); + baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); + + //month with 29 days (2) + roundedUpDate = dateTimeParser.parse("March 1, 2008 0:00:00.000"); + roundedDownDate = dateTimeParser.parse("February 16, 2008 0:00:00.000"); + lastRoundedDownDate = dateTimeParser.parse("February 23, 2008 23:59:59.999"); + baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); - //month with 30 days (1) - roundedUpDate = dateTimeParser.parse("April 16, 2008 0:00:00.000"); - roundedDownDate = dateTimeParser.parse("April 1, 2008 0:00:00.000"); - lastRoundedDownDate = dateTimeParser.parse("April 8, 2008 23:59:59.999"); - baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); + //month with 30 days (1) + roundedUpDate = dateTimeParser.parse("April 16, 2008 0:00:00.000"); + roundedDownDate = dateTimeParser.parse("April 1, 2008 0:00:00.000"); + lastRoundedDownDate = dateTimeParser.parse("April 8, 2008 23:59:59.999"); + baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); - //month with 30 days (2) - roundedUpDate = dateTimeParser.parse("May 1, 2008 0:00:00.000"); - roundedDownDate = dateTimeParser.parse("April 16, 2008 0:00:00.000"); - lastRoundedDownDate = dateTimeParser.parse("April 23, 2008 23:59:59.999"); - baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); - - //month with 31 days (1) - roundedUpDate = dateTimeParser.parse("May 16, 2008 0:00:00.000"); - roundedDownDate = dateTimeParser.parse("May 1, 2008 0:00:00.000"); - lastRoundedDownDate = dateTimeParser.parse("May 8, 2008 23:59:59.999"); - baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); + //month with 30 days (2) + roundedUpDate = dateTimeParser.parse("May 1, 2008 0:00:00.000"); + roundedDownDate = dateTimeParser.parse("April 16, 2008 0:00:00.000"); + lastRoundedDownDate = dateTimeParser.parse("April 23, 2008 23:59:59.999"); + baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); + + //month with 31 days (1) + roundedUpDate = dateTimeParser.parse("May 16, 2008 0:00:00.000"); + roundedDownDate = dateTimeParser.parse("May 1, 2008 0:00:00.000"); + lastRoundedDownDate = dateTimeParser.parse("May 8, 2008 23:59:59.999"); + baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); - //month with 31 days (2) - roundedUpDate = dateTimeParser.parse("June 1, 2008 0:00:00.000"); - roundedDownDate = dateTimeParser.parse("May 16, 2008 0:00:00.000"); - lastRoundedDownDate = dateTimeParser.parse("May 23, 2008 23:59:59.999"); - baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); - - //round to January 1 - minDate = dateTimeParser.parse("December 24, 2007 00:00:00.000"); - maxDate = dateTimeParser.parse("January 8, 2008 23:59:59.999"); - roundToJanuaryFirst(minDate, maxDate, calendarField); - } - - /** - * Tests DateUtils.round()-method with Calendar.DATE - * Includes rounding the extremes of one day - * Includes rounding to January 1 - * - * @throws Exception - * @since 3.0 - */ - public void testRoundDate() throws Exception { - final int calendarField = Calendar.DATE; - Date roundedUpDate, roundedDownDate, lastRoundedDownDate; - Date minDate, maxDate; + //month with 31 days (2) + roundedUpDate = dateTimeParser.parse("June 1, 2008 0:00:00.000"); + roundedDownDate = dateTimeParser.parse("May 16, 2008 0:00:00.000"); + lastRoundedDownDate = dateTimeParser.parse("May 23, 2008 23:59:59.999"); + baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); + + //round to January 1 + minDate = dateTimeParser.parse("December 24, 2007 00:00:00.000"); + maxDate = dateTimeParser.parse("January 8, 2008 23:59:59.999"); + roundToJanuaryFirst(minDate, maxDate, calendarField); + } + + /** + * Tests DateUtils.round()-method with Calendar.DATE + * Includes rounding the extremes of one day + * Includes rounding to January 1 + * + * @throws Exception + * @since 3.0 + */ + public void testRoundDate() throws Exception { + final int calendarField = Calendar.DATE; + Date roundedUpDate, roundedDownDate, lastRoundedDownDate; + Date minDate, maxDate; - roundedUpDate = dateTimeParser.parse("June 2, 2008 0:00:00.000"); - roundedDownDate = targetDateDate; - lastRoundedDownDate = dateTimeParser.parse("June 1, 2008 11:59:59.999"); - baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); - - //round to January 1 - minDate = dateTimeParser.parse("December 31, 2007 12:00:00.000"); - maxDate = dateTimeParser.parse("January 1, 2008 11:59:59.999"); - roundToJanuaryFirst(minDate, maxDate, calendarField); - } - - /** - * Tests DateUtils.round()-method with Calendar.DAY_OF_MONTH - * Includes rounding the extremes of one day - * Includes rounding to January 1 - * - * @throws Exception - * @since 3.0 - */ - public void testRoundDayOfMonth() throws Exception { - final int calendarField = Calendar.DAY_OF_MONTH; - Date roundedUpDate, roundedDownDate, lastRoundedDownDate; - Date minDate, maxDate; + roundedUpDate = dateTimeParser.parse("June 2, 2008 0:00:00.000"); + roundedDownDate = targetDateDate; + lastRoundedDownDate = dateTimeParser.parse("June 1, 2008 11:59:59.999"); + baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); + + //round to January 1 + minDate = dateTimeParser.parse("December 31, 2007 12:00:00.000"); + maxDate = dateTimeParser.parse("January 1, 2008 11:59:59.999"); + roundToJanuaryFirst(minDate, maxDate, calendarField); + } + + /** + * Tests DateUtils.round()-method with Calendar.DAY_OF_MONTH + * Includes rounding the extremes of one day + * Includes rounding to January 1 + * + * @throws Exception + * @since 3.0 + */ + public void testRoundDayOfMonth() throws Exception { + final int calendarField = Calendar.DAY_OF_MONTH; + Date roundedUpDate, roundedDownDate, lastRoundedDownDate; + Date minDate, maxDate; - roundedUpDate = dateTimeParser.parse("June 2, 2008 0:00:00.000"); - roundedDownDate = targetDayOfMonthDate; - lastRoundedDownDate = dateTimeParser.parse("June 1, 2008 11:59:59.999"); - baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); - - //round to January 1 - minDate = dateTimeParser.parse("December 31, 2007 12:00:00.000"); - maxDate = dateTimeParser.parse("January 1, 2008 11:59:59.999"); - roundToJanuaryFirst(minDate, maxDate, calendarField); - } - - /** - * Tests DateUtils.round()-method with Calendar.AM_PM - * Includes rounding the extremes of both AM and PM of one day - * Includes rounding to January 1 - * - * @throws Exception - * @3.0 - */ - public void testRoundAmPm() throws Exception { - final int calendarField = Calendar.AM_PM; - Date roundedUpDate, roundedDownDate, lastRoundedDownDate; - Date minDate, maxDate; + roundedUpDate = dateTimeParser.parse("June 2, 2008 0:00:00.000"); + roundedDownDate = targetDayOfMonthDate; + lastRoundedDownDate = dateTimeParser.parse("June 1, 2008 11:59:59.999"); + baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); + + //round to January 1 + minDate = dateTimeParser.parse("December 31, 2007 12:00:00.000"); + maxDate = dateTimeParser.parse("January 1, 2008 11:59:59.999"); + roundToJanuaryFirst(minDate, maxDate, calendarField); + } + + /** + * Tests DateUtils.round()-method with Calendar.AM_PM + * Includes rounding the extremes of both AM and PM of one day + * Includes rounding to January 1 + * + * @throws Exception + * @3.0 + */ + public void testRoundAmPm() throws Exception { + final int calendarField = Calendar.AM_PM; + Date roundedUpDate, roundedDownDate, lastRoundedDownDate; + Date minDate, maxDate; - //AM - roundedUpDate = dateTimeParser.parse("June 1, 2008 12:00:00.000"); - roundedDownDate = targetAmDate; - lastRoundedDownDate = dateTimeParser.parse("June 1, 2008 5:59:59.999"); - baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); + //AM + roundedUpDate = dateTimeParser.parse("June 1, 2008 12:00:00.000"); + roundedDownDate = targetAmDate; + lastRoundedDownDate = dateTimeParser.parse("June 1, 2008 5:59:59.999"); + baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); - //PM - roundedUpDate = dateTimeParser.parse("June 2, 2008 0:00:00.000"); - roundedDownDate = targetPmDate; - lastRoundedDownDate = dateTimeParser.parse("June 1, 2008 17:59:59.999"); - baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); + //PM + roundedUpDate = dateTimeParser.parse("June 2, 2008 0:00:00.000"); + roundedDownDate = targetPmDate; + lastRoundedDownDate = dateTimeParser.parse("June 1, 2008 17:59:59.999"); + baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); - //round to January 1 - minDate = dateTimeParser.parse("December 31, 2007 18:00:00.000"); - maxDate = dateTimeParser.parse("January 1, 2008 5:59:59.999"); - roundToJanuaryFirst(minDate, maxDate, calendarField); - } - - /** - * Tests DateUtils.round()-method with Calendar.HOUR_OF_DAY - * Includes rounding the extremes of one hour - * Includes rounding to January 1 - * - * @throws Exception - * @since 3.0 - */ - public void testRoundHourOfDay() throws Exception { - final int calendarField = Calendar.HOUR_OF_DAY; - Date roundedUpDate, roundedDownDate, lastRoundedDownDate; - Date minDate, maxDate; + //round to January 1 + minDate = dateTimeParser.parse("December 31, 2007 18:00:00.000"); + maxDate = dateTimeParser.parse("January 1, 2008 5:59:59.999"); + roundToJanuaryFirst(minDate, maxDate, calendarField); + } + + /** + * Tests DateUtils.round()-method with Calendar.HOUR_OF_DAY + * Includes rounding the extremes of one hour + * Includes rounding to January 1 + * + * @throws Exception + * @since 3.0 + */ + public void testRoundHourOfDay() throws Exception { + final int calendarField = Calendar.HOUR_OF_DAY; + Date roundedUpDate, roundedDownDate, lastRoundedDownDate; + Date minDate, maxDate; - roundedUpDate = dateTimeParser.parse("June 1, 2008 9:00:00.000"); - roundedDownDate = targetHourOfDayDate; - lastRoundedDownDate = dateTimeParser.parse("June 1, 2008 8:29:59.999"); - baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); - - //round to January 1 - minDate = dateTimeParser.parse("December 31, 2007 23:30:00.000"); - maxDate = dateTimeParser.parse("January 1, 2008 0:29:59.999"); - roundToJanuaryFirst(minDate, maxDate, calendarField); - } - - /** - * Tests DateUtils.round()-method with Calendar.HOUR - * Includes rounding the extremes of one hour - * Includes rounding to January 1 - * - * @throws Exception - * @since 3.0 - */ - public void testRoundHour() throws Exception { - final int calendarField = Calendar.HOUR; - Date roundedUpDate, roundedDownDate, lastRoundedDownDate; - Date minDate, maxDate; + roundedUpDate = dateTimeParser.parse("June 1, 2008 9:00:00.000"); + roundedDownDate = targetHourOfDayDate; + lastRoundedDownDate = dateTimeParser.parse("June 1, 2008 8:29:59.999"); + baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); + + //round to January 1 + minDate = dateTimeParser.parse("December 31, 2007 23:30:00.000"); + maxDate = dateTimeParser.parse("January 1, 2008 0:29:59.999"); + roundToJanuaryFirst(minDate, maxDate, calendarField); + } + + /** + * Tests DateUtils.round()-method with Calendar.HOUR + * Includes rounding the extremes of one hour + * Includes rounding to January 1 + * + * @throws Exception + * @since 3.0 + */ + public void testRoundHour() throws Exception { + final int calendarField = Calendar.HOUR; + Date roundedUpDate, roundedDownDate, lastRoundedDownDate; + Date minDate, maxDate; - roundedUpDate = dateTimeParser.parse("June 1, 2008 9:00:00.000"); - roundedDownDate = targetHourDate; - lastRoundedDownDate = dateTimeParser.parse("June 1, 2008 8:29:59.999"); - baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); - - //round to January 1 - minDate = dateTimeParser.parse("December 31, 2007 23:30:00.000"); - maxDate = dateTimeParser.parse("January 1, 2008 0:29:59.999"); - roundToJanuaryFirst(minDate, maxDate, calendarField); - } - - /** - * Tests DateUtils.round()-method with Calendar.MINUTE - * Includes rounding the extremes of one minute - * Includes rounding to January 1 - * - * @throws Exception - * @since 3.0 - */ - public void testRoundMinute() throws Exception { - final int calendarField = Calendar.MINUTE; - Date roundedUpDate, roundedDownDate, lastRoundedDownDate; - Date minDate, maxDate; + roundedUpDate = dateTimeParser.parse("June 1, 2008 9:00:00.000"); + roundedDownDate = targetHourDate; + lastRoundedDownDate = dateTimeParser.parse("June 1, 2008 8:29:59.999"); + baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); + + //round to January 1 + minDate = dateTimeParser.parse("December 31, 2007 23:30:00.000"); + maxDate = dateTimeParser.parse("January 1, 2008 0:29:59.999"); + roundToJanuaryFirst(minDate, maxDate, calendarField); + } + + /** + * Tests DateUtils.round()-method with Calendar.MINUTE + * Includes rounding the extremes of one minute + * Includes rounding to January 1 + * + * @throws Exception + * @since 3.0 + */ + public void testRoundMinute() throws Exception { + final int calendarField = Calendar.MINUTE; + Date roundedUpDate, roundedDownDate, lastRoundedDownDate; + Date minDate, maxDate; - roundedUpDate = dateTimeParser.parse("June 1, 2008 8:16:00.000"); - roundedDownDate = targetMinuteDate; - lastRoundedDownDate = dateTimeParser.parse("June 1, 2008 8:15:29.999"); - baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); - - //round to January 1 - minDate = dateTimeParser.parse("December 31, 2007 23:59:30.000"); - maxDate = dateTimeParser.parse("January 1, 2008 0:00:29.999"); - roundToJanuaryFirst(minDate, maxDate, calendarField); - } - - /** - * Tests DateUtils.round()-method with Calendar.SECOND - * Includes rounding the extremes of one second - * Includes rounding to January 1 - * - * @throws Exception - * @since 3.0 - */ - public void testRoundSecond() throws Exception { - final int calendarField = Calendar.SECOND; - Date roundedUpDate, roundedDownDate, lastRoundedDownDate; - Date minDate, maxDate; + roundedUpDate = dateTimeParser.parse("June 1, 2008 8:16:00.000"); + roundedDownDate = targetMinuteDate; + lastRoundedDownDate = dateTimeParser.parse("June 1, 2008 8:15:29.999"); + baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); + + //round to January 1 + minDate = dateTimeParser.parse("December 31, 2007 23:59:30.000"); + maxDate = dateTimeParser.parse("January 1, 2008 0:00:29.999"); + roundToJanuaryFirst(minDate, maxDate, calendarField); + } + + /** + * Tests DateUtils.round()-method with Calendar.SECOND + * Includes rounding the extremes of one second + * Includes rounding to January 1 + * + * @throws Exception + * @since 3.0 + */ + public void testRoundSecond() throws Exception { + final int calendarField = Calendar.SECOND; + Date roundedUpDate, roundedDownDate, lastRoundedDownDate; + Date minDate, maxDate; - roundedUpDate = dateTimeParser.parse("June 1, 2008 8:15:15.000"); - roundedDownDate = targetSecondDate; - lastRoundedDownDate = dateTimeParser.parse("June 1, 2008 8:15:14.499"); - baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); - - //round to January 1 - minDate = dateTimeParser.parse("December 31, 2007 23:59:59.500"); - maxDate = dateTimeParser.parse("January 1, 2008 0:00:00.499"); - roundToJanuaryFirst(minDate, maxDate, calendarField); - } - - /** - * Tests DateUtils.round()-method with Calendar.MILLISECOND - * Includes rounding the extremes of one second - * Includes rounding to January 1 - * - * @throws Exception - * @since 3.0 - */ - public void testRoundMilliSecond() throws Exception { - final int calendarField = Calendar.MILLISECOND; - Date roundedUpDate, roundedDownDate, lastRoundedDownDate; - Date minDate, maxDate; + roundedUpDate = dateTimeParser.parse("June 1, 2008 8:15:15.000"); + roundedDownDate = targetSecondDate; + lastRoundedDownDate = dateTimeParser.parse("June 1, 2008 8:15:14.499"); + baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); + + //round to January 1 + minDate = dateTimeParser.parse("December 31, 2007 23:59:59.500"); + maxDate = dateTimeParser.parse("January 1, 2008 0:00:00.499"); + roundToJanuaryFirst(minDate, maxDate, calendarField); + } + + /** + * Tests DateUtils.round()-method with Calendar.MILLISECOND + * Includes rounding the extremes of one second + * Includes rounding to January 1 + * + * @throws Exception + * @since 3.0 + */ + public void testRoundMilliSecond() throws Exception { + final int calendarField = Calendar.MILLISECOND; + Date roundedUpDate, roundedDownDate, lastRoundedDownDate; + Date minDate, maxDate; - roundedDownDate = lastRoundedDownDate = targetMilliSecondDate; - roundedUpDate = dateTimeParser.parse("June 1, 2008 8:15:14.232"); - baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); - - //round to January 1 - minDate = maxDate = januaryOneDate; - roundToJanuaryFirst(minDate, maxDate, calendarField); - } - - /** - * Test DateUtils.truncate()-method with Calendar.YEAR - * - * @throws Exception - * @since 3.0 - */ - public void testTruncateYear() throws Exception { - final int calendarField = Calendar.YEAR; - Date lastTruncateDate = dateTimeParser.parse("December 31, 2007 23:59:59.999"); - baseTruncateTest(targetYearDate, lastTruncateDate, calendarField); - } + roundedDownDate = lastRoundedDownDate = targetMilliSecondDate; + roundedUpDate = dateTimeParser.parse("June 1, 2008 8:15:14.232"); + baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField); + + //round to January 1 + minDate = maxDate = januaryOneDate; + roundToJanuaryFirst(minDate, maxDate, calendarField); + } + + /** + * Test DateUtils.truncate()-method with Calendar.YEAR + * + * @throws Exception + * @since 3.0 + */ + public void testTruncateYear() throws Exception { + final int calendarField = Calendar.YEAR; + Date lastTruncateDate = dateTimeParser.parse("December 31, 2007 23:59:59.999"); + baseTruncateTest(targetYearDate, lastTruncateDate, calendarField); + } - /** - * Test DateUtils.truncate()-method with Calendar.MONTH - * - * @throws Exception - * @since 3.0 - */ - public void testTruncateMonth() throws Exception { - final int calendarField = Calendar.MONTH; - Date truncatedDate = dateTimeParser.parse("March 1, 2008 0:00:00.000"); - Date lastTruncateDate = dateTimeParser.parse("March 31, 2008 23:59:59.999"); - baseTruncateTest(truncatedDate, lastTruncateDate, calendarField); - } + /** + * Test DateUtils.truncate()-method with Calendar.MONTH + * + * @throws Exception + * @since 3.0 + */ + public void testTruncateMonth() throws Exception { + final int calendarField = Calendar.MONTH; + Date truncatedDate = dateTimeParser.parse("March 1, 2008 0:00:00.000"); + Date lastTruncateDate = dateTimeParser.parse("March 31, 2008 23:59:59.999"); + baseTruncateTest(truncatedDate, lastTruncateDate, calendarField); + } - /** - * Test DateUtils.truncate()-method with DateUtils.SEMI_MONTH - * Includes truncating months with 28, 29, 30 and 31 days, each with first and second half - * - * @throws Exception - * @since 3.0 - */ - public void testTruncateSemiMonth() throws Exception { - final int calendarField = DateUtils.SEMI_MONTH; - Date truncatedDate, lastTruncateDate; - - //month with 28 days (1) - truncatedDate = dateTimeParser.parse("February 1, 2007 0:00:00.000"); - lastTruncateDate = dateTimeParser.parse("February 15, 2007 23:59:59.999"); - baseTruncateTest(truncatedDate, lastTruncateDate, calendarField); + /** + * Test DateUtils.truncate()-method with DateUtils.SEMI_MONTH + * Includes truncating months with 28, 29, 30 and 31 days, each with first and second half + * + * @throws Exception + * @since 3.0 + */ + public void testTruncateSemiMonth() throws Exception { + final int calendarField = DateUtils.SEMI_MONTH; + Date truncatedDate, lastTruncateDate; + + //month with 28 days (1) + truncatedDate = dateTimeParser.parse("February 1, 2007 0:00:00.000"); + lastTruncateDate = dateTimeParser.parse("February 15, 2007 23:59:59.999"); + baseTruncateTest(truncatedDate, lastTruncateDate, calendarField); - //month with 28 days (2) - truncatedDate = dateTimeParser.parse("February 16, 2007 0:00:00.000"); - lastTruncateDate = dateTimeParser.parse("February 28, 2007 23:59:59.999"); - baseTruncateTest(truncatedDate, lastTruncateDate, calendarField); + //month with 28 days (2) + truncatedDate = dateTimeParser.parse("February 16, 2007 0:00:00.000"); + lastTruncateDate = dateTimeParser.parse("February 28, 2007 23:59:59.999"); + baseTruncateTest(truncatedDate, lastTruncateDate, calendarField); - //month with 29 days (1) - truncatedDate = dateTimeParser.parse("February 1, 2008 0:00:00.000"); - lastTruncateDate = dateTimeParser.parse("February 15, 2008 23:59:59.999"); - baseTruncateTest(truncatedDate, lastTruncateDate, calendarField); + //month with 29 days (1) + truncatedDate = dateTimeParser.parse("February 1, 2008 0:00:00.000"); + lastTruncateDate = dateTimeParser.parse("February 15, 2008 23:59:59.999"); + baseTruncateTest(truncatedDate, lastTruncateDate, calendarField); - //month with 29 days (2) - truncatedDate = dateTimeParser.parse("February 16, 2008 0:00:00.000"); - lastTruncateDate = dateTimeParser.parse("February 29, 2008 23:59:59.999"); - baseTruncateTest(truncatedDate, lastTruncateDate, calendarField); + //month with 29 days (2) + truncatedDate = dateTimeParser.parse("February 16, 2008 0:00:00.000"); + lastTruncateDate = dateTimeParser.parse("February 29, 2008 23:59:59.999"); + baseTruncateTest(truncatedDate, lastTruncateDate, calendarField); - //month with 30 days (1) - truncatedDate = dateTimeParser.parse("April 1, 2008 0:00:00.000"); - lastTruncateDate = dateTimeParser.parse("April 15, 2008 23:59:59.999"); - baseTruncateTest(truncatedDate, lastTruncateDate, calendarField); + //month with 30 days (1) + truncatedDate = dateTimeParser.parse("April 1, 2008 0:00:00.000"); + lastTruncateDate = dateTimeParser.parse("April 15, 2008 23:59:59.999"); + baseTruncateTest(truncatedDate, lastTruncateDate, calendarField); - //month with 30 days (2) - truncatedDate = dateTimeParser.parse("April 16, 2008 0:00:00.000"); - lastTruncateDate = dateTimeParser.parse("April 30, 2008 23:59:59.999"); - baseTruncateTest(truncatedDate, lastTruncateDate, calendarField); - - //month with 31 days (1) - truncatedDate = dateTimeParser.parse("March 1, 2008 0:00:00.000"); - lastTruncateDate = dateTimeParser.parse("March 15, 2008 23:59:59.999"); - baseTruncateTest(truncatedDate, lastTruncateDate, calendarField); + //month with 30 days (2) + truncatedDate = dateTimeParser.parse("April 16, 2008 0:00:00.000"); + lastTruncateDate = dateTimeParser.parse("April 30, 2008 23:59:59.999"); + baseTruncateTest(truncatedDate, lastTruncateDate, calendarField); + + //month with 31 days (1) + truncatedDate = dateTimeParser.parse("March 1, 2008 0:00:00.000"); + lastTruncateDate = dateTimeParser.parse("March 15, 2008 23:59:59.999"); + baseTruncateTest(truncatedDate, lastTruncateDate, calendarField); - //month with 31 days (2) - truncatedDate = dateTimeParser.parse("March 16, 2008 0:00:00.000"); - lastTruncateDate = dateTimeParser.parse("March 31, 2008 23:59:59.999"); - baseTruncateTest(truncatedDate, lastTruncateDate, calendarField); + //month with 31 days (2) + truncatedDate = dateTimeParser.parse("March 16, 2008 0:00:00.000"); + lastTruncateDate = dateTimeParser.parse("March 31, 2008 23:59:59.999"); + baseTruncateTest(truncatedDate, lastTruncateDate, calendarField); - } + } - /** - * Test DateUtils.truncate()-method with Calendar.DATE - * - * @throws Exception - * @since 3.0 - */ - public void testTruncateDate() throws Exception { - final int calendarField = Calendar.DATE; - Date lastTruncateDate = dateTimeParser.parse("June 1, 2008 23:59:59.999"); - baseTruncateTest(targetDateDate, lastTruncateDate, calendarField); - } - - /** - * Test DateUtils.truncate()-method with Calendar.DAY_OF_MONTH - * - * @throws Exception - * @since 3.0 - */ - public void testTruncateDayOfMonth() throws Exception { - final int calendarField = Calendar.DAY_OF_MONTH; - Date lastTruncateDate = dateTimeParser.parse("June 1, 2008 23:59:59.999"); - baseTruncateTest(targetDayOfMonthDate, lastTruncateDate, calendarField); - } - - /** - * Test DateUtils.truncate()-method with Calendar.AM_PM - * Includes truncating the extremes of both AM and PM of one day - * - * @throws Exception - * @since 3.0 - */ - public void testTruncateAmPm() throws Exception { - final int calendarField = Calendar.AM_PM; - - //AM - Date lastTruncateDate = dateTimeParser.parse("June 1, 2008 11:59:59.999"); - baseTruncateTest(targetAmDate, lastTruncateDate, calendarField); + /** + * Test DateUtils.truncate()-method with Calendar.DATE + * + * @throws Exception + * @since 3.0 + */ + public void testTruncateDate() throws Exception { + final int calendarField = Calendar.DATE; + Date lastTruncateDate = dateTimeParser.parse("June 1, 2008 23:59:59.999"); + baseTruncateTest(targetDateDate, lastTruncateDate, calendarField); + } + + /** + * Test DateUtils.truncate()-method with Calendar.DAY_OF_MONTH + * + * @throws Exception + * @since 3.0 + */ + public void testTruncateDayOfMonth() throws Exception { + final int calendarField = Calendar.DAY_OF_MONTH; + Date lastTruncateDate = dateTimeParser.parse("June 1, 2008 23:59:59.999"); + baseTruncateTest(targetDayOfMonthDate, lastTruncateDate, calendarField); + } + + /** + * Test DateUtils.truncate()-method with Calendar.AM_PM + * Includes truncating the extremes of both AM and PM of one day + * + * @throws Exception + * @since 3.0 + */ + public void testTruncateAmPm() throws Exception { + final int calendarField = Calendar.AM_PM; + + //AM + Date lastTruncateDate = dateTimeParser.parse("June 1, 2008 11:59:59.999"); + baseTruncateTest(targetAmDate, lastTruncateDate, calendarField); - //PM - lastTruncateDate = dateTimeParser.parse("June 1, 2008 23:59:59.999"); - baseTruncateTest(targetPmDate, lastTruncateDate, calendarField); - } - - /** - * Test DateUtils.truncate()-method with Calendar.HOUR - * - * @throws Exception - * @since 3.0 - */ - public void testTruncateHour() throws Exception { - final int calendarField = Calendar.HOUR; - Date lastTruncateDate = dateTimeParser.parse("June 1, 2008 8:59:59.999"); - baseTruncateTest(targetHourDate, lastTruncateDate, calendarField); - } - - /** - * Test DateUtils.truncate()-method with Calendar.HOUR_OF_DAY - * - * @throws Exception - * @since 3.0 - */ - public void testTruncateHourOfDay() throws Exception { - final int calendarField = Calendar.HOUR_OF_DAY; - Date lastTruncateDate = dateTimeParser.parse("June 1, 2008 8:59:59.999"); - baseTruncateTest(targetHourOfDayDate, lastTruncateDate, calendarField); - } - - /** - * Test DateUtils.truncate()-method with Calendar.MINUTE - * - * @throws Exception - * @since 3.0 - */ - public void testTruncateMinute() throws Exception { - final int calendarField = Calendar.MINUTE; - Date lastTruncateDate = dateTimeParser.parse("June 1, 2008 8:15:59.999"); - baseTruncateTest(targetMinuteDate, lastTruncateDate, calendarField); - } - - /** - * Test DateUtils.truncate()-method with Calendar.SECOND - * - * @throws Exception - * @since 3.0 - */ - public void testTruncateSecond() throws Exception { - final int calendarField = Calendar.SECOND; - Date lastTruncateDate = dateTimeParser.parse("June 1, 2008 8:15:14.999"); - baseTruncateTest(targetSecondDate, lastTruncateDate, calendarField); - } - - /** - * Test DateUtils.truncate()-method with Calendar.SECOND - * - * @throws Exception - * @since 3.0 - */ - public void testTruncateMilliSecond() throws Exception { - final int calendarField = Calendar.MILLISECOND; - baseTruncateTest(targetMilliSecondDate, targetMilliSecondDate, calendarField); - } - - /** - * When using this basetest all extremes are tested.
- * It will test the Date, Calendar and Object-implementation
- * lastRoundDownDate should round down to roundedDownDate
- * lastRoundDownDate + 1 millisecond should round up to roundedUpDate - * - * @param roundedUpDate the next rounded date after roundedDownDate when using calendarField - * @param roundedDownDate the result if lastRoundDownDate was rounded with calendarField - * @param lastRoundDownDate rounding this value with calendarField will result in roundedDownDate - * @param calendarField - * @since 3.0 - */ - protected void baseRoundTest(final Date roundedUpDate, final Date roundedDownDate, final Date lastRoundDownDate, final int calendarField) { - Date firstRoundUpDate = DateUtils.addMilliseconds(lastRoundDownDate, 1); - - //Date-comparison - assertEquals(roundedDownDate, DateUtils.round(roundedDownDate, calendarField)); - assertEquals(roundedUpDate, DateUtils.round(roundedUpDate, calendarField)); - assertEquals(roundedDownDate, DateUtils.round(lastRoundDownDate, calendarField)); - assertEquals(roundedUpDate, DateUtils.round(firstRoundUpDate, calendarField)); - - //Calendar-initiations - Calendar roundedUpCalendar, roundedDownCalendar, lastRoundDownCalendar, firstRoundUpCalendar; - roundedDownCalendar = Calendar.getInstance(); - roundedUpCalendar = Calendar.getInstance(); - lastRoundDownCalendar = Calendar.getInstance(); - firstRoundUpCalendar = Calendar.getInstance(); - roundedDownCalendar.setTime(roundedDownDate); - roundedUpCalendar.setTime(roundedUpDate); - lastRoundDownCalendar.setTime(lastRoundDownDate); - firstRoundUpCalendar.setTime(firstRoundUpDate); + //PM + lastTruncateDate = dateTimeParser.parse("June 1, 2008 23:59:59.999"); + baseTruncateTest(targetPmDate, lastTruncateDate, calendarField); + } + + /** + * Test DateUtils.truncate()-method with Calendar.HOUR + * + * @throws Exception + * @since 3.0 + */ + public void testTruncateHour() throws Exception { + final int calendarField = Calendar.HOUR; + Date lastTruncateDate = dateTimeParser.parse("June 1, 2008 8:59:59.999"); + baseTruncateTest(targetHourDate, lastTruncateDate, calendarField); + } + + /** + * Test DateUtils.truncate()-method with Calendar.HOUR_OF_DAY + * + * @throws Exception + * @since 3.0 + */ + public void testTruncateHourOfDay() throws Exception { + final int calendarField = Calendar.HOUR_OF_DAY; + Date lastTruncateDate = dateTimeParser.parse("June 1, 2008 8:59:59.999"); + baseTruncateTest(targetHourOfDayDate, lastTruncateDate, calendarField); + } + + /** + * Test DateUtils.truncate()-method with Calendar.MINUTE + * + * @throws Exception + * @since 3.0 + */ + public void testTruncateMinute() throws Exception { + final int calendarField = Calendar.MINUTE; + Date lastTruncateDate = dateTimeParser.parse("June 1, 2008 8:15:59.999"); + baseTruncateTest(targetMinuteDate, lastTruncateDate, calendarField); + } + + /** + * Test DateUtils.truncate()-method with Calendar.SECOND + * + * @throws Exception + * @since 3.0 + */ + public void testTruncateSecond() throws Exception { + final int calendarField = Calendar.SECOND; + Date lastTruncateDate = dateTimeParser.parse("June 1, 2008 8:15:14.999"); + baseTruncateTest(targetSecondDate, lastTruncateDate, calendarField); + } + + /** + * Test DateUtils.truncate()-method with Calendar.SECOND + * + * @throws Exception + * @since 3.0 + */ + public void testTruncateMilliSecond() throws Exception { + final int calendarField = Calendar.MILLISECOND; + baseTruncateTest(targetMilliSecondDate, targetMilliSecondDate, calendarField); + } + + /** + * When using this basetest all extremes are tested.
+ * It will test the Date, Calendar and Object-implementation
+ * lastRoundDownDate should round down to roundedDownDate
+ * lastRoundDownDate + 1 millisecond should round up to roundedUpDate + * + * @param roundedUpDate the next rounded date after roundedDownDate when using calendarField + * @param roundedDownDate the result if lastRoundDownDate was rounded with calendarField + * @param lastRoundDownDate rounding this value with calendarField will result in roundedDownDate + * @param calendarField + * @since 3.0 + */ + protected void baseRoundTest(final Date roundedUpDate, final Date roundedDownDate, final Date lastRoundDownDate, final int calendarField) { + Date firstRoundUpDate = DateUtils.addMilliseconds(lastRoundDownDate, 1); + + //Date-comparison + assertEquals(roundedDownDate, DateUtils.round(roundedDownDate, calendarField)); + assertEquals(roundedUpDate, DateUtils.round(roundedUpDate, calendarField)); + assertEquals(roundedDownDate, DateUtils.round(lastRoundDownDate, calendarField)); + assertEquals(roundedUpDate, DateUtils.round(firstRoundUpDate, calendarField)); + + //Calendar-initiations + Calendar roundedUpCalendar, roundedDownCalendar, lastRoundDownCalendar, firstRoundUpCalendar; + roundedDownCalendar = Calendar.getInstance(); + roundedUpCalendar = Calendar.getInstance(); + lastRoundDownCalendar = Calendar.getInstance(); + firstRoundUpCalendar = Calendar.getInstance(); + roundedDownCalendar.setTime(roundedDownDate); + roundedUpCalendar.setTime(roundedUpDate); + lastRoundDownCalendar.setTime(lastRoundDownDate); + firstRoundUpCalendar.setTime(firstRoundUpDate); - //Calendar-comparison - assertEquals(roundedDownCalendar, DateUtils.round(roundedDownCalendar, calendarField)); - assertEquals(roundedUpCalendar, DateUtils.round(roundedUpCalendar, calendarField)); - assertEquals(roundedDownCalendar, DateUtils.round(lastRoundDownCalendar, calendarField)); - assertEquals(roundedUpCalendar, DateUtils.round(firstRoundUpCalendar, calendarField)); + //Calendar-comparison + assertEquals(roundedDownCalendar, DateUtils.round(roundedDownCalendar, calendarField)); + assertEquals(roundedUpCalendar, DateUtils.round(roundedUpCalendar, calendarField)); + assertEquals(roundedDownCalendar, DateUtils.round(lastRoundDownCalendar, calendarField)); + assertEquals(roundedUpCalendar, DateUtils.round(firstRoundUpCalendar, calendarField)); - //Object-comparison - assertEquals(roundedDownDate, DateUtils.round((Object) roundedDownDate, calendarField)); - assertEquals(roundedUpDate, DateUtils.round((Object) roundedUpDate, calendarField)); - assertEquals(roundedDownDate, DateUtils.round((Object) lastRoundDownDate, calendarField)); - assertEquals(roundedUpDate, DateUtils.round((Object) firstRoundUpDate, calendarField)); - assertEquals(roundedDownDate, DateUtils.round((Object) roundedDownCalendar, calendarField)); - assertEquals(roundedUpDate, DateUtils.round((Object) roundedUpCalendar, calendarField)); - assertEquals(roundedDownDate, DateUtils.round((Object) lastRoundDownDate, calendarField)); - assertEquals(roundedUpDate, DateUtils.round((Object) firstRoundUpDate, calendarField)); - } - - /** - * When using this basetest all extremes are tested.
- * It will test the Date, Calendar and Object-implementation
- * lastTruncateDate should round down to truncatedDate
- * lastTruncateDate + 1 millisecond should never round down to truncatedDate - * - * @param truncatedDate expected Date when lastTruncateDate is truncated with calendarField - * @param lastTruncateDate the last possible Date which will truncate to truncatedDate with calendarField - * @param calendarField a Calendar.field value - * @since 3.0 - */ - protected void baseTruncateTest(final Date truncatedDate, final Date lastTruncateDate, final int calendarField) { - Date nextTruncateDate = DateUtils.addMilliseconds(lastTruncateDate, 1); - - //Date-comparison - assertEquals("Truncating "+ fdf.format(truncatedDate) +" as Date with CalendarField-value "+ calendarField +" must return itself", truncatedDate, DateUtils.truncate(truncatedDate, calendarField)); - assertEquals(truncatedDate, DateUtils.truncate(lastTruncateDate, calendarField)); - assertFalse(fdf.format(lastTruncateDate) +" is not an extreme when truncating as Date with CalendarField-value "+ calendarField, truncatedDate.equals(DateUtils.truncate(nextTruncateDate, calendarField))); - - //Calendar-initiations - Calendar truncatedCalendar, lastTruncateCalendar, nextTruncateCalendar; - truncatedCalendar = Calendar.getInstance(); - lastTruncateCalendar = Calendar.getInstance(); - nextTruncateCalendar = Calendar.getInstance(); - truncatedCalendar.setTime(truncatedDate); - lastTruncateCalendar.setTime(lastTruncateDate); - nextTruncateCalendar.setTime(nextTruncateDate); + //Object-comparison + assertEquals(roundedDownDate, DateUtils.round((Object) roundedDownDate, calendarField)); + assertEquals(roundedUpDate, DateUtils.round((Object) roundedUpDate, calendarField)); + assertEquals(roundedDownDate, DateUtils.round((Object) lastRoundDownDate, calendarField)); + assertEquals(roundedUpDate, DateUtils.round((Object) firstRoundUpDate, calendarField)); + assertEquals(roundedDownDate, DateUtils.round((Object) roundedDownCalendar, calendarField)); + assertEquals(roundedUpDate, DateUtils.round((Object) roundedUpCalendar, calendarField)); + assertEquals(roundedDownDate, DateUtils.round((Object) lastRoundDownDate, calendarField)); + assertEquals(roundedUpDate, DateUtils.round((Object) firstRoundUpDate, calendarField)); + } + + /** + * When using this basetest all extremes are tested.
+ * It will test the Date, Calendar and Object-implementation
+ * lastTruncateDate should round down to truncatedDate
+ * lastTruncateDate + 1 millisecond should never round down to truncatedDate + * + * @param truncatedDate expected Date when lastTruncateDate is truncated with calendarField + * @param lastTruncateDate the last possible Date which will truncate to truncatedDate with calendarField + * @param calendarField a Calendar.field value + * @since 3.0 + */ + protected void baseTruncateTest(final Date truncatedDate, final Date lastTruncateDate, final int calendarField) { + Date nextTruncateDate = DateUtils.addMilliseconds(lastTruncateDate, 1); + + //Date-comparison + assertEquals("Truncating "+ fdf.format(truncatedDate) +" as Date with CalendarField-value "+ calendarField +" must return itself", truncatedDate, DateUtils.truncate(truncatedDate, calendarField)); + assertEquals(truncatedDate, DateUtils.truncate(lastTruncateDate, calendarField)); + assertFalse(fdf.format(lastTruncateDate) +" is not an extreme when truncating as Date with CalendarField-value "+ calendarField, truncatedDate.equals(DateUtils.truncate(nextTruncateDate, calendarField))); + + //Calendar-initiations + Calendar truncatedCalendar, lastTruncateCalendar, nextTruncateCalendar; + truncatedCalendar = Calendar.getInstance(); + lastTruncateCalendar = Calendar.getInstance(); + nextTruncateCalendar = Calendar.getInstance(); + truncatedCalendar.setTime(truncatedDate); + lastTruncateCalendar.setTime(lastTruncateDate); + nextTruncateCalendar.setTime(nextTruncateDate); - //Calendar-comparison - assertEquals("Truncating "+ fdf.format(truncatedCalendar) +" as Calendar with CalendarField-value "+ calendarField +" must return itself", truncatedCalendar, DateUtils.truncate(truncatedCalendar, calendarField)); - assertEquals(truncatedCalendar, DateUtils.truncate(lastTruncateCalendar, calendarField)); - assertFalse(fdf.format(lastTruncateCalendar) +" is not an extreme when truncating as Calendar with CalendarField-value "+ calendarField, truncatedCalendar.equals(DateUtils.truncate(nextTruncateCalendar, calendarField))); + //Calendar-comparison + assertEquals("Truncating "+ fdf.format(truncatedCalendar) +" as Calendar with CalendarField-value "+ calendarField +" must return itself", truncatedCalendar, DateUtils.truncate(truncatedCalendar, calendarField)); + assertEquals(truncatedCalendar, DateUtils.truncate(lastTruncateCalendar, calendarField)); + assertFalse(fdf.format(lastTruncateCalendar) +" is not an extreme when truncating as Calendar with CalendarField-value "+ calendarField, truncatedCalendar.equals(DateUtils.truncate(nextTruncateCalendar, calendarField))); - //Object-comparison - assertEquals("Truncating "+ fdf.format(truncatedDate) +" as Date cast to Object with CalendarField-value "+ calendarField +" must return itself as Date", truncatedDate, DateUtils.truncate((Object) truncatedDate, calendarField)); - assertEquals(truncatedDate, DateUtils.truncate((Object) lastTruncateDate, calendarField)); - assertFalse(fdf.format(lastTruncateDate) +" is not an extreme when truncating as Date cast to Object with CalendarField-value "+ calendarField, truncatedDate.equals(DateUtils.truncate((Object) nextTruncateDate, calendarField))); - assertEquals("Truncating "+ fdf.format(truncatedCalendar) +" as Calendar cast to Object with CalendarField-value "+ calendarField +" must return itself as Date", truncatedDate, DateUtils.truncate((Object) truncatedCalendar, calendarField)); - assertEquals(truncatedDate, DateUtils.truncate((Object) lastTruncateCalendar, calendarField)); - assertFalse(fdf.format(lastTruncateCalendar) +" is not an extreme when truncating as Calendar cast to Object with CalendarField-value "+ calendarField, truncatedDate.equals(DateUtils.truncate((Object) nextTruncateCalendar, calendarField))); - } - - /** - * - * Any January 1 could be considered as the ultimate extreme. - * Instead of comparing the results if the input has a difference of 1 millisecond we check the output to be exactly January first. - * - * @param minDate - * @param maxDate - * @param calendarField - * @since 3.0 - */ - protected void roundToJanuaryFirst(Date minDate, Date maxDate, int calendarField) { - assertEquals("Rounding "+ fdf.format(januaryOneDate) +" as Date with CalendarField-value "+ calendarField +" must return itself", januaryOneDate, DateUtils.round(januaryOneDate, calendarField)); - assertEquals(januaryOneDate, DateUtils.round(minDate, calendarField)); - assertEquals(januaryOneDate, DateUtils.round(maxDate, calendarField)); - - Calendar minCalendar = Calendar.getInstance(); - minCalendar.setTime(minDate); - Calendar maxCalendar = Calendar.getInstance(); - maxCalendar.setTime(maxDate); - assertEquals("Rounding "+ fdf.format(januaryOneCalendar) +" as Date with CalendarField-value "+ calendarField +" must return itself", januaryOneCalendar, DateUtils.round(januaryOneCalendar, calendarField)); - assertEquals(januaryOneCalendar, DateUtils.round(minCalendar, calendarField)); - assertEquals(januaryOneCalendar, DateUtils.round(maxCalendar, calendarField)); + //Object-comparison + assertEquals("Truncating "+ fdf.format(truncatedDate) +" as Date cast to Object with CalendarField-value "+ calendarField +" must return itself as Date", truncatedDate, DateUtils.truncate((Object) truncatedDate, calendarField)); + assertEquals(truncatedDate, DateUtils.truncate((Object) lastTruncateDate, calendarField)); + assertFalse(fdf.format(lastTruncateDate) +" is not an extreme when truncating as Date cast to Object with CalendarField-value "+ calendarField, truncatedDate.equals(DateUtils.truncate((Object) nextTruncateDate, calendarField))); + assertEquals("Truncating "+ fdf.format(truncatedCalendar) +" as Calendar cast to Object with CalendarField-value "+ calendarField +" must return itself as Date", truncatedDate, DateUtils.truncate((Object) truncatedCalendar, calendarField)); + assertEquals(truncatedDate, DateUtils.truncate((Object) lastTruncateCalendar, calendarField)); + assertFalse(fdf.format(lastTruncateCalendar) +" is not an extreme when truncating as Calendar cast to Object with CalendarField-value "+ calendarField, truncatedDate.equals(DateUtils.truncate((Object) nextTruncateCalendar, calendarField))); + } + + /** + * + * Any January 1 could be considered as the ultimate extreme. + * Instead of comparing the results if the input has a difference of 1 millisecond we check the output to be exactly January first. + * + * @param minDate + * @param maxDate + * @param calendarField + * @since 3.0 + */ + protected void roundToJanuaryFirst(Date minDate, Date maxDate, int calendarField) { + assertEquals("Rounding "+ fdf.format(januaryOneDate) +" as Date with CalendarField-value "+ calendarField +" must return itself", januaryOneDate, DateUtils.round(januaryOneDate, calendarField)); + assertEquals(januaryOneDate, DateUtils.round(minDate, calendarField)); + assertEquals(januaryOneDate, DateUtils.round(maxDate, calendarField)); + + Calendar minCalendar = Calendar.getInstance(); + minCalendar.setTime(minDate); + Calendar maxCalendar = Calendar.getInstance(); + maxCalendar.setTime(maxDate); + assertEquals("Rounding "+ fdf.format(januaryOneCalendar) +" as Date with CalendarField-value "+ calendarField +" must return itself", januaryOneCalendar, DateUtils.round(januaryOneCalendar, calendarField)); + assertEquals(januaryOneCalendar, DateUtils.round(minCalendar, calendarField)); + assertEquals(januaryOneCalendar, DateUtils.round(maxCalendar, calendarField)); - Date toPrevRoundDate = DateUtils.addMilliseconds(minDate, -1); - Date toNextRoundDate = DateUtils.addMilliseconds(maxDate, 1); - assertFalse(fdf.format(minDate) +" is not an lower-extreme when rounding as Date with CalendarField-value "+ calendarField, januaryOneDate.equals(DateUtils.round(toPrevRoundDate, calendarField))); - assertFalse(fdf.format(maxDate) +" is not an upper-extreme when rounding as Date with CalendarField-value "+ calendarField, januaryOneDate.equals(DateUtils.round(toNextRoundDate, calendarField))); - - Calendar toPrevRoundCalendar = Calendar.getInstance(); - toPrevRoundCalendar.setTime(toPrevRoundDate); - Calendar toNextRoundCalendar = Calendar.getInstance(); - toNextRoundCalendar.setTime(toNextRoundDate); - assertFalse(fdf.format(minCalendar) +" is not an lower-extreme when rounding as Date with CalendarField-value "+ calendarField, januaryOneDate.equals(DateUtils.round(toPrevRoundDate, calendarField))); - assertFalse(fdf.format(maxCalendar) +" is not an upper-extreme when rounding as Date with CalendarField-value "+ calendarField, januaryOneDate.equals(DateUtils.round(toNextRoundDate, calendarField))); - } + Date toPrevRoundDate = DateUtils.addMilliseconds(minDate, -1); + Date toNextRoundDate = DateUtils.addMilliseconds(maxDate, 1); + assertFalse(fdf.format(minDate) +" is not an lower-extreme when rounding as Date with CalendarField-value "+ calendarField, januaryOneDate.equals(DateUtils.round(toPrevRoundDate, calendarField))); + assertFalse(fdf.format(maxDate) +" is not an upper-extreme when rounding as Date with CalendarField-value "+ calendarField, januaryOneDate.equals(DateUtils.round(toNextRoundDate, calendarField))); + + Calendar toPrevRoundCalendar = Calendar.getInstance(); + toPrevRoundCalendar.setTime(toPrevRoundDate); + Calendar toNextRoundCalendar = Calendar.getInstance(); + toNextRoundCalendar.setTime(toNextRoundDate); + assertFalse(fdf.format(minCalendar) +" is not an lower-extreme when rounding as Date with CalendarField-value "+ calendarField, januaryOneDate.equals(DateUtils.round(toPrevRoundDate, calendarField))); + assertFalse(fdf.format(maxCalendar) +" is not an upper-extreme when rounding as Date with CalendarField-value "+ calendarField, januaryOneDate.equals(DateUtils.round(toNextRoundDate, calendarField))); + } } diff --git a/src/test/org/apache/commons/lang/time/DateUtilsTest.java b/src/test/org/apache/commons/lang/time/DateUtilsTest.java index e0952aa6c..59a8be20c 100644 --- a/src/test/org/apache/commons/lang/time/DateUtilsTest.java +++ b/src/test/org/apache/commons/lang/time/DateUtilsTest.java @@ -439,150 +439,150 @@ public class DateUtilsTest extends TestCase { assertDate(result, 2000, 6, 5, 4, 3, 2, 0); } - // ----------------------------------------------------------------------- - public void testSetYears() throws Exception { - Date base = new Date(MILLIS_TEST); - Date result = DateUtils.setYears(base, 2000); - assertNotSame(base, result); - assertDate(base, 2000, 6, 5, 4, 3, 2, 1); - assertDate(result, 2000, 6, 5, 4, 3, 2, 1); + // ----------------------------------------------------------------------- + public void testSetYears() throws Exception { + Date base = new Date(MILLIS_TEST); + Date result = DateUtils.setYears(base, 2000); + assertNotSame(base, result); + assertDate(base, 2000, 6, 5, 4, 3, 2, 1); + assertDate(result, 2000, 6, 5, 4, 3, 2, 1); - result = DateUtils.setYears(base, 2008); - assertNotSame(base, result); - assertDate(base, 2000, 6, 5, 4, 3, 2, 1); - assertDate(result, 2008, 6, 5, 4, 3, 2, 1); + result = DateUtils.setYears(base, 2008); + assertNotSame(base, result); + assertDate(base, 2000, 6, 5, 4, 3, 2, 1); + assertDate(result, 2008, 6, 5, 4, 3, 2, 1); - result = DateUtils.setYears(base, 2005); - assertNotSame(base, result); - assertDate(base, 2000, 6, 5, 4, 3, 2, 1); - assertDate(result, 2005, 6, 5, 4, 3, 2, 1); - } + result = DateUtils.setYears(base, 2005); + assertNotSame(base, result); + assertDate(base, 2000, 6, 5, 4, 3, 2, 1); + assertDate(result, 2005, 6, 5, 4, 3, 2, 1); + } - // ----------------------------------------------------------------------- - public void testSetMonths() throws Exception { - Date base = new Date(MILLIS_TEST); - Date result = DateUtils.setMonths(base, 5); - assertNotSame(base, result); - assertDate(base, 2000, 6, 5, 4, 3, 2, 1); - assertDate(result, 2000, 5, 5, 4, 3, 2, 1); + // ----------------------------------------------------------------------- + public void testSetMonths() throws Exception { + Date base = new Date(MILLIS_TEST); + Date result = DateUtils.setMonths(base, 5); + assertNotSame(base, result); + assertDate(base, 2000, 6, 5, 4, 3, 2, 1); + assertDate(result, 2000, 5, 5, 4, 3, 2, 1); - result = DateUtils.setMonths(base, 1); - assertNotSame(base, result); - assertDate(base, 2000, 6, 5, 4, 3, 2, 1); - assertDate(result, 2000, 1, 5, 4, 3, 2, 1); + result = DateUtils.setMonths(base, 1); + assertNotSame(base, result); + assertDate(base, 2000, 6, 5, 4, 3, 2, 1); + assertDate(result, 2000, 1, 5, 4, 3, 2, 1); - try { - result = DateUtils.setMonths(base, 12); - fail("DateUtils.setMonths did not throw an expected IllegalArguementException."); - } catch (IllegalArgumentException e) { + try { + result = DateUtils.setMonths(base, 12); + fail("DateUtils.setMonths did not throw an expected IllegalArguementException."); + } catch (IllegalArgumentException e) { - } - } + } + } - // ----------------------------------------------------------------------- - public void testSetDays() throws Exception { - Date base = new Date(MILLIS_TEST); - Date result = DateUtils.setDays(base, 1); - assertNotSame(base, result); - assertDate(base, 2000, 6, 5, 4, 3, 2, 1); - assertDate(result, 2000, 6, 1, 4, 3, 2, 1); + // ----------------------------------------------------------------------- + public void testSetDays() throws Exception { + Date base = new Date(MILLIS_TEST); + Date result = DateUtils.setDays(base, 1); + assertNotSame(base, result); + assertDate(base, 2000, 6, 5, 4, 3, 2, 1); + assertDate(result, 2000, 6, 1, 4, 3, 2, 1); - result = DateUtils.setDays(base, 29); - assertNotSame(base, result); - assertDate(base, 2000, 6, 5, 4, 3, 2, 1); - assertDate(result, 2000, 6, 29, 4, 3, 2, 1); + result = DateUtils.setDays(base, 29); + assertNotSame(base, result); + assertDate(base, 2000, 6, 5, 4, 3, 2, 1); + assertDate(result, 2000, 6, 29, 4, 3, 2, 1); - try { - result = DateUtils.setDays(base, 32); - fail("DateUtils.setDays did not throw an expected IllegalArguementException."); - } catch (IllegalArgumentException e) { + try { + result = DateUtils.setDays(base, 32); + fail("DateUtils.setDays did not throw an expected IllegalArguementException."); + } catch (IllegalArgumentException e) { - } - } + } + } - // ----------------------------------------------------------------------- - public void testSetHours() throws Exception { - Date base = new Date(MILLIS_TEST); - Date result = DateUtils.setHours(base, 0); - assertNotSame(base, result); - assertDate(base, 2000, 6, 5, 4, 3, 2, 1); - assertDate(result, 2000, 6, 5, 0, 3, 2, 1); + // ----------------------------------------------------------------------- + public void testSetHours() throws Exception { + Date base = new Date(MILLIS_TEST); + Date result = DateUtils.setHours(base, 0); + assertNotSame(base, result); + assertDate(base, 2000, 6, 5, 4, 3, 2, 1); + assertDate(result, 2000, 6, 5, 0, 3, 2, 1); - result = DateUtils.setHours(base, 23); - assertNotSame(base, result); - assertDate(base, 2000, 6, 5, 4, 3, 2, 1); - assertDate(result, 2000, 6, 5, 23, 3, 2, 1); + result = DateUtils.setHours(base, 23); + assertNotSame(base, result); + assertDate(base, 2000, 6, 5, 4, 3, 2, 1); + assertDate(result, 2000, 6, 5, 23, 3, 2, 1); - try { - result = DateUtils.setHours(base, 24); - fail("DateUtils.setHours did not throw an expected IllegalArguementException."); - } catch (IllegalArgumentException e) { + try { + result = DateUtils.setHours(base, 24); + fail("DateUtils.setHours did not throw an expected IllegalArguementException."); + } catch (IllegalArgumentException e) { - } - } + } + } - // ----------------------------------------------------------------------- - public void testSetMinutes() throws Exception { - Date base = new Date(MILLIS_TEST); - Date result = DateUtils.setMinutes(base, 0); - assertNotSame(base, result); - assertDate(base, 2000, 6, 5, 4, 3, 2, 1); - assertDate(result, 2000, 6, 5, 4, 0, 2, 1); + // ----------------------------------------------------------------------- + public void testSetMinutes() throws Exception { + Date base = new Date(MILLIS_TEST); + Date result = DateUtils.setMinutes(base, 0); + assertNotSame(base, result); + assertDate(base, 2000, 6, 5, 4, 3, 2, 1); + assertDate(result, 2000, 6, 5, 4, 0, 2, 1); - result = DateUtils.setMinutes(base, 59); - assertNotSame(base, result); - assertDate(base, 2000, 6, 5, 4, 3, 2, 1); - assertDate(result, 2000, 6, 5, 4, 59, 2, 1); + result = DateUtils.setMinutes(base, 59); + assertNotSame(base, result); + assertDate(base, 2000, 6, 5, 4, 3, 2, 1); + assertDate(result, 2000, 6, 5, 4, 59, 2, 1); - try { - result = DateUtils.setMinutes(base, 60); - fail("DateUtils.setMinutes did not throw an expected IllegalArguementException."); - } catch (IllegalArgumentException e) { + try { + result = DateUtils.setMinutes(base, 60); + fail("DateUtils.setMinutes did not throw an expected IllegalArguementException."); + } catch (IllegalArgumentException e) { - } - } + } + } - // ----------------------------------------------------------------------- - public void testSetSeconds() throws Exception { - Date base = new Date(MILLIS_TEST); - Date result = DateUtils.setSeconds(base, 0); - assertNotSame(base, result); - assertDate(base, 2000, 6, 5, 4, 3, 2, 1); - assertDate(result, 2000, 6, 5, 4, 3, 0, 1); + // ----------------------------------------------------------------------- + public void testSetSeconds() throws Exception { + Date base = new Date(MILLIS_TEST); + Date result = DateUtils.setSeconds(base, 0); + assertNotSame(base, result); + assertDate(base, 2000, 6, 5, 4, 3, 2, 1); + assertDate(result, 2000, 6, 5, 4, 3, 0, 1); - result = DateUtils.setSeconds(base, 59); - assertNotSame(base, result); - assertDate(base, 2000, 6, 5, 4, 3, 2, 1); - assertDate(result, 2000, 6, 5, 4, 3, 59, 1); + result = DateUtils.setSeconds(base, 59); + assertNotSame(base, result); + assertDate(base, 2000, 6, 5, 4, 3, 2, 1); + assertDate(result, 2000, 6, 5, 4, 3, 59, 1); - try { - result = DateUtils.setSeconds(base, 60); - fail("DateUtils.setSeconds did not throw an expected IllegalArguementException."); - } catch (IllegalArgumentException e) { + try { + result = DateUtils.setSeconds(base, 60); + fail("DateUtils.setSeconds did not throw an expected IllegalArguementException."); + } catch (IllegalArgumentException e) { - } - } + } + } - // ----------------------------------------------------------------------- - public void testSetMilliseconds() throws Exception { - Date base = new Date(MILLIS_TEST); - Date result = DateUtils.setMilliseconds(base, 0); - assertNotSame(base, result); - assertDate(base, 2000, 6, 5, 4, 3, 2, 1); - assertDate(result, 2000, 6, 5, 4, 3, 2, 0); + // ----------------------------------------------------------------------- + public void testSetMilliseconds() throws Exception { + Date base = new Date(MILLIS_TEST); + Date result = DateUtils.setMilliseconds(base, 0); + assertNotSame(base, result); + assertDate(base, 2000, 6, 5, 4, 3, 2, 1); + assertDate(result, 2000, 6, 5, 4, 3, 2, 0); - result = DateUtils.setMilliseconds(base, 999); - assertNotSame(base, result); - assertDate(base, 2000, 6, 5, 4, 3, 2, 1); - assertDate(result, 2000, 6, 5, 4, 3, 2, 999); + result = DateUtils.setMilliseconds(base, 999); + assertNotSame(base, result); + assertDate(base, 2000, 6, 5, 4, 3, 2, 1); + assertDate(result, 2000, 6, 5, 4, 3, 2, 999); - try { - result = DateUtils.setMilliseconds(base, 1000); - fail("DateUtils.setMilliseconds did not throw an expected IllegalArguementException."); - } catch (IllegalArgumentException e) { + try { + result = DateUtils.setMilliseconds(base, 1000); + fail("DateUtils.setMilliseconds did not throw an expected IllegalArguementException."); + } catch (IllegalArgumentException e) { - } - } + } + } //----------------------------------------------------------------------- private void assertDate(Date date, int year, int month, int day, int hour, int min, int sec, int mil) throws Exception {