/** * Returns true if both date are in the same year, month and day of month, false otherwise. * @param actual the actual date. expected not be null * @param other the other date. expected not be null * @return true if both date are in the same year, month and day of month, false otherwise */ private static boolean areInSameDayOfMonth(Date actual, Date other) { return areInSameMonth(actual, other) && dayOfMonthOf(actual) == dayOfMonthOf(other); }
private ShouldBeCloseTo(Date actual, Date other, long deltaInMilliseconds, long difference) { // format Date up to the given ms, because defaut format is the second, thus dates with a difference less than 1s // seems equal in the error message. // Use standard formatting to avoid calling ToString.toStringOf for long that adds a 'L' (like 100L) to // differentiate integer from long (here there is no ambiguity). super(format("expected '%s' to be close to '%s' by less than %sms but difference was of %sms", formatAsDatetimeWithMs(actual), formatAsDatetimeWithMs(other), deltaInMilliseconds, difference)); } }
/** * Returns true if both date are in the same year, month, day of month and hour, false otherwise. * @param actual the actual date. expected not be null * @param other the other date. expected not be null * @return true if both date are in the same year, month, day of month and hour, false otherwise. */ private static boolean areInSameHour(Date actual, Date other) { return areInSameDayOfMonth(actual, other) && hourOfDay(actual) == hourOfDay(other); }
private static String toStringOf(Calendar c) { return Dates.formatAsDatetime(c); }
/** * Returns true if both date are in the same year, month and day of month, hour, minute and second, false otherwise. * @param actual the actual date. expected not be null * @param other the other date. expected not be null * @return true if both date are in the same year, month and day of month, hour, minute and second, false otherwise. */ private static boolean areInSameSecond(Date actual, Date other) { return areInSameMinute(actual, other) && secondOf(actual) == secondOf(other); }
/** * Creates a new </code>{@link ShouldBeBefore}</code>. * @param actual the actual value in the failed assertion. * @param year the year to compare the actual date's year to. * @return the created {@code ErrorMessageFactory}. */ public static ErrorMessageFactory shouldBeBefore(Date actual, int year) { Date januaryTheFirstOfGivenYear = parse(year + "-01-01"); return new ShouldBeBefore(actual, januaryTheFirstOfGivenYear, StandardComparisonStrategy.instance()); }
/** * Returns true if both date are in the same year, month, day of month, hour and minute, false otherwise. * @param actual the actual date. expected not be null * @param other the other date. expected not be null * @return true if both date are in the same year, month, day of month, hour and minute, false otherwise. */ private static boolean areInSameMinute(Date actual, Date other) { return areInSameHour(actual, other) && minuteOf(actual) == minuteOf(other); }
/** * Returns true if both date are in the same year and month, false otherwise. * @param actual the actual date. expected not be null * @param other the other date. expected not be null * @return true if both date are in the same year and month, false otherwise */ private static boolean areInSameMonth(Date actual, Date other) { return areInSameYear(actual, other) && monthOf(actual) == monthOf(other); }
/** * Verifies that the actual {@code Date} day of week is equal to the given day of week. * @param info contains information about the assertion. * @param actual the "actual" {@code Date}. * @param dayOfWeek the day of week to compare actual day of week to, see {@link Calendar#DAY_OF_WEEK} for valid values * @throws AssertionError if {@code actual} is {@code null}. * @throws AssertionError if the actual {@code Date} week is not equal to the given day of week. */ public void assertIsWithinDayOfWeek(AssertionInfo info, Date actual, int dayOfWeek) { assertNotNull(info, actual); if (dayOfWeekOf(actual) == dayOfWeek) return; throw failures.failure(info, shouldBeWithin(actual, "day of week", dayOfWeek)); }
/** * Verifies that the actual {@code Date} millisecond is equal to the given millisecond. * @param info contains information about the assertion. * @param actual the "actual" {@code Date}. * @param millisecond the millisecond to compare actual millisecond to * @throws AssertionError if {@code actual} is {@code null}. * @throws AssertionError if the actual {@code Date} millisecond is not equal to the given millisecond. */ public void assertIsWithinMillisecond(AssertionInfo info, Date actual, int millisecond) { assertNotNull(info, actual); if (millisecondOf(actual) == millisecond) return; throw failures.failure(info, shouldBeWithin(actual, "millisecond", millisecond)); }
private static String toStringOf(Date d) { return Dates.formatAsDatetime(d); }
/** * Verifies that the actual {@code Date} second is equal to the given second. * @param info contains information about the assertion. * @param actual the "actual" {@code Date}. * @param second the second to compare actual second to * @throws AssertionError if {@code actual} is {@code null}. * @throws AssertionError if the actual {@code Date} second is not equal to the given second. */ public void assertIsWithinSecond(AssertionInfo info, Date actual, int second) { assertNotNull(info, actual); if (secondOf(actual) == second) return; throw failures.failure(info, shouldBeWithin(actual, "second", second)); }
/** * Creates a new </code>{@link ShouldBeAfter}</code>. * @param actual the actual value in the failed assertion. * @param year the year to compare the actual date's year to. * @return the created {@code ErrorMessageFactory}. */ public static ErrorMessageFactory shouldBeAfter(Date actual, int year) { Date januaryTheFirstOfGivenYear = parse(year + "-01-01"); return new ShouldBeAfter(actual, januaryTheFirstOfGivenYear, StandardComparisonStrategy.instance()); }
/** * Verifies that the actual {@code Date} minute is equal to the given minute. * @param info contains information about the assertion. * @param actual the "actual" {@code Date}. * @param minute the minute to compare actual minute to * @throws AssertionError if {@code actual} is {@code null}. * @throws AssertionError if the actual {@code Date} minute is not equal to the given minute. */ public void assertIsWithinMinute(AssertionInfo info, Date actual, int minute) { assertNotNull(info, actual); if (minuteOf(actual) == minute) return; throw failures.failure(info, shouldBeWithin(actual, "minute", minute)); }
/** * Verifies that the actual {@code Date} month is equal to the given month, <b>month value starting at 1</b> (January=1, * February=2, ...). * @param info contains information about the assertion. * @param actual the "actual" {@code Date}. * @param month the month to compare actual month to, see {@link Calendar#MONTH} for valid values * @throws AssertionError if {@code actual} is {@code null}. * @throws AssertionError if the actual {@code Date} month is not equal to the given month. */ public void assertIsWithinMonth(AssertionInfo info, Date actual, int month) { assertNotNull(info, actual); if (monthOf(actual) == month) return; throw failures.failure(info, shouldBeWithin(actual, "month", month)); }
/** * Formats the date of the given calendar using the ISO 8601 date-time format (yyyy-MM-dd'T'HH:mm:ss).<br> * Method is thread safe. * <p> * Returns null if the given calendar is null. * @param calendar the calendar to format. * @return the formatted calendar or null if the given calendar was null. */ public static String formatAsDatetime(Calendar calendar) { return calendar == null ? null : formatAsDatetime(calendar.getTime()); }
/** * Verifies that the actual {@code Date} hour od day is equal to the given hour of day (24-hour clock). * @param info contains information about the assertion. * @param actual the "actual" {@code Date}. * @param hourOfDay the hour of day to compare actual hour of day to (24-hour clock) * @throws AssertionError if {@code actual} is {@code null}. * @throws AssertionError if the actual {@code Date} hour is not equal to the given hour. */ public void assertIsWithinHourOfDay(AssertionInfo info, Date actual, int hourOfDay) { assertNotNull(info, actual); if (hourOfDay(actual) == hourOfDay) return; throw failures.failure(info, shouldBeWithin(actual, "hour", hourOfDay)); }
/** * Verifies that the actual {@code Date} day of month is equal to the given day of month. * @param info contains information about the assertion. * @param actual the "actual" {@code Date}. * @param dayOfMonth the day of month to compare actual day of month to * @throws AssertionError if {@code actual} is {@code null}. * @throws AssertionError if the actual {@code Date} month is not equal to the given day of month. */ public void assertIsWithinDayOfMonth(AssertionInfo info, Date actual, int dayOfMonth) { assertNotNull(info, actual); if (dayOfMonthOf(actual) == dayOfMonth) return; throw failures.failure(info, shouldBeWithin(actual, "day of month", dayOfMonth)); }