Sort members.

This commit is contained in:
Gary Gregory 2021-02-24 10:05:34 -05:00
parent b57f292cdf
commit 4859ad52de
15 changed files with 2842 additions and 2842 deletions

View File

@ -26,8 +26,8 @@ import org.junit.jupiter.api.Test;
public class CalendarUtilsTest {
@Test
public void testGetYear() {
assertEquals(Calendar.getInstance().get(Calendar.YEAR), CalendarUtils.INSTANCE.getYear());
public void testGetDayOfMonth() {
assertEquals(Calendar.getInstance().get(Calendar.DAY_OF_MONTH), CalendarUtils.INSTANCE.getDayOfMonth());
}
@Test
@ -36,8 +36,8 @@ public class CalendarUtilsTest {
}
@Test
public void testGetDayOfMonth() {
assertEquals(Calendar.getInstance().get(Calendar.DAY_OF_MONTH), CalendarUtils.INSTANCE.getDayOfMonth());
public void testGetYear() {
assertEquals(Calendar.getInstance().get(Calendar.YEAR), CalendarUtils.INSTANCE.getYear());
}
}

View File

@ -38,6 +38,24 @@ import org.junitpioneer.jupiter.DefaultTimeZone;
*/
@SuppressWarnings("deprecation") // tests lots of deprecated items
public class DateFormatUtilsTest {
private void assertFormats(final String expectedValue, final String pattern, final TimeZone timeZone, final Calendar cal) {
assertEquals(expectedValue, DateFormatUtils.format(cal.getTime(), pattern, timeZone));
assertEquals(expectedValue, DateFormatUtils.format(cal.getTime().getTime(), pattern, timeZone));
assertEquals(expectedValue, DateFormatUtils.format(cal, pattern, timeZone));
}
private Calendar createFebruaryTestDate(final TimeZone timeZone) {
final Calendar cal = Calendar.getInstance(timeZone);
cal.set(2002, Calendar.FEBRUARY, 23, 9, 11, 12);
return cal;
}
private Calendar createJuneTestDate(final TimeZone timeZone) {
final Calendar cal = Calendar.getInstance(timeZone);
cal.set(2003, Calendar.JUNE, 8, 10, 11, 12);
return cal;
}
//-----------------------------------------------------------------------
@Test
public void testConstructor() {
@ -49,6 +67,20 @@ public class DateFormatUtilsTest {
assertFalse(Modifier.isFinal(DateFormatUtils.class.getModifiers()));
}
@Test
public void testDateISO() {
testGmtMinus3("2002-02-23", DateFormatUtils.ISO_DATE_FORMAT.getPattern());
testGmtMinus3("2002-02-23-03:00", DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.getPattern());
testUTC("2002-02-23Z", DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.getPattern());
}
@Test
public void testDateTimeISO() {
testGmtMinus3("2002-02-23T09:11:12", DateFormatUtils.ISO_DATETIME_FORMAT.getPattern());
testGmtMinus3("2002-02-23T09:11:12-03:00", DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern());
testUTC("2002-02-23T09:11:12Z", DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern());
}
//-----------------------------------------------------------------------
@Test
public void testFormat() {
@ -110,77 +142,11 @@ public class DateFormatUtilsTest {
assertEquals ("2005-01-01T12:00:00", DateFormatUtils.formatUTC(c.getTime().getTime(), DateFormatUtils.ISO_DATETIME_FORMAT.getPattern(), Locale.US));
}
private void assertFormats(final String expectedValue, final String pattern, final TimeZone timeZone, final Calendar cal) {
assertEquals(expectedValue, DateFormatUtils.format(cal.getTime(), pattern, timeZone));
assertEquals(expectedValue, DateFormatUtils.format(cal.getTime().getTime(), pattern, timeZone));
assertEquals(expectedValue, DateFormatUtils.format(cal, pattern, timeZone));
}
private Calendar createFebruaryTestDate(final TimeZone timeZone) {
final Calendar cal = Calendar.getInstance(timeZone);
cal.set(2002, Calendar.FEBRUARY, 23, 9, 11, 12);
return cal;
}
private Calendar createJuneTestDate(final TimeZone timeZone) {
final Calendar cal = Calendar.getInstance(timeZone);
cal.set(2003, Calendar.JUNE, 8, 10, 11, 12);
return cal;
}
private void testGmtMinus3(final String expectedValue, final String pattern) {
final TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
assertFormats(expectedValue, pattern, timeZone, createFebruaryTestDate(timeZone));
}
private void testUTC(final String expectedValue, final String pattern) {
final TimeZone timeZone = FastTimeZone.getGmtTimeZone();
assertFormats(expectedValue, pattern, timeZone, createFebruaryTestDate(timeZone));
}
@Test
public void testDateTimeISO() {
testGmtMinus3("2002-02-23T09:11:12", DateFormatUtils.ISO_DATETIME_FORMAT.getPattern());
testGmtMinus3("2002-02-23T09:11:12-03:00", DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern());
testUTC("2002-02-23T09:11:12Z", DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern());
}
@Test
public void testDateISO() {
testGmtMinus3("2002-02-23", DateFormatUtils.ISO_DATE_FORMAT.getPattern());
testGmtMinus3("2002-02-23-03:00", DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.getPattern());
testUTC("2002-02-23Z", DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.getPattern());
}
@Test
public void testTimeISO() {
testGmtMinus3("T09:11:12", DateFormatUtils.ISO_TIME_FORMAT.getPattern());
testGmtMinus3("T09:11:12-03:00", DateFormatUtils.ISO_TIME_TIME_ZONE_FORMAT.getPattern());
testUTC("T09:11:12Z", DateFormatUtils.ISO_TIME_TIME_ZONE_FORMAT.getPattern());
}
@Test
public void testTimeNoTISO() {
testGmtMinus3("09:11:12", DateFormatUtils.ISO_TIME_NO_T_FORMAT.getPattern());
testGmtMinus3("09:11:12-03:00", DateFormatUtils.ISO_TIME_NO_T_TIME_ZONE_FORMAT.getPattern());
testUTC("09:11:12Z", DateFormatUtils.ISO_TIME_NO_T_TIME_ZONE_FORMAT.getPattern());
}
@DefaultLocale(language = "en")
@Test
public void testSMTP() {
TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
Calendar june = createJuneTestDate(timeZone);
assertFormats("Sun, 08 Jun 2003 10:11:12 -0300", DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(),
timeZone, june);
timeZone = FastTimeZone.getGmtTimeZone();
june = createJuneTestDate(timeZone);
assertFormats("Sun, 08 Jun 2003 10:11:12 +0000", DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(),
timeZone, june);
}
@Test
public void testLANG1000() throws Exception {
final String date = "2013-11-18T12:48:05Z";
@ -238,4 +204,38 @@ public class DateFormatUtilsTest {
assertEquals("2009-10-16T07:42:16+01:00", value, "calendar");
}
}
@DefaultLocale(language = "en")
@Test
public void testSMTP() {
TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
Calendar june = createJuneTestDate(timeZone);
assertFormats("Sun, 08 Jun 2003 10:11:12 -0300", DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(),
timeZone, june);
timeZone = FastTimeZone.getGmtTimeZone();
june = createJuneTestDate(timeZone);
assertFormats("Sun, 08 Jun 2003 10:11:12 +0000", DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(),
timeZone, june);
}
@Test
public void testTimeISO() {
testGmtMinus3("T09:11:12", DateFormatUtils.ISO_TIME_FORMAT.getPattern());
testGmtMinus3("T09:11:12-03:00", DateFormatUtils.ISO_TIME_TIME_ZONE_FORMAT.getPattern());
testUTC("T09:11:12Z", DateFormatUtils.ISO_TIME_TIME_ZONE_FORMAT.getPattern());
}
@Test
public void testTimeNoTISO() {
testGmtMinus3("09:11:12", DateFormatUtils.ISO_TIME_NO_T_FORMAT.getPattern());
testGmtMinus3("09:11:12-03:00", DateFormatUtils.ISO_TIME_NO_T_TIME_ZONE_FORMAT.getPattern());
testUTC("09:11:12Z", DateFormatUtils.ISO_TIME_NO_T_TIME_ZONE_FORMAT.getPattern());
}
private void testUTC(final String expectedValue, final String pattern) {
final TimeZone timeZone = FastTimeZone.getGmtTimeZone();
assertFormats(expectedValue, pattern, timeZone, createFebruaryTestDate(timeZone));
}
}

View File

@ -47,137 +47,8 @@ public class DateUtilsFragmentTest {
}
@Test
public void testNullDate() {
assertThrows(
NullPointerException.class,
() -> DateUtils.getFragmentInMilliseconds((Date) null, Calendar.MILLISECOND));
assertThrows(
NullPointerException.class,
() -> DateUtils.getFragmentInSeconds((Date) null, Calendar.MILLISECOND));
assertThrows(
NullPointerException.class,
() -> DateUtils.getFragmentInMinutes((Date) null, Calendar.MILLISECOND));
assertThrows(
NullPointerException.class,
() -> DateUtils.getFragmentInHours((Date) null, Calendar.MILLISECOND));
assertThrows(
NullPointerException.class,
() -> DateUtils.getFragmentInDays((Date) null, Calendar.MILLISECOND));
}
@Test
public void testNullCalendar() {
assertThrows(
IllegalArgumentException.class,
() -> DateUtils.getFragmentInMilliseconds((Calendar) null, Calendar.MILLISECOND));
assertThrows(
IllegalArgumentException.class,
() -> DateUtils.getFragmentInSeconds((Calendar) null, Calendar.MILLISECOND));
assertThrows(
IllegalArgumentException.class,
() -> DateUtils.getFragmentInMinutes((Calendar) null, Calendar.MILLISECOND));
assertThrows(
IllegalArgumentException.class,
() -> DateUtils.getFragmentInHours((Calendar) null, Calendar.MILLISECOND));
assertThrows(
IllegalArgumentException.class,
() -> DateUtils.getFragmentInDays((Calendar) null, Calendar.MILLISECOND));
}
@Test
public void testInvalidFragmentWithDate() {
assertThrows(IllegalArgumentException.class, () -> DateUtils.getFragmentInMilliseconds(aDate, 0));
assertThrows(IllegalArgumentException.class, () -> DateUtils.getFragmentInSeconds(aDate, 0));
assertThrows(IllegalArgumentException.class, () -> DateUtils.getFragmentInMinutes(aDate, 0));
assertThrows(IllegalArgumentException.class, () -> DateUtils.getFragmentInHours(aDate, 0));
assertThrows(IllegalArgumentException.class, () -> DateUtils.getFragmentInDays(aDate, 0));
}
@Test
public void testInvalidFragmentWithCalendar() {
assertThrows(IllegalArgumentException.class, () -> DateUtils.getFragmentInMilliseconds(aCalendar, 0));
assertThrows(IllegalArgumentException.class, () -> DateUtils.getFragmentInSeconds(aCalendar, 0));
assertThrows(IllegalArgumentException.class, () -> DateUtils.getFragmentInMinutes(aCalendar, 0));
assertThrows(IllegalArgumentException.class, () -> DateUtils.getFragmentInHours(aCalendar, 0));
assertThrows(IllegalArgumentException.class, () -> DateUtils.getFragmentInDays(aCalendar, 0));
}
@Test
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));
}
@Test
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));
}
@Test
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));
}
@Test
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));
}
@Test
public void testMinuteFragmentInLargerUnitWithDate() {
assertEquals(0, DateUtils.getFragmentInMinutes(aDate, Calendar.MINUTE));
assertEquals(0, DateUtils.getFragmentInHours(aDate, Calendar.MINUTE));
assertEquals(0, DateUtils.getFragmentInDays(aDate, Calendar.MINUTE));
}
@Test
public void testMinuteFragmentInLargerUnitWithCalendar() {
assertEquals(0, DateUtils.getFragmentInMinutes(aCalendar, Calendar.MINUTE));
assertEquals(0, DateUtils.getFragmentInHours(aCalendar, Calendar.MINUTE));
assertEquals(0, DateUtils.getFragmentInDays(aCalendar, Calendar.MINUTE));
}
@Test
public void testHourOfDayFragmentInLargerUnitWithDate() {
assertEquals(0, DateUtils.getFragmentInHours(aDate, Calendar.HOUR_OF_DAY));
assertEquals(0, DateUtils.getFragmentInDays(aDate, Calendar.HOUR_OF_DAY));
}
@Test
public void testHourOfDayFragmentInLargerUnitWithCalendar() {
assertEquals(0, DateUtils.getFragmentInHours(aCalendar, Calendar.HOUR_OF_DAY));
assertEquals(0, DateUtils.getFragmentInDays(aCalendar, Calendar.HOUR_OF_DAY));
}
@Test
public void testDayOfYearFragmentInLargerUnitWithDate() {
assertEquals(0, DateUtils.getFragmentInDays(aDate, Calendar.DAY_OF_YEAR));
}
@Test
public void testDayOfYearFragmentInLargerUnitWithCalendar() {
assertEquals(0, DateUtils.getFragmentInDays(aCalendar, Calendar.DAY_OF_YEAR));
public void testDateFragmentInLargerUnitWithCalendar() {
assertEquals(0, DateUtils.getFragmentInDays(aCalendar, Calendar.DATE));
}
@Test
@ -186,335 +57,13 @@ public class DateUtilsFragmentTest {
}
@Test
public void testDateFragmentInLargerUnitWithCalendar() {
assertEquals(0, DateUtils.getFragmentInDays(aCalendar, Calendar.DATE));
}
//Calendar.SECOND as useful fragment
@Test
public void testMillisecondsOfSecondWithDate() {
final long testResult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.SECOND);
assertEquals(millis, testResult);
public void testDayOfYearFragmentInLargerUnitWithCalendar() {
assertEquals(0, DateUtils.getFragmentInDays(aCalendar, Calendar.DAY_OF_YEAR));
}
@Test
public void testMillisecondsOfSecondWithCalendar() {
final long testResult = DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.SECOND);
assertEquals(millis, testResult);
assertEquals(aCalendar.get(Calendar.MILLISECOND), testResult);
}
//Calendar.MINUTE as useful fragment
@Test
public void testMillisecondsOfMinuteWithDate() {
final long testResult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.MINUTE);
assertEquals(millis + (seconds * DateUtils.MILLIS_PER_SECOND), testResult);
}
@Test
public void testMillisecondsOfMinuteWithCalender() {
final long testResult = DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.MINUTE);
assertEquals(millis + (seconds * DateUtils.MILLIS_PER_SECOND), testResult);
}
@Test
public void testSecondsofMinuteWithDate() {
final long testResult = DateUtils.getFragmentInSeconds(aDate, Calendar.MINUTE);
assertEquals(seconds, testResult);
}
@Test
public void testSecondsofMinuteWithCalendar() {
final long testResult = DateUtils.getFragmentInSeconds(aCalendar, Calendar.MINUTE);
assertEquals(seconds, testResult);
assertEquals(aCalendar.get(Calendar.SECOND), testResult);
}
//Calendar.HOUR_OF_DAY as useful fragment
@Test
public void testMillisecondsOfHourWithDate() {
final long testResult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.HOUR_OF_DAY);
assertEquals(millis + (seconds * DateUtils.MILLIS_PER_SECOND) + (minutes * DateUtils.MILLIS_PER_MINUTE), testResult);
}
@Test
public void testMillisecondsOfHourWithCalendar() {
final long testResult = DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.HOUR_OF_DAY);
assertEquals(millis + (seconds * DateUtils.MILLIS_PER_SECOND) + (minutes * DateUtils.MILLIS_PER_MINUTE), testResult);
}
@Test
public void testSecondsofHourWithDate() {
final long testResult = DateUtils.getFragmentInSeconds(aDate, Calendar.HOUR_OF_DAY);
assertEquals(
seconds
+ (minutes
* DateUtils.MILLIS_PER_MINUTE / DateUtils.MILLIS_PER_SECOND),
testResult);
}
@Test
public void testSecondsofHourWithCalendar() {
final long testResult = DateUtils.getFragmentInSeconds(aCalendar, Calendar.HOUR_OF_DAY);
assertEquals(
seconds
+ (minutes
* DateUtils.MILLIS_PER_MINUTE / DateUtils.MILLIS_PER_SECOND),
testResult);
}
@Test
public void testMinutesOfHourWithDate() {
final long testResult = DateUtils.getFragmentInMinutes(aDate, Calendar.HOUR_OF_DAY);
assertEquals(minutes, testResult);
}
@Test
public void testMinutesOfHourWithCalendar() {
final long testResult = DateUtils.getFragmentInMinutes(aCalendar, Calendar.HOUR_OF_DAY);
assertEquals(minutes, testResult);
}
//Calendar.DATE and Calendar.DAY_OF_YEAR as useful fragment
@Test
public void testMillisecondsOfDayWithDate() {
long testresult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.DATE);
final 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);
}
@Test
public void testMillisecondsOfDayWithCalendar() {
long testresult = DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.DATE);
final 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);
}
@Test
public void testSecondsOfDayWithDate() {
long testresult = DateUtils.getFragmentInSeconds(aDate, Calendar.DATE);
final 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);
}
@Test
public void testSecondsOfDayWithCalendar() {
long testresult = DateUtils.getFragmentInSeconds(aCalendar, Calendar.DATE);
final 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);
}
@Test
public void testMinutesOfDayWithDate() {
long testResult = DateUtils.getFragmentInMinutes(aDate, Calendar.DATE);
final 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);
}
@Test
public void testMinutesOfDayWithCalendar() {
long testResult = DateUtils.getFragmentInMinutes(aCalendar, Calendar.DATE);
final 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);
}
@Test
public void testHoursOfDayWithDate() {
long testResult = DateUtils.getFragmentInHours(aDate, Calendar.DATE);
final long expectedValue = hours;
assertEquals(expectedValue, testResult);
testResult = DateUtils.getFragmentInHours(aDate, Calendar.DAY_OF_YEAR);
assertEquals(expectedValue, testResult);
}
@Test
public void testHoursOfDayWithCalendar() {
long testResult = DateUtils.getFragmentInHours(aCalendar, Calendar.DATE);
final long expectedValue = hours;
assertEquals(expectedValue, testResult);
testResult = DateUtils.getFragmentInHours(aCalendar, Calendar.DAY_OF_YEAR);
assertEquals(expectedValue, testResult);
}
//Calendar.MONTH as useful fragment
@Test
public void testMillisecondsOfMonthWithDate() {
final 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 - 1) * DateUtils.MILLIS_PER_DAY),
testResult);
}
@Test
public void testMillisecondsOfMonthWithCalendar() {
final 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 - 1) * DateUtils.MILLIS_PER_DAY),
testResult);
}
@Test
public void testSecondsOfMonthWithDate() {
final long testResult = DateUtils.getFragmentInSeconds(aDate, Calendar.MONTH);
assertEquals(
seconds
+ ((minutes * DateUtils.MILLIS_PER_MINUTE)
+ (hours * DateUtils.MILLIS_PER_HOUR) + ((days - 1) * DateUtils.MILLIS_PER_DAY))
/ DateUtils.MILLIS_PER_SECOND,
testResult);
}
@Test
public void testSecondsOfMonthWithCalendar() {
final long testResult = DateUtils.getFragmentInSeconds(aCalendar, Calendar.MONTH);
assertEquals(
seconds
+ ((minutes * DateUtils.MILLIS_PER_MINUTE)
+ (hours * DateUtils.MILLIS_PER_HOUR) + ((days - 1) * DateUtils.MILLIS_PER_DAY))
/ DateUtils.MILLIS_PER_SECOND,
testResult);
}
@Test
public void testMinutesOfMonthWithDate() {
final long testResult = DateUtils.getFragmentInMinutes(aDate, Calendar.MONTH);
assertEquals(minutes
+ ((hours * DateUtils.MILLIS_PER_HOUR) + ((days - 1) * DateUtils.MILLIS_PER_DAY))
/ DateUtils.MILLIS_PER_MINUTE,
testResult);
}
@Test
public void testMinutesOfMonthWithCalendar() {
final long testResult = DateUtils.getFragmentInMinutes(aCalendar, Calendar.MONTH);
assertEquals( minutes +((hours * DateUtils.MILLIS_PER_HOUR) + ((days - 1) * DateUtils.MILLIS_PER_DAY))
/ DateUtils.MILLIS_PER_MINUTE,
testResult);
}
@Test
public void testHoursOfMonthWithDate() {
final long testResult = DateUtils.getFragmentInHours(aDate, Calendar.MONTH);
assertEquals(hours + (((days - 1) * DateUtils.MILLIS_PER_DAY))
/ DateUtils.MILLIS_PER_HOUR,
testResult);
}
@Test
public void testHoursOfMonthWithCalendar() {
final long testResult = DateUtils.getFragmentInHours(aCalendar, Calendar.MONTH);
assertEquals( hours +(((days - 1) * DateUtils.MILLIS_PER_DAY))
/ DateUtils.MILLIS_PER_HOUR,
testResult);
}
//Calendar.YEAR as useful fragment
@Test
public void testMillisecondsOfYearWithDate() {
final long testResult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.YEAR);
final 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) - 1)* DateUtils.MILLIS_PER_DAY),
testResult);
}
@Test
public void testMillisecondsOfYearWithCalendar() {
final 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) - 1) * DateUtils.MILLIS_PER_DAY),
testResult);
}
@Test
public void testSecondsOfYearWithDate() {
final long testResult = DateUtils.getFragmentInSeconds(aDate, Calendar.YEAR);
final 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) - 1) * DateUtils.MILLIS_PER_DAY))
/ DateUtils.MILLIS_PER_SECOND,
testResult);
}
@Test
public void testSecondsOfYearWithCalendar() {
final 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) - 1) * DateUtils.MILLIS_PER_DAY))
/ DateUtils.MILLIS_PER_SECOND,
testResult);
}
@Test
public void testMinutesOfYearWithDate() {
final long testResult = DateUtils.getFragmentInMinutes(aDate, Calendar.YEAR);
final Calendar cal = Calendar.getInstance();
cal.setTime(aDate);
assertEquals(minutes
+ ((hours * DateUtils.MILLIS_PER_HOUR) + ((cal.get(Calendar.DAY_OF_YEAR) - 1) * DateUtils.MILLIS_PER_DAY))
/ DateUtils.MILLIS_PER_MINUTE,
testResult);
}
@Test
public void testMinutesOfYearWithCalendar() {
final long testResult = DateUtils.getFragmentInMinutes(aCalendar, Calendar.YEAR);
assertEquals( minutes +((hours * DateUtils.MILLIS_PER_HOUR) + ((aCalendar.get(Calendar.DAY_OF_YEAR) - 1) * DateUtils.MILLIS_PER_DAY))
/ DateUtils.MILLIS_PER_MINUTE,
testResult);
}
@Test
public void testMinutesOfYearWithWrongOffsetBugWithCalendar() {
final Calendar c = Calendar.getInstance();
c.set(Calendar.MONTH, Calendar.JANUARY);
c.set(Calendar.DAY_OF_YEAR, 1);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
final long testResult = DateUtils.getFragmentInMinutes(c, Calendar.YEAR);
assertEquals( 0, testResult);
}
@Test
public void testHoursOfYearWithDate() {
final long testResult = DateUtils.getFragmentInHours(aDate, Calendar.YEAR);
final Calendar cal = Calendar.getInstance();
cal.setTime(aDate);
assertEquals(hours + (((cal.get(Calendar.DAY_OF_YEAR) - 1) * DateUtils.MILLIS_PER_DAY))
/ DateUtils.MILLIS_PER_HOUR,
testResult);
}
@Test
public void testHoursOfYearWithCalendar() {
final long testResult = DateUtils.getFragmentInHours(aCalendar, Calendar.YEAR);
assertEquals( hours +(((aCalendar.get(Calendar.DAY_OF_YEAR) - 1) * DateUtils.MILLIS_PER_DAY))
/ DateUtils.MILLIS_PER_HOUR,
testResult);
public void testDayOfYearFragmentInLargerUnitWithDate() {
assertEquals(0, DateUtils.getFragmentInDays(aDate, Calendar.DAY_OF_YEAR));
}
@Test
@ -544,4 +93,455 @@ testResult);
cal.setTime(aDate);
assertEquals(cal.get(Calendar.DAY_OF_YEAR), testResult);
}
@Test
public void testHourOfDayFragmentInLargerUnitWithCalendar() {
assertEquals(0, DateUtils.getFragmentInHours(aCalendar, Calendar.HOUR_OF_DAY));
assertEquals(0, DateUtils.getFragmentInDays(aCalendar, Calendar.HOUR_OF_DAY));
}
@Test
public void testHourOfDayFragmentInLargerUnitWithDate() {
assertEquals(0, DateUtils.getFragmentInHours(aDate, Calendar.HOUR_OF_DAY));
assertEquals(0, DateUtils.getFragmentInDays(aDate, Calendar.HOUR_OF_DAY));
}
@Test
public void testHoursOfDayWithCalendar() {
long testResult = DateUtils.getFragmentInHours(aCalendar, Calendar.DATE);
final long expectedValue = hours;
assertEquals(expectedValue, testResult);
testResult = DateUtils.getFragmentInHours(aCalendar, Calendar.DAY_OF_YEAR);
assertEquals(expectedValue, testResult);
}
@Test
public void testHoursOfDayWithDate() {
long testResult = DateUtils.getFragmentInHours(aDate, Calendar.DATE);
final long expectedValue = hours;
assertEquals(expectedValue, testResult);
testResult = DateUtils.getFragmentInHours(aDate, Calendar.DAY_OF_YEAR);
assertEquals(expectedValue, testResult);
}
@Test
public void testHoursOfMonthWithCalendar() {
final long testResult = DateUtils.getFragmentInHours(aCalendar, Calendar.MONTH);
assertEquals( hours +(((days - 1) * DateUtils.MILLIS_PER_DAY))
/ DateUtils.MILLIS_PER_HOUR,
testResult);
}
@Test
public void testHoursOfMonthWithDate() {
final long testResult = DateUtils.getFragmentInHours(aDate, Calendar.MONTH);
assertEquals(hours + (((days - 1) * DateUtils.MILLIS_PER_DAY))
/ DateUtils.MILLIS_PER_HOUR,
testResult);
}
@Test
public void testHoursOfYearWithCalendar() {
final long testResult = DateUtils.getFragmentInHours(aCalendar, Calendar.YEAR);
assertEquals( hours +(((aCalendar.get(Calendar.DAY_OF_YEAR) - 1) * DateUtils.MILLIS_PER_DAY))
/ DateUtils.MILLIS_PER_HOUR,
testResult);
}
@Test
public void testHoursOfYearWithDate() {
final long testResult = DateUtils.getFragmentInHours(aDate, Calendar.YEAR);
final Calendar cal = Calendar.getInstance();
cal.setTime(aDate);
assertEquals(hours + (((cal.get(Calendar.DAY_OF_YEAR) - 1) * DateUtils.MILLIS_PER_DAY))
/ DateUtils.MILLIS_PER_HOUR,
testResult);
}
//Calendar.SECOND as useful fragment
@Test
public void testInvalidFragmentWithCalendar() {
assertThrows(IllegalArgumentException.class, () -> DateUtils.getFragmentInMilliseconds(aCalendar, 0));
assertThrows(IllegalArgumentException.class, () -> DateUtils.getFragmentInSeconds(aCalendar, 0));
assertThrows(IllegalArgumentException.class, () -> DateUtils.getFragmentInMinutes(aCalendar, 0));
assertThrows(IllegalArgumentException.class, () -> DateUtils.getFragmentInHours(aCalendar, 0));
assertThrows(IllegalArgumentException.class, () -> DateUtils.getFragmentInDays(aCalendar, 0));
}
@Test
public void testInvalidFragmentWithDate() {
assertThrows(IllegalArgumentException.class, () -> DateUtils.getFragmentInMilliseconds(aDate, 0));
assertThrows(IllegalArgumentException.class, () -> DateUtils.getFragmentInSeconds(aDate, 0));
assertThrows(IllegalArgumentException.class, () -> DateUtils.getFragmentInMinutes(aDate, 0));
assertThrows(IllegalArgumentException.class, () -> DateUtils.getFragmentInHours(aDate, 0));
assertThrows(IllegalArgumentException.class, () -> DateUtils.getFragmentInDays(aDate, 0));
}
//Calendar.MINUTE as useful fragment
@Test
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));
}
@Test
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));
}
@Test
public void testMillisecondsOfDayWithCalendar() {
long testresult = DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.DATE);
final 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
@Test
public void testMillisecondsOfDayWithDate() {
long testresult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.DATE);
final 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);
}
//Calendar.HOUR_OF_DAY as useful fragment
@Test
public void testMillisecondsOfHourWithCalendar() {
final long testResult = DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.HOUR_OF_DAY);
assertEquals(millis + (seconds * DateUtils.MILLIS_PER_SECOND) + (minutes * DateUtils.MILLIS_PER_MINUTE), testResult);
}
@Test
public void testMillisecondsOfHourWithDate() {
final long testResult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.HOUR_OF_DAY);
assertEquals(millis + (seconds * DateUtils.MILLIS_PER_SECOND) + (minutes * DateUtils.MILLIS_PER_MINUTE), testResult);
}
@Test
public void testMillisecondsOfMinuteWithCalender() {
final long testResult = DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.MINUTE);
assertEquals(millis + (seconds * DateUtils.MILLIS_PER_SECOND), testResult);
}
@Test
public void testMillisecondsOfMinuteWithDate() {
final long testResult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.MINUTE);
assertEquals(millis + (seconds * DateUtils.MILLIS_PER_SECOND), testResult);
}
@Test
public void testMillisecondsOfMonthWithCalendar() {
final 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 - 1) * DateUtils.MILLIS_PER_DAY),
testResult);
}
//Calendar.MONTH as useful fragment
@Test
public void testMillisecondsOfMonthWithDate() {
final 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 - 1) * DateUtils.MILLIS_PER_DAY),
testResult);
}
@Test
public void testMillisecondsOfSecondWithCalendar() {
final long testResult = DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.SECOND);
assertEquals(millis, testResult);
assertEquals(aCalendar.get(Calendar.MILLISECOND), testResult);
}
@Test
public void testMillisecondsOfSecondWithDate() {
final long testResult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.SECOND);
assertEquals(millis, testResult);
}
@Test
public void testMillisecondsOfYearWithCalendar() {
final 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) - 1) * DateUtils.MILLIS_PER_DAY),
testResult);
}
//Calendar.YEAR as useful fragment
@Test
public void testMillisecondsOfYearWithDate() {
final long testResult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.YEAR);
final 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) - 1)* DateUtils.MILLIS_PER_DAY),
testResult);
}
@Test
public void testMinuteFragmentInLargerUnitWithCalendar() {
assertEquals(0, DateUtils.getFragmentInMinutes(aCalendar, Calendar.MINUTE));
assertEquals(0, DateUtils.getFragmentInHours(aCalendar, Calendar.MINUTE));
assertEquals(0, DateUtils.getFragmentInDays(aCalendar, Calendar.MINUTE));
}
@Test
public void testMinuteFragmentInLargerUnitWithDate() {
assertEquals(0, DateUtils.getFragmentInMinutes(aDate, Calendar.MINUTE));
assertEquals(0, DateUtils.getFragmentInHours(aDate, Calendar.MINUTE));
assertEquals(0, DateUtils.getFragmentInDays(aDate, Calendar.MINUTE));
}
@Test
public void testMinutesOfDayWithCalendar() {
long testResult = DateUtils.getFragmentInMinutes(aCalendar, Calendar.DATE);
final 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);
}
@Test
public void testMinutesOfDayWithDate() {
long testResult = DateUtils.getFragmentInMinutes(aDate, Calendar.DATE);
final 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);
}
@Test
public void testMinutesOfHourWithCalendar() {
final long testResult = DateUtils.getFragmentInMinutes(aCalendar, Calendar.HOUR_OF_DAY);
assertEquals(minutes, testResult);
}
@Test
public void testMinutesOfHourWithDate() {
final long testResult = DateUtils.getFragmentInMinutes(aDate, Calendar.HOUR_OF_DAY);
assertEquals(minutes, testResult);
}
@Test
public void testMinutesOfMonthWithCalendar() {
final long testResult = DateUtils.getFragmentInMinutes(aCalendar, Calendar.MONTH);
assertEquals( minutes +((hours * DateUtils.MILLIS_PER_HOUR) + ((days - 1) * DateUtils.MILLIS_PER_DAY))
/ DateUtils.MILLIS_PER_MINUTE,
testResult);
}
@Test
public void testMinutesOfMonthWithDate() {
final long testResult = DateUtils.getFragmentInMinutes(aDate, Calendar.MONTH);
assertEquals(minutes
+ ((hours * DateUtils.MILLIS_PER_HOUR) + ((days - 1) * DateUtils.MILLIS_PER_DAY))
/ DateUtils.MILLIS_PER_MINUTE,
testResult);
}
@Test
public void testMinutesOfYearWithCalendar() {
final long testResult = DateUtils.getFragmentInMinutes(aCalendar, Calendar.YEAR);
assertEquals( minutes +((hours * DateUtils.MILLIS_PER_HOUR) + ((aCalendar.get(Calendar.DAY_OF_YEAR) - 1) * DateUtils.MILLIS_PER_DAY))
/ DateUtils.MILLIS_PER_MINUTE,
testResult);
}
@Test
public void testMinutesOfYearWithDate() {
final long testResult = DateUtils.getFragmentInMinutes(aDate, Calendar.YEAR);
final Calendar cal = Calendar.getInstance();
cal.setTime(aDate);
assertEquals(minutes
+ ((hours * DateUtils.MILLIS_PER_HOUR) + ((cal.get(Calendar.DAY_OF_YEAR) - 1) * DateUtils.MILLIS_PER_DAY))
/ DateUtils.MILLIS_PER_MINUTE,
testResult);
}
@Test
public void testMinutesOfYearWithWrongOffsetBugWithCalendar() {
final Calendar c = Calendar.getInstance();
c.set(Calendar.MONTH, Calendar.JANUARY);
c.set(Calendar.DAY_OF_YEAR, 1);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
final long testResult = DateUtils.getFragmentInMinutes(c, Calendar.YEAR);
assertEquals( 0, testResult);
}
@Test
public void testNullCalendar() {
assertThrows(
IllegalArgumentException.class,
() -> DateUtils.getFragmentInMilliseconds((Calendar) null, Calendar.MILLISECOND));
assertThrows(
IllegalArgumentException.class,
() -> DateUtils.getFragmentInSeconds((Calendar) null, Calendar.MILLISECOND));
assertThrows(
IllegalArgumentException.class,
() -> DateUtils.getFragmentInMinutes((Calendar) null, Calendar.MILLISECOND));
assertThrows(
IllegalArgumentException.class,
() -> DateUtils.getFragmentInHours((Calendar) null, Calendar.MILLISECOND));
assertThrows(
IllegalArgumentException.class,
() -> DateUtils.getFragmentInDays((Calendar) null, Calendar.MILLISECOND));
}
@Test
public void testNullDate() {
assertThrows(
NullPointerException.class,
() -> DateUtils.getFragmentInMilliseconds((Date) null, Calendar.MILLISECOND));
assertThrows(
NullPointerException.class,
() -> DateUtils.getFragmentInSeconds((Date) null, Calendar.MILLISECOND));
assertThrows(
NullPointerException.class,
() -> DateUtils.getFragmentInMinutes((Date) null, Calendar.MILLISECOND));
assertThrows(
NullPointerException.class,
() -> DateUtils.getFragmentInHours((Date) null, Calendar.MILLISECOND));
assertThrows(
NullPointerException.class,
() -> DateUtils.getFragmentInDays((Date) null, Calendar.MILLISECOND));
}
@Test
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));
}
@Test
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));
}
@Test
public void testSecondsOfDayWithCalendar() {
long testresult = DateUtils.getFragmentInSeconds(aCalendar, Calendar.DATE);
final 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);
}
@Test
public void testSecondsOfDayWithDate() {
long testresult = DateUtils.getFragmentInSeconds(aDate, Calendar.DATE);
final 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);
}
@Test
public void testSecondsofHourWithCalendar() {
final long testResult = DateUtils.getFragmentInSeconds(aCalendar, Calendar.HOUR_OF_DAY);
assertEquals(
seconds
+ (minutes
* DateUtils.MILLIS_PER_MINUTE / DateUtils.MILLIS_PER_SECOND),
testResult);
}
@Test
public void testSecondsofHourWithDate() {
final long testResult = DateUtils.getFragmentInSeconds(aDate, Calendar.HOUR_OF_DAY);
assertEquals(
seconds
+ (minutes
* DateUtils.MILLIS_PER_MINUTE / DateUtils.MILLIS_PER_SECOND),
testResult);
}
@Test
public void testSecondsofMinuteWithCalendar() {
final long testResult = DateUtils.getFragmentInSeconds(aCalendar, Calendar.MINUTE);
assertEquals(seconds, testResult);
assertEquals(aCalendar.get(Calendar.SECOND), testResult);
}
@Test
public void testSecondsofMinuteWithDate() {
final long testResult = DateUtils.getFragmentInSeconds(aDate, Calendar.MINUTE);
assertEquals(seconds, testResult);
}
@Test
public void testSecondsOfMonthWithCalendar() {
final long testResult = DateUtils.getFragmentInSeconds(aCalendar, Calendar.MONTH);
assertEquals(
seconds
+ ((minutes * DateUtils.MILLIS_PER_MINUTE)
+ (hours * DateUtils.MILLIS_PER_HOUR) + ((days - 1) * DateUtils.MILLIS_PER_DAY))
/ DateUtils.MILLIS_PER_SECOND,
testResult);
}
@Test
public void testSecondsOfMonthWithDate() {
final long testResult = DateUtils.getFragmentInSeconds(aDate, Calendar.MONTH);
assertEquals(
seconds
+ ((minutes * DateUtils.MILLIS_PER_MINUTE)
+ (hours * DateUtils.MILLIS_PER_HOUR) + ((days - 1) * DateUtils.MILLIS_PER_DAY))
/ DateUtils.MILLIS_PER_SECOND,
testResult);
}
@Test
public void testSecondsOfYearWithCalendar() {
final 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) - 1) * DateUtils.MILLIS_PER_DAY))
/ DateUtils.MILLIS_PER_SECOND,
testResult);
}
@Test
public void testSecondsOfYearWithDate() {
final long testResult = DateUtils.getFragmentInSeconds(aDate, Calendar.YEAR);
final 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) - 1) * DateUtils.MILLIS_PER_DAY))
/ DateUtils.MILLIS_PER_SECOND,
testResult);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -37,6 +37,52 @@ import org.junit.jupiter.api.Test;
*/
public class DurationFormatUtilsTest {
private static final int FOUR_YEARS = 365 * 3 + 366;
private void assertEqualDuration(final String expected, final int[] start, final int[] end, final String format) {
assertEqualDuration(null, expected, start, end, format);
}
private void assertEqualDuration(final String message, final String expected, final int[] start, final int[] end, final String format) {
final Calendar cal1 = Calendar.getInstance();
cal1.set(start[0], start[1], start[2], start[3], start[4], start[5]);
cal1.set(Calendar.MILLISECOND, 0);
final Calendar cal2 = Calendar.getInstance();
cal2.set(end[0], end[1], end[2], end[3], end[4], end[5]);
cal2.set(Calendar.MILLISECOND, 0);
final long milli1 = cal1.getTime().getTime();
final long milli2 = cal2.getTime().getTime();
final String result = DurationFormatUtils.formatPeriod(milli1, milli2, format);
if (message == null) {
assertEquals(expected, result);
} else {
assertEquals(expected, result, message);
}
}
private void bruteForce(final int year, final int month, final int day, final String format, final int calendarType) {
final String msg = year + "-" + month + "-" + day + " to ";
final Calendar c = Calendar.getInstance();
c.set(year, month, day, 0, 0, 0);
final int[] array1 = new int[] { year, month, day, 0, 0, 0 };
final int[] array2 = new int[] { year, month, day, 0, 0, 0 };
for (int i=0; i < FOUR_YEARS; i++) {
array2[0] = c.get(Calendar.YEAR);
array2[1] = c.get(Calendar.MONTH);
array2[2] = c.get(Calendar.DAY_OF_MONTH);
final String tmpMsg = msg + array2[0] + "-" + array2[1] + "-" + array2[2] + " at ";
assertEqualDuration( tmpMsg + i, Integer.toString(i), array1, array2, format );
c.add(calendarType, 1);
}
}
// https://issues.apache.org/bugzilla/show_bug.cgi?id=38401
@Test
public void testBugzilla38401() {
assertEqualDuration( "0000/00/30 16:00:00 000", new int[] { 2006, 0, 26, 18, 47, 34 },
new int[] { 2006, 1, 26, 10, 47, 34 }, "yyyy/MM/dd HH:mm:ss SSS");
}
// -----------------------------------------------------------------------
@Test
public void testConstructor() {
@ -48,404 +94,16 @@ public class DurationFormatUtilsTest {
assertFalse(Modifier.isFinal(DurationFormatUtils.class.getModifiers()));
}
// -----------------------------------------------------------------------
@Test
public void testFormatDurationWords() {
String text;
public void testDurationsByBruteForce() {
bruteForce(2006, 0, 1, "d", Calendar.DAY_OF_MONTH);
bruteForce(2006, 0, 2, "d", Calendar.DAY_OF_MONTH);
bruteForce(2007, 1, 2, "d", Calendar.DAY_OF_MONTH);
bruteForce(2004, 1, 29, "d", Calendar.DAY_OF_MONTH);
bruteForce(1996, 1, 29, "d", Calendar.DAY_OF_MONTH);
text = DurationFormatUtils.formatDurationWords(50 * 1000, true, false);
assertEquals("50 seconds", text);
text = DurationFormatUtils.formatDurationWords(65 * 1000, true, false);
assertEquals("1 minute 5 seconds", text);
text = DurationFormatUtils.formatDurationWords(120 * 1000, true, false);
assertEquals("2 minutes 0 seconds", text);
text = DurationFormatUtils.formatDurationWords(121 * 1000, true, false);
assertEquals("2 minutes 1 second", text);
text = DurationFormatUtils.formatDurationWords(72 * 60 * 1000, true, false);
assertEquals("1 hour 12 minutes 0 seconds", text);
text = DurationFormatUtils.formatDurationWords(24 * 60 * 60 * 1000, true, false);
assertEquals("1 day 0 hours 0 minutes 0 seconds", text);
text = DurationFormatUtils.formatDurationWords(50 * 1000, true, true);
assertEquals("50 seconds", text);
text = DurationFormatUtils.formatDurationWords(65 * 1000, true, true);
assertEquals("1 minute 5 seconds", text);
text = DurationFormatUtils.formatDurationWords(120 * 1000, true, true);
assertEquals("2 minutes", text);
text = DurationFormatUtils.formatDurationWords(121 * 1000, true, true);
assertEquals("2 minutes 1 second", text);
text = DurationFormatUtils.formatDurationWords(72 * 60 * 1000, true, true);
assertEquals("1 hour 12 minutes", text);
text = DurationFormatUtils.formatDurationWords(24 * 60 * 60 * 1000, true, true);
assertEquals("1 day", text);
text = DurationFormatUtils.formatDurationWords(50 * 1000, false, true);
assertEquals("0 days 0 hours 0 minutes 50 seconds", text);
text = DurationFormatUtils.formatDurationWords(65 * 1000, false, true);
assertEquals("0 days 0 hours 1 minute 5 seconds", text);
text = DurationFormatUtils.formatDurationWords(120 * 1000, false, true);
assertEquals("0 days 0 hours 2 minutes", text);
text = DurationFormatUtils.formatDurationWords(121 * 1000, false, true);
assertEquals("0 days 0 hours 2 minutes 1 second", text);
text = DurationFormatUtils.formatDurationWords(72 * 60 * 1000, false, true);
assertEquals("0 days 1 hour 12 minutes", text);
text = DurationFormatUtils.formatDurationWords(24 * 60 * 60 * 1000, false, true);
assertEquals("1 day", text);
text = DurationFormatUtils.formatDurationWords(50 * 1000, false, false);
assertEquals("0 days 0 hours 0 minutes 50 seconds", text);
text = DurationFormatUtils.formatDurationWords(65 * 1000, false, false);
assertEquals("0 days 0 hours 1 minute 5 seconds", text);
text = DurationFormatUtils.formatDurationWords(120 * 1000, false, false);
assertEquals("0 days 0 hours 2 minutes 0 seconds", text);
text = DurationFormatUtils.formatDurationWords(121 * 1000, false, false);
assertEquals("0 days 0 hours 2 minutes 1 second", text);
text = DurationFormatUtils.formatDurationWords(72 * 60 * 1000, false, false);
assertEquals("0 days 1 hour 12 minutes 0 seconds", text);
text = DurationFormatUtils.formatDurationWords(24 * 60 * 60 * 1000 + 72 * 60 * 1000, false, false);
assertEquals("1 day 1 hour 12 minutes 0 seconds", text);
text = DurationFormatUtils.formatDurationWords(2 * 24 * 60 * 60 * 1000 + 72 * 60 * 1000, false, false);
assertEquals("2 days 1 hour 12 minutes 0 seconds", text);
for (int i = 2; i < 31; i++) {
text = DurationFormatUtils.formatDurationWords(i * 24 * 60 * 60 * 1000L, false, false);
assertEquals(i + " days 0 hours 0 minutes 0 seconds", text);
}
}
/**
* Tests that "1 &lt;unit&gt;s" gets converted to "1 &lt;unit&gt;" but that "11 &lt;unit&gt;s" is left alone.
*/
@Test
public void testFormatDurationPluralWords() {
final long oneSecond = 1000;
final long oneMinute = oneSecond * 60;
final long oneHour = oneMinute * 60;
final long oneDay = oneHour * 24;
String text;
text = DurationFormatUtils.formatDurationWords(oneSecond, false, false);
assertEquals("0 days 0 hours 0 minutes 1 second", text);
text = DurationFormatUtils.formatDurationWords(oneSecond * 2, false, false);
assertEquals("0 days 0 hours 0 minutes 2 seconds", text);
text = DurationFormatUtils.formatDurationWords(oneSecond * 11, false, false);
assertEquals("0 days 0 hours 0 minutes 11 seconds", text);
text = DurationFormatUtils.formatDurationWords(oneMinute, false, false);
assertEquals("0 days 0 hours 1 minute 0 seconds", text);
text = DurationFormatUtils.formatDurationWords(oneMinute * 2, false, false);
assertEquals("0 days 0 hours 2 minutes 0 seconds", text);
text = DurationFormatUtils.formatDurationWords(oneMinute * 11, false, false);
assertEquals("0 days 0 hours 11 minutes 0 seconds", text);
text = DurationFormatUtils.formatDurationWords(oneMinute + oneSecond, false, false);
assertEquals("0 days 0 hours 1 minute 1 second", text);
text = DurationFormatUtils.formatDurationWords(oneHour, false, false);
assertEquals("0 days 1 hour 0 minutes 0 seconds", text);
text = DurationFormatUtils.formatDurationWords(oneHour * 2, false, false);
assertEquals("0 days 2 hours 0 minutes 0 seconds", text);
text = DurationFormatUtils.formatDurationWords(oneHour * 11, false, false);
assertEquals("0 days 11 hours 0 minutes 0 seconds", text);
text = DurationFormatUtils.formatDurationWords(oneHour + oneMinute + oneSecond, false, false);
assertEquals("0 days 1 hour 1 minute 1 second", text);
text = DurationFormatUtils.formatDurationWords(oneDay, false, false);
assertEquals("1 day 0 hours 0 minutes 0 seconds", text);
text = DurationFormatUtils.formatDurationWords(oneDay * 2, false, false);
assertEquals("2 days 0 hours 0 minutes 0 seconds", text);
text = DurationFormatUtils.formatDurationWords(oneDay * 11, false, false);
assertEquals("11 days 0 hours 0 minutes 0 seconds", text);
text = DurationFormatUtils.formatDurationWords(oneDay + oneHour + oneMinute + oneSecond, false, false);
assertEquals("1 day 1 hour 1 minute 1 second", text);
}
@Test
public void testFormatNegativeDurationWords() {
assertThrows(IllegalArgumentException.class, () -> DurationFormatUtils.formatDurationWords(-5000, true, true));
}
@Test
public void testFormatDurationHMS() {
long time = 0;
assertEquals("00:00:00.000", DurationFormatUtils.formatDurationHMS(time));
time = 1;
assertEquals("00:00:00.001", DurationFormatUtils.formatDurationHMS(time));
time = 15;
assertEquals("00:00:00.015", DurationFormatUtils.formatDurationHMS(time));
time = 165;
assertEquals("00:00:00.165", DurationFormatUtils.formatDurationHMS(time));
time = 1675;
assertEquals("00:00:01.675", DurationFormatUtils.formatDurationHMS(time));
time = 13465;
assertEquals("00:00:13.465", DurationFormatUtils.formatDurationHMS(time));
time = 72789;
assertEquals("00:01:12.789", DurationFormatUtils.formatDurationHMS(time));
time = 12789 + 32 * 60000;
assertEquals("00:32:12.789", DurationFormatUtils.formatDurationHMS(time));
time = 12789 + 62 * 60000;
assertEquals("01:02:12.789", DurationFormatUtils.formatDurationHMS(time));
}
@Test
public void testFormatNegativeDurationHMS() {
assertThrows(IllegalArgumentException.class, () -> DurationFormatUtils.formatDurationHMS(-5000));
}
@Test
public void testFormatDurationISO() {
assertEquals("P0Y0M0DT0H0M0.000S", DurationFormatUtils.formatDurationISO(0L));
assertEquals("P0Y0M0DT0H0M0.001S", DurationFormatUtils.formatDurationISO(1L));
assertEquals("P0Y0M0DT0H0M0.010S", DurationFormatUtils.formatDurationISO(10L));
assertEquals("P0Y0M0DT0H0M0.100S", DurationFormatUtils.formatDurationISO(100L));
assertEquals("P0Y0M0DT0H1M15.321S", DurationFormatUtils.formatDurationISO(75321L));
}
@Test
public void testFormatNegativeDurationISO() {
assertThrows(IllegalArgumentException.class, () -> DurationFormatUtils.formatDurationISO(-5000));
}
@Test
public void testFormatDuration() {
long duration = 0;
assertEquals("0", DurationFormatUtils.formatDuration(duration, "y"));
assertEquals("0", DurationFormatUtils.formatDuration(duration, "M"));
assertEquals("0", DurationFormatUtils.formatDuration(duration, "d"));
assertEquals("0", DurationFormatUtils.formatDuration(duration, "H"));
assertEquals("0", DurationFormatUtils.formatDuration(duration, "m"));
assertEquals("0", DurationFormatUtils.formatDuration(duration, "s"));
assertEquals("0", DurationFormatUtils.formatDuration(duration, "S"));
assertEquals("0000", DurationFormatUtils.formatDuration(duration, "SSSS"));
assertEquals("0000", DurationFormatUtils.formatDuration(duration, "yyyy"));
assertEquals("0000", DurationFormatUtils.formatDuration(duration, "yyMM"));
duration = 60 * 1000;
assertEquals("0", DurationFormatUtils.formatDuration(duration, "y"));
assertEquals("0", DurationFormatUtils.formatDuration(duration, "M"));
assertEquals("0", DurationFormatUtils.formatDuration(duration, "d"));
assertEquals("0", DurationFormatUtils.formatDuration(duration, "H"));
assertEquals("1", DurationFormatUtils.formatDuration(duration, "m"));
assertEquals("60", DurationFormatUtils.formatDuration(duration, "s"));
assertEquals("60000", DurationFormatUtils.formatDuration(duration, "S"));
assertEquals("01:00", DurationFormatUtils.formatDuration(duration, "mm:ss"));
final Calendar base = Calendar.getInstance();
base.set(2000, Calendar.JANUARY, 1, 0, 0, 0);
base.set(Calendar.MILLISECOND, 0);
final Calendar cal = Calendar.getInstance();
cal.set(2003, Calendar.FEBRUARY, 1, 0, 0, 0);
cal.set(Calendar.MILLISECOND, 0);
duration = cal.getTime().getTime() - base.getTime().getTime(); // duration from 2000-01-01 to cal
// don't use 1970 in test as time zones were less reliable in 1970 than now
// remember that duration formatting ignores time zones, working on strict hour lengths
final int days = 366 + 365 + 365 + 31;
assertEquals("0 0 " + days, DurationFormatUtils.formatDuration(duration, "y M d"));
}
@Test
public void testFormatNegativeDuration() {
assertThrows(IllegalArgumentException.class, () -> DurationFormatUtils.formatDuration(-5000, "S", true));
}
@SuppressWarnings("deprecation")
@Test
public void testFormatPeriodISO() {
final TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
final Calendar base = Calendar.getInstance(timeZone);
base.set(1970, Calendar.JANUARY, 1, 0, 0, 0);
base.set(Calendar.MILLISECOND, 0);
final Calendar cal = Calendar.getInstance(timeZone);
cal.set(2002, Calendar.FEBRUARY, 23, 9, 11, 12);
cal.set(Calendar.MILLISECOND, 1);
String text;
// repeat a test from testDateTimeISO to compare extended and not extended.
text = DateFormatUtils.format(cal, DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern(), timeZone);
assertEquals("2002-02-23T09:11:12-03:00", text);
// test fixture is the same as above, but now with extended format.
text = DurationFormatUtils.formatPeriod(base.getTime().getTime(), cal.getTime().getTime(),
DurationFormatUtils.ISO_EXTENDED_FORMAT_PATTERN, false, timeZone);
assertEquals("P32Y1M22DT9H11M12.001S", text);
// test fixture from example in http://www.w3.org/TR/xmlschema-2/#duration
cal.set(1971, Calendar.FEBRUARY, 3, 10, 30, 0);
cal.set(Calendar.MILLISECOND, 0);
text = DurationFormatUtils.formatPeriod(base.getTime().getTime(), cal.getTime().getTime(),
DurationFormatUtils.ISO_EXTENDED_FORMAT_PATTERN, false, timeZone);
assertEquals("P1Y1M2DT10H30M0.000S", text);
// want a way to say 'don't print the seconds in format()' or other fields for that matter:
// assertEquals("P1Y2M3DT10H30M", text);
}
@Test
public void testFormatPeriodISOStartGreaterEnd() {
assertThrows(IllegalArgumentException.class, () -> DurationFormatUtils.formatPeriodISO(5000, 2000));
}
@Test
public void testFormatPeriodISOMethod() {
assertEquals("P0Y0M0DT0H0M0.000S", DurationFormatUtils.formatPeriodISO(0L, 0L));
assertEquals("P0Y0M0DT0H0M1.000S", DurationFormatUtils.formatPeriodISO(0L, 1000L));
assertEquals("P0Y0M0DT0H1M1.000S", DurationFormatUtils.formatPeriodISO(0L, 61000L));
}
@Test
public void testFormatPeriod() {
final Calendar cal1970 = Calendar.getInstance();
cal1970.set(1970, Calendar.JANUARY, 1, 0, 0, 0);
cal1970.set(Calendar.MILLISECOND, 0);
final long time1970 = cal1970.getTime().getTime();
assertEquals("0", DurationFormatUtils.formatPeriod(time1970, time1970, "y"));
assertEquals("0", DurationFormatUtils.formatPeriod(time1970, time1970, "M"));
assertEquals("0", DurationFormatUtils.formatPeriod(time1970, time1970, "d"));
assertEquals("0", DurationFormatUtils.formatPeriod(time1970, time1970, "H"));
assertEquals("0", DurationFormatUtils.formatPeriod(time1970, time1970, "m"));
assertEquals("0", DurationFormatUtils.formatPeriod(time1970, time1970, "s"));
assertEquals("0", DurationFormatUtils.formatPeriod(time1970, time1970, "S"));
assertEquals("0000", DurationFormatUtils.formatPeriod(time1970, time1970, "SSSS"));
assertEquals("0000", DurationFormatUtils.formatPeriod(time1970, time1970, "yyyy"));
assertEquals("0000", DurationFormatUtils.formatPeriod(time1970, time1970, "yyMM"));
long time = time1970 + 60 * 1000;
assertEquals("0", DurationFormatUtils.formatPeriod(time1970, time, "y"));
assertEquals("0", DurationFormatUtils.formatPeriod(time1970, time, "M"));
assertEquals("0", DurationFormatUtils.formatPeriod(time1970, time, "d"));
assertEquals("0", DurationFormatUtils.formatPeriod(time1970, time, "H"));
assertEquals("1", DurationFormatUtils.formatPeriod(time1970, time, "m"));
assertEquals("60", DurationFormatUtils.formatPeriod(time1970, time, "s"));
assertEquals("60000", DurationFormatUtils.formatPeriod(time1970, time, "S"));
assertEquals("01:00", DurationFormatUtils.formatPeriod(time1970, time, "mm:ss"));
final Calendar cal = Calendar.getInstance();
cal.set(1973, Calendar.JULY, 1, 0, 0, 0);
cal.set(Calendar.MILLISECOND, 0);
time = cal.getTime().getTime();
assertEquals("36", DurationFormatUtils.formatPeriod(time1970, time, "yM"));
assertEquals("3 years 6 months", DurationFormatUtils.formatPeriod(time1970, time, "y' years 'M' months'"));
assertEquals("03/06", DurationFormatUtils.formatPeriod(time1970, time, "yy/MM"));
cal.set(1973, Calendar.NOVEMBER, 1, 0, 0, 0);
cal.set(Calendar.MILLISECOND, 0);
time = cal.getTime().getTime();
assertEquals("310", DurationFormatUtils.formatPeriod(time1970, time, "yM"));
assertEquals("3 years 10 months", DurationFormatUtils.formatPeriod(time1970, time, "y' years 'M' months'"));
assertEquals("03/10", DurationFormatUtils.formatPeriod(time1970, time, "yy/MM"));
cal.set(1974, Calendar.JANUARY, 1, 0, 0, 0);
cal.set(Calendar.MILLISECOND, 0);
time = cal.getTime().getTime();
assertEquals("40", DurationFormatUtils.formatPeriod(time1970, time, "yM"));
assertEquals("4 years 0 months", DurationFormatUtils.formatPeriod(time1970, time, "y' years 'M' months'"));
assertEquals("04/00", DurationFormatUtils.formatPeriod(time1970, time, "yy/MM"));
assertEquals("48", DurationFormatUtils.formatPeriod(time1970, time, "M"));
assertEquals("48", DurationFormatUtils.formatPeriod(time1970, time, "MM"));
assertEquals("048", DurationFormatUtils.formatPeriod(time1970, time, "MMM"));
}
@Test
public void testFormatPeriodeStartGreaterEnd() {
assertThrows(IllegalArgumentException.class, () -> DurationFormatUtils.formatPeriod(5000, 2500, "yy/MM"));
}
@Test
public void testLexx() {
// tests each constant
assertArrayEquals(new DurationFormatUtils.Token[]{
new DurationFormatUtils.Token(DurationFormatUtils.y, 1),
new DurationFormatUtils.Token(DurationFormatUtils.M, 1),
new DurationFormatUtils.Token(DurationFormatUtils.d, 1),
new DurationFormatUtils.Token(DurationFormatUtils.H, 1),
new DurationFormatUtils.Token(DurationFormatUtils.m, 1),
new DurationFormatUtils.Token(DurationFormatUtils.s, 1),
new DurationFormatUtils.Token(DurationFormatUtils.S, 1)}, DurationFormatUtils.lexx("yMdHmsS"));
// tests the ISO 8601-like
assertArrayEquals(new DurationFormatUtils.Token[]{
new DurationFormatUtils.Token(DurationFormatUtils.H, 2),
new DurationFormatUtils.Token(new StringBuilder(":"), 1),
new DurationFormatUtils.Token(DurationFormatUtils.m, 2),
new DurationFormatUtils.Token(new StringBuilder(":"), 1),
new DurationFormatUtils.Token(DurationFormatUtils.s, 2),
new DurationFormatUtils.Token(new StringBuilder("."), 1),
new DurationFormatUtils.Token(DurationFormatUtils.S, 3)}, DurationFormatUtils.lexx("HH:mm:ss.SSS"));
// test the iso extended format
assertArrayEquals(new DurationFormatUtils.Token[]{
new DurationFormatUtils.Token(new StringBuilder("P"), 1),
new DurationFormatUtils.Token(DurationFormatUtils.y, 4),
new DurationFormatUtils.Token(new StringBuilder("Y"), 1),
new DurationFormatUtils.Token(DurationFormatUtils.M, 1),
new DurationFormatUtils.Token(new StringBuilder("M"), 1),
new DurationFormatUtils.Token(DurationFormatUtils.d, 1),
new DurationFormatUtils.Token(new StringBuilder("DT"), 1),
new DurationFormatUtils.Token(DurationFormatUtils.H, 1),
new DurationFormatUtils.Token(new StringBuilder("H"), 1),
new DurationFormatUtils.Token(DurationFormatUtils.m, 1),
new DurationFormatUtils.Token(new StringBuilder("M"), 1),
new DurationFormatUtils.Token(DurationFormatUtils.s, 1),
new DurationFormatUtils.Token(new StringBuilder("."), 1),
new DurationFormatUtils.Token(DurationFormatUtils.S, 3),
new DurationFormatUtils.Token(new StringBuilder("S"), 1)}, DurationFormatUtils
.lexx(DurationFormatUtils.ISO_EXTENDED_FORMAT_PATTERN));
// test failures in equals
final DurationFormatUtils.Token token = new DurationFormatUtils.Token(DurationFormatUtils.y, 4);
assertNotEquals(token, new Object(), "Token equal to non-Token class. ");
assertNotEquals(token, new DurationFormatUtils.Token(new Object()), "Token equal to Token with wrong value class. ");
assertNotEquals(token, new DurationFormatUtils.Token(DurationFormatUtils.y, 1), "Token equal to Token with different count. ");
final DurationFormatUtils.Token numToken = new DurationFormatUtils.Token(Integer.valueOf(1), 4);
assertEquals(numToken, numToken, "Token with Number value not equal to itself. ");
}
// https://issues.apache.org/bugzilla/show_bug.cgi?id=38401
@Test
public void testBugzilla38401() {
assertEqualDuration( "0000/00/30 16:00:00 000", new int[] { 2006, 0, 26, 18, 47, 34 },
new int[] { 2006, 1, 26, 10, 47, 34 }, "yyyy/MM/dd HH:mm:ss SSS");
}
// https://issues.apache.org/jira/browse/LANG-281
@Test
public void testJiraLang281() {
assertEqualDuration( "09", new int[] { 2005, 11, 31, 0, 0, 0 },
new int[] { 2006, 9, 6, 0, 0, 0 }, "MM");
}
@Test
public void testLANG815() {
final Calendar calendar = Calendar.getInstance();
calendar.set(2012, Calendar.JULY, 30, 0, 0, 0);
final long startMillis = calendar.getTimeInMillis();
calendar.set(2012, Calendar.SEPTEMBER, 8);
final long endMillis = calendar.getTimeInMillis();
assertEquals("1 9", DurationFormatUtils.formatPeriod(startMillis, endMillis, "M d"));
}
// Testing the under a day range in DurationFormatUtils.formatPeriod
@Test
public void testLowDurations() {
for (int hr=0; hr < 24; hr++) {
for (int min=0; min < 60; min++) {
for (int sec=0; sec < 60; sec++) {
assertEqualDuration( hr + ":" + min + ":" + sec,
new int[] { 2000, 0, 1, 0, 0, 0, 0 },
new int[] { 2000, 0, 1, hr, min, sec },
"H:m:s"
);
}
}
}
bruteForce(1969, 1, 28, "M", Calendar.MONTH); // tests for 48 years
//bruteForce(1996, 1, 29, "M", Calendar.MONTH); // this will fail
}
// Attempting to test edge cases in DurationFormatUtils.formatPeriod
@ -542,13 +200,335 @@ public class DurationFormatUtilsTest {
}
@Test
public void testLANG984() { // Long durations
assertEquals("0", DurationFormatUtils.formatDuration(0, "S"));
assertEquals(Integer.toString(Integer.MAX_VALUE), DurationFormatUtils.formatDuration(Integer.MAX_VALUE, "S"));
long maxIntPlus=Integer.MAX_VALUE;
maxIntPlus++;
assertEquals(Long.toString(maxIntPlus), DurationFormatUtils.formatDuration(maxIntPlus, "S"));
assertEquals(Long.toString(Long.MAX_VALUE), DurationFormatUtils.formatDuration(Long.MAX_VALUE, "S"));
public void testFormatDuration() {
long duration = 0;
assertEquals("0", DurationFormatUtils.formatDuration(duration, "y"));
assertEquals("0", DurationFormatUtils.formatDuration(duration, "M"));
assertEquals("0", DurationFormatUtils.formatDuration(duration, "d"));
assertEquals("0", DurationFormatUtils.formatDuration(duration, "H"));
assertEquals("0", DurationFormatUtils.formatDuration(duration, "m"));
assertEquals("0", DurationFormatUtils.formatDuration(duration, "s"));
assertEquals("0", DurationFormatUtils.formatDuration(duration, "S"));
assertEquals("0000", DurationFormatUtils.formatDuration(duration, "SSSS"));
assertEquals("0000", DurationFormatUtils.formatDuration(duration, "yyyy"));
assertEquals("0000", DurationFormatUtils.formatDuration(duration, "yyMM"));
duration = 60 * 1000;
assertEquals("0", DurationFormatUtils.formatDuration(duration, "y"));
assertEquals("0", DurationFormatUtils.formatDuration(duration, "M"));
assertEquals("0", DurationFormatUtils.formatDuration(duration, "d"));
assertEquals("0", DurationFormatUtils.formatDuration(duration, "H"));
assertEquals("1", DurationFormatUtils.formatDuration(duration, "m"));
assertEquals("60", DurationFormatUtils.formatDuration(duration, "s"));
assertEquals("60000", DurationFormatUtils.formatDuration(duration, "S"));
assertEquals("01:00", DurationFormatUtils.formatDuration(duration, "mm:ss"));
final Calendar base = Calendar.getInstance();
base.set(2000, Calendar.JANUARY, 1, 0, 0, 0);
base.set(Calendar.MILLISECOND, 0);
final Calendar cal = Calendar.getInstance();
cal.set(2003, Calendar.FEBRUARY, 1, 0, 0, 0);
cal.set(Calendar.MILLISECOND, 0);
duration = cal.getTime().getTime() - base.getTime().getTime(); // duration from 2000-01-01 to cal
// don't use 1970 in test as time zones were less reliable in 1970 than now
// remember that duration formatting ignores time zones, working on strict hour lengths
final int days = 366 + 365 + 365 + 31;
assertEquals("0 0 " + days, DurationFormatUtils.formatDuration(duration, "y M d"));
}
@Test
public void testFormatDurationHMS() {
long time = 0;
assertEquals("00:00:00.000", DurationFormatUtils.formatDurationHMS(time));
time = 1;
assertEquals("00:00:00.001", DurationFormatUtils.formatDurationHMS(time));
time = 15;
assertEquals("00:00:00.015", DurationFormatUtils.formatDurationHMS(time));
time = 165;
assertEquals("00:00:00.165", DurationFormatUtils.formatDurationHMS(time));
time = 1675;
assertEquals("00:00:01.675", DurationFormatUtils.formatDurationHMS(time));
time = 13465;
assertEquals("00:00:13.465", DurationFormatUtils.formatDurationHMS(time));
time = 72789;
assertEquals("00:01:12.789", DurationFormatUtils.formatDurationHMS(time));
time = 12789 + 32 * 60000;
assertEquals("00:32:12.789", DurationFormatUtils.formatDurationHMS(time));
time = 12789 + 62 * 60000;
assertEquals("01:02:12.789", DurationFormatUtils.formatDurationHMS(time));
}
@Test
public void testFormatDurationISO() {
assertEquals("P0Y0M0DT0H0M0.000S", DurationFormatUtils.formatDurationISO(0L));
assertEquals("P0Y0M0DT0H0M0.001S", DurationFormatUtils.formatDurationISO(1L));
assertEquals("P0Y0M0DT0H0M0.010S", DurationFormatUtils.formatDurationISO(10L));
assertEquals("P0Y0M0DT0H0M0.100S", DurationFormatUtils.formatDurationISO(100L));
assertEquals("P0Y0M0DT0H1M15.321S", DurationFormatUtils.formatDurationISO(75321L));
}
/**
* Tests that "1 &lt;unit&gt;s" gets converted to "1 &lt;unit&gt;" but that "11 &lt;unit&gt;s" is left alone.
*/
@Test
public void testFormatDurationPluralWords() {
final long oneSecond = 1000;
final long oneMinute = oneSecond * 60;
final long oneHour = oneMinute * 60;
final long oneDay = oneHour * 24;
String text;
text = DurationFormatUtils.formatDurationWords(oneSecond, false, false);
assertEquals("0 days 0 hours 0 minutes 1 second", text);
text = DurationFormatUtils.formatDurationWords(oneSecond * 2, false, false);
assertEquals("0 days 0 hours 0 minutes 2 seconds", text);
text = DurationFormatUtils.formatDurationWords(oneSecond * 11, false, false);
assertEquals("0 days 0 hours 0 minutes 11 seconds", text);
text = DurationFormatUtils.formatDurationWords(oneMinute, false, false);
assertEquals("0 days 0 hours 1 minute 0 seconds", text);
text = DurationFormatUtils.formatDurationWords(oneMinute * 2, false, false);
assertEquals("0 days 0 hours 2 minutes 0 seconds", text);
text = DurationFormatUtils.formatDurationWords(oneMinute * 11, false, false);
assertEquals("0 days 0 hours 11 minutes 0 seconds", text);
text = DurationFormatUtils.formatDurationWords(oneMinute + oneSecond, false, false);
assertEquals("0 days 0 hours 1 minute 1 second", text);
text = DurationFormatUtils.formatDurationWords(oneHour, false, false);
assertEquals("0 days 1 hour 0 minutes 0 seconds", text);
text = DurationFormatUtils.formatDurationWords(oneHour * 2, false, false);
assertEquals("0 days 2 hours 0 minutes 0 seconds", text);
text = DurationFormatUtils.formatDurationWords(oneHour * 11, false, false);
assertEquals("0 days 11 hours 0 minutes 0 seconds", text);
text = DurationFormatUtils.formatDurationWords(oneHour + oneMinute + oneSecond, false, false);
assertEquals("0 days 1 hour 1 minute 1 second", text);
text = DurationFormatUtils.formatDurationWords(oneDay, false, false);
assertEquals("1 day 0 hours 0 minutes 0 seconds", text);
text = DurationFormatUtils.formatDurationWords(oneDay * 2, false, false);
assertEquals("2 days 0 hours 0 minutes 0 seconds", text);
text = DurationFormatUtils.formatDurationWords(oneDay * 11, false, false);
assertEquals("11 days 0 hours 0 minutes 0 seconds", text);
text = DurationFormatUtils.formatDurationWords(oneDay + oneHour + oneMinute + oneSecond, false, false);
assertEquals("1 day 1 hour 1 minute 1 second", text);
}
// -----------------------------------------------------------------------
@Test
public void testFormatDurationWords() {
String text;
text = DurationFormatUtils.formatDurationWords(50 * 1000, true, false);
assertEquals("50 seconds", text);
text = DurationFormatUtils.formatDurationWords(65 * 1000, true, false);
assertEquals("1 minute 5 seconds", text);
text = DurationFormatUtils.formatDurationWords(120 * 1000, true, false);
assertEquals("2 minutes 0 seconds", text);
text = DurationFormatUtils.formatDurationWords(121 * 1000, true, false);
assertEquals("2 minutes 1 second", text);
text = DurationFormatUtils.formatDurationWords(72 * 60 * 1000, true, false);
assertEquals("1 hour 12 minutes 0 seconds", text);
text = DurationFormatUtils.formatDurationWords(24 * 60 * 60 * 1000, true, false);
assertEquals("1 day 0 hours 0 minutes 0 seconds", text);
text = DurationFormatUtils.formatDurationWords(50 * 1000, true, true);
assertEquals("50 seconds", text);
text = DurationFormatUtils.formatDurationWords(65 * 1000, true, true);
assertEquals("1 minute 5 seconds", text);
text = DurationFormatUtils.formatDurationWords(120 * 1000, true, true);
assertEquals("2 minutes", text);
text = DurationFormatUtils.formatDurationWords(121 * 1000, true, true);
assertEquals("2 minutes 1 second", text);
text = DurationFormatUtils.formatDurationWords(72 * 60 * 1000, true, true);
assertEquals("1 hour 12 minutes", text);
text = DurationFormatUtils.formatDurationWords(24 * 60 * 60 * 1000, true, true);
assertEquals("1 day", text);
text = DurationFormatUtils.formatDurationWords(50 * 1000, false, true);
assertEquals("0 days 0 hours 0 minutes 50 seconds", text);
text = DurationFormatUtils.formatDurationWords(65 * 1000, false, true);
assertEquals("0 days 0 hours 1 minute 5 seconds", text);
text = DurationFormatUtils.formatDurationWords(120 * 1000, false, true);
assertEquals("0 days 0 hours 2 minutes", text);
text = DurationFormatUtils.formatDurationWords(121 * 1000, false, true);
assertEquals("0 days 0 hours 2 minutes 1 second", text);
text = DurationFormatUtils.formatDurationWords(72 * 60 * 1000, false, true);
assertEquals("0 days 1 hour 12 minutes", text);
text = DurationFormatUtils.formatDurationWords(24 * 60 * 60 * 1000, false, true);
assertEquals("1 day", text);
text = DurationFormatUtils.formatDurationWords(50 * 1000, false, false);
assertEquals("0 days 0 hours 0 minutes 50 seconds", text);
text = DurationFormatUtils.formatDurationWords(65 * 1000, false, false);
assertEquals("0 days 0 hours 1 minute 5 seconds", text);
text = DurationFormatUtils.formatDurationWords(120 * 1000, false, false);
assertEquals("0 days 0 hours 2 minutes 0 seconds", text);
text = DurationFormatUtils.formatDurationWords(121 * 1000, false, false);
assertEquals("0 days 0 hours 2 minutes 1 second", text);
text = DurationFormatUtils.formatDurationWords(72 * 60 * 1000, false, false);
assertEquals("0 days 1 hour 12 minutes 0 seconds", text);
text = DurationFormatUtils.formatDurationWords(24 * 60 * 60 * 1000 + 72 * 60 * 1000, false, false);
assertEquals("1 day 1 hour 12 minutes 0 seconds", text);
text = DurationFormatUtils.formatDurationWords(2 * 24 * 60 * 60 * 1000 + 72 * 60 * 1000, false, false);
assertEquals("2 days 1 hour 12 minutes 0 seconds", text);
for (int i = 2; i < 31; i++) {
text = DurationFormatUtils.formatDurationWords(i * 24 * 60 * 60 * 1000L, false, false);
assertEquals(i + " days 0 hours 0 minutes 0 seconds", text);
}
}
@Test
public void testFormatNegativeDuration() {
assertThrows(IllegalArgumentException.class, () -> DurationFormatUtils.formatDuration(-5000, "S", true));
}
@Test
public void testFormatNegativeDurationHMS() {
assertThrows(IllegalArgumentException.class, () -> DurationFormatUtils.formatDurationHMS(-5000));
}
@Test
public void testFormatNegativeDurationISO() {
assertThrows(IllegalArgumentException.class, () -> DurationFormatUtils.formatDurationISO(-5000));
}
@Test
public void testFormatNegativeDurationWords() {
assertThrows(IllegalArgumentException.class, () -> DurationFormatUtils.formatDurationWords(-5000, true, true));
}
@Test
public void testFormatPeriod() {
final Calendar cal1970 = Calendar.getInstance();
cal1970.set(1970, Calendar.JANUARY, 1, 0, 0, 0);
cal1970.set(Calendar.MILLISECOND, 0);
final long time1970 = cal1970.getTime().getTime();
assertEquals("0", DurationFormatUtils.formatPeriod(time1970, time1970, "y"));
assertEquals("0", DurationFormatUtils.formatPeriod(time1970, time1970, "M"));
assertEquals("0", DurationFormatUtils.formatPeriod(time1970, time1970, "d"));
assertEquals("0", DurationFormatUtils.formatPeriod(time1970, time1970, "H"));
assertEquals("0", DurationFormatUtils.formatPeriod(time1970, time1970, "m"));
assertEquals("0", DurationFormatUtils.formatPeriod(time1970, time1970, "s"));
assertEquals("0", DurationFormatUtils.formatPeriod(time1970, time1970, "S"));
assertEquals("0000", DurationFormatUtils.formatPeriod(time1970, time1970, "SSSS"));
assertEquals("0000", DurationFormatUtils.formatPeriod(time1970, time1970, "yyyy"));
assertEquals("0000", DurationFormatUtils.formatPeriod(time1970, time1970, "yyMM"));
long time = time1970 + 60 * 1000;
assertEquals("0", DurationFormatUtils.formatPeriod(time1970, time, "y"));
assertEquals("0", DurationFormatUtils.formatPeriod(time1970, time, "M"));
assertEquals("0", DurationFormatUtils.formatPeriod(time1970, time, "d"));
assertEquals("0", DurationFormatUtils.formatPeriod(time1970, time, "H"));
assertEquals("1", DurationFormatUtils.formatPeriod(time1970, time, "m"));
assertEquals("60", DurationFormatUtils.formatPeriod(time1970, time, "s"));
assertEquals("60000", DurationFormatUtils.formatPeriod(time1970, time, "S"));
assertEquals("01:00", DurationFormatUtils.formatPeriod(time1970, time, "mm:ss"));
final Calendar cal = Calendar.getInstance();
cal.set(1973, Calendar.JULY, 1, 0, 0, 0);
cal.set(Calendar.MILLISECOND, 0);
time = cal.getTime().getTime();
assertEquals("36", DurationFormatUtils.formatPeriod(time1970, time, "yM"));
assertEquals("3 years 6 months", DurationFormatUtils.formatPeriod(time1970, time, "y' years 'M' months'"));
assertEquals("03/06", DurationFormatUtils.formatPeriod(time1970, time, "yy/MM"));
cal.set(1973, Calendar.NOVEMBER, 1, 0, 0, 0);
cal.set(Calendar.MILLISECOND, 0);
time = cal.getTime().getTime();
assertEquals("310", DurationFormatUtils.formatPeriod(time1970, time, "yM"));
assertEquals("3 years 10 months", DurationFormatUtils.formatPeriod(time1970, time, "y' years 'M' months'"));
assertEquals("03/10", DurationFormatUtils.formatPeriod(time1970, time, "yy/MM"));
cal.set(1974, Calendar.JANUARY, 1, 0, 0, 0);
cal.set(Calendar.MILLISECOND, 0);
time = cal.getTime().getTime();
assertEquals("40", DurationFormatUtils.formatPeriod(time1970, time, "yM"));
assertEquals("4 years 0 months", DurationFormatUtils.formatPeriod(time1970, time, "y' years 'M' months'"));
assertEquals("04/00", DurationFormatUtils.formatPeriod(time1970, time, "yy/MM"));
assertEquals("48", DurationFormatUtils.formatPeriod(time1970, time, "M"));
assertEquals("48", DurationFormatUtils.formatPeriod(time1970, time, "MM"));
assertEquals("048", DurationFormatUtils.formatPeriod(time1970, time, "MMM"));
}
@Test
public void testFormatPeriodeStartGreaterEnd() {
assertThrows(IllegalArgumentException.class, () -> DurationFormatUtils.formatPeriod(5000, 2500, "yy/MM"));
}
@SuppressWarnings("deprecation")
@Test
public void testFormatPeriodISO() {
final TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
final Calendar base = Calendar.getInstance(timeZone);
base.set(1970, Calendar.JANUARY, 1, 0, 0, 0);
base.set(Calendar.MILLISECOND, 0);
final Calendar cal = Calendar.getInstance(timeZone);
cal.set(2002, Calendar.FEBRUARY, 23, 9, 11, 12);
cal.set(Calendar.MILLISECOND, 1);
String text;
// repeat a test from testDateTimeISO to compare extended and not extended.
text = DateFormatUtils.format(cal, DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern(), timeZone);
assertEquals("2002-02-23T09:11:12-03:00", text);
// test fixture is the same as above, but now with extended format.
text = DurationFormatUtils.formatPeriod(base.getTime().getTime(), cal.getTime().getTime(),
DurationFormatUtils.ISO_EXTENDED_FORMAT_PATTERN, false, timeZone);
assertEquals("P32Y1M22DT9H11M12.001S", text);
// test fixture from example in http://www.w3.org/TR/xmlschema-2/#duration
cal.set(1971, Calendar.FEBRUARY, 3, 10, 30, 0);
cal.set(Calendar.MILLISECOND, 0);
text = DurationFormatUtils.formatPeriod(base.getTime().getTime(), cal.getTime().getTime(),
DurationFormatUtils.ISO_EXTENDED_FORMAT_PATTERN, false, timeZone);
assertEquals("P1Y1M2DT10H30M0.000S", text);
// want a way to say 'don't print the seconds in format()' or other fields for that matter:
// assertEquals("P1Y2M3DT10H30M", text);
}
@Test
public void testFormatPeriodISOMethod() {
assertEquals("P0Y0M0DT0H0M0.000S", DurationFormatUtils.formatPeriodISO(0L, 0L));
assertEquals("P0Y0M0DT0H0M1.000S", DurationFormatUtils.formatPeriodISO(0L, 1000L));
assertEquals("P0Y0M0DT0H1M1.000S", DurationFormatUtils.formatPeriodISO(0L, 61000L));
}
@Test
public void testFormatPeriodISOStartGreaterEnd() {
assertThrows(IllegalArgumentException.class, () -> DurationFormatUtils.formatPeriodISO(5000, 2000));
}
// https://issues.apache.org/jira/browse/LANG-281
@Test
public void testJiraLang281() {
assertEqualDuration( "09", new int[] { 2005, 11, 31, 0, 0, 0 },
new int[] { 2006, 9, 6, 0, 0, 0 }, "MM");
}
@Test
public void testLANG815() {
final Calendar calendar = Calendar.getInstance();
calendar.set(2012, Calendar.JULY, 30, 0, 0, 0);
final long startMillis = calendar.getTimeInMillis();
calendar.set(2012, Calendar.SEPTEMBER, 8);
final long endMillis = calendar.getTimeInMillis();
assertEquals("1 9", DurationFormatUtils.formatPeriod(startMillis, endMillis, "M d"));
}
@Test
public void testLANG981() { // unmatched quote char in lexx
assertThrows(IllegalArgumentException.class, () -> DurationFormatUtils.lexx("'yMdHms''S"));
}
@Test
@ -563,25 +543,6 @@ public class DurationFormatUtilsTest {
assertEquals("1 01999", DurationFormatUtils.formatDuration(61999, "m SSSSS"));
}
@Test
public void testDurationsByBruteForce() {
bruteForce(2006, 0, 1, "d", Calendar.DAY_OF_MONTH);
bruteForce(2006, 0, 2, "d", Calendar.DAY_OF_MONTH);
bruteForce(2007, 1, 2, "d", Calendar.DAY_OF_MONTH);
bruteForce(2004, 1, 29, "d", Calendar.DAY_OF_MONTH);
bruteForce(1996, 1, 29, "d", Calendar.DAY_OF_MONTH);
bruteForce(1969, 1, 28, "M", Calendar.MONTH); // tests for 48 years
//bruteForce(1996, 1, 29, "M", Calendar.MONTH); // this will fail
}
@Test
public void testLANG981() { // unmatched quote char in lexx
assertThrows(IllegalArgumentException.class, () -> DurationFormatUtils.lexx("'yMdHms''S"));
}
private static final int FOUR_YEARS = 365 * 3 + 366;
// Takes a minute to run, so generally turned off
// public void testBrutally() {
// Calendar c = Calendar.getInstance();
@ -592,39 +553,78 @@ public class DurationFormatUtilsTest {
// }
// }
private void bruteForce(final int year, final int month, final int day, final String format, final int calendarType) {
final String msg = year + "-" + month + "-" + day + " to ";
final Calendar c = Calendar.getInstance();
c.set(year, month, day, 0, 0, 0);
final int[] array1 = new int[] { year, month, day, 0, 0, 0 };
final int[] array2 = new int[] { year, month, day, 0, 0, 0 };
for (int i=0; i < FOUR_YEARS; i++) {
array2[0] = c.get(Calendar.YEAR);
array2[1] = c.get(Calendar.MONTH);
array2[2] = c.get(Calendar.DAY_OF_MONTH);
final String tmpMsg = msg + array2[0] + "-" + array2[1] + "-" + array2[2] + " at ";
assertEqualDuration( tmpMsg + i, Integer.toString(i), array1, array2, format );
c.add(calendarType, 1);
}
@Test
public void testLANG984() { // Long durations
assertEquals("0", DurationFormatUtils.formatDuration(0, "S"));
assertEquals(Integer.toString(Integer.MAX_VALUE), DurationFormatUtils.formatDuration(Integer.MAX_VALUE, "S"));
long maxIntPlus=Integer.MAX_VALUE;
maxIntPlus++;
assertEquals(Long.toString(maxIntPlus), DurationFormatUtils.formatDuration(maxIntPlus, "S"));
assertEquals(Long.toString(Long.MAX_VALUE), DurationFormatUtils.formatDuration(Long.MAX_VALUE, "S"));
}
private void assertEqualDuration(final String expected, final int[] start, final int[] end, final String format) {
assertEqualDuration(null, expected, start, end, format);
@Test
public void testLexx() {
// tests each constant
assertArrayEquals(new DurationFormatUtils.Token[]{
new DurationFormatUtils.Token(DurationFormatUtils.y, 1),
new DurationFormatUtils.Token(DurationFormatUtils.M, 1),
new DurationFormatUtils.Token(DurationFormatUtils.d, 1),
new DurationFormatUtils.Token(DurationFormatUtils.H, 1),
new DurationFormatUtils.Token(DurationFormatUtils.m, 1),
new DurationFormatUtils.Token(DurationFormatUtils.s, 1),
new DurationFormatUtils.Token(DurationFormatUtils.S, 1)}, DurationFormatUtils.lexx("yMdHmsS"));
// tests the ISO 8601-like
assertArrayEquals(new DurationFormatUtils.Token[]{
new DurationFormatUtils.Token(DurationFormatUtils.H, 2),
new DurationFormatUtils.Token(new StringBuilder(":"), 1),
new DurationFormatUtils.Token(DurationFormatUtils.m, 2),
new DurationFormatUtils.Token(new StringBuilder(":"), 1),
new DurationFormatUtils.Token(DurationFormatUtils.s, 2),
new DurationFormatUtils.Token(new StringBuilder("."), 1),
new DurationFormatUtils.Token(DurationFormatUtils.S, 3)}, DurationFormatUtils.lexx("HH:mm:ss.SSS"));
// test the iso extended format
assertArrayEquals(new DurationFormatUtils.Token[]{
new DurationFormatUtils.Token(new StringBuilder("P"), 1),
new DurationFormatUtils.Token(DurationFormatUtils.y, 4),
new DurationFormatUtils.Token(new StringBuilder("Y"), 1),
new DurationFormatUtils.Token(DurationFormatUtils.M, 1),
new DurationFormatUtils.Token(new StringBuilder("M"), 1),
new DurationFormatUtils.Token(DurationFormatUtils.d, 1),
new DurationFormatUtils.Token(new StringBuilder("DT"), 1),
new DurationFormatUtils.Token(DurationFormatUtils.H, 1),
new DurationFormatUtils.Token(new StringBuilder("H"), 1),
new DurationFormatUtils.Token(DurationFormatUtils.m, 1),
new DurationFormatUtils.Token(new StringBuilder("M"), 1),
new DurationFormatUtils.Token(DurationFormatUtils.s, 1),
new DurationFormatUtils.Token(new StringBuilder("."), 1),
new DurationFormatUtils.Token(DurationFormatUtils.S, 3),
new DurationFormatUtils.Token(new StringBuilder("S"), 1)}, DurationFormatUtils
.lexx(DurationFormatUtils.ISO_EXTENDED_FORMAT_PATTERN));
// test failures in equals
final DurationFormatUtils.Token token = new DurationFormatUtils.Token(DurationFormatUtils.y, 4);
assertNotEquals(token, new Object(), "Token equal to non-Token class. ");
assertNotEquals(token, new DurationFormatUtils.Token(new Object()), "Token equal to Token with wrong value class. ");
assertNotEquals(token, new DurationFormatUtils.Token(DurationFormatUtils.y, 1), "Token equal to Token with different count. ");
final DurationFormatUtils.Token numToken = new DurationFormatUtils.Token(Integer.valueOf(1), 4);
assertEquals(numToken, numToken, "Token with Number value not equal to itself. ");
}
private void assertEqualDuration(final String message, final String expected, final int[] start, final int[] end, final String format) {
final Calendar cal1 = Calendar.getInstance();
cal1.set(start[0], start[1], start[2], start[3], start[4], start[5]);
cal1.set(Calendar.MILLISECOND, 0);
final Calendar cal2 = Calendar.getInstance();
cal2.set(end[0], end[1], end[2], end[3], end[4], end[5]);
cal2.set(Calendar.MILLISECOND, 0);
final long milli1 = cal1.getTime().getTime();
final long milli2 = cal2.getTime().getTime();
final String result = DurationFormatUtils.formatPeriod(milli1, milli2, format);
if (message == null) {
assertEquals(expected, result);
} else {
assertEquals(expected, result, message);
// Testing the under a day range in DurationFormatUtils.formatPeriod
@Test
public void testLowDurations() {
for (int hr=0; hr < 24; hr++) {
for (int min=0; min < 60; min++) {
for (int sec=0; sec < 60; sec++) {
assertEqualDuration( hr + ":" + min + ":" + sec,
new int[] { 2000, 0, 1, 0, 0, 0, 0 },
new int[] { 2000, 0, 1, hr, min, sec },
"H:m:s"
);
}
}
}
}

View File

@ -45,62 +45,51 @@ import org.junitpioneer.jupiter.DefaultTimeZone;
* @since 2.0
*/
public class FastDateFormatTest {
/*
* Only the cache methods need to be tested here.
* The print methods are tested by {@link FastDateFormat_PrinterTest}
* and the parse methods are tested by {@link FastDateFormat_ParserTest}
*/
@Test
public void test_getInstance() {
final FastDateFormat format1 = FastDateFormat.getInstance();
final FastDateFormat format2 = FastDateFormat.getInstance();
assertSame(format1, format2);
}
private static final int NTHREADS = 10;
@Test
public void test_getInstance_String() {
final FastDateFormat format1 = FastDateFormat.getInstance("MM/DD/yyyy");
final FastDateFormat format2 = FastDateFormat.getInstance("MM-DD-yyyy");
final FastDateFormat format3 = FastDateFormat.getInstance("MM-DD-yyyy");
private static final int NROUNDS = 10000;
assertNotSame(format1, format2);
assertSame(format2, format3);
assertEquals("MM/DD/yyyy", format1.getPattern());
assertEquals(TimeZone.getDefault(), format1.getTimeZone());
assertEquals(TimeZone.getDefault(), format2.getTimeZone());
}
private AtomicLongArray measureTime(final Format printer, final Format parser) throws InterruptedException {
final ExecutorService pool = Executors.newFixedThreadPool(NTHREADS);
final AtomicInteger failures = new AtomicInteger(0);
final AtomicLongArray totalElapsed = new AtomicLongArray(2);
try {
for (int i = 0; i < NTHREADS; ++i) {
pool.submit(() -> {
for (int j = 0; j < NROUNDS; ++j) {
try {
final Date date = new Date();
@DefaultLocale(language = "en", country = "US")
@DefaultTimeZone("America/New_York")
@Test
public void test_getInstance_String_TimeZone() {
final long t0Millis = System.currentTimeMillis();
final String formattedDate = printer.format(date);
totalElapsed.addAndGet(0, System.currentTimeMillis() - t0Millis);
final FastDateFormat format1 = FastDateFormat.getInstance("MM/DD/yyyy",
TimeZone.getTimeZone("Atlantic/Reykjavik"));
final FastDateFormat format2 = FastDateFormat.getInstance("MM/DD/yyyy");
final FastDateFormat format3 = FastDateFormat.getInstance("MM/DD/yyyy", TimeZone.getDefault());
final FastDateFormat format4 = FastDateFormat.getInstance("MM/DD/yyyy", TimeZone.getDefault());
final FastDateFormat format5 = FastDateFormat.getInstance("MM-DD-yyyy", TimeZone.getDefault());
final FastDateFormat format6 = FastDateFormat.getInstance("MM-DD-yyyy");
final long t1Millis = System.currentTimeMillis();
final Object pd = parser.parseObject(formattedDate);
totalElapsed.addAndGet(1, System.currentTimeMillis() - t1Millis);
assertNotSame(format1, format2);
assertEquals(TimeZone.getTimeZone("Atlantic/Reykjavik"), format1.getTimeZone());
assertEquals(TimeZone.getDefault(), format2.getTimeZone());
assertSame(format3, format4);
assertNotSame(format3, format5);
assertNotSame(format4, format6);
}
@DefaultLocale(language = "en", country = "US")
@Test
public void test_getInstance_String_Locale() {
final FastDateFormat format1 = FastDateFormat.getInstance("MM/DD/yyyy", Locale.GERMANY);
final FastDateFormat format2 = FastDateFormat.getInstance("MM/DD/yyyy");
final FastDateFormat format3 = FastDateFormat.getInstance("MM/DD/yyyy", Locale.GERMANY);
assertNotSame(format1, format2);
assertSame(format1, format3);
assertEquals(Locale.GERMANY, format1.getLocale());
if (!date.equals(pd)) {
failures.incrementAndGet();
}
} catch (final Exception e) {
failures.incrementAndGet();
e.printStackTrace();
}
}
});
}
} finally {
pool.shutdown();
// depending on the performance of the machine used to run the parsing,
// the tests can run for a while. It should however complete within
// 30 seconds. Might need increase on very slow machines.
if (!pool.awaitTermination(30, TimeUnit.SECONDS)) {
pool.shutdownNow();
fail("did not complete tasks");
}
}
assertEquals(0, failures.get());
return totalElapsed;
}
@DefaultLocale(language = "en", country = "US")
@ -133,6 +122,64 @@ public class FastDateFormatTest {
assertNotSame(format2, format3);
}
/*
* Only the cache methods need to be tested here.
* The print methods are tested by {@link FastDateFormat_PrinterTest}
* and the parse methods are tested by {@link FastDateFormat_ParserTest}
*/
@Test
public void test_getInstance() {
final FastDateFormat format1 = FastDateFormat.getInstance();
final FastDateFormat format2 = FastDateFormat.getInstance();
assertSame(format1, format2);
}
@Test
public void test_getInstance_String() {
final FastDateFormat format1 = FastDateFormat.getInstance("MM/DD/yyyy");
final FastDateFormat format2 = FastDateFormat.getInstance("MM-DD-yyyy");
final FastDateFormat format3 = FastDateFormat.getInstance("MM-DD-yyyy");
assertNotSame(format1, format2);
assertSame(format2, format3);
assertEquals("MM/DD/yyyy", format1.getPattern());
assertEquals(TimeZone.getDefault(), format1.getTimeZone());
assertEquals(TimeZone.getDefault(), format2.getTimeZone());
}
@DefaultLocale(language = "en", country = "US")
@Test
public void test_getInstance_String_Locale() {
final FastDateFormat format1 = FastDateFormat.getInstance("MM/DD/yyyy", Locale.GERMANY);
final FastDateFormat format2 = FastDateFormat.getInstance("MM/DD/yyyy");
final FastDateFormat format3 = FastDateFormat.getInstance("MM/DD/yyyy", Locale.GERMANY);
assertNotSame(format1, format2);
assertSame(format1, format3);
assertEquals(Locale.GERMANY, format1.getLocale());
}
@DefaultLocale(language = "en", country = "US")
@DefaultTimeZone("America/New_York")
@Test
public void test_getInstance_String_TimeZone() {
final FastDateFormat format1 = FastDateFormat.getInstance("MM/DD/yyyy",
TimeZone.getTimeZone("Atlantic/Reykjavik"));
final FastDateFormat format2 = FastDateFormat.getInstance("MM/DD/yyyy");
final FastDateFormat format3 = FastDateFormat.getInstance("MM/DD/yyyy", TimeZone.getDefault());
final FastDateFormat format4 = FastDateFormat.getInstance("MM/DD/yyyy", TimeZone.getDefault());
final FastDateFormat format5 = FastDateFormat.getInstance("MM-DD-yyyy", TimeZone.getDefault());
final FastDateFormat format6 = FastDateFormat.getInstance("MM-DD-yyyy");
assertNotSame(format1, format2);
assertEquals(TimeZone.getTimeZone("Atlantic/Reykjavik"), format1.getTimeZone());
assertEquals(TimeZone.getDefault(), format2.getTimeZone());
assertSame(format3, format4);
assertNotSame(format3, format5);
assertNotSame(format4, format6);
}
@DefaultLocale(language = "en", country = "US")
@DefaultTimeZone("America/New_York")
@Test
@ -193,27 +240,28 @@ public class FastDateFormatTest {
}
@Test
public void testTimeDefaults() {
assertEquals(FastDateFormat.getTimeInstance(FastDateFormat.LONG, Locale.CANADA),
FastDateFormat.getTimeInstance(FastDateFormat.LONG, TimeZone.getDefault(), Locale.CANADA));
public void testLANG_1152() {
final TimeZone utc = FastTimeZone.getGmtTimeZone();
final Date date = new Date(Long.MAX_VALUE);
assertEquals(FastDateFormat.getTimeInstance(FastDateFormat.LONG, TimeZone.getTimeZone("America/New_York")),
FastDateFormat.getTimeInstance(FastDateFormat.LONG, TimeZone.getTimeZone("America/New_York"), Locale.getDefault()));
String dateAsString = FastDateFormat.getInstance("yyyy-MM-dd", utc, Locale.US).format(date);
assertEquals("292278994-08-17", dateAsString);
assertEquals(FastDateFormat.getTimeInstance(FastDateFormat.LONG),
FastDateFormat.getTimeInstance(FastDateFormat.LONG, TimeZone.getDefault(), Locale.getDefault()));
dateAsString = FastDateFormat.getInstance("dd/MM/yyyy", utc, Locale.US).format(date);
assertEquals("17/08/292278994", dateAsString);
}
@Test
public void testLANG_1267() {
FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
}
/**
* According to LANG-954 (https://issues.apache.org/jira/browse/LANG-954) this is broken in Android 2.1.
*/
@Test
public void testTimeDateDefaults() {
assertEquals(FastDateFormat.getDateTimeInstance(FastDateFormat.LONG, FastDateFormat.MEDIUM, Locale.CANADA),
FastDateFormat.getDateTimeInstance(FastDateFormat.LONG, FastDateFormat.MEDIUM, TimeZone.getDefault(), Locale.CANADA));
assertEquals(FastDateFormat.getDateTimeInstance(FastDateFormat.LONG, FastDateFormat.MEDIUM, TimeZone.getTimeZone("America/New_York")),
FastDateFormat.getDateTimeInstance(FastDateFormat.LONG, FastDateFormat.MEDIUM, TimeZone.getTimeZone("America/New_York"), Locale.getDefault()));
assertEquals(FastDateFormat.getDateTimeInstance(FastDateFormat.LONG, FastDateFormat.MEDIUM),
FastDateFormat.getDateTimeInstance(FastDateFormat.LONG, FastDateFormat.MEDIUM, TimeZone.getDefault(), Locale.getDefault()));
public void testLANG_954() {
final String pattern = "yyyy-MM-dd'T'";
FastDateFormat.getInstance(pattern);
}
@Test
@ -248,75 +296,27 @@ public class FastDateFormatTest {
//System.out.println(">>FastDateFormatTest: FastDateParser:"+fdfTime.get(1)+" SimpleDateFormat:"+sdfTime.get(1));
}
private static final int NTHREADS = 10;
private static final int NROUNDS = 10000;
private AtomicLongArray measureTime(final Format printer, final Format parser) throws InterruptedException {
final ExecutorService pool = Executors.newFixedThreadPool(NTHREADS);
final AtomicInteger failures = new AtomicInteger(0);
final AtomicLongArray totalElapsed = new AtomicLongArray(2);
try {
for (int i = 0; i < NTHREADS; ++i) {
pool.submit(() -> {
for (int j = 0; j < NROUNDS; ++j) {
try {
final Date date = new Date();
final long t0Millis = System.currentTimeMillis();
final String formattedDate = printer.format(date);
totalElapsed.addAndGet(0, System.currentTimeMillis() - t0Millis);
final long t1Millis = System.currentTimeMillis();
final Object pd = parser.parseObject(formattedDate);
totalElapsed.addAndGet(1, System.currentTimeMillis() - t1Millis);
if (!date.equals(pd)) {
failures.incrementAndGet();
}
} catch (final Exception e) {
failures.incrementAndGet();
e.printStackTrace();
}
}
});
}
} finally {
pool.shutdown();
// depending on the performance of the machine used to run the parsing,
// the tests can run for a while. It should however complete within
// 30 seconds. Might need increase on very slow machines.
if (!pool.awaitTermination(30, TimeUnit.SECONDS)) {
pool.shutdownNow();
fail("did not complete tasks");
}
}
assertEquals(0, failures.get());
return totalElapsed;
}
/**
* According to LANG-954 (https://issues.apache.org/jira/browse/LANG-954) this is broken in Android 2.1.
*/
@Test
public void testLANG_954() {
final String pattern = "yyyy-MM-dd'T'";
FastDateFormat.getInstance(pattern);
public void testTimeDateDefaults() {
assertEquals(FastDateFormat.getDateTimeInstance(FastDateFormat.LONG, FastDateFormat.MEDIUM, Locale.CANADA),
FastDateFormat.getDateTimeInstance(FastDateFormat.LONG, FastDateFormat.MEDIUM, TimeZone.getDefault(), Locale.CANADA));
assertEquals(FastDateFormat.getDateTimeInstance(FastDateFormat.LONG, FastDateFormat.MEDIUM, TimeZone.getTimeZone("America/New_York")),
FastDateFormat.getDateTimeInstance(FastDateFormat.LONG, FastDateFormat.MEDIUM, TimeZone.getTimeZone("America/New_York"), Locale.getDefault()));
assertEquals(FastDateFormat.getDateTimeInstance(FastDateFormat.LONG, FastDateFormat.MEDIUM),
FastDateFormat.getDateTimeInstance(FastDateFormat.LONG, FastDateFormat.MEDIUM, TimeZone.getDefault(), Locale.getDefault()));
}
@Test
public void testLANG_1152() {
final TimeZone utc = FastTimeZone.getGmtTimeZone();
final Date date = new Date(Long.MAX_VALUE);
public void testTimeDefaults() {
assertEquals(FastDateFormat.getTimeInstance(FastDateFormat.LONG, Locale.CANADA),
FastDateFormat.getTimeInstance(FastDateFormat.LONG, TimeZone.getDefault(), Locale.CANADA));
String dateAsString = FastDateFormat.getInstance("yyyy-MM-dd", utc, Locale.US).format(date);
assertEquals("292278994-08-17", dateAsString);
assertEquals(FastDateFormat.getTimeInstance(FastDateFormat.LONG, TimeZone.getTimeZone("America/New_York")),
FastDateFormat.getTimeInstance(FastDateFormat.LONG, TimeZone.getTimeZone("America/New_York"), Locale.getDefault()));
dateAsString = FastDateFormat.getInstance("dd/MM/yyyy", utc, Locale.US).format(date);
assertEquals("17/08/292278994", dateAsString);
}
@Test
public void testLANG_1267() {
FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
assertEquals(FastDateFormat.getTimeInstance(FastDateFormat.LONG),
FastDateFormat.getTimeInstance(FastDateFormat.LONG, TimeZone.getDefault(), Locale.getDefault()));
}
}

View File

@ -38,6 +38,8 @@ import org.junit.jupiter.params.provider.MethodSource;
*/
public class FastDateParserSDFTest {
private static final TimeZone timeZone = TimeZone.getDefault();
public static Stream<Arguments> data() {
return Stream.of(
// General Time zone tests
@ -102,44 +104,6 @@ public class FastDateParserSDFTest {
);
}
private static final TimeZone timeZone = TimeZone.getDefault();
@ParameterizedTest
@MethodSource("data")
public void testOriginal(final String format, final String input, final Locale locale, final boolean valid) {
checkParse(input, format, locale, valid);
}
@ParameterizedTest
@MethodSource("data")
public void testOriginalPP(final String format, final String input, final Locale locale, final boolean valid) {
checkParsePosition(input, format, locale, valid);
}
@ParameterizedTest
@MethodSource("data")
public void testUpperCase(final String format, final String input, final Locale locale, final boolean valid) {
checkParse(input.toUpperCase(locale), format, locale, valid);
}
@ParameterizedTest
@MethodSource("data")
public void testUpperCasePP(final String format, final String input, final Locale locale, final boolean valid) {
checkParsePosition(input.toUpperCase(locale), format, locale, valid);
}
@ParameterizedTest
@MethodSource("data")
public void testLowerCase(final String format, final String input, final Locale locale, final boolean valid) {
checkParse(input.toLowerCase(locale), format, locale, valid);
}
@ParameterizedTest
@MethodSource("data")
public void testLowerCasePP(final String format, final String input, final Locale locale, final boolean valid) {
checkParsePosition(input.toLowerCase(locale), format, locale, valid);
}
private void checkParse(final String formattedDate, final String format, final Locale locale, final boolean valid) {
final SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
sdf.setTimeZone(timeZone);
@ -176,6 +140,7 @@ public class FastDateParserSDFTest {
assertEquals(sdfE, fdfE, locale.toString()+" "+formattedDate + " expected same Exception ");
}
}
private void checkParsePosition(final String formattedDate, final String format, final Locale locale, final boolean valid) {
final SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
sdf.setTimeZone(timeZone);
@ -214,4 +179,39 @@ public class FastDateParserSDFTest {
"FDF error index ("+ fdferrorIndex + ") should approximate SDF index (" + sdferrorIndex + ")");
}
}
@ParameterizedTest
@MethodSource("data")
public void testLowerCase(final String format, final String input, final Locale locale, final boolean valid) {
checkParse(input.toLowerCase(locale), format, locale, valid);
}
@ParameterizedTest
@MethodSource("data")
public void testLowerCasePP(final String format, final String input, final Locale locale, final boolean valid) {
checkParsePosition(input.toLowerCase(locale), format, locale, valid);
}
@ParameterizedTest
@MethodSource("data")
public void testOriginal(final String format, final String input, final Locale locale, final boolean valid) {
checkParse(input, format, locale, valid);
}
@ParameterizedTest
@MethodSource("data")
public void testOriginalPP(final String format, final String input, final Locale locale, final boolean valid) {
checkParsePosition(input, format, locale, valid);
}
@ParameterizedTest
@MethodSource("data")
public void testUpperCase(final String format, final String input, final Locale locale, final boolean valid) {
checkParse(input.toUpperCase(locale), format, locale, valid);
}
@ParameterizedTest
@MethodSource("data")
public void testUpperCasePP(final String format, final String input, final Locale locale, final boolean valid) {
checkParsePosition(input.toUpperCase(locale), format, locale, valid);
}
}

View File

@ -95,6 +95,28 @@ public class FastDateParserTest {
private static final Locale SWEDEN = new Locale("sv", "SE");
static void checkParse(final Locale locale, final Calendar cal, final SimpleDateFormat simpleDateFormat,
final DateParser dateParser) {
final String formattedDate = simpleDateFormat.format(cal.getTime());
checkParse(locale, simpleDateFormat, dateParser, formattedDate, formattedDate);
checkParse(locale, simpleDateFormat, dateParser, formattedDate.toLowerCase(locale), formattedDate);
checkParse(locale, simpleDateFormat, dateParser, formattedDate.toUpperCase(locale), formattedDate);
}
static void checkParse(final Locale locale, final SimpleDateFormat simpleDateFormat, final DateParser dateParser,
final String formattedDate, final String originalFormattedDate) {
try {
final Date expectedTime = simpleDateFormat.parse(formattedDate);
final Date actualTime = dateParser.parse(formattedDate);
assertEquals(expectedTime, actualTime,
"locale: " + locale + ", formattedDate: '" + formattedDate + "', originalFormattedDate: '"
+ originalFormattedDate + ", simpleDateFormat.pattern: '" + simpleDateFormat + "', Java: "
+ SystemUtils.JAVA_RUNTIME_VERSION + "\n");
} catch (final Exception e) {
fail("locale: " + locale + ", formattedDate: '" + formattedDate + "', error : " + e + "\n", e);
}
}
static Stream<Arguments> dateParserParameters() {
return Stream.of(
// @formatter:off
@ -120,28 +142,6 @@ public class FastDateParserTest {
private final TriFunction<String, TimeZone, Locale, DateParser> dateParserProvider = (format, timeZone,
locale) -> new FastDateParser(format, timeZone, locale, null);
static void checkParse(final Locale locale, final Calendar cal, final SimpleDateFormat simpleDateFormat,
final DateParser dateParser) {
final String formattedDate = simpleDateFormat.format(cal.getTime());
checkParse(locale, simpleDateFormat, dateParser, formattedDate, formattedDate);
checkParse(locale, simpleDateFormat, dateParser, formattedDate.toLowerCase(locale), formattedDate);
checkParse(locale, simpleDateFormat, dateParser, formattedDate.toUpperCase(locale), formattedDate);
}
static void checkParse(final Locale locale, final SimpleDateFormat simpleDateFormat, final DateParser dateParser,
final String formattedDate, final String originalFormattedDate) {
try {
final Date expectedTime = simpleDateFormat.parse(formattedDate);
final Date actualTime = dateParser.parse(formattedDate);
assertEquals(expectedTime, actualTime,
"locale: " + locale + ", formattedDate: '" + formattedDate + "', originalFormattedDate: '"
+ originalFormattedDate + ", simpleDateFormat.pattern: '" + simpleDateFormat + "', Java: "
+ SystemUtils.JAVA_RUNTIME_VERSION + "\n");
} catch (final Exception e) {
fail("locale: " + locale + ", formattedDate: '" + formattedDate + "', error : " + e + "\n", e);
}
}
private DateParser getDateInstance(final int dateStyle, final Locale locale) {
return getInstance(null, FormatCache.getPatternForStyle(Integer.valueOf(dateStyle), null, locale),
TimeZone.getDefault(), Locale.getDefault());

View File

@ -32,6 +32,27 @@ public class FastDateParser_MoreOrLessTest {
private static final TimeZone NEW_YORK = TimeZone.getTimeZone("America/New_York");
@Test
public void testInputHasLessCharacters() {
final FastDateParser parser = new FastDateParser("MM/dd/yyy", TimeZone.getDefault(), Locale.getDefault());
final ParsePosition parsePosition = new ParsePosition(0);
assertNull(parser.parse("03/23", parsePosition));
assertEquals(5, parsePosition.getErrorIndex());
}
@Test
public void testInputHasMoreCharacters() {
final FastDateParser parser = new FastDateParser("MM/dd", TimeZone.getDefault(), Locale.getDefault());
final ParsePosition parsePosition = new ParsePosition(0);
final Date date = parser.parse("3/23/61", parsePosition);
assertEquals(4, parsePosition.getIndex());
final Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
assertEquals(2, calendar.get(Calendar.MONTH));
assertEquals(23, calendar.get(Calendar.DATE));
}
@Test
public void testInputHasPrecedingCharacters() {
final FastDateParser parser = new FastDateParser("MM/dd", TimeZone.getDefault(), Locale.getDefault());
@ -57,19 +78,6 @@ public class FastDateParser_MoreOrLessTest {
assertEquals(23, calendar.get(Calendar.DATE));
}
@Test
public void testInputHasMoreCharacters() {
final FastDateParser parser = new FastDateParser("MM/dd", TimeZone.getDefault(), Locale.getDefault());
final ParsePosition parsePosition = new ParsePosition(0);
final Date date = parser.parse("3/23/61", parsePosition);
assertEquals(4, parsePosition.getIndex());
final Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
assertEquals(2, calendar.get(Calendar.MONTH));
assertEquals(23, calendar.get(Calendar.DATE));
}
@Test
public void testInputHasWrongCharacters() {
final FastDateParser parser = new FastDateParser("MM-dd-yyy", TimeZone.getDefault(), Locale.getDefault());
@ -79,11 +87,16 @@ public class FastDateParser_MoreOrLessTest {
}
@Test
public void testInputHasLessCharacters() {
final FastDateParser parser = new FastDateParser("MM/dd/yyy", TimeZone.getDefault(), Locale.getDefault());
public void testInputHasWrongDay() {
final FastDateParser parser = new FastDateParser("EEEE, MM/dd/yyy", NEW_YORK, Locale.US);
final String input = "Thursday, 03/23/61";
final ParsePosition parsePosition = new ParsePosition(0);
assertNull(parser.parse("03/23", parsePosition));
assertEquals(5, parsePosition.getErrorIndex());
assertNotNull(parser.parse(input, parsePosition));
assertEquals(input.length(), parsePosition.getIndex());
parsePosition.setIndex(0);
assertNull(parser.parse( "Thorsday, 03/23/61", parsePosition));
assertEquals(0, parsePosition.getErrorIndex());
}
@Test
@ -99,17 +112,4 @@ public class FastDateParser_MoreOrLessTest {
assertNull(parser.parse( "11:23 Pacific Standard ", parsePosition));
assertEquals(6, parsePosition.getErrorIndex());
}
@Test
public void testInputHasWrongDay() {
final FastDateParser parser = new FastDateParser("EEEE, MM/dd/yyy", NEW_YORK, Locale.US);
final String input = "Thursday, 03/23/61";
final ParsePosition parsePosition = new ParsePosition(0);
assertNotNull(parser.parse(input, parsePosition));
assertEquals(input.length(), parsePosition.getIndex());
parsePosition.setIndex(0);
assertNull(parser.parse( "Thorsday, 03/23/61", parsePosition));
assertEquals(0, parsePosition.getErrorIndex());
}
}

View File

@ -30,6 +30,15 @@ import org.junit.jupiter.params.provider.MethodSource;
class FastDateParser_TimeZoneStrategyTest {
@Test
void testLang1219() throws ParseException {
final FastDateParser parser = new FastDateParser("dd.MM.yyyy HH:mm:ss z", TimeZone.getDefault(), Locale.GERMAN);
final Date summer = parser.parse("26.10.2014 02:00:00 MESZ");
final Date standard = parser.parse("26.10.2014 02:00:00 MEZ");
assertNotEquals(summer.getTime(), standard.getTime());
}
@ParameterizedTest
@MethodSource("java.util.Locale#getAvailableLocales")
void testTimeZoneStrategyPattern(final Locale locale) throws ParseException {
@ -46,13 +55,4 @@ class FastDateParser_TimeZoneStrategyTest {
}
}
}
@Test
void testLang1219() throws ParseException {
final FastDateParser parser = new FastDateParser("dd.MM.yyyy HH:mm:ss z", TimeZone.getDefault(), Locale.GERMAN);
final Date summer = parser.parse("26.10.2014 02:00:00 MESZ");
final Date standard = parser.parse("26.10.2014 02:00:00 MEZ");
assertNotEquals(summer.getTime(), standard.getTime());
}
}

View File

@ -42,20 +42,49 @@ import org.junitpioneer.jupiter.DefaultTimeZone;
*/
public class FastDatePrinterTest {
private enum Expected1806 {
India(INDIA, "+05", "+0530", "+05:30"), Greenwich(GMT, "Z", "Z", "Z"), NewYork(
NEW_YORK, "-05", "-0500", "-05:00");
final TimeZone zone;
final String one;
final String two;
final String three;
Expected1806(final TimeZone zone, final String one, final String two, final String three) {
this.zone = zone;
this.one = one;
this.two = two;
this.three = three;
}
}
private static final String YYYY_MM_DD = "yyyy/MM/dd";
private static final TimeZone NEW_YORK = TimeZone.getTimeZone("America/New_York");
private static final TimeZone GMT = TimeZone.getTimeZone("GMT");
private static final TimeZone INDIA = TimeZone.getTimeZone("Asia/Calcutta");
private static final Locale SWEDEN = new Locale("sv", "SE");
DatePrinter getInstance(final String format) {
return getInstance(format, TimeZone.getDefault(), Locale.getDefault());
private static Calendar initializeCalendar(final TimeZone tz) {
final Calendar cal = Calendar.getInstance(tz);
cal.set(Calendar.YEAR, 2001);
cal.set(Calendar.MONTH, 1); // not daylight savings
cal.set(Calendar.DAY_OF_MONTH, 4);
cal.set(Calendar.HOUR_OF_DAY, 12);
cal.set(Calendar.MINUTE, 8);
cal.set(Calendar.SECOND, 56);
cal.set(Calendar.MILLISECOND, 235);
return cal;
}
private DatePrinter getDateInstance(final int dateStyle, final Locale locale) {
return getInstance(FormatCache.getPatternForStyle(Integer.valueOf(dateStyle), null, locale), TimeZone.getDefault(), Locale.getDefault());
}
DatePrinter getInstance(final String format) {
return getInstance(format, TimeZone.getDefault(), Locale.getDefault());
}
private DatePrinter getInstance(final String format, final Locale locale) {
return getInstance(format, TimeZone.getDefault(), locale);
}
@ -75,6 +104,68 @@ public class FastDatePrinterTest {
return new FastDatePrinter(format, timeZone, locale);
}
@Test
public void test1806() {
for (final Expected1806 trial : Expected1806.values()) {
final Calendar cal = initializeCalendar(trial.zone);
DatePrinter printer = getInstance("X", trial.zone);
assertEquals(trial.one, printer.format(cal));
printer = getInstance("XX", trial.zone);
assertEquals(trial.two, printer.format(cal));
printer = getInstance("XXX", trial.zone);
assertEquals(trial.three, printer.format(cal));
}
}
@Test
public void test1806Argument() {
assertThrows(IllegalArgumentException.class, () -> getInstance("XXXX"));
}
@Test
public void testAppendableOptions() {
final DatePrinter format = getInstance("yyyy-MM-dd HH:mm:ss.SSS Z", TimeZone.getTimeZone("GMT"));
final Calendar calendar = Calendar.getInstance();
final StringBuilder sb = new StringBuilder();
final String expected = format.format(calendar, sb).toString();
sb.setLength(0);
final Date date = calendar.getTime();
assertEquals(expected, format.format(date, sb).toString());
sb.setLength(0);
final long epoch = date.getTime();
assertEquals(expected, format.format(epoch, sb).toString());
}
@Test
public void testDayNumberOfWeek() {
final DatePrinter printer = getInstance("u");
final Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
assertEquals("1", printer.format(calendar.getTime()));
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
assertEquals("6", printer.format(calendar.getTime()));
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
assertEquals("7", printer.format(calendar.getTime()));
}
@Test
public void testEquals() {
final DatePrinter printer1= getInstance(YYYY_MM_DD);
final DatePrinter printer2= getInstance(YYYY_MM_DD);
assertEquals(printer1, printer2);
assertEquals(printer1.hashCode(), printer2.hashCode());
assertNotEquals(printer1, new Object());
}
@DefaultLocale(language = "en", country = "US")
@DefaultTimeZone("America/New_York")
@Test
@ -123,67 +214,32 @@ public class FastDatePrinterTest {
assertEquals(sdf.format(date2).replaceAll("2003 03 03 03", "2003 2003 03 2003"), fdf.format(date2));
}
/**
* Test case for {@link FastDateParser#FastDateParser(String, TimeZone, Locale)}.
*/
@Test
public void testShortDateStyleWithLocales() {
final Locale usLocale = Locale.US;
final Locale swedishLocale = new Locale("sv", "SE");
final Calendar cal = Calendar.getInstance();
cal.set(2004, Calendar.FEBRUARY, 3);
DatePrinter fdf = getDateInstance(FastDateFormat.SHORT, usLocale);
assertEquals("2/3/04", fdf.format(cal));
public void testHourFormats() {
final Calendar calendar = Calendar.getInstance();
calendar.clear();
final DatePrinter printer = getInstance("K k H h");
fdf = getDateInstance(FastDateFormat.SHORT, swedishLocale);
assertEquals("2004-02-03", fdf.format(cal));
calendar.set(Calendar.HOUR_OF_DAY, 0);
assertEquals("0 24 0 12", printer.format(calendar));
calendar.set(Calendar.HOUR_OF_DAY, 12);
assertEquals("0 12 12 12", printer.format(calendar));
calendar.set(Calendar.HOUR_OF_DAY, 23);
assertEquals("11 23 23 11", printer.format(calendar));
}
/**
* Tests that pre-1000AD years get padded with yyyy
*/
@Test
public void testLowYearPadding() {
final Calendar cal = Calendar.getInstance();
final DatePrinter format = getInstance(YYYY_MM_DD);
public void testLang1103() {
final Calendar cal = Calendar.getInstance(SWEDEN);
cal.set(Calendar.DAY_OF_MONTH, 2);
cal.set(1, Calendar.JANUARY, 1);
assertEquals("0001/01/01", format.format(cal));
cal.set(10, Calendar.JANUARY, 1);
assertEquals("0010/01/01", format.format(cal));
cal.set(100, Calendar.JANUARY, 1);
assertEquals("0100/01/01", format.format(cal));
cal.set(999, Calendar.JANUARY, 1);
assertEquals("0999/01/01", format.format(cal));
}
/**
* Show Bug #39410 is solved
*/
@Test
public void testMilleniumBug() {
final Calendar cal = Calendar.getInstance();
final DatePrinter format = getInstance("dd.MM.yyyy");
cal.set(1000, Calendar.JANUARY, 1);
assertEquals("01.01.1000", format.format(cal));
}
/**
* testLowYearPadding showed that the date was buggy
* This test confirms it, getting 366 back as a date
*/
@Test
public void testSimpleDate() {
final Calendar cal = Calendar.getInstance();
final DatePrinter format = getInstance(YYYY_MM_DD);
cal.set(2004, Calendar.DECEMBER, 31);
assertEquals("2004/12/31", format.format(cal));
cal.set(999, Calendar.DECEMBER, 31);
assertEquals("0999/12/31", format.format(cal));
cal.set(1, Calendar.MARCH, 2);
assertEquals("0001/03/02", format.format(cal));
assertEquals("2", getInstance("d", SWEDEN).format(cal));
assertEquals("02", getInstance("dd", SWEDEN).format(cal));
assertEquals("002", getInstance("ddd", SWEDEN).format(cal));
assertEquals("0002", getInstance("dddd", SWEDEN).format(cal));
assertEquals("00002", getInstance("ddddd", SWEDEN).format(cal));
}
@Test
@ -224,117 +280,6 @@ public class FastDatePrinterTest {
assertEquals("fredag, week 53", fdf.format(d));
}
@Test
public void testEquals() {
final DatePrinter printer1= getInstance(YYYY_MM_DD);
final DatePrinter printer2= getInstance(YYYY_MM_DD);
assertEquals(printer1, printer2);
assertEquals(printer1.hashCode(), printer2.hashCode());
assertNotEquals(printer1, new Object());
}
@Test
public void testToStringContainsName() {
final DatePrinter printer= getInstance(YYYY_MM_DD);
assertTrue(printer.toString().startsWith("FastDate"));
}
@Test
public void testPatternMatches() {
final DatePrinter printer= getInstance(YYYY_MM_DD);
assertEquals(YYYY_MM_DD, printer.getPattern());
}
@Test
public void testLocaleMatches() {
final DatePrinter printer= getInstance(YYYY_MM_DD, SWEDEN);
assertEquals(SWEDEN, printer.getLocale());
}
@Test
public void testTimeZoneMatches() {
final DatePrinter printer= getInstance(YYYY_MM_DD, NEW_YORK);
assertEquals(NEW_YORK, printer.getTimeZone());
}
@DefaultTimeZone("UTC")
@Test
public void testTimeZoneAsZ() {
final Calendar c = Calendar.getInstance(FastTimeZone.getGmtTimeZone());
final FastDateFormat noColonFormat = FastDateFormat.getInstance("Z");
assertEquals("+0000", noColonFormat.format(c));
final FastDateFormat isoFormat = FastDateFormat.getInstance("ZZ");
assertEquals("Z", isoFormat.format(c));
final FastDateFormat colonFormat = FastDateFormat.getInstance("ZZZ");
assertEquals("+00:00", colonFormat.format(c));
}
private static Calendar initializeCalendar(final TimeZone tz) {
final Calendar cal = Calendar.getInstance(tz);
cal.set(Calendar.YEAR, 2001);
cal.set(Calendar.MONTH, 1); // not daylight savings
cal.set(Calendar.DAY_OF_MONTH, 4);
cal.set(Calendar.HOUR_OF_DAY, 12);
cal.set(Calendar.MINUTE, 8);
cal.set(Calendar.SECOND, 56);
cal.set(Calendar.MILLISECOND, 235);
return cal;
}
@Test
public void test1806Argument() {
assertThrows(IllegalArgumentException.class, () -> getInstance("XXXX"));
}
private enum Expected1806 {
India(INDIA, "+05", "+0530", "+05:30"), Greenwich(GMT, "Z", "Z", "Z"), NewYork(
NEW_YORK, "-05", "-0500", "-05:00");
Expected1806(final TimeZone zone, final String one, final String two, final String three) {
this.zone = zone;
this.one = one;
this.two = two;
this.three = three;
}
final TimeZone zone;
final String one;
final String two;
final String three;
}
@Test
public void test1806() {
for (final Expected1806 trial : Expected1806.values()) {
final Calendar cal = initializeCalendar(trial.zone);
DatePrinter printer = getInstance("X", trial.zone);
assertEquals(trial.one, printer.format(cal));
printer = getInstance("XX", trial.zone);
assertEquals(trial.two, printer.format(cal));
printer = getInstance("XXX", trial.zone);
assertEquals(trial.three, printer.format(cal));
}
}
@Test
public void testLang1103() {
final Calendar cal = Calendar.getInstance(SWEDEN);
cal.set(Calendar.DAY_OF_MONTH, 2);
assertEquals("2", getInstance("d", SWEDEN).format(cal));
assertEquals("02", getInstance("dd", SWEDEN).format(cal));
assertEquals("002", getInstance("ddd", SWEDEN).format(cal));
assertEquals("0002", getInstance("dddd", SWEDEN).format(cal));
assertEquals("00002", getInstance("ddddd", SWEDEN).format(cal));
}
/**
* According to LANG-916 (https://issues.apache.org/jira/browse/LANG-916),
* the format method did contain a bug: it did not use the TimeZone data.
@ -364,19 +309,79 @@ public class FastDatePrinterTest {
}
@Test
public void testHourFormats() {
final Calendar calendar = Calendar.getInstance();
calendar.clear();
final DatePrinter printer = getInstance("K k H h");
public void testLocaleMatches() {
final DatePrinter printer= getInstance(YYYY_MM_DD, SWEDEN);
assertEquals(SWEDEN, printer.getLocale());
}
calendar.set(Calendar.HOUR_OF_DAY, 0);
assertEquals("0 24 0 12", printer.format(calendar));
/**
* Tests that pre-1000AD years get padded with yyyy
*/
@Test
public void testLowYearPadding() {
final Calendar cal = Calendar.getInstance();
final DatePrinter format = getInstance(YYYY_MM_DD);
calendar.set(Calendar.HOUR_OF_DAY, 12);
assertEquals("0 12 12 12", printer.format(calendar));
cal.set(1, Calendar.JANUARY, 1);
assertEquals("0001/01/01", format.format(cal));
cal.set(10, Calendar.JANUARY, 1);
assertEquals("0010/01/01", format.format(cal));
cal.set(100, Calendar.JANUARY, 1);
assertEquals("0100/01/01", format.format(cal));
cal.set(999, Calendar.JANUARY, 1);
assertEquals("0999/01/01", format.format(cal));
}
calendar.set(Calendar.HOUR_OF_DAY, 23);
assertEquals("11 23 23 11", printer.format(calendar));
/**
* Show Bug #39410 is solved
*/
@Test
public void testMilleniumBug() {
final Calendar cal = Calendar.getInstance();
final DatePrinter format = getInstance("dd.MM.yyyy");
cal.set(1000, Calendar.JANUARY, 1);
assertEquals("01.01.1000", format.format(cal));
}
@Test
public void testPatternMatches() {
final DatePrinter printer= getInstance(YYYY_MM_DD);
assertEquals(YYYY_MM_DD, printer.getPattern());
}
/**
* Test case for {@link FastDateParser#FastDateParser(String, TimeZone, Locale)}.
*/
@Test
public void testShortDateStyleWithLocales() {
final Locale usLocale = Locale.US;
final Locale swedishLocale = new Locale("sv", "SE");
final Calendar cal = Calendar.getInstance();
cal.set(2004, Calendar.FEBRUARY, 3);
DatePrinter fdf = getDateInstance(FastDateFormat.SHORT, usLocale);
assertEquals("2/3/04", fdf.format(cal));
fdf = getDateInstance(FastDateFormat.SHORT, swedishLocale);
assertEquals("2004-02-03", fdf.format(cal));
}
/**
* testLowYearPadding showed that the date was buggy
* This test confirms it, getting 366 back as a date
*/
@Test
public void testSimpleDate() {
final Calendar cal = Calendar.getInstance();
final DatePrinter format = getInstance(YYYY_MM_DD);
cal.set(2004, Calendar.DECEMBER, 31);
assertEquals("2004/12/31", format.format(cal));
cal.set(999, Calendar.DECEMBER, 31);
assertEquals("0999/12/31", format.format(cal));
cal.set(1, Calendar.MARCH, 2);
assertEquals("0001/03/02", format.format(cal));
}
@SuppressWarnings("deprecation")
@ -402,35 +407,30 @@ public class FastDatePrinterTest {
assertEquals(expected, format.format(epoch, sb).toString());
}
@DefaultTimeZone("UTC")
@Test
public void testAppendableOptions() {
final DatePrinter format = getInstance("yyyy-MM-dd HH:mm:ss.SSS Z", TimeZone.getTimeZone("GMT"));
final Calendar calendar = Calendar.getInstance();
final StringBuilder sb = new StringBuilder();
final String expected = format.format(calendar, sb).toString();
sb.setLength(0);
public void testTimeZoneAsZ() {
final Calendar c = Calendar.getInstance(FastTimeZone.getGmtTimeZone());
final FastDateFormat noColonFormat = FastDateFormat.getInstance("Z");
assertEquals("+0000", noColonFormat.format(c));
final Date date = calendar.getTime();
assertEquals(expected, format.format(date, sb).toString());
sb.setLength(0);
final FastDateFormat isoFormat = FastDateFormat.getInstance("ZZ");
assertEquals("Z", isoFormat.format(c));
final long epoch = date.getTime();
assertEquals(expected, format.format(epoch, sb).toString());
final FastDateFormat colonFormat = FastDateFormat.getInstance("ZZZ");
assertEquals("+00:00", colonFormat.format(c));
}
@Test
public void testDayNumberOfWeek() {
final DatePrinter printer = getInstance("u");
final Calendar calendar = Calendar.getInstance();
public void testTimeZoneMatches() {
final DatePrinter printer= getInstance(YYYY_MM_DD, NEW_YORK);
assertEquals(NEW_YORK, printer.getTimeZone());
}
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
assertEquals("1", printer.format(calendar.getTime()));
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
assertEquals("6", printer.format(calendar.getTime()));
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
assertEquals("7", printer.format(calendar.getTime()));
@Test
public void testToStringContainsName() {
final DatePrinter printer= getInstance(YYYY_MM_DD);
assertTrue(printer.toString().startsWith("FastDate"));
}
@DefaultLocale(language = "en", country = "US")

View File

@ -32,35 +32,14 @@ public class FastTimeZoneTest {
private static final int MINUTES_59 = 59 * 60 * 1000;
private static final int MINUTES_5 = 5 * 60 * 1000;
@Test
public void testGetGmtTimeZone() {
assertEquals(0, FastTimeZone.getGmtTimeZone().getRawOffset());
}
@Test
public void testBareGmt() {
assertEquals(FastTimeZone.getGmtTimeZone(), FastTimeZone.getTimeZone("GMT"));
}
@Test
public void testZ() {
assertEquals(FastTimeZone.getGmtTimeZone(), FastTimeZone.getTimeZone("Z"));
}
@Test
public void testUTC() {
assertEquals(FastTimeZone.getGmtTimeZone(), FastTimeZone.getTimeZone("UTC"));
}
@Test
public void testZeroOffsetsReturnSingleton() {
assertEquals(FastTimeZone.getGmtTimeZone(), FastTimeZone.getTimeZone("+0"));
assertEquals(FastTimeZone.getGmtTimeZone(), FastTimeZone.getTimeZone("-0"));
}
@Test
public void testOlson() {
assertEquals(TimeZone.getTimeZone("America/New_York"), FastTimeZone.getTimeZone("America/New_York"));
public void testGetGmtTimeZone() {
assertEquals(0, FastTimeZone.getGmtTimeZone().getRawOffset());
}
@Test
@ -69,14 +48,6 @@ public class FastTimeZoneTest {
assertEquals(-HOURS_23, FastTimeZone.getGmtTimeZone("GMT-23:00").getRawOffset());
}
@Test
public void testSign() {
assertEquals(HOURS_23, FastTimeZone.getGmtTimeZone("+23:00").getRawOffset());
assertEquals(HOURS_2, FastTimeZone.getGmtTimeZone("+2:00").getRawOffset());
assertEquals(-HOURS_23, FastTimeZone.getGmtTimeZone("-23:00").getRawOffset());
assertEquals(-HOURS_2, FastTimeZone.getGmtTimeZone("-2:00").getRawOffset());
}
@Test
public void testHoursColonMinutes() {
assertEquals(HOURS_23, FastTimeZone.getGmtTimeZone("23:00").getRawOffset());
@ -97,4 +68,33 @@ public class FastTimeZoneTest {
assertEquals(HOURS_2+MINUTES_5, FastTimeZone.getGmtTimeZone("0205").getRawOffset());
}
@Test
public void testOlson() {
assertEquals(TimeZone.getTimeZone("America/New_York"), FastTimeZone.getTimeZone("America/New_York"));
}
@Test
public void testSign() {
assertEquals(HOURS_23, FastTimeZone.getGmtTimeZone("+23:00").getRawOffset());
assertEquals(HOURS_2, FastTimeZone.getGmtTimeZone("+2:00").getRawOffset());
assertEquals(-HOURS_23, FastTimeZone.getGmtTimeZone("-23:00").getRawOffset());
assertEquals(-HOURS_2, FastTimeZone.getGmtTimeZone("-2:00").getRawOffset());
}
@Test
public void testUTC() {
assertEquals(FastTimeZone.getGmtTimeZone(), FastTimeZone.getTimeZone("UTC"));
}
@Test
public void testZ() {
assertEquals(FastTimeZone.getGmtTimeZone(), FastTimeZone.getTimeZone("Z"));
}
@Test
public void testZeroOffsetsReturnSingleton() {
assertEquals(FastTimeZone.getGmtTimeZone(), FastTimeZone.getTimeZone("+0"));
assertEquals(FastTimeZone.getGmtTimeZone(), FastTimeZone.getTimeZone("-0"));
}
}

View File

@ -27,41 +27,6 @@ import org.junit.jupiter.api.Test;
*/
public class GmtTimeZoneTest {
@Test
public void hoursOutOfRange() {
assertThrows(IllegalArgumentException.class, () -> new GmtTimeZone(false, 24, 0));
}
@Test
public void hoursInRange() {
assertEquals(23 * 60 * 60 * 1000, new GmtTimeZone(false, 23, 0).getRawOffset());
}
@Test
public void minutesOutOfRange() {
assertThrows(IllegalArgumentException.class, () -> new GmtTimeZone(false, 0, 60));
}
@Test
public void minutesInRange() {
assertEquals(59 * 60 * 1000, new GmtTimeZone(false, 0, 59).getRawOffset());
}
@Test
public void getOffset() {
assertEquals(0, new GmtTimeZone(false, 0, 0).getOffset(234304));
}
@Test
public void setRawOffset() {
assertThrows(UnsupportedOperationException.class, () -> new GmtTimeZone(false, 0, 0).setRawOffset(0));
}
@Test
public void getRawOffset() {
assertEquals(0, new GmtTimeZone(false, 0, 0).getRawOffset());
}
@Test
public void getID() {
assertEquals("GMT+00:00", new GmtTimeZone(false, 0, 0).getID());
@ -72,8 +37,23 @@ public class GmtTimeZoneTest {
}
@Test
public void useDaylightTime() {
assertFalse(new GmtTimeZone(false, 0, 0).useDaylightTime());
public void getOffset() {
assertEquals(0, new GmtTimeZone(false, 0, 0).getOffset(234304));
}
@Test
public void getRawOffset() {
assertEquals(0, new GmtTimeZone(false, 0, 0).getRawOffset());
}
@Test
public void hoursInRange() {
assertEquals(23 * 60 * 60 * 1000, new GmtTimeZone(false, 23, 0).getRawOffset());
}
@Test
public void hoursOutOfRange() {
assertThrows(IllegalArgumentException.class, () -> new GmtTimeZone(false, 24, 0));
}
@Test
@ -81,6 +61,27 @@ public class GmtTimeZoneTest {
assertFalse(new GmtTimeZone(false, 0, 0).useDaylightTime());
}
@Test
public void minutesInRange() {
assertEquals(59 * 60 * 1000, new GmtTimeZone(false, 0, 59).getRawOffset());
}
@Test
public void minutesOutOfRange() {
assertThrows(IllegalArgumentException.class, () -> new GmtTimeZone(false, 0, 60));
}
@Test
public void setRawOffset() {
assertThrows(UnsupportedOperationException.class, () -> new GmtTimeZone(false, 0, 0).setRawOffset(0));
}
@Test
public void testGetOffset() {
assertEquals(-(6 * 60 + 30) * 60 * 1000,
new GmtTimeZone(true, 6, 30).getOffset(1, 1, 1, 1, 1, 1));
}
@Test
public void testToString() {
assertEquals("[GmtTimeZone id=\"GMT-12:00\",offset=-43200000]",
@ -88,8 +89,7 @@ public class GmtTimeZoneTest {
}
@Test
public void testGetOffset() {
assertEquals(-(6 * 60 + 30) * 60 * 1000,
new GmtTimeZone(true, 6, 30).getOffset(1, 1, 1, 1, 1, 1));
public void useDaylightTime() {
assertFalse(new GmtTimeZone(false, 0, 0).useDaylightTime());
}
}

View File

@ -215,6 +215,20 @@ public class StopWatchTest {
assertTrue(stopWatch.toSplitString().startsWith(MESSAGE));
}
@Test
public void testStopTimeSimple() throws InterruptedException {
final StopWatch watch = StopWatch.createStarted();
final long testStartMillis = System.currentTimeMillis();
sleepQuietly(MILLIS_550);
watch.stop();
final long testEndMillis = System.currentTimeMillis();
final long stopTime = watch.getStopTime();
assertEquals(stopTime, watch.getStopTime());
assertTrue(stopTime >= testStartMillis);
assertTrue(stopTime <= testEndMillis);
}
@Test
public void testStopWatchGetWithTimeUnit() {
// Create a mock StopWatch with a time of 2:59:01.999
@ -247,20 +261,6 @@ public class StopWatchTest {
assertEquals(0, watch.getTime());
}
@Test
public void testStopTimeSimple() throws InterruptedException {
final StopWatch watch = StopWatch.createStarted();
final long testStartMillis = System.currentTimeMillis();
sleepQuietly(MILLIS_550);
watch.stop();
final long testEndMillis = System.currentTimeMillis();
final long stopTime = watch.getStopTime();
assertEquals(stopTime, watch.getStopTime());
assertTrue(stopTime >= testStartMillis);
assertTrue(stopTime <= testEndMillis);
}
@Test
public void testStopWatchSimpleGet() throws InterruptedException {
final StopWatch watch = new StopWatch();