Class Dates


  • public class Dates
    extends java.lang.Object
    Reusable assertions for Dates.
    Author:
    Joel Costigliola
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void assertHasTime​(AssertionInfo info, java.util.Date actual, long timestamp)
      Verifies that the actual Date time is equal to the given timestamp.
      void assertIsAfter​(AssertionInfo info, java.util.Date actual, java.util.Date other)
      Verifies that the actual Date is strictly after the given one.
      void assertIsAfterOrEqualsTo​(AssertionInfo info, java.util.Date actual, java.util.Date other)
      Verifies that the actual Date is after or equal to the given one.
      void assertIsAfterYear​(AssertionInfo info, java.util.Date actual, int year)
      Verifies that the actual Date is strictly after the given year.
      void assertIsBefore​(AssertionInfo info, java.util.Date actual, java.util.Date other)
      Verifies that the actual Date is strictly before the given one.
      void assertIsBeforeOrEqualsTo​(AssertionInfo info, java.util.Date actual, java.util.Date other)
      Verifies that the actual Date is before or equal to the given one.
      void assertIsBeforeYear​(AssertionInfo info, java.util.Date actual, int year)
      Verifies that the actual Date is strictly before the given year.
      void assertIsBetween​(AssertionInfo info, java.util.Date actual, java.util.Date start, java.util.Date end, boolean inclusiveStart, boolean inclusiveEnd)
      Verifies that the actual Date is in start:end period.
      start date belongs to the period if inclusiveStart is true.
      end date belongs to the period if inclusiveEnd is true.
      void assertIsCloseTo​(AssertionInfo info, java.util.Date actual, java.util.Date other, long deltaInMilliseconds)
      Verifies that the actual Date is close to the other date by less than delta, if difference is equals to delta it is ok.
      Note that delta expressed in milliseconds.
      Use handy TimeUnit to convert a duration in milliseconds, for example you can express a delta of 5 seconds with TimeUnit.SECONDS.toMillis(5).
      void assertIsInSameDayAs​(AssertionInfo info, java.util.Date actual, java.util.Date other)
      Verifies that actual and given Date are chronologically in the same day of month (and thus in the same month and year).
      void assertIsInSameHourAs​(AssertionInfo info, java.util.Date actual, java.util.Date other)
      Verifies that actual and given Date are chronologically in the same hour (and thus in the same day of month, month and year).
      void assertIsInSameMinuteAs​(AssertionInfo info, java.util.Date actual, java.util.Date other)
      Verifies that actual and given Date are chronologically in the same minute (and thus in the same hour, day of month, month and year).
      void assertIsInSameMonthAs​(AssertionInfo info, java.util.Date actual, java.util.Date other)
      Verifies that actual and given Date are chronologically in the same month (and thus in the same year).
      void assertIsInSameSecondAs​(AssertionInfo info, java.util.Date actual, java.util.Date other)
      Verifies that actual and given Date are chronologically in the same second (and thus in the same minute, hour, day of month, month and year).
      void assertIsInSameYearAs​(AssertionInfo info, java.util.Date actual, java.util.Date other)
      Verifies that actual and given Date are in the same year.
      void assertIsInTheFuture​(AssertionInfo info, java.util.Date actual)
      Verifies that the actual Date is strictly in the future.
      void assertIsInThePast​(AssertionInfo info, java.util.Date actual)
      Verifies that the actual Date is strictly in the past.
      void assertIsNotBetween​(AssertionInfo info, java.util.Date actual, java.util.Date start, java.util.Date end, boolean inclusiveStart, boolean inclusiveEnd)
      Verifies that the actual Date is not in start:end period..
      start date belongs to the period if inclusiveStart is true.
      end date belongs to the period if inclusiveEnd is true.
      void assertIsToday​(AssertionInfo info, java.util.Date actual)
      Verifies that the actual Date is today, by comparing only year, month and day of actual to today (ie.
      void assertIsWithinDayOfMonth​(AssertionInfo info, java.util.Date actual, int dayOfMonth)
      Verifies that the actual Date day of month is equal to the given day of month.
      void assertIsWithinDayOfWeek​(AssertionInfo info, java.util.Date actual, int dayOfWeek)
      Verifies that the actual Date day of week is equal to the given day of week.
      void assertIsWithinHourOfDay​(AssertionInfo info, java.util.Date actual, int hourOfDay)
      Verifies that the actual Date hour od day is equal to the given hour of day (24-hour clock).
      void assertIsWithinMillisecond​(AssertionInfo info, java.util.Date actual, int millisecond)
      Verifies that the actual Date millisecond is equal to the given millisecond.
      void assertIsWithinMinute​(AssertionInfo info, java.util.Date actual, int minute)
      Verifies that the actual Date minute is equal to the given minute.
      void assertIsWithinMonth​(AssertionInfo info, java.util.Date actual, int month)
      Verifies that the actual Date month is equal to the given month, month value starting at 1 (January=1, February=2, ...).
      void assertIsWithinSecond​(AssertionInfo info, java.util.Date actual, int second)
      Verifies that the actual Date second is equal to the given second.
      void assertIsWithinYear​(AssertionInfo info, java.util.Date actual, int year)
      Verifies that the actual Date year is equal to the given year.
      java.util.Comparator<?> getComparator()  
      static Dates instance()
      Returns the singleton instance of this class.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • instance

        public static Dates instance()
        Returns the singleton instance of this class.
        Returns:
        the singleton instance of this class.
      • getComparator

        public java.util.Comparator<?> getComparator()
      • assertIsBefore

        public void assertIsBefore​(AssertionInfo info,
                                   java.util.Date actual,
                                   java.util.Date other)
        Verifies that the actual Date is strictly before the given one.
        Parameters:
        info - contains information about the assertion.
        actual - the "actual" Date.
        other - the other date to compare actual with.
        Throws:
        java.lang.AssertionError - if actual is null.
        java.lang.NullPointerException - if other Date is null.
        java.lang.AssertionError - if the actual Date is not strictly before the given one.
      • assertIsBeforeOrEqualsTo

        public void assertIsBeforeOrEqualsTo​(AssertionInfo info,
                                             java.util.Date actual,
                                             java.util.Date other)
        Verifies that the actual Date is before or equal to the given one.
        Parameters:
        info - contains information about the assertion.
        actual - the "actual" Date.
        other - the other date to compare actual with.
        Throws:
        java.lang.AssertionError - if actual is null.
        java.lang.NullPointerException - if other Date is null.
        java.lang.AssertionError - if the actual Date is not before or equal to the given one.
      • assertIsAfter

        public void assertIsAfter​(AssertionInfo info,
                                  java.util.Date actual,
                                  java.util.Date other)
        Verifies that the actual Date is strictly after the given one.
        Parameters:
        info - contains information about the assertion.
        actual - the "actual" Date.
        other - the given Date.
        Throws:
        java.lang.AssertionError - if actual is null.
        java.lang.NullPointerException - if other Date is null.
        java.lang.AssertionError - if the actual Date is not strictly after the given one.
      • assertIsAfterOrEqualsTo

        public void assertIsAfterOrEqualsTo​(AssertionInfo info,
                                            java.util.Date actual,
                                            java.util.Date other)
        Verifies that the actual Date is after or equal to the given one.
        Parameters:
        info - contains information about the assertion.
        actual - the "actual" Date.
        other - the given Date.
        Throws:
        java.lang.AssertionError - if actual is null.
        java.lang.NullPointerException - if other Date is null.
        java.lang.AssertionError - if the actual Date is not after or equal to the given one.
      • assertIsBetween

        public void assertIsBetween​(AssertionInfo info,
                                    java.util.Date actual,
                                    java.util.Date start,
                                    java.util.Date end,
                                    boolean inclusiveStart,
                                    boolean inclusiveEnd)
        Verifies that the actual Date is in start:end period.
        start date belongs to the period if inclusiveStart is true.
        end date belongs to the period if inclusiveEnd is true.
        Parameters:
        info - contains information about the assertion.
        actual - the "actual" Date.
        start - the period start, expected not to be null.
        end - the period end, expected not to be null.
        inclusiveStart - wether to include start date in period.
        inclusiveEnd - wether to include end date in period.
        Throws:
        java.lang.AssertionError - if actual is null.
        java.lang.NullPointerException - if start Date is null.
        java.lang.NullPointerException - if end Date is null.
        java.lang.AssertionError - if the actual Date is not in start:end period.
      • assertIsNotBetween

        public void assertIsNotBetween​(AssertionInfo info,
                                       java.util.Date actual,
                                       java.util.Date start,
                                       java.util.Date end,
                                       boolean inclusiveStart,
                                       boolean inclusiveEnd)
        Verifies that the actual Date is not in start:end period..
        start date belongs to the period if inclusiveStart is true.
        end date belongs to the period if inclusiveEnd is true.
        Parameters:
        info - contains information about the assertion.
        actual - the "actual" Date.
        start - the period start, expected not to be null.
        end - the period end, expected not to be null.
        inclusiveStart - wether to include start date in period.
        inclusiveEnd - wether to include end date in period.
        Throws:
        java.lang.AssertionError - if actual is null.
        java.lang.NullPointerException - if start Date is null.
        java.lang.NullPointerException - if end Date is null.
        java.lang.AssertionError - if the actual Date is in start:end period.
      • assertIsInThePast

        public void assertIsInThePast​(AssertionInfo info,
                                      java.util.Date actual)
        Verifies that the actual Date is strictly in the past.
        Parameters:
        info - contains information about the assertion.
        actual - the "actual" Date.
        Throws:
        java.lang.AssertionError - if actual is null.
        java.lang.AssertionError - if the actual Date is not in the past.
      • assertIsToday

        public void assertIsToday​(AssertionInfo info,
                                  java.util.Date actual)
        Verifies that the actual Date is today, by comparing only year, month and day of actual to today (ie. we don't check hours).
        Parameters:
        info - contains information about the assertion.
        actual - the "actual" Date.
        Throws:
        java.lang.AssertionError - if actual is null.
        java.lang.AssertionError - if the actual Date is not today.
      • assertIsInTheFuture

        public void assertIsInTheFuture​(AssertionInfo info,
                                        java.util.Date actual)
        Verifies that the actual Date is strictly in the future.
        Parameters:
        info - contains information about the assertion.
        actual - the "actual" Date.
        Throws:
        java.lang.AssertionError - if actual is null.
        java.lang.AssertionError - if the actual Date is not in the future.
      • assertIsBeforeYear

        public void assertIsBeforeYear​(AssertionInfo info,
                                       java.util.Date actual,
                                       int year)
        Verifies that the actual Date is strictly before the given year.
        Parameters:
        info - contains information about the assertion.
        actual - the "actual" Date.
        year - the year to compare actual year to
        Throws:
        java.lang.AssertionError - if actual is null.
        java.lang.AssertionError - if the actual Date year is after or equal to the given year.
      • assertIsAfterYear

        public void assertIsAfterYear​(AssertionInfo info,
                                      java.util.Date actual,
                                      int year)
        Verifies that the actual Date is strictly after the given year.
        Parameters:
        info - contains information about the assertion.
        actual - the "actual" Date.
        year - the year to compare actual year to
        Throws:
        java.lang.AssertionError - if actual is null.
        java.lang.AssertionError - if the actual Date year is before or equal to the given year.
      • assertIsWithinYear

        public void assertIsWithinYear​(AssertionInfo info,
                                       java.util.Date actual,
                                       int year)
        Verifies that the actual Date year is equal to the given year.
        Parameters:
        year - the year to compare actual year to
        info - contains information about the assertion.
        actual - the "actual" Date.
        Throws:
        java.lang.AssertionError - if actual is null.
        java.lang.AssertionError - if the actual Date year is not equal to the given year.
      • assertIsWithinMonth

        public void assertIsWithinMonth​(AssertionInfo info,
                                        java.util.Date actual,
                                        int month)
        Verifies that the actual Date month is equal to the given month, month value starting at 1 (January=1, February=2, ...).
        Parameters:
        info - contains information about the assertion.
        actual - the "actual" Date.
        month - the month to compare actual month to, see Calendar.MONTH for valid values
        Throws:
        java.lang.AssertionError - if actual is null.
        java.lang.AssertionError - if the actual Date month is not equal to the given month.
      • assertIsWithinDayOfMonth

        public void assertIsWithinDayOfMonth​(AssertionInfo info,
                                             java.util.Date actual,
                                             int dayOfMonth)
        Verifies that the actual Date day of month is equal to the given day of month.
        Parameters:
        info - contains information about the assertion.
        actual - the "actual" Date.
        dayOfMonth - the day of month to compare actual day of month to
        Throws:
        java.lang.AssertionError - if actual is null.
        java.lang.AssertionError - if the actual Date month is not equal to the given day of month.
      • assertIsWithinDayOfWeek

        public void assertIsWithinDayOfWeek​(AssertionInfo info,
                                            java.util.Date actual,
                                            int dayOfWeek)
        Verifies that the actual Date day of week is equal to the given day of week.
        Parameters:
        info - contains information about the assertion.
        actual - the "actual" Date.
        dayOfWeek - the day of week to compare actual day of week to, see Calendar.DAY_OF_WEEK for valid values
        Throws:
        java.lang.AssertionError - if actual is null.
        java.lang.AssertionError - if the actual Date week is not equal to the given day of week.
      • assertIsWithinHourOfDay

        public void assertIsWithinHourOfDay​(AssertionInfo info,
                                            java.util.Date actual,
                                            int hourOfDay)
        Verifies that the actual Date hour od day is equal to the given hour of day (24-hour clock).
        Parameters:
        info - contains information about the assertion.
        actual - the "actual" Date.
        hourOfDay - the hour of day to compare actual hour of day to (24-hour clock)
        Throws:
        java.lang.AssertionError - if actual is null.
        java.lang.AssertionError - if the actual Date hour is not equal to the given hour.
      • assertIsWithinMinute

        public void assertIsWithinMinute​(AssertionInfo info,
                                         java.util.Date actual,
                                         int minute)
        Verifies that the actual Date minute is equal to the given minute.
        Parameters:
        info - contains information about the assertion.
        actual - the "actual" Date.
        minute - the minute to compare actual minute to
        Throws:
        java.lang.AssertionError - if actual is null.
        java.lang.AssertionError - if the actual Date minute is not equal to the given minute.
      • assertIsWithinSecond

        public void assertIsWithinSecond​(AssertionInfo info,
                                         java.util.Date actual,
                                         int second)
        Verifies that the actual Date second is equal to the given second.
        Parameters:
        info - contains information about the assertion.
        actual - the "actual" Date.
        second - the second to compare actual second to
        Throws:
        java.lang.AssertionError - if actual is null.
        java.lang.AssertionError - if the actual Date second is not equal to the given second.
      • assertIsWithinMillisecond

        public void assertIsWithinMillisecond​(AssertionInfo info,
                                              java.util.Date actual,
                                              int millisecond)
        Verifies that the actual Date millisecond is equal to the given millisecond.
        Parameters:
        info - contains information about the assertion.
        actual - the "actual" Date.
        millisecond - the millisecond to compare actual millisecond to
        Throws:
        java.lang.AssertionError - if actual is null.
        java.lang.AssertionError - if the actual Date millisecond is not equal to the given millisecond.
      • assertIsInSameYearAs

        public void assertIsInSameYearAs​(AssertionInfo info,
                                         java.util.Date actual,
                                         java.util.Date other)
        Verifies that actual and given Date are in the same year.
        Parameters:
        info - contains information about the assertion.
        actual - the "actual" Date.
        other - the given Date to compare actual Date to.
        Throws:
        java.lang.AssertionError - if actual is null.
        java.lang.NullPointerException - if other Date is null.
        java.lang.AssertionError - if actual and given Date are not in the same year.
      • assertIsInSameMonthAs

        public void assertIsInSameMonthAs​(AssertionInfo info,
                                          java.util.Date actual,
                                          java.util.Date other)
        Verifies that actual and given Date are chronologically in the same month (and thus in the same year).
        Parameters:
        info - contains information about the assertion.
        actual - the "actual" Date.
        other - the given Date to compare actual Date to.
        Throws:
        java.lang.AssertionError - if actual is null.
        java.lang.NullPointerException - if other Date is null.
        java.lang.AssertionError - if actual and given Date are not chronologically speaking in the same month.
      • assertIsInSameDayAs

        public void assertIsInSameDayAs​(AssertionInfo info,
                                        java.util.Date actual,
                                        java.util.Date other)
        Verifies that actual and given Date are chronologically in the same day of month (and thus in the same month and year).
        Parameters:
        info - contains information about the assertion.
        actual - the "actual" Date.
        other - the given Date to compare actual Date to.
        Throws:
        java.lang.AssertionError - if actual is null.
        java.lang.NullPointerException - if other Date is null.
        java.lang.AssertionError - if actual and given Date are not chronologically speaking in the same day of month.
      • assertIsInSameHourAs

        public void assertIsInSameHourAs​(AssertionInfo info,
                                         java.util.Date actual,
                                         java.util.Date other)
        Verifies that actual and given Date are chronologically in the same hour (and thus in the same day of month, month and year).
        Parameters:
        info - contains information about the assertion.
        actual - the "actual" Date.
        other - the given Date to compare actual Date to.
        Throws:
        java.lang.AssertionError - if actual is null.
        java.lang.NullPointerException - if other Date is null.
        java.lang.AssertionError - if actual and given Date are not chronologically speaking in the same hour.
      • assertIsInSameMinuteAs

        public void assertIsInSameMinuteAs​(AssertionInfo info,
                                           java.util.Date actual,
                                           java.util.Date other)
        Verifies that actual and given Date are chronologically in the same minute (and thus in the same hour, day of month, month and year).
        Parameters:
        info - contains information about the assertion.
        actual - the "actual" Date.
        other - the given Date to compare actual Date to.
        Throws:
        java.lang.AssertionError - if actual is null.
        java.lang.NullPointerException - if other Date is null.
        java.lang.AssertionError - if actual and given Date are not chronologically speaking in the same minute.
      • assertIsInSameSecondAs

        public void assertIsInSameSecondAs​(AssertionInfo info,
                                           java.util.Date actual,
                                           java.util.Date other)
        Verifies that actual and given Date are chronologically in the same second (and thus in the same minute, hour, day of month, month and year).
        Parameters:
        info - contains information about the assertion.
        actual - the "actual" Date.
        other - the given Date to compare actual Date to.
        Throws:
        java.lang.AssertionError - if actual is null.
        java.lang.NullPointerException - if other Date is null.
        java.lang.AssertionError - if actual and given Date are not chronologically speaking in the same second.
      • assertIsCloseTo

        public void assertIsCloseTo​(AssertionInfo info,
                                    java.util.Date actual,
                                    java.util.Date other,
                                    long deltaInMilliseconds)
        Verifies that the actual Date is close to the other date by less than delta, if difference is equals to delta it is ok.
        Note that delta expressed in milliseconds.
        Use handy TimeUnit to convert a duration in milliseconds, for example you can express a delta of 5 seconds with TimeUnit.SECONDS.toMillis(5).
        Parameters:
        info - contains information about the assertion.
        actual - the "actual" Date.
        other - the given Date to compare actual Date to.
        deltaInMilliseconds - the delta used for date comparison, expressed in milliseconds
        Throws:
        java.lang.AssertionError - if actual is null.
        java.lang.NullPointerException - if other Date is null.
        java.lang.AssertionError - if the actual Date week is not close to the given date by less than delta.
      • assertHasTime

        public void assertHasTime​(AssertionInfo info,
                                  java.util.Date actual,
                                  long timestamp)
        Verifies that the actual Date time is equal to the given timestamp.
        Parameters:
        info - contains information about the assertion.
        actual - the "actual" Date.
        timestamp - the timestamp to compare actual time to
        Throws:
        java.lang.AssertionError - if actual is null.
        java.lang.AssertionError - if the actual Date time is not equal to the given timestamp.