diff --git a/src/main/java/org/exparity/hamcrest/date/DateMatchers.java b/src/main/java/org/exparity/hamcrest/date/DateMatchers.java index 0811878..8f0d9f7 100644 --- a/src/main/java/org/exparity/hamcrest/date/DateMatchers.java +++ b/src/main/java/org/exparity/hamcrest/date/DateMatchers.java @@ -1,40 +1,50 @@ package org.exparity.hamcrest.date; -import static java.time.DayOfWeek.FRIDAY; -import static java.time.DayOfWeek.MONDAY; -import static java.time.DayOfWeek.SATURDAY; -import static java.time.DayOfWeek.SUNDAY; -import static java.time.DayOfWeek.THURSDAY; -import static java.time.DayOfWeek.TUESDAY; -import static java.time.DayOfWeek.WEDNESDAY; -import static java.time.Month.APRIL; -import static java.time.Month.AUGUST; -import static java.time.Month.DECEMBER; -import static java.time.Month.FEBRUARY; -import static java.time.Month.JANUARY; -import static java.time.Month.JULY; -import static java.time.Month.JUNE; -import static java.time.Month.MARCH; -import static java.time.Month.MAY; -import static java.time.Month.NOVEMBER; -import static java.time.Month.OCTOBER; -import static java.time.Month.SEPTEMBER; - -import java.time.*; +import static java.time.DayOfWeek.*; +import static java.time.Month.*; +import static org.exparity.hamcrest.date.core.TemporalConverters.*; +import static org.exparity.hamcrest.date.core.TemporalFunctions.INSTANT; +import static org.exparity.hamcrest.date.core.TemporalFunctions.LOCALDATE; +import static org.exparity.hamcrest.date.core.TemporalFunctions.LOCALDATETIME; +import static org.exparity.hamcrest.date.core.TemporalFunctions.SQLDATE; +import static org.exparity.hamcrest.date.core.TemporalFunctions.JAVADATE; +import static org.exparity.hamcrest.date.core.TemporalProviders.*; + +import java.time.DayOfWeek; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.Month; +import java.time.ZoneId; import java.time.temporal.ChronoField; import java.time.temporal.ChronoUnit; -import java.time.temporal.Temporal; import java.util.Date; import java.util.concurrent.TimeUnit; -import java.util.stream.Stream; -import org.exparity.hamcrest.date.core.*; -import org.exparity.hamcrest.date.core.format.DateFormatter; -import org.exparity.hamcrest.date.core.format.DatePartFormatter; -import org.exparity.hamcrest.date.core.wrapper.FieldDateAdaptor; -import org.exparity.hamcrest.date.core.wrapper.FieldDateRangeAdaptor; -import org.exparity.hamcrest.date.core.wrapper.DateWrapper; -import org.exparity.hamcrest.date.core.wrapper.FieldDateWrapper; +import org.exparity.hamcrest.date.core.DateMatcher; +import org.exparity.hamcrest.date.core.IsAfter; +import org.exparity.hamcrest.date.core.IsBefore; +import org.exparity.hamcrest.date.core.IsDayOfMonth; +import org.exparity.hamcrest.date.core.IsDayOfWeek; +import org.exparity.hamcrest.date.core.IsFirstDayOfMonth; +import org.exparity.hamcrest.date.core.IsHour; +import org.exparity.hamcrest.date.core.IsLastDayOfMonth; +import org.exparity.hamcrest.date.core.IsLeapYear; +import org.exparity.hamcrest.date.core.IsMaximum; +import org.exparity.hamcrest.date.core.IsMillisecond; +import org.exparity.hamcrest.date.core.IsMinimum; +import org.exparity.hamcrest.date.core.IsMinute; +import org.exparity.hamcrest.date.core.IsMonth; +import org.exparity.hamcrest.date.core.IsSame; +import org.exparity.hamcrest.date.core.IsSameDay; +import org.exparity.hamcrest.date.core.IsSameOrAfter; +import org.exparity.hamcrest.date.core.IsSameOrBefore; +import org.exparity.hamcrest.date.core.IsSecond; +import org.exparity.hamcrest.date.core.IsWithin; +import org.exparity.hamcrest.date.core.IsYear; +import org.exparity.hamcrest.date.core.TemporalConversionException; +import org.exparity.hamcrest.date.core.function.DateFunction; +import org.exparity.hamcrest.date.core.types.Interval; /** * Static factory for creating {@link org.hamcrest.Matcher} instances for comparing dates @@ -57,7 +67,7 @@ public abstract class DateMatchers { * @param date the reference date against which the examined date is checked */ public static DateMatcher after(final Date date) { - return new IsAfter<>(new DateWrapper(date), new DateFormatter()); + return new IsAfter<>(JAVADATE_AS_JAVADATE, javaDate(date), new DateFunction()); } /** @@ -72,7 +82,7 @@ public static DateMatcher after(final Date date) { * @param date the reference date against which the examined date is checked */ public static DateMatcher after(final java.sql.Date date) { - return new IsAfter<>(new DateWrapper(date), new DateFormatter()); + return new IsAfter<>(JAVADATE_AS_SQLDATE, sqlDate(date), SQLDATE); } /** @@ -104,9 +114,39 @@ public static DateMatcher after(final DayMonthYear date) { * @param date the reference date against which the examined date is checked */ public static DateMatcher after(final LocalDate date) { - return new IsAfter<>(new DateWrapper(date), new DateFormatter()); + return new IsAfter<>(JAVADATE_AS_LOCALDATE, localDate(date), LOCALDATE); + } + + /** + * Creates a matcher that matches when the examined date is after the reference date + *

+ * For example: + * + *

+     * assertThat(myDate, after(Moments.today()))
+     * 
+ * + * @param date the reference date against which the examined date is checked + */ + public static DateMatcher after(final LocalDateTime date) { + return new IsAfter<>(JAVADATE_AS_LOCALDATETIME, localDateTime(date), LOCALDATETIME); } + /** + * Creates a matcher that matches when the examined date is after the reference date + *

+ * For example: + * + *

+     * assertThat(myDate, after(Moments.today()))
+     * 
+ * + * @param date the reference date against which the examined date is checked + */ + public static DateMatcher after(final Instant date) { + return new IsAfter<>(JAVADATE_AS_INSTANT, instant(date), INSTANT); + } + /** * Creates a matcher that matches when the examined date is after the end of the reference year *

@@ -123,7 +163,7 @@ public static DateMatcher after(final LocalDate date) { */ @Deprecated public static DateMatcher after(final int year, final Months month, final int day) { - return new IsAfter<>(new DateWrapper(year, month.month(), day), new DateFormatter()); + return after(year, month.month(), day); } /** @@ -140,7 +180,7 @@ public static DateMatcher after(final int year, final Months month, final * @param day the day of the month against which the examined date is checked */ public static DateMatcher after(final int year, final Month month, final int day) { - return new IsAfter<>(new DateWrapper(year, month, day), new DateFormatter()); + return after(LocalDate.of(year, month, day)); } /** @@ -192,7 +232,7 @@ public static DateMatcher after(final int year, final int hour, final int minute, final int second) { - return new IsAfter<>(new DateWrapper(year, month, dayOfMonth, hour, minute, second), new DateFormatter()); + return after(LocalDateTime.of(year, month, dayOfMonth, hour, minute, second)); } /** @@ -207,7 +247,7 @@ public static DateMatcher after(final int year, * @param date the reference date against which the examined date is checked */ public static DateMatcher before(final Date date) { - return new IsBefore<>(new DateWrapper(date), new DateFormatter()); + return new IsBefore<>(JAVADATE_AS_JAVADATE, javaDate(date), new DateFunction()); } /** @@ -222,7 +262,7 @@ public static DateMatcher before(final Date date) { * @param date the reference date against which the examined date is checked */ public static DateMatcher before(final java.sql.Date date) { - return new IsBefore<>(new DateWrapper(date), new DateFormatter()); + return new IsBefore<>(JAVADATE_AS_SQLDATE, sqlDate(date), SQLDATE); } /** @@ -231,13 +271,15 @@ public static DateMatcher before(final java.sql.Date date) { * For example: * *

-     * assertThat(myDate, before(LocalDate.now()))
+     * assertThat(myDate, before(Moments.today()))
      * 
* * @param date the reference date against which the examined date is checked + * @deprecated Use {@link #before(LocalDate)} */ - public static DateMatcher before(final LocalDate date) { - return new IsBefore<>(new DateWrapper(date), new DateFormatter()); + @Deprecated + public static DateMatcher before(final DayMonthYear date) { + return before(date.toLocalDate()); } /** @@ -246,19 +288,47 @@ public static DateMatcher before(final LocalDate date) { * For example: * *
-     * assertThat(myDate, before(Moments.today()));
+     * assertThat(myDate, before(Moments.today()))
      * 
* * @param date the reference date against which the examined date is checked - * @deprecated Use {@link #before(LocalDate)} */ - @Deprecated - public static DateMatcher before(final DayMonthYear date) { - return before(date.toLocalDate()); + public static DateMatcher before(final LocalDate date) { + return new IsBefore<>(JAVADATE_AS_LOCALDATE, localDate(date), LOCALDATE); + } + + /** + * Creates a matcher that matches when the examined date is before the reference date + *

+ * For example: + * + *

+     * assertThat(myDate, before(Moments.today()))
+     * 
+ * + * @param date the reference date against which the examined date is checked + */ + public static DateMatcher before(final LocalDateTime date) { + return new IsBefore<>(JAVADATE_AS_LOCALDATETIME, localDateTime(date), LOCALDATETIME); } /** - * Creates a matcher that matches when the examined date is before the start of reference day + * Creates a matcher that matches when the examined date is before the reference date + *

+ * For example: + * + *

+     * assertThat(myDate, before(Moments.today()))
+     * 
+ * + * @param date the reference date against which the examined date is checked + */ + public static DateMatcher before(final Instant date) { + return new IsBefore<>(JAVADATE_AS_INSTANT, instant(date), INSTANT); + } + + /** + * Creates a matcher that matches when the examined date is before the end of the reference year *

* For example: * @@ -268,16 +338,16 @@ public static DateMatcher before(final DayMonthYear date) { * * @param year the year against which the examined date is checked * @param month the month against which the examined date is checked - * @param dayOfMonth the day of the month against which the examined date is checked + * @param day the day of the month against which the examined date is checked * @deprecated Use {@link #before(int, Month, int)} */ @Deprecated - public static DateMatcher before(final int year, final Months month, final int dayOfMonth) { - return new IsBefore<>(new DateWrapper(year, month.month(), dayOfMonth), new DateFormatter()); + public static DateMatcher before(final int year, final Months month, final int day) { + return before(year, month.month(), day); } /** - * Creates a matcher that matches when the examined date is before the start of reference day + * Creates a matcher that matches when the examined date is before the end of the reference year *

* For example: * @@ -287,14 +357,14 @@ public static DateMatcher before(final int year, final Months month, final * * @param year the year against which the examined date is checked * @param month the month against which the examined date is checked - * @param dayOfMonth the day of the month against which the examined date is checked + * @param day the day of the month against which the examined date is checked */ - public static DateMatcher before(final int year, final Month month, final int dayOfMonth) { - return new IsBefore<>(new DateWrapper(year, month, dayOfMonth), new DateFormatter()); + public static DateMatcher before(final int year, final Month month, final int day) { + return before(LocalDate.of(year, month, day)); } /** - * Creates a matcher that matches when the examined date is before the start of the reference date and time + * Creates a matcher that matches when the examined date is before the end of the reference year *

* For example: * @@ -321,7 +391,7 @@ public static DateMatcher before(final int year, } /** - * Creates a matcher that matches when the examined date is before the start of the reference date and time + * Creates a matcher that matches when the examined date is before the end of the reference year *

* For example: * @@ -342,7 +412,7 @@ public static DateMatcher before(final int year, final int hour, final int minute, final int second) { - return new IsBefore<>(new DateWrapper(year, month, dayOfMonth, hour, minute, second), new DateFormatter()); + return before(LocalDateTime.of(year, month, dayOfMonth, hour, minute, second)); } /** @@ -404,8 +474,7 @@ public static DateMatcher isDayOfWeek(final Weekdays dayOfWeek) { * @param dayOfWeek the reference weekday against which the examined date is checked */ public static DateMatcher isDayOfWeek(final DayOfWeek dayOfWeek) { - return new IsDayOfWeek<>(new FieldDateWrapper(dayOfWeek.getValue(), ChronoField.DAY_OF_WEEK), - new FieldDateAdaptor(ChronoField.DAY_OF_WEEK)); + return new IsDayOfWeek<>(JAVADATE_AS_DAYOFWEEK, daysOfWeek(dayOfWeek)); } /** @@ -420,8 +489,7 @@ public static DateMatcher isDayOfWeek(final DayOfWeek dayOfWeek) { * @param daysOfWeek the reference weekdays against which the examined date is checked */ public static DateMatcher isDayOfWeek(final DayOfWeek... daysOfWeek) { - return new AnyOf<>(Stream.of(daysOfWeek).map(DateMatchers::isDayOfWeek), - (d, z) -> "the date is on a " + toDayOfWeek(d, z).name().toLowerCase()); + return new IsDayOfWeek<>(JAVADATE_AS_DAYOFWEEK, daysOfWeek(daysOfWeek)); } /** @@ -436,24 +504,7 @@ public static DateMatcher isDayOfWeek(final DayOfWeek... daysOfWeek) { * @param date the reference date against which the examined date is checked */ public static DateMatcher sameDayOfMonth(final Date date) { - return new IsDayOfMonth<>(new FieldDateWrapper(date, ChronoField.DAY_OF_MONTH), - new FieldDateAdaptor(ChronoField.DAY_OF_MONTH)); - } - - /** - * Creates a matcher that matches when the examined date is on the same day of the month as the reference date - *

- * For example: - * - *

-     * assertThat(myDate, sameDayOfMonth(new Date()))
-     * 
- * - * @param date the reference date against which the examined date is checked - */ - public static DateMatcher sameDayOfMonth(final java.sql.Date date) { - return new IsDayOfMonth<>(new FieldDateWrapper(date, ChronoField.DAY_OF_MONTH), - new FieldDateAdaptor(ChronoField.DAY_OF_MONTH)); + return new IsDayOfMonth<>(JAVADATE_AS_DAYOFMONTH, dayOfMonth(date)); } /** @@ -468,8 +519,7 @@ public static DateMatcher sameDayOfMonth(final java.sql.Date date) { * @param dayOfMonth the expected day of the month */ public static DateMatcher isDayOfMonth(final int dayOfMonth) { - return new IsDayOfMonth<>(new FieldDateWrapper(dayOfMonth, ChronoField.DAY_OF_MONTH), - new FieldDateAdaptor(ChronoField.DAY_OF_MONTH)); + return new IsDayOfMonth<>(JAVADATE_AS_DAYOFMONTH, dayOfMonth(dayOfMonth)); } /** @@ -484,7 +534,7 @@ public static DateMatcher isDayOfMonth(final int dayOfMonth) { * @param date the reference date against which the examined date is checked */ public static DateMatcher sameDay(final Date date) { - return new IsSameDay<>(new DateWrapper(date, ChronoUnit.DAYS), new DateFormatter()); + return new IsSameDay<>(JAVADATE_AS_LOCALDATE, localDate(date)); } /** @@ -499,7 +549,7 @@ public static DateMatcher sameDay(final Date date) { * @param date the reference date against which the examined date is checked */ public static DateMatcher sameDay(final java.sql.Date date) { - return new IsSameDay<>(new DateWrapper(date, ChronoUnit.DAYS), new DateFormatter()); + return new IsSameDay<>(JAVADATE_AS_LOCALDATE, localDate(date)); } /** @@ -531,7 +581,7 @@ public static DateMatcher sameDay(final DayMonthYear date) { * @param date the reference date against which the examined date is checked */ public static DateMatcher sameDay(final LocalDate date) { - return new IsSameDay<>(new DateWrapper(date), new DateFormatter()); + return new IsSameDay<>(JAVADATE_AS_LOCALDATE, localDate(date)); } /** @@ -591,13 +641,13 @@ public static DateMatcher sameHour(final Date date) { * @deprecated java.sql.Date does not support time-based comparisons. Prefer {@link SqlDateMatchers} for * java.sql.Date appropriate matchers * @param date the reference date against which the examined date is checked - * @throws IllegalArgumentException + * @throws TemporalConversionException */ @Deprecated public static DateMatcher sameHour(final java.sql.Date date) { - throw new IllegalArgumentException(UNSUPPORTED_SQL_DATE_UNIT); + throw new TemporalConversionException(UNSUPPORTED_SQL_DATE_UNIT); } - + /** * Creates a matcher that matches when the examined date is on the same hour as the reference date *

@@ -610,24 +660,17 @@ public static DateMatcher sameHour(final java.sql.Date date) { * @param date the reference date against which the examined date is checked */ public static DateMatcher sameHourOfDay(final Date date) { - return new IsHour<>(new FieldDateWrapper(date, ChronoField.HOUR_OF_DAY), - new FieldDateAdaptor(ChronoField.HOUR_OF_DAY)); + return new IsHour<>(JAVADATE_AS_HOUR, hour(date)); } - /** - * Creates a matcher that matches when the examined date is on the same hour as the reference date - *

- * For example: - * - *

-     * assertThat(myDate, sameHourOfDay(new Date()))
-     * 
- * - * @param date the reference date against which the examined date is checked - */ + /** + * @deprecated java.sql.Date does not support time-based comparisons. Prefer {@link SqlDateMatchers} for + * java.sql.Date appropriate matchers + * @param date the reference date against which the examined date is checked + * @throws TemporalConversionException + */ public static DateMatcher sameHourOfDay(final java.sql.Date date) { - return new IsHour<>(new FieldDateWrapper(date, ChronoField.HOUR_OF_DAY), - new FieldDateAdaptor(ChronoField.HOUR_OF_DAY)); + throw new TemporalConversionException(UNSUPPORTED_SQL_DATE_UNIT); } /** @@ -658,10 +701,8 @@ public static DateMatcher sameHour(final int hour) { * * @param hour the reference hour against which the examined date is checked */ - public static DateMatcher isHour(final int hour) { - return new IsHour<>(new FieldDateWrapper(hour, ChronoField.HOUR_OF_DAY), - new FieldDateAdaptor(ChronoField.HOUR_OF_DAY)); + return new IsHour<>(JAVADATE_AS_HOUR, hour(hour)); } /** @@ -676,20 +717,36 @@ public static DateMatcher isHour(final int hour) { * @param date the reference date against which the examined date is checked */ public static DateMatcher sameInstant(final Date date) { - return new IsSame<>(new DateWrapper(date), new DateFormatter()); + return new IsSame<>(JAVADATE_AS_INSTANT, instant(date), INSTANT); } /** * @deprecated java.sql.Date does not support time-based comparisons. Prefer {@link SqlDateMatchers} for * java.sql.Date appropriate matchers * @param date the reference date against which the examined date is checked - * @throws IllegalArgumentException + * @throws TemporalConversionException */ @Deprecated public static DateMatcher sameInstant(final java.sql.Date date) { - throw new IllegalArgumentException(UNSUPPORTED_SQL_DATE_UNIT); + throw new TemporalConversionException(UNSUPPORTED_SQL_DATE_UNIT); } - + + /** + * Creates a matcher that matches when the examined date is on the same UTC instant as the reference Instant + * supplied + *

+ * For example: + * + *

+     * assertThat(myDate, sameInstant(Instant.now()))
+     * 
+ * + * @param timestamp the time as milliseconds since the Unix epoch time + */ + public static DateMatcher sameInstant(final Instant instant) { + return new IsSame<>(JAVADATE_AS_INSTANT, instant(instant), INSTANT); + } + /** * Creates a matcher that matches when the examined date is on the same UTC instant as the reference UTC epoch time * supplied @@ -703,7 +760,7 @@ public static DateMatcher sameInstant(final java.sql.Date date) { * @param timestamp the time as milliseconds since the Unix epoch time */ public static DateMatcher sameInstant(final long timestamp) { - return new IsSame<>(new DateWrapper(new Date(timestamp)), new DateFormatter()); + return sameInstant(Instant.ofEpochMilli(timestamp)); } /** @@ -759,8 +816,9 @@ public static DateMatcher isInstant(final int year, final int minute, final int second, final int milliseconds) { - return new IsSame<>(new DateWrapper(year, month, dayOfMonth, hour, minute, second, milliseconds), - new DateFormatter()); + return new IsSame<>(JAVADATE_AS_LOCALDATETIME, + localDateTime(LocalDateTime.of(year, month, dayOfMonth, hour, minute, second, milliseconds * 1000000)), + LOCALDATETIME); } /** @@ -775,7 +833,7 @@ public static DateMatcher isInstant(final int year, * @param date the reference date against which the examined date is checked */ public static DateMatcher sameOrBefore(final Date date) { - return new IsSameOrBefore<>(new DateWrapper(date), new DateFormatter()); + return new IsSameOrBefore<>(JAVADATE_AS_JAVADATE, javaDate(date), JAVADATE); } /** @@ -790,7 +848,7 @@ public static DateMatcher sameOrBefore(final Date date) { * @param date the reference date against which the examined date is checked */ public static DateMatcher sameOrBefore(final java.sql.Date date) { - return new IsSameOrBefore<>(new DateWrapper(date), new DateFormatter()); + return new IsSameOrBefore<>(JAVADATE_AS_SQLDATE, sqlDate(date), SQLDATE); } /** @@ -805,9 +863,24 @@ public static DateMatcher sameOrBefore(final java.sql.Date date) { * @param date the reference date against which the examined date is checked */ public static DateMatcher sameOrBefore(final LocalDate date) { - return new IsSameOrBefore<>(new DateWrapper(date), new DateFormatter()); + return new IsSameOrBefore<>(JAVADATE_AS_LOCALDATE, localDate(date), LOCALDATE); } + /** + * Creates a matcher that matches when the examined date is at the same instant or before the reference date + *

+ * For example: + * + *

+     * assertThat(myDate, sameOrBefore(LocalDateTime.now()))
+     * 
+ * + * @param date the reference date against which the examined date is checked + */ + public static DateMatcher sameOrBefore(final LocalDateTime date) { + return new IsSameOrBefore<>(JAVADATE_AS_LOCALDATETIME, localDateTime(date), LOCALDATETIME); + } + /** * Creates a matcher that matches when the examined date is at the same date or before the reference date *

@@ -860,7 +933,7 @@ public static DateMatcher sameOrBefore(final int year, final Months month, * @param dayOfMonth the day of the month against which the examined date is checked */ public static DateMatcher sameOrBefore(final int year, final Month month, final int dayOfMonth) { - return new IsSameOrBefore<>(new DateWrapper(LocalDate.of(year, month, dayOfMonth)), new DateFormatter()); + return sameOrBefore(LocalDate.of(year, month, dayOfMonth)); } /** @@ -914,8 +987,7 @@ public static DateMatcher sameOrBefore(final int year, final int hour, final int minute, final int second) { - return new IsSameOrBefore<>(new DateWrapper(year, month, dayOfMonth, hour, minute, second), - new DateFormatter()); + return sameOrBefore(LocalDateTime.of(year, month, dayOfMonth, hour, minute, second)); } /** @@ -930,7 +1002,7 @@ public static DateMatcher sameOrBefore(final int year, * @param date the reference date against which the examined date is checked */ public static DateMatcher sameOrAfter(final Date date) { - return new IsSameOrAfter<>(new DateWrapper(date), new DateFormatter()); + return new IsSameOrAfter<>(JAVADATE_AS_JAVADATE, javaDate(date), JAVADATE); } /** @@ -945,7 +1017,7 @@ public static DateMatcher sameOrAfter(final Date date) { * @param date the reference date against which the examined date is checked */ public static DateMatcher sameOrAfter(final java.sql.Date date) { - return new IsSameOrAfter<>(new DateWrapper(date), new DateFormatter()); + return new IsSameOrAfter<>(JAVADATE_AS_SQLDATE, sqlDate(date), SQLDATE); } /** @@ -960,7 +1032,22 @@ public static DateMatcher sameOrAfter(final java.sql.Date date) { * @param date the reference date against which the examined date is checked */ public static DateMatcher sameOrAfter(final LocalDate date) { - return new IsSameOrAfter<>(new DateWrapper(date), new DateFormatter()); + return new IsSameOrAfter<>(JAVADATE_AS_LOCALDATE, localDate(date), LOCALDATE); + } + + /** + * F Creates a matcher that matches when the examined date is at the same instant or after the reference date + *

+ * For example: + * + *

+     * assertThat(myDate, sameOrAfter(LocalDateTime.now()))
+     * 
+ * + * @param date the reference date against which the examined date is checked + */ + public static DateMatcher sameOrAfter(final LocalDateTime date) { + return new IsSameOrAfter<>(JAVADATE_AS_LOCALDATETIME, localDateTime(date), LOCALDATETIME); } /** @@ -1013,7 +1100,7 @@ public static DateMatcher sameOrAfter(final int year, final Months month, * @param dayOfMonth the day of the month against which the examined date is checked */ public static DateMatcher sameOrAfter(final int year, final Month month, final int dayOfMonth) { - return new IsSameOrAfter<>(new DateWrapper(year, month, dayOfMonth), new DateFormatter()); + return sameOrAfter(LocalDate.of(year, month, dayOfMonth)); } /** @@ -1061,14 +1148,13 @@ public static DateMatcher sameOrAfter(final int year, * @param minute the minute of the hour against which the examined date is checked * @param second the second of the minute against which the examined date is checked */ - public static DateMatcher sameOrAfter(final int year, final Month month, final int dayOfMonth, final int hour, final int minute, final int second) { - return new IsSameOrAfter<>(new DateWrapper(year, month, dayOfMonth, hour, minute, second), new DateFormatter()); + return sameOrAfter(LocalDateTime.of(year, month, dayOfMonth, hour, minute, second)); } /** @@ -1088,21 +1174,15 @@ public static DateMatcher sameMinute(final Date date) { return sameMinuteOfHour(date); } - /** - * Creates a matcher that matches when the examined date is on the same minute as the reference date - *

- * For example: - * - *

-     * assertThat(myDate, sameMinute(new Date()))
-     * 
- * - * @param date the reference date against which the examined date is checked - * @deprecated Use {@link #sameMinuteOfHour(Date)} instead - */ - @Deprecated + /** + * @deprecated java.sql.Date does not support time-based comparisons. Prefer {@link SqlDateMatchers} for + * java.sql.Date appropriate matchers + * @param date the reference date against which the examined date is checked + * @throws TemporalConversionException + */ + @Deprecated public static DateMatcher sameMinute(final java.sql.Date date) { - return sameMinuteOfHour(date); + throw new TemporalConversionException(UNSUPPORTED_SQL_DATE_UNIT); } /** @@ -1117,24 +1197,18 @@ public static DateMatcher sameMinute(final java.sql.Date date) { * @param date the reference date against which the examined date is checked */ public static DateMatcher sameMinuteOfHour(final Date date) { - return new IsMinute<>(new FieldDateWrapper(date, ChronoField.MINUTE_OF_HOUR), - new FieldDateAdaptor(ChronoField.MINUTE_OF_HOUR)); + return new IsMinute<>(JAVADATE_AS_MINUTE, minute(date)); } - /** - * Creates a matcher that matches when the examined date is on the same minute as the reference date - *

- * For example: - * - *

-     * assertThat(myDate, sameMinute(new Date()))
-     * 
- * - * @param date the reference date against which the examined date is checked - */ + /** + * @deprecated java.sql.Date does not support time-based comparisons. Prefer {@link SqlDateMatchers} for + * java.sql.Date appropriate matchers + * @param date the reference date against which the examined date is checked + * @throws TemporalConversionException + */ + @Deprecated public static DateMatcher sameMinuteOfHour(final java.sql.Date date) { - return new IsMinute<>(new FieldDateWrapper(date, ChronoField.MINUTE_OF_HOUR), - new FieldDateAdaptor(ChronoField.MINUTE_OF_HOUR)); + throw new TemporalConversionException(UNSUPPORTED_SQL_DATE_UNIT); } /** @@ -1166,8 +1240,7 @@ public static DateMatcher sameMinute(final int minute) { * @param minute the reference minute against which the examined date is checked */ public static DateMatcher isMinute(final int minute) { - return new IsMinute<>(new FieldDateWrapper(minute, ChronoField.MINUTE_OF_HOUR), - new FieldDateAdaptor(ChronoField.MINUTE_OF_HOUR)); + return new IsMinute<>(JAVADATE_AS_MINUTE, minute(minute)); } /** @@ -1216,7 +1289,7 @@ public static DateMatcher sameMonth(final java.sql.Date date) { * @param date the reference date against which the examined date is checked */ public static DateMatcher sameMonthOfYear(final Date date) { - return isMonth(date.toInstant().atZone(ZoneId.systemDefault()).getMonth()); + return new IsMonth<>(JAVADATE_AS_MONTH, month(date)); } /** @@ -1231,7 +1304,7 @@ public static DateMatcher sameMonthOfYear(final Date date) { * @param date the reference date against which the examined date is checked */ public static DateMatcher sameMonthOfYear(final java.sql.Date date) { - return isMonth(date.toLocalDate().getMonth()); + return new IsMonth<>(JAVADATE_AS_MONTH, month(date)); } /** @@ -1289,7 +1362,7 @@ public static DateMatcher sameSecond(final Date date) { * @deprecated java.sql.Date does not support time-based comparisons. Prefer {@link SqlDateMatchers} for * java.sql.Date appropriate matchers * @param date the reference date against which the examined date is checked - * @throws IllegalArgumentException + * @throws TemporalConversionException */ @Deprecated public static DateMatcher sameSecond(final java.sql.Date date) { @@ -1308,19 +1381,18 @@ public static DateMatcher sameSecond(final java.sql.Date date) { * @param date the reference date against which the examined date is checked */ public static DateMatcher sameSecondOfMinute(final Date date) { - return new IsSecond<>(new FieldDateWrapper(date, ChronoField.SECOND_OF_MINUTE), - new FieldDateAdaptor(ChronoField.SECOND_OF_MINUTE)); + return new IsSecond<>(JAVADATE_AS_SECOND, second(date)); } /** * @deprecated java.sql.Date does not support time-based comparisons. Prefer {@link SqlDateMatchers} for * java.sql.Date appropriate matchers * @param date the reference date against which the examined date is checked - * @throws IllegalArgumentException + * @throws TemporalConversionException */ @Deprecated public static DateMatcher sameSecondOfMinute(final java.sql.Date date) { - throw new IllegalArgumentException(UNSUPPORTED_SQL_DATE_UNIT); + throw new TemporalConversionException(UNSUPPORTED_SQL_DATE_UNIT); } /** @@ -1352,8 +1424,7 @@ public static DateMatcher sameSecond(final int second) { * @param second the reference date against which the examined date is checked */ public static DateMatcher isSecond(final int second) { - return new IsSecond<>(new FieldDateWrapper(second, ChronoField.SECOND_OF_MINUTE), - new FieldDateAdaptor(ChronoField.SECOND_OF_MINUTE)); + return new IsSecond<>(JAVADATE_AS_SECOND, second(second)); } /** @@ -1377,13 +1448,13 @@ public static DateMatcher sameMillisecond(final Date date) { * @deprecated java.sql.Date does not support time-based comparisons. Prefer {@link SqlDateMatchers} for * java.sql.Date appropriate matchers * @param date the reference date against which the examined date is checked - * @throws IllegalArgumentException + * @throws TemporalConversionException */ @Deprecated public static DateMatcher sameMillisecond(final java.sql.Date date) { - return sameMillisecondOfSecond(date); + throw new TemporalConversionException(UNSUPPORTED_SQL_DATE_UNIT); } - + /** * Creates a matcher that matches when the examined date is on the same millisecond as the reference date *

@@ -1396,19 +1467,18 @@ public static DateMatcher sameMillisecond(final java.sql.Date date) { * @param date the reference date against which the examined date is checked */ public static DateMatcher sameMillisecondOfSecond(final Date date) { - return new IsMillisecond<>(new FieldDateWrapper(date, ChronoField.MILLI_OF_SECOND), - new FieldDateAdaptor(ChronoField.MILLI_OF_SECOND)); + return new IsMillisecond<>(JAVADATE_AS_MILLISECOND, millisecondOfMinute(date)); } /** * @deprecated java.sql.Date does not support time-based comparisons. Prefer {@link SqlDateMatchers} for * java.sql.Date appropriate matchers * @param date the reference date against which the examined date is checked - * @throws IllegalArgumentException + * @throws TemporalConversionException */ @Deprecated public static DateMatcher sameMillisecondOfSecond(final java.sql.Date date) { - throw new IllegalArgumentException(UNSUPPORTED_SQL_DATE_UNIT); + throw new TemporalConversionException(UNSUPPORTED_SQL_DATE_UNIT); } /** @@ -1440,8 +1510,7 @@ public static DateMatcher sameMillisecond(final int millisecond) { * @param millisecond the millisecond against which the examined date is checked */ public static DateMatcher isMillisecond(final int millisecond) { - return new IsMillisecond<>(new FieldDateWrapper(millisecond, ChronoField.MILLI_OF_SECOND), - new FieldDateAdaptor(ChronoField.MILLI_OF_SECOND)); + return new IsMillisecond<>(JAVADATE_AS_MILLISECOND, millisecondOfMinute(millisecond)); } /** @@ -1456,7 +1525,7 @@ public static DateMatcher isMillisecond(final int millisecond) { * @param date the reference date against which the examined date is checked */ public static DateMatcher sameYear(final Date date) { - return new IsYear<>(new FieldDateWrapper(date, ChronoField.YEAR), new FieldDateAdaptor(ChronoField.YEAR)); + return new IsYear<>(JAVADATE_AS_YEAR, year(date)); } /** @@ -1471,7 +1540,7 @@ public static DateMatcher sameYear(final Date date) { * @param date the reference date against which the examined date is checked */ public static DateMatcher sameYear(final java.sql.Date date) { - return new IsYear<>(new FieldDateWrapper(date, ChronoField.YEAR), new FieldDateAdaptor(ChronoField.YEAR)); + return new IsYear<>(JAVADATE_AS_YEAR, year(date)); } /** @@ -1486,7 +1555,7 @@ public static DateMatcher sameYear(final java.sql.Date date) { * @param year the reference year against which the examined date is checked */ public static DateMatcher isYear(final int year) { - return new IsYear<>(new FieldDateWrapper(year, ChronoField.YEAR), new FieldDateAdaptor(ChronoField.YEAR)); + return new IsYear<>(JAVADATE_AS_YEAR, year(year)); } /** @@ -1518,7 +1587,7 @@ public static DateMatcher within(final long period, final TimeUnit unit, f * @param date the reference date against which the examined date is checked */ public static DateMatcher within(final long period, final ChronoUnit unit, final Date date) { - return new IsWithin<>(period, unit, new DateWrapper(date), new DateFormatter()); + return new IsWithin<>(Interval.of(period, unit), JAVADATE_AS_JAVADATE, javaDate(date), JAVADATE); } /** @@ -1550,7 +1619,7 @@ public static DateMatcher within(final long period, final TimeUnit unit, f * @param date the reference date against which the examined date is checked */ public static DateMatcher within(final long period, final ChronoUnit unit, final java.sql.Date date) { - return new IsWithin<>(period, unit, new DateWrapper(date), new DateFormatter()); + return new IsWithin<>(Interval.of(period, unit), JAVADATE_AS_SQLDATE, sqlDate(date), SQLDATE); } /** @@ -1565,9 +1634,24 @@ public static DateMatcher within(final long period, final ChronoUnit unit, * @param date the reference date against which the examined date is checked */ public static DateMatcher within(final long period, final ChronoUnit unit, final LocalDate date) { - return new IsWithin<>(period, unit, new DateWrapper(date), new DateFormatter()); + return new IsWithin<>(Interval.of(period, unit), JAVADATE_AS_LOCALDATE, localDate(date), LOCALDATE); } + /** + * Creates a matcher that matches when the examined date is within a defined period the reference date + *

+ * For example: + * + *

+     * assertThat(myDate, within(10, ChronoUnit.DAYS, LocalDateTime.now()))
+     * 
+ * + * @param date the reference date against which the examined date is checked + */ + public static DateMatcher within(final long period, final ChronoUnit unit, final LocalDateTime date) { + return new IsWithin<>(Interval.of(period, unit), JAVADATE_AS_LOCALDATETIME, localDateTime(date), LOCALDATETIME); + } + /** * Creates a matcher that matches when the examined date is within a defined period the reference date *

@@ -1694,10 +1778,7 @@ public static DateMatcher within(final long period, final int minute, final int second, final int milliseconds) { - return new IsWithin<>(period, - unit, - new DateWrapper(year, month, dayOfMonth, hour, minute, second, milliseconds), - new DateFormatter()); + return within(period, unit, LocalDateTime.of(year, month, dayOfMonth, hour, minute, second, milliseconds*1000000)); } /** @@ -1866,11 +1947,7 @@ public static DateMatcher isWeekend() { * */ public static DateMatcher isFirstDayOfMonth() { - return new IsMinimum<>(ChronoField.DAY_OF_MONTH, - new FieldDateAdaptor(ChronoField.DAY_OF_MONTH), - new FieldDateRangeAdaptor(ChronoField.DAY_OF_MONTH), - new DatePartFormatter(), - () -> "the date is the first day of the month"); + return new IsFirstDayOfMonth<>(JAVADATE_AS_TEMPORAL); } /** @@ -1886,10 +1963,7 @@ public static DateMatcher isFirstDayOfMonth() { * @param field the temporal field to check */ public static DateMatcher isMinimum(final ChronoField field) { - return new IsMinimum<>(field, - new FieldDateAdaptor(field), - new FieldDateRangeAdaptor(field), - new DatePartFormatter()); + return new IsMinimum<>(JAVADATE_AS_TEMPORAL, field); } /** @@ -1902,11 +1976,7 @@ public static DateMatcher isMinimum(final ChronoField field) { * */ public static DateMatcher isLastDayOfMonth() { - return new IsMaximum<>(ChronoField.DAY_OF_MONTH, - new FieldDateAdaptor(ChronoField.DAY_OF_MONTH), - new FieldDateRangeAdaptor(ChronoField.DAY_OF_MONTH), - new DatePartFormatter(), - () -> "the date is the last day of the month"); + return new IsLastDayOfMonth<>(JAVADATE_AS_TEMPORAL); } /** @@ -1922,10 +1992,7 @@ public static DateMatcher isLastDayOfMonth() { * @param field the temporal field to check */ public static DateMatcher isMaximum(final ChronoField field) { - return new IsMaximum<>(field, - new FieldDateAdaptor(field), - new FieldDateRangeAdaptor(field), - new DatePartFormatter()); + return new IsMaximum<>(JAVADATE_AS_TEMPORAL, field); } /** @@ -1938,8 +2005,7 @@ public static DateMatcher isMaximum(final ChronoField field) { * */ public static DateMatcher isMonth(final Month month) { - return new IsMonth<>(new FieldDateWrapper(month.getValue(), ChronoField.MONTH_OF_YEAR), - new FieldDateAdaptor(ChronoField.MONTH_OF_YEAR)); + return new IsMonth<>(JAVADATE_AS_MONTH, month(month)); } /** @@ -2108,7 +2174,7 @@ public static DateMatcher isDecember() { * */ public static DateMatcher isLeapYear() { - return new IsLeapYear<>(DateMatchers::toZoneDateTime, new DateFormatter()); + return new IsLeapYear<>(JAVADATE_AS_YEAR); } private static ChronoUnit convertUnit(final TimeUnit unit) { @@ -2131,14 +2197,6 @@ private static ChronoUnit convertUnit(final TimeUnit unit) { throw new IllegalArgumentException("Unknown TimeUnit '" + unit + "'"); } } - - private static Temporal toZoneDateTime(final Date date, final ZoneId zone) { - if (date instanceof java.sql.Date) { - return ((java.sql.Date) date).toLocalDate(); - } else { - return date.toInstant().atZone(zone); - } - } private static DayOfWeek toDayOfWeek(Date date, ZoneId zone) { if ( date instanceof java.sql.Date) { diff --git a/src/main/java/org/exparity/hamcrest/date/LocalDateMatchers.java b/src/main/java/org/exparity/hamcrest/date/LocalDateMatchers.java index faca85a..f6c27bc 100644 --- a/src/main/java/org/exparity/hamcrest/date/LocalDateMatchers.java +++ b/src/main/java/org/exparity/hamcrest/date/LocalDateMatchers.java @@ -2,20 +2,36 @@ import static java.time.DayOfWeek.*; import static java.time.Month.*; +import static org.exparity.hamcrest.date.core.TemporalConverters.*; +import static org.exparity.hamcrest.date.core.TemporalFunctions.LOCALDATE; +import static org.exparity.hamcrest.date.core.TemporalProviders.*; import java.time.DayOfWeek; import java.time.LocalDate; import java.time.Month; +import java.time.Period; import java.time.temporal.ChronoField; import java.time.temporal.ChronoUnit; -import java.util.stream.Stream; -import org.exparity.hamcrest.date.core.*; -import org.exparity.hamcrest.date.core.format.DatePartFormatter; -import org.exparity.hamcrest.date.core.format.LocalDateFormatter; -import org.exparity.hamcrest.date.core.wrapper.FieldLocalDateWrapper; -import org.exparity.hamcrest.date.core.wrapper.LocalDateWrapper; -import org.hamcrest.Factory; +import org.exparity.hamcrest.date.core.DateMatcher; +import org.exparity.hamcrest.date.core.IsAfter; +import org.exparity.hamcrest.date.core.IsBefore; +import org.exparity.hamcrest.date.core.IsDayOfMonth; +import org.exparity.hamcrest.date.core.IsDayOfWeek; +import org.exparity.hamcrest.date.core.IsFirstDayOfMonth; +import org.exparity.hamcrest.date.core.IsLastDayOfMonth; +import org.exparity.hamcrest.date.core.IsLeapYear; +import org.exparity.hamcrest.date.core.IsMaximum; +import org.exparity.hamcrest.date.core.IsMinimum; +import org.exparity.hamcrest.date.core.IsMonth; +import org.exparity.hamcrest.date.core.IsSameDay; +import org.exparity.hamcrest.date.core.IsSameOrAfter; +import org.exparity.hamcrest.date.core.IsSameOrBefore; +import org.exparity.hamcrest.date.core.IsWithin; +import org.exparity.hamcrest.date.core.IsYear; +import org.exparity.hamcrest.date.core.TemporalConverters; +import org.exparity.hamcrest.date.core.TemporalProviders; +import org.exparity.hamcrest.date.core.types.Interval; /** * Static factory for creating {@link org.hamcrest.Matcher} instances for comparing {@link LocalDate} instances @@ -36,7 +52,7 @@ public abstract class LocalDateMatchers { * @param date the reference date against which the examined date is checked */ public static DateMatcher after(final LocalDate date) { - return new IsAfter<>(new LocalDateWrapper(date), new LocalDateFormatter()); + return new IsAfter<>(LOCALDATE_AS_LOCALDATE, localDate(date), LOCALDATE); } /** @@ -68,7 +84,7 @@ public static DateMatcher after(final int year, final Month month, fi * @param date the reference date against which the examined date is checked */ public static DateMatcher before(final LocalDate date) { - return new IsBefore<>(new LocalDateWrapper(date), new LocalDateFormatter()); + return new IsBefore<>(LOCALDATE_AS_LOCALDATE, localDate(date), LOCALDATE); } /** @@ -100,7 +116,7 @@ public static DateMatcher before(final int year, final Month month, f * @param date the reference date against which the examined date is checked */ public static DateMatcher sameDay(final LocalDate date) { - return new IsSameDay<>(new LocalDateWrapper(date), new LocalDateFormatter()); + return new IsSameDay<>(LOCALDATE_AS_LOCALDATE, localDate(date)); } /** @@ -132,7 +148,7 @@ public static DateMatcher isDay(final int year, final Month month, fi * @param date the reference date against which the examined date is checked */ public static DateMatcher sameOrBefore(final LocalDate date) { - return new IsSameOrBefore<>(new LocalDateWrapper(date), new LocalDateFormatter()); + return new IsSameOrBefore<>(LOCALDATE_AS_LOCALDATE, localDate(date), LOCALDATE); } /** @@ -149,7 +165,6 @@ public static DateMatcher sameOrBefore(final LocalDate date) { * @param month the month against which the examined date is checked * @param day the day of the month against which the examined date is checked */ - @Factory public static DateMatcher sameOrBefore(final int year, final Month month, final int day) { return sameOrBefore(LocalDate.of(year, month, day)); } @@ -166,7 +181,7 @@ public static DateMatcher sameOrBefore(final int year, final Month mo * @param date the reference date against which the examined date is checked */ public static DateMatcher sameOrAfter(final LocalDate date) { - return new IsSameOrAfter<>(new LocalDateWrapper(date), new LocalDateFormatter()); + return new IsSameOrAfter<>(LOCALDATE_AS_LOCALDATE, localDate(date), LOCALDATE); } /** @@ -213,10 +228,7 @@ public static DateMatcher sameMonthOfYear(final LocalDate date) { * @param date the reference date against which the examined date is checked */ public static DateMatcher sameDayOfMonth(final LocalDate date) { - return new IsDayOfMonth<>( - new FieldLocalDateWrapper(date, ChronoField.DAY_OF_MONTH), - (d, z) -> d.atStartOfDay(z).getDayOfMonth() - ); + return new IsDayOfMonth<>(LOCALDATE_AS_DAYOFMONTH, dayOfMonth(date)); } /** @@ -231,10 +243,7 @@ public static DateMatcher sameDayOfMonth(final LocalDate date) { * @param dayOfMonth the expected day of the month */ public static DateMatcher isDayOfMonth(final int dayOfMonth) { - return new IsDayOfMonth<>( - new FieldLocalDateWrapper(dayOfMonth, ChronoField.DAY_OF_MONTH), - (d, z) -> d.atStartOfDay(z).getDayOfMonth() - ); + return new IsDayOfMonth<>(LOCALDATE_AS_DAYOFMONTH, dayOfMonth(dayOfMonth)); } /** @@ -249,10 +258,7 @@ public static DateMatcher isDayOfMonth(final int dayOfMonth) { * @param date the reference date against which the examined date is checked */ public static DateMatcher sameYear(final LocalDate date) { - return new IsYear<>( - new FieldLocalDateWrapper(date, ChronoField.YEAR), - (d, z) -> d.atStartOfDay(z).getYear() - ); + return new IsYear<>(LOCALDATE_AS_YEAR, year(date)); } /** @@ -267,12 +273,24 @@ public static DateMatcher sameYear(final LocalDate date) { * @param year the reference year against which the examined date is checked */ public static DateMatcher isYear(final int year) { - return new IsYear<>( - new FieldLocalDateWrapper(year, ChronoField.YEAR), - (d, z) -> d.atStartOfDay(z).getYear() - ); + return new IsYear<>(TemporalConverters.LOCALDATE_AS_YEAR, TemporalProviders.year(year)); } + /** + * Creates a matcher that matches when the examined date is within a defined period the reference date + *

+ * For example: + * + *

+     * assertThat(myDate, within(10, TimeUnit.DAYS, Moments.today()))
+     * 
+ * + * @param date the reference date against which the examined date is checked + */ + public static DateMatcher within(final Period period , final LocalDate date) { + return new IsWithin<>(Interval.of(period), LOCALDATE_AS_LOCALDATE, localDate(date), LOCALDATE); + } + /** * Creates a matcher that matches when the examined date is within a defined period the reference date *

@@ -285,7 +303,7 @@ public static DateMatcher isYear(final int year) { * @param date the reference date against which the examined date is checked */ public static DateMatcher within(final long period, final ChronoUnit unit, final LocalDate date) { - return new IsWithin<>(period, unit, new LocalDateWrapper(date), new LocalDateFormatter()); + return new IsWithin<>(Interval.of(period, unit), LOCALDATE_AS_LOCALDATE, localDate(date), LOCALDATE); } /** @@ -375,10 +393,7 @@ public static DateMatcher sameDayOfWeek(final LocalDate date) { * */ public static DateMatcher isDayOfWeek(final DayOfWeek dayOfWeek) { - return new IsDayOfWeek<>( - new FieldLocalDateWrapper(dayOfWeek.getValue(), ChronoField.DAY_OF_WEEK), - (d, z) -> d.atStartOfDay(z).getDayOfWeek().getValue() - ); + return new IsDayOfWeek<>(LOCALDATE_AS_DAYOFWEEK, daysOfWeek(dayOfWeek)); } /** @@ -391,10 +406,7 @@ public static DateMatcher isDayOfWeek(final DayOfWeek dayOfWeek) { * */ public static DateMatcher isDayOfWeek(final DayOfWeek... daysOfWeek) { - return new AnyOf<>( - Stream.of(daysOfWeek).map(LocalDateMatchers::isDayOfWeek), - (d, z) -> "the date is on a " + d.atStartOfDay(z).getDayOfWeek().name().toLowerCase() - ); + return new IsDayOfWeek<>(LOCALDATE_AS_DAYOFWEEK, daysOfWeek(daysOfWeek)); } /** @@ -524,13 +536,7 @@ public static DateMatcher isWeekend() { * */ public static DateMatcher isFirstDayOfMonth() { - return new IsMinimum<>( - ChronoField.DAY_OF_MONTH, - (d, z) -> d.atStartOfDay(z).get(ChronoField.DAY_OF_MONTH), - (d, z) -> ChronoField.DAY_OF_MONTH.rangeRefinedBy(d.atStartOfDay(z)), - new DatePartFormatter(), - () -> "the date is the first day of the month" - ); + return new IsFirstDayOfMonth<>(LOCALDATE_AS_LOCALDATE); } /** @@ -546,12 +552,7 @@ public static DateMatcher isFirstDayOfMonth() { * @param field the temporal field to check */ public static DateMatcher isMinimum(final ChronoField field) { - return new IsMinimum<>( - field, - (d, z) -> d.atStartOfDay(z).get(field), - (d, z) -> field.rangeRefinedBy(d.atStartOfDay(z)), - new DatePartFormatter() - ); + return new IsMinimum<>(LOCALDATE_AS_LOCALDATE, field); } /** @@ -564,13 +565,7 @@ public static DateMatcher isMinimum(final ChronoField field) { * */ public static DateMatcher isLastDayOfMonth() { - return new IsMaximum<>( - ChronoField.DAY_OF_MONTH, - (d, z) -> d.atStartOfDay(z).get(ChronoField.DAY_OF_MONTH), - (d, z) -> ChronoField.DAY_OF_MONTH.rangeRefinedBy(d.atStartOfDay(z)), - new DatePartFormatter(), - () -> "the date is the last day of the month" - ); + return new IsLastDayOfMonth<>(LOCALDATE_AS_LOCALDATE); } /** @@ -586,12 +581,7 @@ public static DateMatcher isLastDayOfMonth() { * @param field the temporal field to check */ public static DateMatcher isMaximum(final ChronoField field) { - return new IsMaximum<>( - field, - (d, z) -> d.atStartOfDay(z).get(field), - (d, z) -> field.rangeRefinedBy(d.atStartOfDay(z)), - new DatePartFormatter() - ); + return new IsMaximum<>(LOCALDATE_AS_LOCALDATE, field); } /** @@ -604,10 +594,7 @@ public static DateMatcher isMaximum(final ChronoField field) { * */ public static DateMatcher isMonth(final Month month) { - return new IsMonth<>( - new FieldLocalDateWrapper(month.getValue(), ChronoField.MONTH_OF_YEAR), - (d, z) -> d.atStartOfDay(z).getMonthValue() - ); + return new IsMonth<>(LOCALDATE_AS_MONTH, month(month)); } /** @@ -776,6 +763,6 @@ public static DateMatcher isDecember() { * */ public static DateMatcher isLeapYear() { - return new IsLeapYear<>((d, z) -> d, new LocalDateFormatter()); + return new IsLeapYear<>(LOCALDATE_AS_YEAR); } } diff --git a/src/main/java/org/exparity/hamcrest/date/LocalDateTimeMatchers.java b/src/main/java/org/exparity/hamcrest/date/LocalDateTimeMatchers.java index 81d3964..d489dd5 100644 --- a/src/main/java/org/exparity/hamcrest/date/LocalDateTimeMatchers.java +++ b/src/main/java/org/exparity/hamcrest/date/LocalDateTimeMatchers.java @@ -2,17 +2,39 @@ import static java.time.DayOfWeek.*; import static java.time.Month.*; - -import java.time.*; +import static org.exparity.hamcrest.date.core.TemporalConverters.*; +import static org.exparity.hamcrest.date.core.TemporalFunctions.LOCALDATETIME; +import static org.exparity.hamcrest.date.core.TemporalProviders.*; + +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.Month; +import java.time.ZoneId; import java.time.temporal.ChronoField; import java.time.temporal.ChronoUnit; -import java.util.stream.Stream; -import org.exparity.hamcrest.date.core.*; -import org.exparity.hamcrest.date.core.format.DatePartFormatter; -import org.exparity.hamcrest.date.core.format.LocalDateTimeFormatter; -import org.exparity.hamcrest.date.core.wrapper.FieldLocalDateTimeWrapper; -import org.exparity.hamcrest.date.core.wrapper.LocalDateTimeWrapper; +import org.exparity.hamcrest.date.core.DateMatcher; +import org.exparity.hamcrest.date.core.IsAfter; +import org.exparity.hamcrest.date.core.IsBefore; +import org.exparity.hamcrest.date.core.IsDayOfMonth; +import org.exparity.hamcrest.date.core.IsDayOfWeek; +import org.exparity.hamcrest.date.core.IsFirstDayOfMonth; +import org.exparity.hamcrest.date.core.IsHour; +import org.exparity.hamcrest.date.core.IsLastDayOfMonth; +import org.exparity.hamcrest.date.core.IsLeapYear; +import org.exparity.hamcrest.date.core.IsMaximum; +import org.exparity.hamcrest.date.core.IsMinimum; +import org.exparity.hamcrest.date.core.IsMinute; +import org.exparity.hamcrest.date.core.IsMonth; +import org.exparity.hamcrest.date.core.IsSame; +import org.exparity.hamcrest.date.core.IsSameDay; +import org.exparity.hamcrest.date.core.IsSameOrAfter; +import org.exparity.hamcrest.date.core.IsSameOrBefore; +import org.exparity.hamcrest.date.core.IsSecond; +import org.exparity.hamcrest.date.core.IsWithin; +import org.exparity.hamcrest.date.core.IsYear; +import org.exparity.hamcrest.date.core.types.Interval; import org.hamcrest.Factory; /** @@ -22,105 +44,100 @@ */ public abstract class LocalDateTimeMatchers { - /** - * Creates a matcher that matches when the examined date is after the reference date - *

- * For example: - * - *

-     * MatcherAssert.assertThat(myDate, LocalDateTimeMatchers.after(LocalDateTime.now()));
-     * 
- * - * @param date the reference date against which the examined date is checked - */ - public static DateMatcher after(final LocalDateTime date) { - return new IsAfter<>(new LocalDateTimeWrapper(date), new LocalDateTimeFormatter()); - } - - /** - * Creates a matcher that matches when the examined date is after the end of the reference year - *

- * For example: - * - *

-     * MatcherAssert.assertThat(myDate, LocalDateTimeMatchers.after(2012, Month.MAY, 12));
-     * 
- * - * @param year the year against which the examined date is checked - * @param month the month against which the examined date is checked - * @param dayOfMonth the day of the month against which the examined date is checked - * @param hour the hour of the day - * @param minute the minute of the hour - * @param second the second of the minute - */ - public static DateMatcher after(final int year, - final Month month, - final int dayOfMonth, - final int hour, - final int minute, - final int second) { - return new IsAfter<>(new LocalDateTimeWrapper(year, month, dayOfMonth, hour, minute, second), - new LocalDateTimeFormatter()); - } - - /** - * Creates a matcher that matches when the examined date is before the reference date - *

- * For example: - * - *

-     * MatcherAssert.assertThat(myDate, LocalDateTimeMatchers.before(LocalDateTime.now()));
-     * 
- * - * @param date the reference date against which the examined date is checked - */ - public static DateMatcher before(final LocalDateTime date) { - return new IsBefore<>(new LocalDateTimeWrapper(date), new LocalDateTimeFormatter()); - } - - /** - * Creates a matcher that matches when the examined date is before the end of the reference year - *

- * For example: - * - *

-     * MatcherAssert.assertThat(myDate, LocalDateTimeMatchers.before(2012, Month.MAY, 12));
-     * 
- * - * @param year the year against which the examined date is checked - * @param month the month against which the examined date is checked - * @param dayOfMonth the day of the month against which the examined date is checked - * @param hour the hour of the day - * @param minute the minute of the hour - * @param second the second of the minute - */ - public static DateMatcher before(final int year, - final Month month, - final int dayOfMonth, - final int hour, - final int minute, - final int second) { - return new IsBefore<>(new LocalDateTimeWrapper(year, month, dayOfMonth, hour, minute, second), - new LocalDateTimeFormatter()); - } - - /** - * Creates a matcher that matches when the examined date is on the same day of the year as the reference date - *

- * For example: - * - *

-     * assertThat(myDate, sameDay(LocalDateTime.now()));
-     * 
- * - * @param date the reference date against which the examined date is checked - */ - public static DateMatcher sameDay(final LocalDateTime date) { - return new IsSameDay<>( - new LocalDateTimeWrapper(date), - new LocalDateTimeFormatter() - ); - } + /** + * Creates a matcher that matches when the examined date is after the reference date + *

+ * For example: + * + *

+	 * MatcherAssert.assertThat(myDate, LocalDateTimeMatchers.after(LocalDateTime.now()));
+	 * 
+ * + * @param date the reference date against which the examined date is checked + */ + public static DateMatcher after(final LocalDateTime date) { + return new IsAfter<>(LOCALDATETIME_AS_LOCALDATETIME, localDateTime(date), LOCALDATETIME); + } + + /** + * Creates a matcher that matches when the examined date is after the end of the reference year + *

+ * For example: + * + *

+	 * MatcherAssert.assertThat(myDate, LocalDateTimeMatchers.after(2012, Month.MAY, 12));
+	 * 
+ * + * @param year the year against which the examined date is checked + * @param month the month against which the examined date is checked + * @param dayOfMonth the day of the month against which the examined date is checked + * @param hour the hour of the day + * @param minute the minute of the hour + * @param second the second of the minute + */ + public static DateMatcher after(final int year, + final Month month, + final int dayOfMonth, + final int hour, + final int minute, + final int second) { + return after(LocalDateTime.of(year, month, dayOfMonth, hour, minute, second)); + } + + /** + * Creates a matcher that matches when the examined date is before the reference date + *

+ * For example: + * + *

+	 * MatcherAssert.assertThat(myDate, LocalDateTimeMatchers.before(LocalDateTime.now()));
+	 * 
+ * + * @param date the reference date against which the examined date is checked + */ + public static DateMatcher before(final LocalDateTime date) { + return new IsBefore<>(LOCALDATETIME_AS_LOCALDATETIME, localDateTime(date), LOCALDATETIME); + } + + /** + * Creates a matcher that matches when the examined date is before the end of the reference year + *

+ * For example: + * + *

+	 * MatcherAssert.assertThat(myDate, LocalDateTimeMatchers.before(2012, Month.MAY, 12));
+	 * 
+ * + * @param year the year against which the examined date is checked + * @param month the month against which the examined date is checked + * @param dayOfMonth the day of the month against which the examined date is checked + * @param hour the hour of the day + * @param minute the minute of the hour + * @param second the second of the minute + */ + public static DateMatcher before(final int year, + final Month month, + final int dayOfMonth, + final int hour, + final int minute, + final int second) { + return before(LocalDateTime.of(year, month, dayOfMonth, hour, minute, second)); + } + + /** + * Creates a matcher that matches when the examined date is on the same day of the year as the reference date + *

+ * For example: + * + *

+	 * assertThat(myDate, sameDay(LocalDateTime.now()));
+	 * 
+ * + * @param date the reference date against which the examined date is checked + */ + public static DateMatcher sameDay(final LocalDateTime date) { + return new IsSameDay<>(LOCALDATETIME_AS_LOCALDATE, localDate(date)); + } /** * Creates a matcher that matches when the examined date is on the same day of the year as the reference date @@ -128,855 +145,803 @@ public static DateMatcher sameDay(final LocalDateTime date) { * For example: * *
-     * assertThat(myDate, sameDay(2012, Month.JAN, 1))
-     * 
- * - * @param dayOfMonth the reference day of the month against which the examined date is checked - * @param month the reference month against which the examined date is checked - * @param year the reference year against which the examined date is checked - */ - public static DateMatcher isDay(final int year, final Month month, final int dayOfMonth) { - return new IsSameDay<>( - new LocalDateTimeWrapper(year, month, dayOfMonth), - new LocalDateTimeFormatter() - ); - } - - /** - * Creates a matcher that matches when the examined date is at the same instant as the reference date - *

- * For example: - * - *

-     * assertThat(myDate, sameInstant(LocalDateTime.now()));
-     * 
- * - * @param date the reference date against which the examined date is checked - */ - public static DateMatcher sameInstant(final LocalDateTime date) { - return new IsSame<>(new LocalDateTimeWrapper(date), new LocalDateTimeFormatter()); - } - - /** - * Creates a matcher that matches when the examined date is at the same specified instance down to the second - *

- * For example: - * - *

-     * assertThat(myDate, sameInstant(2012, Month.JAN, 1, 3, 15, 0))
-     * 
- * - * @param dayOfMonth the reference day of the month against which the examined date is checked - * @param month the reference month against which the examined date is checked - * @param year the reference year against which the examined date is checked - * @param hour the hour of the day - * @param minute the minute of the hour - * @param second the second of the minute - * @param nanos the nanosecond of the second - */ - public static DateMatcher isInstant(final int year, - final Month month, - final int dayOfMonth, - final int hour, - final int minute, - final int second, - final int nanos) { - return new IsSame<>(new LocalDateTimeWrapper(year, month, dayOfMonth, hour, minute, second, nanos), - new LocalDateTimeFormatter()); - } - - /** - * Creates a matcher that matches when the examined date is at the same instant or before the reference date - *

- * For example: - * - *

-     * assertThat(myDate, isSameOrBefore(new Date()))
-     * 
- * - * @param date the reference date against which the examined date is checked - */ - public static DateMatcher sameOrBefore(final LocalDateTime date) { - return new IsSameOrBefore<>(new LocalDateTimeWrapper(date), new LocalDateTimeFormatter()); - } - - /** - * Creates a matcher that matches when the examined date is on the same day or before the start of the reference - * date - *

- * For example: - * - *

-     * assertThat(myDate, isSameOrBefore(2012, Months.MAY, 12));
-     * 
- * - * @param year the year against which the examined date is checked - * @param month the month against which the examined date is checked - * @param day the day of the month against which the examined date is checked - * @param hour the hour of the day - * @param minute the minute of the hour - * @param second the second of the minute - */ - @Factory - public static DateMatcher sameOrBefore(final int year, - final Month month, - final int day, - final int hour, - final int minute, - final int second) { - return new IsSameOrBefore<>(new LocalDateTimeWrapper(year, month, day, hour, minute, second), - new LocalDateTimeFormatter()); - } - - /** - * Creates a matcher that matches when the examined date is at the same instant or after the reference date - *

- * For example: - * - *

-     * assertThat(myDate, isSameOrAfter(new Date()))
-     * 
- * - * @param date the reference date against which the examined date is checked - */ - public static DateMatcher sameOrAfter(final LocalDateTime date) { - return new IsSameOrAfter<>(new LocalDateTimeWrapper(date), new LocalDateTimeFormatter()); - } - - /** - * Creates a matcher that matches when the examined date is on the same day or after the start of the reference date - *

- * For example: - * - *

-     * assertThat(myDate, isSameOrAfter(2012, Months.MAY, 12));
-     * 
- * - * @param year the year against which the examined date is checked - * @param month the month against which the examined date is checked - * @param day the day of the month against which the examined date is checked - * @param hour the hour of the day - * @param minute the minute of the hour - * @param second the second of the minute - */ - public static DateMatcher sameOrAfter(final int year, - final Month month, - final int day, - final int hour, - final int minute, - final int second) { - return sameOrAfter(LocalDateTime.of(year, month, day, hour, minute, second)); - } - - /** - * Creates a matcher that matches when the examined date is on the same month as the reference date - *

- * For example: - * - *

-     * assertThat(myDate, sameMonth(new Date()))
-     * 
- * - * @param date the reference date against which the examined date is checked - */ - public static DateMatcher sameMonthOfYear(final LocalDateTime date) { - return new IsMonth<>( - new FieldLocalDateTimeWrapper(date, ChronoField.MONTH_OF_YEAR), - (d, z) -> ZonedDateTime.of(d, z).getMonthValue() - ); - } - - /** - * Creates a matcher that matches when the examined date is on the same day of the month as the reference date - *

- * For example: - * - *

-     * assertThat(myDate, sameDayOfMonth(new Date()))
-     * 
- * - * @param date the reference date against which the examined date is checked - */ - public static DateMatcher sameDayOfMonth(final LocalDateTime date) { - return new IsDayOfMonth<>( - new FieldLocalDateTimeWrapper(date, ChronoField.DAY_OF_MONTH), - (d, z) -> ZonedDateTime.of(d, z).getDayOfMonth() - ); - } - - /** - * Creates a matcher that matches when the examined date is on the expected day of the month - *

- * For example: - * - *

-     * assertThat(myDate, isDayOfMonth(4))
-     * 
- * - * @param dayOfMonth the expected day of the month - */ - public static DateMatcher isDayOfMonth(final int dayOfMonth) { - return new IsDayOfMonth<>( - new FieldLocalDateTimeWrapper(dayOfMonth, ChronoField.DAY_OF_MONTH), - (d, z) -> ZonedDateTime.of(d, z).getDayOfMonth() - ); - } - - /** - * Creates a matcher that matches when the examined date is on the same year as the reference date - *

- * For example: - * - *

-     * assertThat(myDate, sameYear(new Date()))
-     * 
- * - * @param date the reference date against which the examined date is checked - */ - public static DateMatcher sameYear(final LocalDateTime date) { - return new IsYear<>( - new FieldLocalDateTimeWrapper(date, ChronoField.YEAR), - (d, z) -> ZonedDateTime.of(d ,z).getYear() - ); - } - - /** - * Creates a matcher that matches when the examined date is on the same year as the reference year - *

- * For example: - * - *

-     * assertThat(myDate, sameYear(2013))
-     * 
- * - * @param year the reference year against which the examined date is checked - */ - public static DateMatcher isYear(final int year) { - return new IsYear<>( - new FieldLocalDateTimeWrapper(year, ChronoField.YEAR), - (d, z) -> ZonedDateTime.of(d ,z).getYear() - ); - } - - /** - * Creates a matcher that matches when the examined date is within a defined period the reference date - *

- * For example: - * - *

-     * assertThat(myDate, within(10, TimeUnit.DAYS, Moments.today()))
-     * 
- * - * @param date the reference date against which the examined date is checked - */ - public static DateMatcher within(final long period, final ChronoUnit unit, final LocalDateTime date) { - return new IsWithin<>(period, unit, new LocalDateTimeWrapper(date), new LocalDateTimeFormatter()); - } - - /** - * Creates a matcher that matches when the examined date is within a given period of the reference date - *

- * For example: - * - *

-     * assertThat(myDate, within(5, TimeUnit.DAYS, 2012, Months.MAY, 12));
-     * 
- * - * @param period the timeunit interval the examined date should be with - * @param unit the timeunit to define the length of the period - * @param year the year against which the examined date is checked - * @param month the month against which the examined date is checked - * @param dayofMonth the day of the month against which the examined date is checked - * @param hour the hour of the day - * @param minute the minute of the hour - * @param second the second of the minute - * @param nanos the nanoseconds of the second - */ - public static DateMatcher within(final long period, - final ChronoUnit unit, - final int year, - final Month month, - final int dayofMonth, - final int hour, - final int minute, - final int second, - final int nanos) { - return new IsWithin<>(period, unit, - new LocalDateTimeWrapper(year, month, dayofMonth, hour, minute, second, nanos), - new LocalDateTimeFormatter()); - } - - /** - * Creates a matcher that matches when the examined date is yesterday - *

- * For example: - * - *

-     * assertThat(myDate, isToday());
-     * 
- */ - public static DateMatcher isYesterday() { - return sameDay(LocalDateTime.now().minusDays(1)); - } - - /** - * Creates a matcher that matches when the examined date is today - *

- * For example: - * - *

-     * assertThat(myDate, isToday());
-     * 
- */ - public static DateMatcher isToday() { - return sameDay(LocalDateTime.now()); - } - - /** - * Creates a matcher that matches when the examined date is tomorrow - *

- * For example: - * - *

-     * assertThat(myDate, isTomorrow());
-     * 
- */ - public static DateMatcher isTomorrow() { - return sameDay(LocalDateTime.now(ZoneId.systemDefault()).plusDays(1)); - } - - /** - * Creates a matcher that matches when the examined date is on the same day of the week as the reference date - *

- * For example: - * - *

-     * assertThat(myDate, sameDayOfWeek(LocalDateTime.now()))
+     * assertThat(myDate, sameDay(LocalDate.now()))
      * 
* * @param date the reference date against which the examined date is checked */ - public static DateMatcher sameDayOfWeek(final LocalDateTime date) { - return isDayOfWeek(DayOfWeek.from(date)); - } - - /** - * Creates a matcher that matches when the examined date is on a monday - *

- * For example: - * - *

-     * assertThat(myDate, isMonday());
-     * 
- */ - public static DateMatcher isDayOfWeek(final DayOfWeek dayOfWeek) { - return new IsDayOfWeek<>( - new FieldLocalDateTimeWrapper(dayOfWeek.getValue(), ChronoField.DAY_OF_WEEK), - (d, z) -> ZonedDateTime.of(d, z).getDayOfWeek().getValue() - ); - } - - /** - * Creates a matcher that matches when the examined date is on a monday - *

- * For example: - * - *

-     * assertThat(myDate, isMonday());
-     * 
- */ - public static DateMatcher isDayOfWeek(final DayOfWeek... daysOfWeek) { - return new AnyOf<>( - Stream.of(daysOfWeek).map(LocalDateTimeMatchers::isDayOfWeek), - (d, z) -> "the date is on a " + d.atZone(z).getDayOfWeek().name().toLowerCase() - ); - } - - /** - * Creates a matcher that matches when the examined date is on a monday - *

- * For example: - * - *

-     * assertThat(myDate, isMonday());
-     * 
- */ - public static DateMatcher isMonday() { - return isDayOfWeek(MONDAY); - } - - /** - * Creates a matcher that matches when the examined date is on a tuesday - *

- * For example: - * - *

-     * assertThat(myDate, isTuesday());
-     * 
- */ - public static DateMatcher isTuesday() { - return isDayOfWeek(TUESDAY); - } - - /** - * Creates a matcher that matches when the examined date is on a wednesday - *

- * For example: - * - *

-     * assertThat(myDate, isWednesday());
-     * 
- */ - public static DateMatcher isWednesday() { - return isDayOfWeek(WEDNESDAY); - } - - /** - * Creates a matcher that matches when the examined date is on a thursday - *

- * For example: - * - *

-     * assertThat(myDate, isThursday());
-     * 
- */ - public static DateMatcher isThursday() { - return isDayOfWeek(THURSDAY); - } - - /** - * Creates a matcher that matches when the examined date is on a friday - *

- * For example: - * - *

-     * assertThat(myDate, isFriday());
-     * 
- */ - public static DateMatcher isFriday() { - return isDayOfWeek(FRIDAY); - } - - /** - * Creates a matcher that matches when the examined date is on a saturday - *

- * For example: - * - *

-     * assertThat(myDate, isSaturday());
-     * 
- */ - public static DateMatcher isSaturday() { - return isDayOfWeek(SATURDAY); - } - - /** - * Creates a matcher that matches when the examined date is on a sunday - *

- * For example: - * - *

-     * assertThat(myDate, isSunday());
-     * 
- */ - public static DateMatcher isSunday() { - return isDayOfWeek(SUNDAY); - } - - /** - * Creates a matcher that matches when the examined date is on a weekday - *

- * For example: - * - *

-     * assertThat(myDate, isWeekday());
-     * 
- */ - public static DateMatcher isWeekday() { - return isDayOfWeek(MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY); - } - - /** - * Creates a matcher that matches when the examined date is on a weekend - *

- * For example: - * - *

-     * assertThat(myDate, isWeekend());
-     * 
- */ - public static DateMatcher isWeekend() { - return isDayOfWeek(SATURDAY, SUNDAY); - } - - /** - * Creates a matcher that matches when the examined date is on the first day of the month - *

- * For example: - * - *

-     * assertThat(myDate, isFirstDayOfMonth());
-     * 
- */ - public static DateMatcher isFirstDayOfMonth() { - return new IsMinimum<>( - ChronoField.DAY_OF_MONTH, - (d, z) -> d.atZone(z).get(ChronoField.DAY_OF_MONTH), - (d, z) -> ChronoField.DAY_OF_MONTH.rangeRefinedBy(d.atZone(z)), - new DatePartFormatter(), - () -> "the date is the first day of the month"); - } - - /** - * Creates a matcher that matches when the examined date is on the maximum value of the given date part in its - * period - *

- * For example: - * - *

-     * assertThat(myDate, isMaximumDayOfMonth(ChronoField.DAY_OF_MONTH));
-     * 
- * - * @param field the temporal field to check - */ - public static DateMatcher isMinimum(final ChronoField field) { - return new IsMinimum<>( - field, - (d, z) -> d.atZone(z).get(field), - (d, z) -> field.rangeRefinedBy(d.atZone(z)), - new DatePartFormatter() - ); - } - - /** - * Creates a matcher that matches when the examined date is on the first day of the month - *

- * For example: - * - *

-     * assertThat(myDate, isFirstDayOfMonth());
-     * 
- */ - public static DateMatcher isLastDayOfMonth() { - return new IsMaximum<>( - ChronoField.DAY_OF_MONTH, - (d, z) -> d.atZone(z).get(ChronoField.DAY_OF_MONTH), - (d, z) -> ChronoField.DAY_OF_MONTH.rangeRefinedBy(d.atZone(z)), - new DatePartFormatter(), - () -> "the date is the last day of the month"); - } - - /** - * Creates a matcher that matches when the examined date is on the maximum value of the given date part in its - * period - *

- * For example: - * - *

-     * assertThat(myDate, isMaximum(ChronoField.DAY_OF_MONTH));
-     * 
- * - * @param field the temporal field to check - */ - public static DateMatcher isMaximum(final ChronoField field) { - return new IsMaximum<>( - field, - (d, z) -> d.atZone(z).get(field), - (d, z) -> field.rangeRefinedBy(d.atZone(z)), - new DatePartFormatter() - ); - } - - /** - * Creates a matcher that matches when the examined date is in the expected month - *

- * For example: - * - *

-     * assertThat(myDate, isMonth(Month.AUGUST));
-     * 
- */ - public static DateMatcher isMonth(final Month month) { - return new IsMonth<>( - new FieldLocalDateTimeWrapper(month.getValue(), ChronoField.MONTH_OF_YEAR), - (d, z) -> ZonedDateTime.of(d, z).getMonthValue() - ); - } - - /** - * Creates a matcher that matches when the examined date is in January - *

- * For example: - * - *

-     * assertThat(myDate, isJanuary());
-     * 
- */ - public static DateMatcher isJanuary() { - return isMonth(JANUARY); - } - - /** - * Creates a matcher that matches when the examined date is in February - *

- * For example: - * - *

-     * assertThat(myDate, isFebruary());
-     * 
- */ - public static DateMatcher isFebruary() { - return isMonth(FEBRUARY); - } - - /** - * Creates a matcher that matches when the examined date is in March - *

- * For example: - * - *

-     * assertThat(myDate, isMarch());
-     * 
- */ - public static DateMatcher isMarch() { - return isMonth(MARCH); - } - - /** - * Creates a matcher that matches when the examined date is in April - *

- * For example: - * - *

-     * assertThat(myDate, isApril());
-     * 
- */ - public static DateMatcher isApril() { - return isMonth(APRIL); - } - - /** - * Creates a matcher that matches when the examined date is in May - *

- * For example: - * - *

-     * assertThat(myDate, isMay());
-     * 
- */ - public static DateMatcher isMay() { - return isMonth(MAY); - } - - /** - * Creates a matcher that matches when the examined date is in June - *

- * For example: - * - *

-     * assertThat(myDate, isJune());
-     * 
- */ - public static DateMatcher isJune() { - return isMonth(JUNE); - } - - /** - * Creates a matcher that matches when the examined date is in July - *

- * For example: - * - *

-     * assertThat(myDate, isJuly());
-     * 
- */ - public static DateMatcher isJuly() { - return isMonth(JULY); - } - - /** - * Creates a matcher that matches when the examined date is in August - *

- * For example: - * - *

-     * assertThat(myDate, isAugust());
-     * 
- */ - public static DateMatcher isAugust() { - return isMonth(AUGUST); - } - - /** - * Creates a matcher that matches when the examined date is in September - *

- * For example: - * - *

-     * assertThat(myDate, isSeptember());
-     * 
- */ - public static DateMatcher isSeptember() { - return isMonth(SEPTEMBER); - } - - /** - * Creates a matcher that matches when the examined date is in October - *

- * For example: - * - *

-     * assertThat(myDate, isOctober());
-     * 
- */ - public static DateMatcher isOctober() { - return isMonth(OCTOBER); - } - - /** - * Creates a matcher that matches when the examined date is in November - *

- * For example: - * - *

-     * assertThat(myDate, isNovember());
-     * 
- */ - public static DateMatcher isNovember() { - return isMonth(NOVEMBER); - } - - /** - * Creates a matcher that matches when the examined date is in December - *

- * For example: - * - *

-     * assertThat(myDate, isDecember());
-     * 
- */ - public static DateMatcher isDecember() { - return isMonth(DECEMBER); - } - - /** - * Creates a matcher that matches when the examined date is a leap year - *

- * For example: - * - *

-     * assertThat(myDate, isLeapYear());
-     * 
- */ - public static DateMatcher isLeapYear() { - return new IsLeapYear<>((d, z) -> d, new LocalDateTimeFormatter()); - } - - /** - * Creates a matcher that matches when the examined date is on the expected hour (0-23) - *

- * For example: - * - *

-     * assertThat(myDate, isHour(12));
-     * 
- * - * @param hour the hour of the day (0-23) - */ - public static DateMatcher isHour(final int hour) { - return new IsHour<>( - new FieldLocalDateTimeWrapper(hour, ChronoField.HOUR_OF_DAY), - (d, z) -> ZonedDateTime.of(d, z).getHour() - ); - } - - /** - * Creates a matcher that matches when the examined date is on the same hour as the reference date - *

- * For example: - * - *

-     * assertThat(myDate, sameHourOfDay(LocalDateTime.now()))
-     * 
- * - * @param date the reference date against which the examined date is checked - */ - public static DateMatcher sameHourOfDay(final LocalDateTime date) { - return new IsHour<>( - new FieldLocalDateTimeWrapper(date, ChronoField.HOUR_OF_DAY), - (d, z) -> ZonedDateTime.of(d, z).getHour() - ); - } - - /** - * Creates a matcher that matches when the examined date is on the expected minute (0-59) - *

- * For example: - * - *

-     * assertThat(myDate, isMinute(12));
-     * 
- * - * @param minute the minute of the day (0-59) - */ - public static DateMatcher isMinute(final int minute) { - return new IsMinute<>( - new FieldLocalDateTimeWrapper(minute, ChronoField.MINUTE_OF_HOUR), - (d, z) -> ZonedDateTime.of(d, z).getMinute() - ); - } - - /** - * Creates a matcher that matches when the examined date is on the same minute as the reference date - *

- * For example: - * - *

-     * assertThat(myDate, sameMinuteOfHour(LocalDateTime.now()))
-     * 
- * - * @param date the reference date against which the examined date is checked - */ - public static DateMatcher sameMinuteOfHour(final LocalDateTime date) { - return new IsMinute<>( - new FieldLocalDateTimeWrapper(date, ChronoField.MINUTE_OF_HOUR), - (d, z) -> ZonedDateTime.of(d, z).getMinute() - ); - } - - /** - * Creates a matcher that matches when the examined date is on the expected second (0-59) - *

- * For example: - * - *

-     * assertThat(myDate, isSecond(12));
-     * 
- * - * @param second the second of the day (0-59) - */ - public static DateMatcher isSecond(final int second) { - return new IsSecond<>( - new FieldLocalDateTimeWrapper(second, ChronoField.SECOND_OF_MINUTE), - (d, z) -> ZonedDateTime.of(d, z).getSecond() - ); - } - - /** - * Creates a matcher that matches when the examined date is on the same second as the reference date - *

- * For example: - * - *

-     * assertThat(myDate, sameSecondOfMinute(LocalDateTime.now()))
-     * 
- * - * @param date the reference date against which the examined date is checked - */ - public static DateMatcher sameSecondOfMinute(final LocalDateTime date) { - return new IsSecond<>( - new FieldLocalDateTimeWrapper(date, ChronoField.SECOND_OF_MINUTE), - (d, z) -> ZonedDateTime.of(d, z).getSecond() - ); - } + public static DateMatcher isDay(LocalDate date) { + return new IsSameDay<>(LOCALDATETIME_AS_LOCALDATE, localDate(date)); + } + + /** + * Creates a matcher that matches when the examined date is on the same day of the year as the reference date + *

+ * For example: + * + *

+	 * assertThat(myDate, sameDay(2012, Month.JAN, 1))
+	 * 
+ * + * @param dayOfMonth the reference day of the month against which the examined date is checked + * @param month the reference month against which the examined date is checked + * @param year the reference year against which the examined date is checked + */ + public static DateMatcher isDay(final int year, final Month month, final int dayOfMonth) { + return isDay(LocalDate.of(year, month, dayOfMonth)); + } + + /** + * Creates a matcher that matches when the examined date is at the same instant as the reference date + *

+ * For example: + * + *

+	 * assertThat(myDate, sameInstant(LocalDateTime.now()));
+	 * 
+ * + * @param date the reference date against which the examined date is checked + */ + public static DateMatcher sameInstant(final LocalDateTime date) { + return new IsSame<>(LOCALDATETIME_AS_LOCALDATETIME, localDateTime(date), LOCALDATETIME); + } + + /** + * Creates a matcher that matches when the examined date is at the same specified instance down to the second + *

+ * For example: + * + *

+	 * assertThat(myDate, sameInstant(2012, Month.JAN, 1, 3, 15, 0))
+	 * 
+ * + * @param dayOfMonth the reference day of the month against which the examined date is checked + * @param month the reference month against which the examined date is checked + * @param year the reference year against which the examined date is checked + * @param hour the hour of the day + * @param minute the minute of the hour + * @param second the second of the minute + * @param nanos the nanosecond of the second + */ + public static DateMatcher isInstant(final int year, + final Month month, + final int dayOfMonth, + final int hour, + final int minute, + final int second, + final int nanos) { + return sameInstant(LocalDateTime.of(year, month, dayOfMonth, hour, minute, second, nanos)); + } + + /** + * Creates a matcher that matches when the examined date is at the same instant or before the reference date + *

+ * For example: + * + *

+	 * assertThat(myDate, isSameOrBefore(new Date()))
+	 * 
+ * + * @param date the reference date against which the examined date is checked + */ + public static DateMatcher sameOrBefore(final LocalDateTime date) { + return new IsSameOrBefore<>(LOCALDATETIME_AS_LOCALDATETIME, localDateTime(date), LOCALDATETIME); + } + + /** + * Creates a matcher that matches when the examined date is on the same day or before the start of the reference + * date + *

+ * For example: + * + *

+	 * assertThat(myDate, isSameOrBefore(2012, Months.MAY, 12));
+	 * 
+ * + * @param year the year against which the examined date is checked + * @param month the month against which the examined date is checked + * @param day the day of the month against which the examined date is checked + * @param hour the hour of the day + * @param minute the minute of the hour + * @param second the second of the minute + */ + @Factory + public static DateMatcher sameOrBefore(final int year, + final Month month, + final int day, + final int hour, + final int minute, + final int second) { + return sameOrBefore(LocalDateTime.of(year, month, day, hour, minute, second)); + } + + /** + * Creates a matcher that matches when the examined date is at the same instant or after the reference date + *

+ * For example: + * + *

+	 * assertThat(myDate, isSameOrAfter(new Date()))
+	 * 
+ * + * @param date the reference date against which the examined date is checked + */ + public static DateMatcher sameOrAfter(final LocalDateTime date) { + return new IsSameOrAfter<>(LOCALDATETIME_AS_LOCALDATETIME, localDateTime(date), LOCALDATETIME); + } + + /** + * Creates a matcher that matches when the examined date is on the same day or after the start of the reference date + *

+ * For example: + * + *

+	 * assertThat(myDate, isSameOrAfter(2012, Months.MAY, 12));
+	 * 
+ * + * @param year the year against which the examined date is checked + * @param month the month against which the examined date is checked + * @param day the day of the month against which the examined date is checked + * @param hour the hour of the day + * @param minute the minute of the hour + * @param second the second of the minute + */ + public static DateMatcher sameOrAfter(final int year, + final Month month, + final int day, + final int hour, + final int minute, + final int second) { + return sameOrAfter(LocalDateTime.of(year, month, day, hour, minute, second)); + } + + /** + * Creates a matcher that matches when the examined date is on the same month as the reference date + *

+ * For example: + * + *

+	 * assertThat(myDate, sameMonth(new Date()))
+	 * 
+ * + * @param date the reference date against which the examined date is checked + */ + public static DateMatcher sameMonthOfYear(final LocalDateTime date) { + return new IsMonth<>(LOCALDATETIME_AS_MONTH, month(date)); + } + + /** + * Creates a matcher that matches when the examined date is on the same day of the month as the reference date + *

+ * For example: + * + *

+	 * assertThat(myDate, sameDayOfMonth(new Date()))
+	 * 
+ * + * @param date the reference date against which the examined date is checked + */ + public static DateMatcher sameDayOfMonth(final LocalDateTime date) { + return new IsDayOfMonth<>(LOCALDATETIME_AS_DAYOFMONTH, dayOfMonth(date)); + } + + /** + * Creates a matcher that matches when the examined date is on the expected day of the month + *

+ * For example: + * + *

+	 * assertThat(myDate, isDayOfMonth(4))
+	 * 
+ * + * @param dayOfMonth the expected day of the month + */ + public static DateMatcher isDayOfMonth(final int dayOfMonth) { + return new IsDayOfMonth<>(LOCALDATETIME_AS_DAYOFMONTH, dayOfMonth(dayOfMonth)); + } + + /** + * Creates a matcher that matches when the examined date is on the same year as the reference date + *

+ * For example: + * + *

+	 * assertThat(myDate, sameYear(new Date()))
+	 * 
+ * + * @param date the reference date against which the examined date is checked + */ + public static DateMatcher sameYear(final LocalDateTime date) { + return new IsYear<>(LOCALDATETIME_AS_YEAR, year(date)); + } + + /** + * Creates a matcher that matches when the examined date is on the same year as the reference year + *

+ * For example: + * + *

+	 * assertThat(myDate, sameYear(2013))
+	 * 
+ * + * @param year the reference year against which the examined date is checked + */ + public static DateMatcher isYear(final int year) { + return new IsYear<>(LOCALDATETIME_AS_YEAR, year(year)); + } + + /** + * Creates a matcher that matches when the examined date is within a defined period the reference date + *

+ * For example: + * + *

+	 * assertThat(myDate, within(10, TimeUnit.DAYS, Moments.today()))
+	 * 
+ * + * @param date the reference date against which the examined date is checked + */ + public static DateMatcher within(final long period, + final ChronoUnit unit, + final LocalDateTime date) { + return new IsWithin<>(Interval.of(period, unit), LOCALDATETIME_AS_LOCALDATETIME, localDateTime(date), LOCALDATETIME); + } + + /** + * Creates a matcher that matches when the examined date is within a given period of the reference date + *

+ * For example: + * + *

+	 * assertThat(myDate, within(5, TimeUnit.DAYS, 2012, Months.MAY, 12));
+	 * 
+ * + * @param period the timeunit interval the examined date should be with + * @param unit the timeunit to define the length of the period + * @param year the year against which the examined date is checked + * @param month the month against which the examined date is checked + * @param dayofMonth the day of the month against which the examined date is checked + * @param hour the hour of the day + * @param minute the minute of the hour + * @param second the second of the minute + * @param nanos the nanoseconds of the second + */ + public static DateMatcher within(final long period, + final ChronoUnit unit, + final int year, + final Month month, + final int dayofMonth, + final int hour, + final int minute, + final int second, + final int nanos) { + return within(period, unit, LocalDateTime.of(year, month, dayofMonth, hour, minute, second, nanos)); + } + + /** + * Creates a matcher that matches when the examined date is yesterday + *

+ * For example: + * + *

+	 * assertThat(myDate, isToday());
+	 * 
+ */ + public static DateMatcher isYesterday() { + return sameDay(LocalDateTime.now().minusDays(1)); + } + + /** + * Creates a matcher that matches when the examined date is today + *

+ * For example: + * + *

+	 * assertThat(myDate, isToday());
+	 * 
+ */ + public static DateMatcher isToday() { + return sameDay(LocalDateTime.now()); + } + + /** + * Creates a matcher that matches when the examined date is tomorrow + *

+ * For example: + * + *

+	 * assertThat(myDate, isTomorrow());
+	 * 
+ */ + public static DateMatcher isTomorrow() { + return sameDay(LocalDateTime.now(ZoneId.systemDefault()).plusDays(1)); + } + + /** + * Creates a matcher that matches when the examined date is on the same day of the week as the reference date + *

+ * For example: + * + *

+	 * assertThat(myDate, sameDayOfWeek(LocalDateTime.now()))
+	 * 
+ * + * @param date the reference date against which the examined date is checked + */ + public static DateMatcher sameDayOfWeek(final LocalDateTime date) { + return isDayOfWeek(DayOfWeek.from(date)); + } + + /** + * Creates a matcher that matches when the examined date is on a monday + *

+ * For example: + * + *

+	 * assertThat(myDate, isMonday());
+	 * 
+ */ + public static DateMatcher isDayOfWeek(final DayOfWeek dayOfWeek) { + return new IsDayOfWeek<>(LOCALDATETIME_AS_DAYOFWEEK, daysOfWeek(dayOfWeek)); + } + + /** + * Creates a matcher that matches when the examined date is on a monday + *

+ * For example: + * + *

+	 * assertThat(myDate, isMonday());
+	 * 
+ */ + public static DateMatcher isDayOfWeek(final DayOfWeek... daysOfWeek) { + return new IsDayOfWeek<>(LOCALDATETIME_AS_DAYOFWEEK, daysOfWeek(daysOfWeek)); + } + + /** + * Creates a matcher that matches when the examined date is on a monday + *

+ * For example: + * + *

+	 * assertThat(myDate, isMonday());
+	 * 
+ */ + public static DateMatcher isMonday() { + return isDayOfWeek(MONDAY); + } + + /** + * Creates a matcher that matches when the examined date is on a tuesday + *

+ * For example: + * + *

+	 * assertThat(myDate, isTuesday());
+	 * 
+ */ + public static DateMatcher isTuesday() { + return isDayOfWeek(TUESDAY); + } + + /** + * Creates a matcher that matches when the examined date is on a wednesday + *

+ * For example: + * + *

+	 * assertThat(myDate, isWednesday());
+	 * 
+ */ + public static DateMatcher isWednesday() { + return isDayOfWeek(WEDNESDAY); + } + + /** + * Creates a matcher that matches when the examined date is on a thursday + *

+ * For example: + * + *

+	 * assertThat(myDate, isThursday());
+	 * 
+ */ + public static DateMatcher isThursday() { + return isDayOfWeek(THURSDAY); + } + + /** + * Creates a matcher that matches when the examined date is on a friday + *

+ * For example: + * + *

+	 * assertThat(myDate, isFriday());
+	 * 
+ */ + public static DateMatcher isFriday() { + return isDayOfWeek(FRIDAY); + } + + /** + * Creates a matcher that matches when the examined date is on a saturday + *

+ * For example: + * + *

+	 * assertThat(myDate, isSaturday());
+	 * 
+ */ + public static DateMatcher isSaturday() { + return isDayOfWeek(SATURDAY); + } + + /** + * Creates a matcher that matches when the examined date is on a sunday + *

+ * For example: + * + *

+	 * assertThat(myDate, isSunday());
+	 * 
+ */ + public static DateMatcher isSunday() { + return isDayOfWeek(SUNDAY); + } + + /** + * Creates a matcher that matches when the examined date is on a weekday + *

+ * For example: + * + *

+	 * assertThat(myDate, isWeekday());
+	 * 
+ */ + public static DateMatcher isWeekday() { + return isDayOfWeek(MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY); + } + + /** + * Creates a matcher that matches when the examined date is on a weekend + *

+ * For example: + * + *

+	 * assertThat(myDate, isWeekend());
+	 * 
+ */ + public static DateMatcher isWeekend() { + return isDayOfWeek(SATURDAY, SUNDAY); + } + + /** + * Creates a matcher that matches when the examined date is on the first day of the month + *

+ * For example: + * + *

+	 * assertThat(myDate, isFirstDayOfMonth());
+	 * 
+ */ + public static DateMatcher isFirstDayOfMonth() { + return new IsFirstDayOfMonth<>(LOCALDATETIME_AS_LOCALDATETIME); + } + + /** + * Creates a matcher that matches when the examined date is on the maximum value of the given date part in its + * period + *

+ * For example: + * + *

+	 * assertThat(myDate, isMaximumDayOfMonth(ChronoField.DAY_OF_MONTH));
+	 * 
+ * + * @param field the temporal field to check + */ + public static DateMatcher isMinimum(final ChronoField field) { + return new IsMinimum<>(LOCALDATETIME_AS_LOCALDATETIME, field); + } + + /** + * Creates a matcher that matches when the examined date is on the first day of the month + *

+ * For example: + * + *

+	 * assertThat(myDate, isFirstDayOfMonth());
+	 * 
+ */ + public static DateMatcher isLastDayOfMonth() { + return new IsLastDayOfMonth<>(LOCALDATETIME_AS_LOCALDATETIME); + } + + /** + * Creates a matcher that matches when the examined date is on the maximum value of the given date part in its + * period + *

+ * For example: + * + *

+	 * assertThat(myDate, isMaximum(ChronoField.DAY_OF_MONTH));
+	 * 
+ * + * @param field the temporal field to check + */ + public static DateMatcher isMaximum(final ChronoField field) { + return new IsMaximum<>(LOCALDATETIME_AS_LOCALDATETIME, field); + } + + /** + * Creates a matcher that matches when the examined date is in the expected month + *

+ * For example: + * + *

+	 * assertThat(myDate, isMonth(Month.AUGUST));
+	 * 
+ */ + public static DateMatcher isMonth(final Month month) { + return new IsMonth<>(LOCALDATETIME_AS_MONTH, month(month)); + } + + /** + * Creates a matcher that matches when the examined date is in January + *

+ * For example: + * + *

+	 * assertThat(myDate, isJanuary());
+	 * 
+ */ + public static DateMatcher isJanuary() { + return isMonth(JANUARY); + } + + /** + * Creates a matcher that matches when the examined date is in February + *

+ * For example: + * + *

+	 * assertThat(myDate, isFebruary());
+	 * 
+ */ + public static DateMatcher isFebruary() { + return isMonth(FEBRUARY); + } + + /** + * Creates a matcher that matches when the examined date is in March + *

+ * For example: + * + *

+	 * assertThat(myDate, isMarch());
+	 * 
+ */ + public static DateMatcher isMarch() { + return isMonth(MARCH); + } + + /** + * Creates a matcher that matches when the examined date is in April + *

+ * For example: + * + *

+	 * assertThat(myDate, isApril());
+	 * 
+ */ + public static DateMatcher isApril() { + return isMonth(APRIL); + } + + /** + * Creates a matcher that matches when the examined date is in May + *

+ * For example: + * + *

+	 * assertThat(myDate, isMay());
+	 * 
+ */ + public static DateMatcher isMay() { + return isMonth(MAY); + } + + /** + * Creates a matcher that matches when the examined date is in June + *

+ * For example: + * + *

+	 * assertThat(myDate, isJune());
+	 * 
+ */ + public static DateMatcher isJune() { + return isMonth(JUNE); + } + + /** + * Creates a matcher that matches when the examined date is in July + *

+ * For example: + * + *

+	 * assertThat(myDate, isJuly());
+	 * 
+ */ + public static DateMatcher isJuly() { + return isMonth(JULY); + } + + /** + * Creates a matcher that matches when the examined date is in August + *

+ * For example: + * + *

+	 * assertThat(myDate, isAugust());
+	 * 
+ */ + public static DateMatcher isAugust() { + return isMonth(AUGUST); + } + + /** + * Creates a matcher that matches when the examined date is in September + *

+ * For example: + * + *

+	 * assertThat(myDate, isSeptember());
+	 * 
+ */ + public static DateMatcher isSeptember() { + return isMonth(SEPTEMBER); + } + + /** + * Creates a matcher that matches when the examined date is in October + *

+ * For example: + * + *

+	 * assertThat(myDate, isOctober());
+	 * 
+ */ + public static DateMatcher isOctober() { + return isMonth(OCTOBER); + } + + /** + * Creates a matcher that matches when the examined date is in November + *

+ * For example: + * + *

+	 * assertThat(myDate, isNovember());
+	 * 
+ */ + public static DateMatcher isNovember() { + return isMonth(NOVEMBER); + } + + /** + * Creates a matcher that matches when the examined date is in December + *

+ * For example: + * + *

+	 * assertThat(myDate, isDecember());
+	 * 
+ */ + public static DateMatcher isDecember() { + return isMonth(DECEMBER); + } + + /** + * Creates a matcher that matches when the examined date is a leap year + *

+ * For example: + * + *

+	 * assertThat(myDate, isLeapYear());
+	 * 
+ */ + public static DateMatcher isLeapYear() { + return new IsLeapYear<>(LOCALDATETIME_AS_YEAR); + } + + /** + * Creates a matcher that matches when the examined date is on the expected hour (0-23) + *

+ * For example: + * + *

+	 * assertThat(myDate, isHour(12));
+	 * 
+ * + * @param hour the hour of the day (0-23) + */ + public static DateMatcher isHour(final int hour) { + return new IsHour<>(LOCALDATETIME_AS_HOUR, hour(hour)); + } + + /** + * Creates a matcher that matches when the examined date is on the same hour as the reference date + *

+ * For example: + * + *

+	 * assertThat(myDate, sameHourOfDay(LocalDateTime.now()))
+	 * 
+ * + * @param date the reference date against which the examined date is checked + */ + public static DateMatcher sameHourOfDay(final LocalDateTime date) { + return new IsHour<>(LOCALDATETIME_AS_HOUR, hour(date)); + } + + /** + * Creates a matcher that matches when the examined date is on the expected minute (0-59) + *

+ * For example: + * + *

+	 * assertThat(myDate, isMinute(12));
+	 * 
+ * + * @param minute the minute of the day (0-59) + */ + public static DateMatcher isMinute(final int minute) { + return new IsMinute<>(LOCALDATETIME_AS_MINUTE, minute(minute)); + } + + /** + * Creates a matcher that matches when the examined date is on the same minute as the reference date + *

+ * For example: + * + *

+	 * assertThat(myDate, sameMinuteOfHour(LocalDateTime.now()))
+	 * 
+ * + * @param date the reference date against which the examined date is checked + */ + public static DateMatcher sameMinuteOfHour(final LocalDateTime date) { + return new IsMinute<>(LOCALDATETIME_AS_MINUTE, minute(date)); + } + + /** + * Creates a matcher that matches when the examined date is on the expected second (0-59) + *

+ * For example: + * + *

+	 * assertThat(myDate, isSecond(12));
+	 * 
+ * + * @param second the second of the day (0-59) + */ + public static DateMatcher isSecond(final int second) { + return new IsSecond<>(LOCALDATETIME_AS_SECOND, second(second)); + } + + /** + * Creates a matcher that matches when the examined date is on the same second as the reference date + *

+ * For example: + * + *

+	 * assertThat(myDate, sameSecondOfMinute(LocalDateTime.now()))
+	 * 
+ * + * @param date the reference date against which the examined date is checked + */ + public static DateMatcher sameSecondOfMinute(final LocalDateTime date) { + return new IsSecond<>(LOCALDATETIME_AS_SECOND, second(date)); + } } diff --git a/src/main/java/org/exparity/hamcrest/date/LocalTimeMatchers.java b/src/main/java/org/exparity/hamcrest/date/LocalTimeMatchers.java index 633fc17..037b8bb 100644 --- a/src/main/java/org/exparity/hamcrest/date/LocalTimeMatchers.java +++ b/src/main/java/org/exparity/hamcrest/date/LocalTimeMatchers.java @@ -1,15 +1,30 @@ package org.exparity.hamcrest.date; +import static org.exparity.hamcrest.date.core.TemporalConverters.LOCALTIME_AS_HOUR; +import static org.exparity.hamcrest.date.core.TemporalConverters.LOCALTIME_AS_LOCALTIME; +import static org.exparity.hamcrest.date.core.TemporalConverters.LOCALTIME_AS_MINUTE; +import static org.exparity.hamcrest.date.core.TemporalConverters.LOCALTIME_AS_SECOND; +import static org.exparity.hamcrest.date.core.TemporalFunctions.LOCALTIME; +import static org.exparity.hamcrest.date.core.TemporalProviders.hour; +import static org.exparity.hamcrest.date.core.TemporalProviders.localTime; +import static org.exparity.hamcrest.date.core.TemporalProviders.minute; +import static org.exparity.hamcrest.date.core.TemporalProviders.second; + import java.time.LocalTime; import java.time.temporal.ChronoField; import java.time.temporal.ChronoUnit; -import org.exparity.hamcrest.date.core.*; -import org.exparity.hamcrest.date.core.format.DatePartFormatter; -import org.exparity.hamcrest.date.core.format.LocalTimeFormatter; -import org.exparity.hamcrest.date.core.wrapper.FieldLocalTimeWrapper; -import org.exparity.hamcrest.date.core.wrapper.LocalTimeWrapper; -import org.hamcrest.Factory; +import org.exparity.hamcrest.date.core.IsAfter; +import org.exparity.hamcrest.date.core.IsBefore; +import org.exparity.hamcrest.date.core.IsHour; +import org.exparity.hamcrest.date.core.IsMaximum; +import org.exparity.hamcrest.date.core.IsMinimum; +import org.exparity.hamcrest.date.core.IsMinute; +import org.exparity.hamcrest.date.core.IsSameOrAfter; +import org.exparity.hamcrest.date.core.IsSameOrBefore; +import org.exparity.hamcrest.date.core.IsSecond; +import org.exparity.hamcrest.date.core.IsWithin; +import org.exparity.hamcrest.date.core.types.Interval; import org.hamcrest.Matcher; /** @@ -31,7 +46,7 @@ public abstract class LocalTimeMatchers { * @param time the reference time against which the examined time is checked */ public static Matcher after(final LocalTime time) { - return new IsAfter<>(new LocalTimeWrapper(time), new LocalTimeFormatter()); + return new IsAfter<>(LOCALTIME_AS_LOCALTIME, localTime(time), LOCALTIME); } /** @@ -48,7 +63,7 @@ public static Matcher after(final LocalTime time) { * @param second the second of the minute */ public static Matcher after(final int hour, final int minute, final int second) { - return new IsAfter<>(new LocalTimeWrapper(hour, minute, second), new LocalTimeFormatter()); + return after(LocalTime.of(hour, minute, second)); } /** @@ -63,7 +78,7 @@ public static Matcher after(final int hour, final int minute, final i * @param time the reference time against which the examined time is checked */ public static Matcher before(final LocalTime time) { - return new IsBefore<>(new LocalTimeWrapper(time), new LocalTimeFormatter()); + return new IsBefore<>(LOCALTIME_AS_LOCALTIME, localTime(time), LOCALTIME); } /** @@ -80,7 +95,7 @@ public static Matcher before(final LocalTime time) { * @param second the second of the minute */ public static Matcher before(final int hour, final int minute, final int second) { - return new IsBefore<>(new LocalTimeWrapper(hour, minute, second), new LocalTimeFormatter()); + return before(LocalTime.of(hour, minute, second)); } /** @@ -95,7 +110,7 @@ public static Matcher before(final int hour, final int minute, final * @param time the reference time against which the examined time is checked */ public static Matcher sameOrBefore(final LocalTime time) { - return new IsSameOrBefore<>(new LocalTimeWrapper(time), new LocalTimeFormatter()); + return new IsSameOrBefore<>(LOCALTIME_AS_LOCALTIME, localTime(time), LOCALTIME); } /** @@ -112,9 +127,8 @@ public static Matcher sameOrBefore(final LocalTime time) { * @param minute the minute of the hour * @param second the second of the minute */ - @Factory public static Matcher sameOrBefore(final int hour, final int minute, final int second) { - return new IsSameOrBefore<>(new LocalTimeWrapper(hour, minute, second), new LocalTimeFormatter()); + return sameOrBefore(LocalTime.of(hour, minute, second)); } /** @@ -129,7 +143,7 @@ public static Matcher sameOrBefore(final int hour, final int minute, * @param time the reference time against which the examined time is checked */ public static Matcher sameOrAfter(final LocalTime time) { - return new IsSameOrAfter<>(new LocalTimeWrapper(time), new LocalTimeFormatter()); + return new IsSameOrAfter<>(LOCALTIME_AS_LOCALTIME, localTime(time), LOCALTIME); } /** @@ -161,7 +175,7 @@ public static Matcher sameOrAfter(final int hour, final int minute, f * @param time the reference time against which the examined time is checked */ public static Matcher within(final long period, final ChronoUnit unit, final LocalTime time) { - return new IsWithin<>(period, unit, new LocalTimeWrapper(time), new LocalTimeFormatter()); + return new IsWithin<>(Interval.of(period, unit), LOCALTIME_AS_LOCALTIME, localTime(time), LOCALTIME); } /** @@ -184,10 +198,7 @@ public static Matcher within(final long period, final int hour, final int minute, final int second) { - return new IsWithin<>(period, - unit, - new LocalTimeWrapper(hour, minute, second), - new LocalTimeFormatter()); + return within(period, unit, LocalTime.of(hour, minute, second)); } /** @@ -203,12 +214,7 @@ public static Matcher within(final long period, * @param field the temporal field to check */ public static Matcher isMinimum(final ChronoField field) { - return new IsMinimum<>( - field, - (d, z) -> d.get(field), - (d, z) -> field.rangeRefinedBy(d), - new DatePartFormatter() - ); + return new IsMinimum<>(LOCALTIME_AS_LOCALTIME, field); } /** @@ -224,12 +230,7 @@ public static Matcher isMinimum(final ChronoField field) { * @param field the temporal field to check */ public static Matcher isMaximum(final ChronoField field) { - return new IsMaximum<>( - field, - (d, z) -> d.get(field), - (d, z) -> field.rangeRefinedBy(d), - new DatePartFormatter() - ); + return new IsMaximum<>(LOCALTIME_AS_LOCALTIME, field); } /** @@ -244,10 +245,7 @@ public static Matcher isMaximum(final ChronoField field) { * @param hour the hour of the day (0-23) */ public static Matcher isHour(final int hour) { - return new IsHour<>( - new FieldLocalTimeWrapper(hour, ChronoField.HOUR_OF_DAY), - (d, ignored) -> d.getHour() - ); + return new IsHour<>(LOCALTIME_AS_HOUR, hour(hour)); } /** @@ -262,10 +260,7 @@ public static Matcher isHour(final int hour) { * @param time the reference time against which the examined time is checked */ public static Matcher sameHourOfDay(final LocalTime time) { - return new IsHour<>( - new FieldLocalTimeWrapper(time, ChronoField.HOUR_OF_DAY), - (d, ignored) -> d.getHour() - ); + return new IsHour<>(LOCALTIME_AS_HOUR, hour(time)); } /** @@ -280,10 +275,7 @@ public static Matcher sameHourOfDay(final LocalTime time) { * @param minute the minute of the day (0-59) */ public static Matcher isMinute(final int minute) { - return new IsMinute<>( - new FieldLocalTimeWrapper(minute, ChronoField.MINUTE_OF_HOUR), - (d, ignored) -> d.getMinute() - ); + return new IsMinute<>(LOCALTIME_AS_MINUTE, minute(minute)); } /** @@ -298,10 +290,7 @@ public static Matcher isMinute(final int minute) { * @param time the reference time against which the examined time is checked */ public static Matcher sameMinuteOfHour(final LocalTime time) { - return new IsMinute<>( - new FieldLocalTimeWrapper(time, ChronoField.MINUTE_OF_HOUR), - (d, ignored) -> d.getMinute() - ); + return new IsMinute<>(LOCALTIME_AS_MINUTE, minute(time)); } /** @@ -316,10 +305,7 @@ public static Matcher sameMinuteOfHour(final LocalTime time) { * @param second the second of the day (0-59) */ public static Matcher isSecond(final int second) { - return new IsSecond<>( - new FieldLocalTimeWrapper(second, ChronoField.SECOND_OF_MINUTE), - (d, ignored) -> d.getSecond() - ); + return new IsSecond<>(LOCALTIME_AS_SECOND, second(second)); } /** @@ -334,9 +320,6 @@ public static Matcher isSecond(final int second) { * @param time the reference time against which the examined time is checked */ public static Matcher sameSecondOfMinute(final LocalTime time) { - return new IsSecond<>( - new FieldLocalTimeWrapper(time, ChronoField.SECOND_OF_MINUTE), - (d, ignored) -> d.getSecond() - ); + return new IsSecond<>(LOCALTIME_AS_SECOND, second(time)); } } diff --git a/src/main/java/org/exparity/hamcrest/date/SqlDateMatchers.java b/src/main/java/org/exparity/hamcrest/date/SqlDateMatchers.java index b7229dc..d0e5c60 100644 --- a/src/main/java/org/exparity/hamcrest/date/SqlDateMatchers.java +++ b/src/main/java/org/exparity/hamcrest/date/SqlDateMatchers.java @@ -2,6 +2,10 @@ import static java.time.DayOfWeek.*; import static java.time.Month.*; +import static org.exparity.hamcrest.date.core.TemporalConverters.*; +import static org.exparity.hamcrest.date.core.TemporalFunctions.LOCALDATE; +import static org.exparity.hamcrest.date.core.TemporalFunctions.SQLDATE; +import static org.exparity.hamcrest.date.core.TemporalProviders.*; import java.sql.Date; import java.time.DayOfWeek; @@ -11,14 +15,14 @@ import java.time.temporal.ChronoField; import java.time.temporal.ChronoUnit; import java.util.concurrent.TimeUnit; -import java.util.stream.Stream; -import org.exparity.hamcrest.date.core.AnyOf; import org.exparity.hamcrest.date.core.DateMatcher; import org.exparity.hamcrest.date.core.IsAfter; import org.exparity.hamcrest.date.core.IsBefore; import org.exparity.hamcrest.date.core.IsDayOfMonth; import org.exparity.hamcrest.date.core.IsDayOfWeek; +import org.exparity.hamcrest.date.core.IsFirstDayOfMonth; +import org.exparity.hamcrest.date.core.IsLastDayOfMonth; import org.exparity.hamcrest.date.core.IsLeapYear; import org.exparity.hamcrest.date.core.IsMaximum; import org.exparity.hamcrest.date.core.IsMinimum; @@ -28,12 +32,7 @@ import org.exparity.hamcrest.date.core.IsSameOrBefore; import org.exparity.hamcrest.date.core.IsWithin; import org.exparity.hamcrest.date.core.IsYear; -import org.exparity.hamcrest.date.core.format.DatePartFormatter; -import org.exparity.hamcrest.date.core.format.SqlDateFormatter; -import org.exparity.hamcrest.date.core.wrapper.FieldSqlDateAdaptor; -import org.exparity.hamcrest.date.core.wrapper.FieldSqlDateRangeAdaptor; -import org.exparity.hamcrest.date.core.wrapper.FieldSqlDateWrapper; -import org.exparity.hamcrest.date.core.wrapper.SqlDateWrapper; +import org.exparity.hamcrest.date.core.types.Interval; /** * Static factory for creating {@link org.hamcrest.Matcher} instances for comparing dates @@ -54,7 +53,7 @@ public abstract class SqlDateMatchers { * @param date the reference date against which the examined date is checked */ public static DateMatcher after(final Date date) { - return new IsAfter<>(new SqlDateWrapper(date), new SqlDateFormatter()); + return new IsAfter<>(SQLDATE_AS_SQLDATE, sqlDate(date), SQLDATE); } /** @@ -69,7 +68,7 @@ public static DateMatcher after(final Date date) { * @param date the reference date against which the examined date is checked */ public static DateMatcher after(final java.util.Date date) { - return new IsAfter<>(new SqlDateWrapper(date), new SqlDateFormatter()); + return new IsAfter<>(SQLDATE_AS_SQLDATE, sqlDate(date), SQLDATE); } /** @@ -101,7 +100,7 @@ public static DateMatcher after(final DayMonthYear date) { * @param date the reference date against which the examined date is checked */ public static DateMatcher after(final LocalDate date) { - return new IsAfter<>(new SqlDateWrapper(date), new SqlDateFormatter()); + return new IsAfter<>(SQLDATE_AS_LOCALDATE, localDate(date), LOCALDATE); } /** @@ -120,7 +119,7 @@ public static DateMatcher after(final LocalDate date) { */ @Deprecated public static DateMatcher after(final int year, final Months month, final int day) { - return new IsAfter<>(new SqlDateWrapper(year, month.month(), day), new SqlDateFormatter()); + return after(year, month.month(), day); } /** @@ -137,7 +136,7 @@ public static DateMatcher after(final int year, final Months mont * @param day the day of the month against which the examined date is checked */ public static DateMatcher after(final int year, final Month month, final int day) { - return new IsAfter<>(new SqlDateWrapper(year, month, day), new SqlDateFormatter()); + return after(LocalDate.of(year, month, day)); } /** @@ -152,7 +151,7 @@ public static DateMatcher after(final int year, final Month month * @param date the reference date against which the examined date is checked */ public static DateMatcher before(final Date date) { - return new IsBefore<>(new SqlDateWrapper(date), new SqlDateFormatter()); + return new IsBefore<>(SQLDATE_AS_SQLDATE, sqlDate(date), SQLDATE); } /** @@ -167,7 +166,7 @@ public static DateMatcher before(final Date date) { * @param date the reference date against which the examined date is checked */ public static DateMatcher before(final java.util.Date date) { - return new IsBefore<>(new SqlDateWrapper(date), new SqlDateFormatter()); + return new IsBefore<>(SQLDATE_AS_SQLDATE, sqlDate(date), SQLDATE); } /** @@ -176,13 +175,15 @@ public static DateMatcher before(final java.util.Date date) { * For example: * *
-	 * assertThat(myDate, before(LocalDate.now()))
+	 * assertThat(myDate, before(Moments.today()))
 	 * 
* * @param date the reference date against which the examined date is checked + * @deprecated Use {@link #before(LocalDate)} */ - public static DateMatcher before(final LocalDate date) { - return new IsBefore<>(new SqlDateWrapper(date), new SqlDateFormatter()); + @Deprecated + public static DateMatcher before(final DayMonthYear date) { + return before(date.toLocalDate()); } /** @@ -191,19 +192,17 @@ public static DateMatcher before(final LocalDate date) { * For example: * *
-	 * assertThat(myDate, before(Moments.today()));
+	 * assertThat(myDate, before(Moments.today()))
 	 * 
* * @param date the reference date against which the examined date is checked - * @deprecated Use {@link #before(LocalDate)} */ - @Deprecated - public static DateMatcher before(final DayMonthYear date) { - return before(date.toLocalDate()); + public static DateMatcher before(final LocalDate date) { + return new IsBefore<>(SQLDATE_AS_LOCALDATE, localDate(date), LOCALDATE); } /** - * Creates a matcher that matches when the examined date is before the start of reference day + * Creates a matcher that matches when the examined date is before the end of the reference year *

* For example: * @@ -213,16 +212,16 @@ public static DateMatcher before(final DayMonthYear date) { * * @param year the year against which the examined date is checked * @param month the month against which the examined date is checked - * @param dayOfMonth the day of the month against which the examined date is checked + * @param day the day of the month against which the examined date is checked * @deprecated Use {@link #before(int, Month, int)} */ @Deprecated - public static DateMatcher before(final int year, final Months month, final int dayOfMonth) { - return new IsBefore<>(new SqlDateWrapper(year, month.month(), dayOfMonth), new SqlDateFormatter()); + public static DateMatcher before(final int year, final Months month, final int day) { + return before(year, month.month(), day); } /** - * Creates a matcher that matches when the examined date is before the start of reference day + * Creates a matcher that matches when the examined date is before the end of the reference year *

* For example: * @@ -232,10 +231,10 @@ public static DateMatcher before(final int year, final Months mon * * @param year the year against which the examined date is checked * @param month the month against which the examined date is checked - * @param dayOfMonth the day of the month against which the examined date is checked + * @param day the day of the month against which the examined date is checked */ - public static DateMatcher before(final int year, final Month month, final int dayOfMonth) { - return new IsBefore<>(new SqlDateWrapper(year, month, dayOfMonth), new SqlDateFormatter()); + public static DateMatcher before(final int year, final Month month, final int day) { + return before(LocalDate.of(year, month, day)); } /** @@ -297,8 +296,7 @@ public static DateMatcher isDayOfWeek(final Weekdays dayOfWeek) { * @param dayOfWeek the reference weekday against which the examined date is checked */ public static DateMatcher isDayOfWeek(final DayOfWeek dayOfWeek) { - return new IsDayOfWeek<>(new FieldSqlDateWrapper(dayOfWeek.getValue(), ChronoField.DAY_OF_WEEK), - new FieldSqlDateAdaptor(ChronoField.DAY_OF_WEEK)); + return new IsDayOfWeek<>(SQLDATE_AS_DAYOFWEEK, daysOfWeek(dayOfWeek)); } /** @@ -313,8 +311,7 @@ public static DateMatcher isDayOfWeek(final DayOfWeek dayOfWeek) * @param daysOfWeek the reference weekdays against which the examined date is checked */ public static DateMatcher isDayOfWeek(final DayOfWeek... daysOfWeek) { - return new AnyOf<>(Stream.of(daysOfWeek).map(SqlDateMatchers::isDayOfWeek), - (d, z) -> "the date is on a " + toDayOfWeek(d).name().toLowerCase()); + return new IsDayOfWeek<>(SQLDATE_AS_DAYOFWEEK, daysOfWeek(daysOfWeek)); } /** @@ -329,8 +326,7 @@ public static DateMatcher isDayOfWeek(final DayOfWeek... daysOfWe * @param date the reference date against which the examined date is checked */ public static DateMatcher sameDayOfMonth(final Date date) { - return new IsDayOfMonth<>(new FieldSqlDateWrapper(date, ChronoField.DAY_OF_MONTH), - new FieldSqlDateAdaptor(ChronoField.DAY_OF_MONTH)); + return new IsDayOfMonth<>(SQLDATE_AS_DAYOFMONTH, dayOfMonth(date)); } /** @@ -345,8 +341,7 @@ public static DateMatcher sameDayOfMonth(final Date date) { * @param date the reference date against which the examined date is checked */ public static DateMatcher sameDayOfMonth(final java.util.Date date) { - return new IsDayOfMonth<>(new FieldSqlDateWrapper(date, ChronoField.DAY_OF_MONTH), - new FieldSqlDateAdaptor(ChronoField.DAY_OF_MONTH)); + return new IsDayOfMonth<>(SQLDATE_AS_DAYOFMONTH, dayOfMonth(date)); } /** @@ -361,8 +356,7 @@ public static DateMatcher sameDayOfMonth(final java.util.Date dat * @param dayOfMonth the expected day of the month */ public static DateMatcher isDayOfMonth(final int dayOfMonth) { - return new IsDayOfMonth<>(new FieldSqlDateWrapper(dayOfMonth, ChronoField.DAY_OF_MONTH), - new FieldSqlDateAdaptor(ChronoField.DAY_OF_MONTH)); + return new IsDayOfMonth<>(SQLDATE_AS_DAYOFMONTH, dayOfMonth(dayOfMonth)); } /** @@ -377,7 +371,7 @@ public static DateMatcher isDayOfMonth(final int dayOfMonth) { * @param date the reference date against which the examined date is checked */ public static DateMatcher sameDay(final java.sql.Date date) { - return new IsSameDay<>(new SqlDateWrapper(date), new SqlDateFormatter()); + return new IsSameDay<>(SQLDATE_AS_LOCALDATE, localDate(date)); } /** @@ -409,7 +403,7 @@ public static DateMatcher sameDay(final DayMonthYear date) { * @param date the reference date against which the examined date is checked */ public static DateMatcher sameDay(final LocalDate date) { - return new IsSameDay<>(new SqlDateWrapper(date), new SqlDateFormatter()); + return new IsSameDay<>(SQLDATE_AS_LOCALDATE, localDate(date)); } /** @@ -460,7 +454,7 @@ public static DateMatcher isDay(final int year, final Month month * @param date the reference date against which the examined date is checked */ public static DateMatcher sameOrBefore(final Date date) { - return new IsSameOrBefore<>(new SqlDateWrapper(date), new SqlDateFormatter()); + return new IsSameOrBefore<>(SQLDATE_AS_SQLDATE, sqlDate(date), SQLDATE); } /** @@ -475,7 +469,7 @@ public static DateMatcher sameOrBefore(final Date date) { * @param date the reference date against which the examined date is checked */ public static DateMatcher sameOrBefore(final java.util.Date date) { - return new IsSameOrBefore<>(new SqlDateWrapper(date), new SqlDateFormatter()); + return new IsSameOrBefore<>(SQLDATE_AS_SQLDATE, sqlDate(date), SQLDATE); } /** @@ -490,7 +484,7 @@ public static DateMatcher sameOrBefore(final java.util.Date date) * @param date the reference date against which the examined date is checked */ public static DateMatcher sameOrBefore(final LocalDate date) { - return new IsSameOrBefore<>(new SqlDateWrapper(date), new SqlDateFormatter()); + return new IsSameOrBefore<>(SQLDATE_AS_LOCALDATE, localDate(date), LOCALDATE); } /** @@ -545,7 +539,7 @@ public static DateMatcher sameOrBefore(final int year, final Mont * @param dayOfMonth the day of the month against which the examined date is checked */ public static DateMatcher sameOrBefore(final int year, final Month month, final int dayOfMonth) { - return new IsSameOrBefore<>(new SqlDateWrapper(LocalDate.of(year, month, dayOfMonth)), new SqlDateFormatter()); + return sameOrBefore(LocalDate.of(year, month, dayOfMonth)); } /** @@ -560,7 +554,7 @@ public static DateMatcher sameOrBefore(final int year, final Mont * @param date the reference date against which the examined date is checked */ public static DateMatcher sameOrAfter(final Date date) { - return new IsSameOrAfter<>(new SqlDateWrapper(date), new SqlDateFormatter()); + return new IsSameOrAfter<>(SQLDATE_AS_SQLDATE, sqlDate(date), SQLDATE); } /** @@ -575,7 +569,7 @@ public static DateMatcher sameOrAfter(final Date date) { * @param date the reference date against which the examined date is checked */ public static DateMatcher sameOrAfter(final java.util.Date date) { - return new IsSameOrAfter<>(new SqlDateWrapper(date), new SqlDateFormatter()); + return new IsSameOrAfter<>(SQLDATE_AS_SQLDATE, sqlDate(date), SQLDATE); } /** @@ -590,7 +584,7 @@ public static DateMatcher sameOrAfter(final java.util.Date date) * @param date the reference date against which the examined date is checked */ public static DateMatcher sameOrAfter(final LocalDate date) { - return new IsSameOrAfter<>(new SqlDateWrapper(date), new SqlDateFormatter()); + return new IsSameOrAfter<>(SQLDATE_AS_LOCALDATE, localDate(date), LOCALDATE); } /** @@ -643,7 +637,7 @@ public static DateMatcher sameOrAfter(final int year, final Month * @param dayOfMonth the day of the month against which the examined date is checked */ public static DateMatcher sameOrAfter(final int year, final Month month, final int dayOfMonth) { - return new IsSameOrAfter<>(new SqlDateWrapper(year, month, dayOfMonth), new SqlDateFormatter()); + return sameOrAfter(LocalDate.of(year, month, dayOfMonth)); } /** @@ -692,7 +686,7 @@ public static DateMatcher sameMonth(final java.util.Date date) { * @param date the reference date against which the examined date is checked */ public static DateMatcher sameMonthOfYear(final Date date) { - return isMonth(toMonth(date)); + return isMonth(SQLDATE_AS_MONTH.apply(date, ZoneId.systemDefault())); } /** @@ -756,7 +750,7 @@ public static DateMatcher sameMonthOfYear(final Months month) { * @param date the reference date against which the examined date is checked */ public static DateMatcher sameYear(final Date date) { - return new IsYear<>(new FieldSqlDateWrapper(date, ChronoField.YEAR), new FieldSqlDateAdaptor(ChronoField.YEAR)); + return new IsYear<>(SQLDATE_AS_YEAR, year(date)); } /** @@ -771,7 +765,7 @@ public static DateMatcher sameYear(final Date date) { * @param date the reference date against which the examined date is checked */ public static DateMatcher sameYear(final java.util.Date date) { - return new IsYear<>(new FieldSqlDateWrapper(date, ChronoField.YEAR), new FieldSqlDateAdaptor(ChronoField.YEAR)); + return new IsYear<>(SQLDATE_AS_YEAR, year(date)); } /** @@ -786,7 +780,7 @@ public static DateMatcher sameYear(final java.util.Date date) { * @param year the reference year against which the examined date is checked */ public static DateMatcher isYear(final int year) { - return new IsYear<>(new FieldSqlDateWrapper(year, ChronoField.YEAR), new FieldSqlDateAdaptor(ChronoField.YEAR)); + return new IsYear<>(SQLDATE_AS_YEAR, year(year)); } /** @@ -835,7 +829,7 @@ public static DateMatcher within(final long period, final TimeUni * @param date the reference date against which the examined date is checked */ public static DateMatcher within(final long period, final ChronoUnit unit, final Date date) { - return new IsWithin<>(period, unit, new SqlDateWrapper(date), new SqlDateFormatter()); + return new IsWithin<>(Interval.of(period, unit), SQLDATE_AS_SQLDATE, sqlDate(date), SQLDATE); } /** @@ -852,7 +846,7 @@ public static DateMatcher within(final long period, final ChronoU public static DateMatcher within(final long period, final ChronoUnit unit, final java.util.Date date) { - return new IsWithin<>(period, unit, new SqlDateWrapper(date), new SqlDateFormatter()); + return new IsWithin<>(Interval.of(period, unit), SQLDATE_AS_SQLDATE, sqlDate(date), SQLDATE); } /** @@ -867,7 +861,7 @@ public static DateMatcher within(final long period, * @param date the reference date against which the examined date is checked */ public static DateMatcher within(final long period, final ChronoUnit unit, final LocalDate date) { - return new IsWithin<>(period, unit, new SqlDateWrapper(date), new SqlDateFormatter()); + return new IsWithin<>(Interval.of(period, unit), SQLDATE_AS_LOCALDATE, localDate(date), LOCALDATE); } /** @@ -1101,11 +1095,7 @@ public static DateMatcher isWeekend() { * */ public static DateMatcher isFirstDayOfMonth() { - return new IsMinimum<>(ChronoField.DAY_OF_MONTH, - new FieldSqlDateAdaptor(ChronoField.DAY_OF_MONTH), - new FieldSqlDateRangeAdaptor(ChronoField.DAY_OF_MONTH), - new DatePartFormatter(), - () -> "the date is the first day of the month"); + return new IsFirstDayOfMonth<>(SQLDATE_AS_LOCALDATE); } /** @@ -1121,10 +1111,7 @@ public static DateMatcher isFirstDayOfMonth() { * @param field the temporal field to check */ public static DateMatcher isMinimum(final ChronoField field) { - return new IsMinimum<>(field, - new FieldSqlDateAdaptor(field), - new FieldSqlDateRangeAdaptor(field), - new DatePartFormatter()); + return new IsMinimum<>(SQLDATE_AS_LOCALDATE, field); } /** @@ -1137,11 +1124,7 @@ public static DateMatcher isMinimum(final ChronoField field) { * */ public static DateMatcher isLastDayOfMonth() { - return new IsMaximum<>(ChronoField.DAY_OF_MONTH, - new FieldSqlDateAdaptor(ChronoField.DAY_OF_MONTH), - new FieldSqlDateRangeAdaptor(ChronoField.DAY_OF_MONTH), - new DatePartFormatter(), - () -> "the date is the last day of the month"); + return new IsLastDayOfMonth<>(SQLDATE_AS_LOCALDATE); } /** @@ -1157,10 +1140,7 @@ public static DateMatcher isLastDayOfMonth() { * @param field the temporal field to check */ public static DateMatcher isMaximum(final ChronoField field) { - return new IsMaximum<>(field, - new FieldSqlDateAdaptor(field), - new FieldSqlDateRangeAdaptor(field), - new DatePartFormatter()); + return new IsMaximum<>(SQLDATE_AS_LOCALDATE, field); } /** @@ -1171,10 +1151,11 @@ public static DateMatcher isMaximum(final ChronoField field) { *

 	 * assertThat(myDate, isMonth(Month.AUGUST));
 	 * 
+ * + * O */ public static DateMatcher isMonth(final Month month) { - return new IsMonth<>(new FieldSqlDateWrapper(month.getValue(), ChronoField.MONTH_OF_YEAR), - new FieldSqlDateAdaptor(ChronoField.MONTH_OF_YEAR)); + return new IsMonth<>(SQLDATE_AS_MONTH, month(month)); } /** @@ -1343,7 +1324,7 @@ public static DateMatcher isDecember() { * */ public static DateMatcher isLeapYear() { - return new IsLeapYear<>((d, x) -> d.toLocalDate(), new SqlDateFormatter()); + return new IsLeapYear<>(SQLDATE_AS_YEAR); } private static ChronoUnit convertUnit(final TimeUnit unit) { @@ -1370,18 +1351,13 @@ private static ChronoUnit convertUnit(final TimeUnit unit) { private static DayOfWeek toDayOfWeek(Date date) { return date.toLocalDate().getDayOfWeek(); } - - private static Month toMonth(final Date date) { - return date.toLocalDate().getMonth(); - } - + private static DayOfWeek toDayOfWeek(java.util.Date date, ZoneId zone) { return date.toInstant().atZone(zone).getDayOfWeek(); } - + private static Month toMonth(final java.util.Date date, ZoneId zone) { return date.toInstant().atZone(zone).getMonth(); } - } diff --git a/src/main/java/org/exparity/hamcrest/date/ZonedDateTimeMatchers.java b/src/main/java/org/exparity/hamcrest/date/ZonedDateTimeMatchers.java index 4b35b28..8481d02 100644 --- a/src/main/java/org/exparity/hamcrest/date/ZonedDateTimeMatchers.java +++ b/src/main/java/org/exparity/hamcrest/date/ZonedDateTimeMatchers.java @@ -1,38 +1,40 @@ package org.exparity.hamcrest.date; -import static java.time.DayOfWeek.FRIDAY; -import static java.time.DayOfWeek.MONDAY; -import static java.time.DayOfWeek.SATURDAY; -import static java.time.DayOfWeek.SUNDAY; -import static java.time.DayOfWeek.THURSDAY; -import static java.time.DayOfWeek.TUESDAY; -import static java.time.DayOfWeek.WEDNESDAY; -import static java.time.Month.APRIL; -import static java.time.Month.AUGUST; -import static java.time.Month.DECEMBER; -import static java.time.Month.FEBRUARY; -import static java.time.Month.JANUARY; -import static java.time.Month.JULY; -import static java.time.Month.JUNE; -import static java.time.Month.MARCH; -import static java.time.Month.MAY; -import static java.time.Month.NOVEMBER; -import static java.time.Month.OCTOBER; -import static java.time.Month.SEPTEMBER; +import static java.time.DayOfWeek.*; +import static java.time.Month.*; +import static org.exparity.hamcrest.date.core.TemporalConverters.*; +import static org.exparity.hamcrest.date.core.TemporalFunctions.ZONEDDATETIME; +import static org.exparity.hamcrest.date.core.TemporalProviders.*; import java.time.DayOfWeek; +import java.time.LocalDate; import java.time.Month; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.temporal.ChronoField; import java.time.temporal.ChronoUnit; -import java.util.stream.Stream; -import org.exparity.hamcrest.date.core.*; -import org.exparity.hamcrest.date.core.format.DatePartFormatter; -import org.exparity.hamcrest.date.core.format.ZonedDateTimeFormatter; -import org.exparity.hamcrest.date.core.wrapper.FieldZonedDateTimeWrapper; -import org.exparity.hamcrest.date.core.wrapper.ZonedDateTimeWrapper; +import org.exparity.hamcrest.date.core.DateMatcher; +import org.exparity.hamcrest.date.core.IsAfter; +import org.exparity.hamcrest.date.core.IsBefore; +import org.exparity.hamcrest.date.core.IsDayOfMonth; +import org.exparity.hamcrest.date.core.IsDayOfWeek; +import org.exparity.hamcrest.date.core.IsFirstDayOfMonth; +import org.exparity.hamcrest.date.core.IsHour; +import org.exparity.hamcrest.date.core.IsLastDayOfMonth; +import org.exparity.hamcrest.date.core.IsLeapYear; +import org.exparity.hamcrest.date.core.IsMaximum; +import org.exparity.hamcrest.date.core.IsMinimum; +import org.exparity.hamcrest.date.core.IsMinute; +import org.exparity.hamcrest.date.core.IsMonth; +import org.exparity.hamcrest.date.core.IsSame; +import org.exparity.hamcrest.date.core.IsSameDay; +import org.exparity.hamcrest.date.core.IsSameOrAfter; +import org.exparity.hamcrest.date.core.IsSameOrBefore; +import org.exparity.hamcrest.date.core.IsSecond; +import org.exparity.hamcrest.date.core.IsWithin; +import org.exparity.hamcrest.date.core.IsYear; +import org.exparity.hamcrest.date.core.types.Interval; import org.hamcrest.Factory; /** @@ -54,7 +56,7 @@ public abstract class ZonedDateTimeMatchers { * @param date the reference date against which the examined date is checked */ public static DateMatcher after(final ZonedDateTime date) { - return new IsAfter<>(new ZonedDateTimeWrapper(date), new ZonedDateTimeFormatter()); + return new IsAfter<>(ZONEDDATETIME_AS_ZONEDDATETIME, zonedDateTime(date), ZONEDDATETIME); } /** @@ -82,9 +84,7 @@ public static DateMatcher after(final int year, final int second, final int nanos, final ZoneId tz) { - return new IsAfter<>( - new ZonedDateTimeWrapper(year, month, dayOfMonth, hour, minute, second, nanos, tz), - new ZonedDateTimeFormatter()); + return after(ZonedDateTime.of(year, month.getValue(), dayOfMonth, hour, minute, second, nanos, tz)); } /** @@ -93,13 +93,13 @@ public static DateMatcher after(final int year, * For example: * *
-     * MatcherAssert.assertThat(myDate, ZonedDateTimeMatchers.before(ZonedDateTime.now()));
+     * MatcherAssert.assertThat(myDate, before(ZonedDateTime.now()));
      * 
* * @param date the reference date against which the examined date is checked */ public static DateMatcher before(final ZonedDateTime date) { - return new IsBefore<>(new ZonedDateTimeWrapper(date), new ZonedDateTimeFormatter()); + return new IsBefore<>(ZONEDDATETIME_AS_ZONEDDATETIME, zonedDateTime(date), ZONEDDATETIME); } /** @@ -108,7 +108,7 @@ public static DateMatcher before(final ZonedDateTime date) { * For example: * *
-     * MatcherAssert.assertThat(myDate, ZonedDateTimeMatchers.before(2012, Month.MAY, 12));
+     * MatcherAssert.assertThat(myDate, before(2012, Month.MAY, 12));
      * 
* * @param year the year against which the examined date is checked @@ -117,7 +117,7 @@ public static DateMatcher before(final ZonedDateTime date) { * @param hour the hour of the day * @param minute the minute of the hour * @param second the second of the minute - * @param nanos the nanoseconds of the second + * @param nanos the nanos of the second */ public static DateMatcher before(final int year, final Month month, @@ -127,8 +127,7 @@ public static DateMatcher before(final int year, final int second, final int nanos, final ZoneId tz) { - return new IsBefore<>(new ZonedDateTimeWrapper(year, month, dayOfMonth, hour, minute, second, tz), - new ZonedDateTimeFormatter()); + return before(ZonedDateTime.of(year, month.getValue(), dayOfMonth, hour, minute, second, nanos, tz)); } /** @@ -143,7 +142,7 @@ public static DateMatcher before(final int year, * @param date the reference date against which the examined date is checked */ public static DateMatcher sameDay(final ZonedDateTime date) { - return new IsSameDay<>(new ZonedDateTimeWrapper(date, ChronoUnit.DAYS), new ZonedDateTimeFormatter()); + return new IsSameDay<>(ZONEDDATETIME_AS_LOCALDATE, localDate(date)); } /** @@ -152,22 +151,49 @@ public static DateMatcher sameDay(final ZonedDateTime date) { * For example: * *
-     * assertThat(myDate, sameDay(2012, Month.JAN, 1, ZoneId.systemDefault()))
+     * assertThat(myDate, sameDay(LocalDate.now()))
+     * 
+ * + * @param date the reference date against which the examined date is checked + */ + public static DateMatcher isDay(LocalDate date) { + return new IsSameDay<>(ZONEDDATETIME_AS_LOCALDATE, localDate(date)); + } + + /** + * Creates a matcher that matches when the examined date is on the same day of the year as the reference date + *

+ * For example: + * + *

+     * assertThat(myDate, sameDay(2012, Month.JAN, 1))
      * 
* * @param dayOfMonth the reference day of the month against which the examined date is checked * @param month the reference month against which the examined date is checked * @param year the reference year against which the examined date is checked - * @param tz the reference time zone */ - public static DateMatcher isDay(final int year, - final Month month, - final int dayOfMonth, - final ZoneId tz) { - return new IsSameDay<>(new ZonedDateTimeWrapper(year, month, dayOfMonth, tz), - new ZonedDateTimeFormatter()); + public static DateMatcher isDay(final int year, final Month month, final int dayOfMonth) { + return isDay(LocalDate.of(year, month, dayOfMonth)); } + /** + * Creates a matcher that matches when the examined date is on the same day of the year as the reference date + *

+ * For example: + * + *

+     * assertThat(myDate, sameDay(2012, Month.JAN, 1))
+     * 
+ * + * @param dayOfMonth the reference day of the month against which the examined date is checked + * @param month the reference month against which the examined date is checked + * @param year the reference year against which the examined date is checked + */ + public static DateMatcher isDay(final int year, final Month month, final int dayOfMonth, final ZoneId zone) { + return isDay(LocalDate.of(year, month, dayOfMonth)).atZone(zone); + } + /** * Creates a matcher that matches when the examined date is at the same instant as the reference date *

@@ -180,7 +206,7 @@ public static DateMatcher isDay(final int year, * @param date the reference date against which the examined date is checked */ public static DateMatcher sameInstant(final ZonedDateTime date) { - return new IsSame<>(new ZonedDateTimeWrapper(date), new ZonedDateTimeFormatter()); + return new IsSame<>(ZONEDDATETIME_AS_ZONEDDATETIME, zonedDateTime(date), ZONEDDATETIME); } /** @@ -209,9 +235,7 @@ public static DateMatcher isInstant(final int year, final int second, final int nanos, final ZoneId tz) { - return new IsSame<>( - new ZonedDateTimeWrapper(year, month, dayOfMonth, hour, minute, second, nanos, tz), - new ZonedDateTimeFormatter()); + return sameInstant(ZonedDateTime.of(year, month.getValue(), dayOfMonth, hour, minute, second, nanos, tz)); } /** @@ -226,7 +250,7 @@ public static DateMatcher isInstant(final int year, * @param date the reference date against which the examined date is checked */ public static DateMatcher sameOrBefore(final ZonedDateTime date) { - return new IsSameOrBefore<>(new ZonedDateTimeWrapper(date), new ZonedDateTimeFormatter()); + return new IsSameOrBefore<>(ZONEDDATETIME_AS_ZONEDDATETIME, zonedDateTime(date), ZONEDDATETIME); } /** @@ -257,9 +281,7 @@ public static DateMatcher sameOrBefore(final int year, final int second, final int nanos, final ZoneId tz) { - return new IsSameOrBefore<>( - new ZonedDateTimeWrapper(year, month, day, hour, minute, second, nanos, tz), - new ZonedDateTimeFormatter()); + return sameOrBefore(ZonedDateTime.of(year, month.getValue(), day, hour, minute, second, nanos, tz)); } /** @@ -274,7 +296,7 @@ public static DateMatcher sameOrBefore(final int year, * @param date the reference date against which the examined date is checked */ public static DateMatcher sameOrAfter(final ZonedDateTime date) { - return new IsSameOrAfter<>(new ZonedDateTimeWrapper(date), new ZonedDateTimeFormatter()); + return new IsSameOrAfter<>(ZONEDDATETIME_AS_ZONEDDATETIME, zonedDateTime(date), ZONEDDATETIME); } /** @@ -305,9 +327,7 @@ public static DateMatcher sameOrAfter(final int year, final int second, final int nanos, final ZoneId tz) { - return new IsSameOrAfter<>( - new ZonedDateTimeWrapper(year, month, day, hour, minute, second, nanos, tz), - new ZonedDateTimeFormatter()); + return sameOrAfter(ZonedDateTime.of(year, month.getValue(), day, hour, minute, second, nanos, tz)); } /** @@ -322,10 +342,7 @@ public static DateMatcher sameOrAfter(final int year, * @param date the reference date against which the examined date is checked */ public static DateMatcher sameMonthOfYear(final ZonedDateTime date) { - return new IsMonth<>( - new FieldZonedDateTimeWrapper(date, ChronoField.MONTH_OF_YEAR), - (d, z) -> ZonedDateTime.ofInstant(d.toInstant(), z).get(ChronoField.MONTH_OF_YEAR) - ); + return new IsMonth<>(ZONEDDATETIME_AS_MONTH, month(date)); } /** @@ -340,10 +357,7 @@ public static DateMatcher sameMonthOfYear(final ZonedDateTime dat * @param date the reference date against which the examined date is checked */ public static DateMatcher sameDayOfMonth(final ZonedDateTime date) { - return new IsDayOfMonth<>( - new FieldZonedDateTimeWrapper(date, ChronoField.DAY_OF_MONTH), - (d, z) -> ZonedDateTime.ofInstant(d.toInstant(), z).get(ChronoField.DAY_OF_MONTH) - ); + return new IsDayOfMonth<>(ZONEDDATETIME_AS_DAYOFMONTH, dayOfMonth(date)); } /** @@ -358,10 +372,7 @@ public static DateMatcher sameDayOfMonth(final ZonedDateTime date * @param dayOfMonth the expected day of the month */ public static DateMatcher isDayOfMonth(final int dayOfMonth) { - return new IsDayOfMonth<>( - new FieldZonedDateTimeWrapper(dayOfMonth, ChronoField.DAY_OF_MONTH), - (d, z) -> ZonedDateTime.ofInstant(d.toInstant(), z).get(ChronoField.DAY_OF_MONTH) - ); + return new IsDayOfMonth<>(ZONEDDATETIME_AS_DAYOFMONTH, dayOfMonth(dayOfMonth)); } /** @@ -391,10 +402,7 @@ public static DateMatcher sameYear(final ZonedDateTime date) { * @param year the reference year against which the examined date is checked */ public static DateMatcher isYear(final int year) { - return new IsYear<>( - new FieldZonedDateTimeWrapper(year, ChronoField.YEAR), - (d, z) -> ZonedDateTime.ofInstant(d.toInstant(), z).get(ChronoField.YEAR) - ); + return new IsYear<>(ZONEDDATETIME_AS_YEAR, year(year)); } /** @@ -408,8 +416,13 @@ public static DateMatcher isYear(final int year) { * * @param date the reference date against which the examined date is checked */ - public static DateMatcher within(final long period, final ChronoUnit unit, final ZonedDateTime date) { - return new IsWithin<>(period, unit, new ZonedDateTimeWrapper(date), new ZonedDateTimeFormatter()); + public static DateMatcher within(final long period, + final ChronoUnit unit, + final ZonedDateTime date) { + return new IsWithin<>(Interval.of(period, unit), + ZONEDDATETIME_AS_ZONEDDATETIME, + zonedDateTime(date), + ZONEDDATETIME); } /** @@ -442,10 +455,9 @@ public static DateMatcher within(final long period, final int second, final int nanos, final ZoneId tz) { - return new IsWithin<>(period, - unit, - new ZonedDateTimeWrapper(year, month, dayofMonth, hour, minute, second, nanos, tz), - new ZonedDateTimeFormatter()); + return within(period, + unit, + ZonedDateTime.of(year, month.getValue(), dayofMonth, hour, minute, second, nanos, tz)); } /** @@ -512,10 +524,7 @@ public static DateMatcher sameDayOfWeek(final ZonedDateTime date) * */ public static DateMatcher isDayOfWeek(final DayOfWeek dayOfWeek) { - return new IsDayOfWeek<>( - new FieldZonedDateTimeWrapper(dayOfWeek.getValue(), ChronoField.DAY_OF_WEEK), - (d, z) -> ZonedDateTime.ofInstant(d.toInstant(), z).get(ChronoField.DAY_OF_WEEK) - ); + return new IsDayOfWeek<>(ZONEDDATETIME_AS_DAYOFWEEK, daysOfWeek(dayOfWeek)); } /** @@ -528,10 +537,7 @@ public static DateMatcher isDayOfWeek(final DayOfWeek dayOfWeek) * */ public static DateMatcher isDayOfWeek(final DayOfWeek... daysOfWeek) { - return new AnyOf<>( - Stream.of(daysOfWeek).map(ZonedDateTimeMatchers::isDayOfWeek), - (d, z) -> "the date is on a " + d.withZoneSameInstant(z).getDayOfWeek().name().toLowerCase() - ); + return new IsDayOfWeek<>(ZONEDDATETIME_AS_DAYOFWEEK, daysOfWeek(daysOfWeek)); } /** @@ -661,11 +667,7 @@ public static DateMatcher isWeekend() { * */ public static DateMatcher isFirstDayOfMonth() { - return new IsMinimum<>(ChronoField.DAY_OF_MONTH, - (d, z) -> d.withZoneSameInstant(z).get(ChronoField.DAY_OF_MONTH), - (d, z) -> ChronoField.DAY_OF_MONTH.rangeRefinedBy(d.withZoneSameInstant(z)), - new DatePartFormatter(), - () -> "the date is the first day of the month"); + return new IsFirstDayOfMonth<>(ZONEDDATETIME_AS_ZONEDDATETIME); } /** @@ -681,12 +683,7 @@ public static DateMatcher isFirstDayOfMonth() { * @param field the temporal field to check */ public static DateMatcher isMinimum(final ChronoField field) { - return new IsMinimum<>( - field, - (d, z) -> d.withZoneSameInstant(z).get(field), - (d, z) -> field.rangeRefinedBy(d.withZoneSameInstant(z)), - new DatePartFormatter() - ); + return new IsMinimum<>(ZONEDDATETIME_AS_ZONEDDATETIME, field); } /** @@ -699,13 +696,7 @@ public static DateMatcher isMinimum(final ChronoField field) { * */ public static DateMatcher isLastDayOfMonth() { - return new IsMaximum<>( - ChronoField.DAY_OF_MONTH, - (d, z) -> d.withZoneSameInstant(z).get(ChronoField.DAY_OF_MONTH), - (d, z) -> ChronoField.DAY_OF_MONTH.rangeRefinedBy(d.withZoneSameInstant(z)), - new DatePartFormatter(), - () -> "the date is the last day of the month" - ); + return new IsLastDayOfMonth<>(ZONEDDATETIME_AS_ZONEDDATETIME); } /** @@ -721,12 +712,7 @@ public static DateMatcher isLastDayOfMonth() { * @param field the temporal field to check */ public static DateMatcher isMaximum(final ChronoField field) { - return new IsMaximum<>( - field, - (d, z) -> d.withZoneSameInstant(z).get(field), - (d, z) -> field.rangeRefinedBy(d.withZoneSameInstant(z)), - new DatePartFormatter() - ); + return new IsMaximum<>(ZONEDDATETIME_AS_ZONEDDATETIME, field); } /** @@ -739,10 +725,7 @@ public static DateMatcher isMaximum(final ChronoField field) { * */ public static DateMatcher isMonth(final Month month) { - return new IsMonth<>( - new FieldZonedDateTimeWrapper(month.getValue(), ChronoField.MONTH_OF_YEAR), - (d, z) -> ZonedDateTime.ofInstant(d.toInstant(), z).get(ChronoField.MONTH_OF_YEAR) - ); + return new IsMonth<>(ZONEDDATETIME_AS_MONTH, month(month)); } /** @@ -911,10 +894,7 @@ public static DateMatcher isDecember() { * */ public static DateMatcher isLeapYear() { - return new IsLeapYear<>( - (d, z) -> d.withZoneSameInstant(z), - new ZonedDateTimeFormatter() - ); + return new IsLeapYear<>(ZONEDDATETIME_AS_YEAR); } /** @@ -929,10 +909,7 @@ public static DateMatcher isLeapYear() { * @param hour the hour of the day (0-23) */ public static DateMatcher isHour(final int hour) { - return new IsHour<>( - new FieldZonedDateTimeWrapper(hour, ChronoField.HOUR_OF_DAY), - (d, z) -> ZonedDateTime.ofInstant(d.toInstant(), z).get(ChronoField.HOUR_OF_DAY) - ); + return new IsHour<>(ZONEDDATETIME_AS_HOUR, hour(hour)); } /** @@ -947,10 +924,7 @@ public static DateMatcher isHour(final int hour) { * @param date the reference date against which the examined date is checked */ public static DateMatcher sameHourOfDay(final ZonedDateTime date) { - return new IsHour<>( - new FieldZonedDateTimeWrapper(date, ChronoField.HOUR_OF_DAY), - (d, z) -> ZonedDateTime.ofInstant(d.toInstant(), z).get(ChronoField.HOUR_OF_DAY) - ); + return new IsHour<>(ZONEDDATETIME_AS_HOUR, hour(date)); } /** @@ -965,10 +939,7 @@ public static DateMatcher sameHourOfDay(final ZonedDateTime date) * @param minute the minute of the day (0-59) */ public static DateMatcher isMinute(final int minute) { - return new IsMinute<>( - new FieldZonedDateTimeWrapper(minute, ChronoField.MINUTE_OF_HOUR), - (d, z) -> ZonedDateTime.ofInstant(d.toInstant(), z).get(ChronoField.MINUTE_OF_HOUR) - ); + return new IsMinute<>(ZONEDDATETIME_AS_MINUTE, minute(minute)); } /** @@ -983,10 +954,7 @@ public static DateMatcher isMinute(final int minute) { * @param date the reference date against which the examined date is checked */ public static DateMatcher sameMinuteOfHour(final ZonedDateTime date) { - return new IsMinute<>( - new FieldZonedDateTimeWrapper(date, ChronoField.MINUTE_OF_HOUR), - (d, z) -> ZonedDateTime.ofInstant(d.toInstant(), z).get(ChronoField.MINUTE_OF_HOUR) - ); + return new IsMinute<>(ZONEDDATETIME_AS_MINUTE, minute(date)); } /** @@ -1001,10 +969,7 @@ public static DateMatcher sameMinuteOfHour(final ZonedDateTime da * @param second the second of the day (0-59) */ public static DateMatcher isSecond(final int second) { - return new IsSecond<>( - new FieldZonedDateTimeWrapper(second, ChronoField.SECOND_OF_MINUTE), - (d, z) -> ZonedDateTime.ofInstant(d.toInstant(), z).get(ChronoField.SECOND_OF_MINUTE) - ); + return new IsSecond<>(ZONEDDATETIME_AS_SECOND, second(second)); } /** @@ -1019,9 +984,6 @@ public static DateMatcher isSecond(final int second) { * @param date the reference date against which the examined date is checked */ public static DateMatcher sameSecondOfMinute(final ZonedDateTime date) { - return new IsSecond<>( - new FieldZonedDateTimeWrapper(date, ChronoField.SECOND_OF_MINUTE), - (d, z) -> ZonedDateTime.ofInstant(d.toInstant(), z).get(ChronoField.SECOND_OF_MINUTE) - ); + return new IsSecond<>(ZONEDDATETIME_AS_SECOND, second(date)); } } diff --git a/src/main/java/org/exparity/hamcrest/date/core/AnyOf.java b/src/main/java/org/exparity/hamcrest/date/core/AnyOf.java deleted file mode 100644 index 906d745..0000000 --- a/src/main/java/org/exparity/hamcrest/date/core/AnyOf.java +++ /dev/null @@ -1,57 +0,0 @@ -package org.exparity.hamcrest.date.core; - -import org.hamcrest.Description; -import org.hamcrest.Matcher; - -import java.time.ZoneId; -import java.util.function.BiFunction; -import java.util.stream.Stream; - -import static java.util.stream.Collectors.toList; - -/** - * A matcher that tests that the examined date matches at least one of the given matchers - * - * @author Thomas Naskali - */ -public class AnyOf extends DateMatcher { - - private final Stream> dateMatchers; - private final ZoneId zone; - private final BiFunction descriptionProvider; - private final org.hamcrest.core.AnyOf anyOf; - - private AnyOf(final Stream> dateMatchers, final ZoneId zone, final BiFunction descriptionProvider) { - this.dateMatchers = dateMatchers; - this.zone = zone; - this.descriptionProvider = descriptionProvider; - this.anyOf = new org.hamcrest.core.AnyOf<>( - this.dateMatchers.map(m -> (Matcher) m.atZone(zone)).collect(toList()) - ); - } - - public AnyOf(final Stream> dateMatchers, final BiFunction descriptionProvider) { - this(dateMatchers, ZoneId.systemDefault(), descriptionProvider); - } - - @Override - protected boolean matchesSafely(final T t, final Description mismatchDescription) { - if (!anyOf.matches(t)) { - mismatchDescription.appendText(descriptionProvider.apply(t, zone)); - return false; - } else { - return true; - } - } - - @Override - public void describeTo(final Description description) { - anyOf.describeTo(description); - } - - @Override - public DateMatcher atZone(final ZoneId zone) { - return new AnyOf<>(dateMatchers, zone, descriptionProvider); - } - -} diff --git a/src/main/java/org/exparity/hamcrest/date/core/IsAfter.java b/src/main/java/org/exparity/hamcrest/date/core/IsAfter.java index 4c255b7..55848a1 100644 --- a/src/main/java/org/exparity/hamcrest/date/core/IsAfter.java +++ b/src/main/java/org/exparity/hamcrest/date/core/IsAfter.java @@ -1,6 +1,7 @@ package org.exparity.hamcrest.date.core; import java.time.ZoneId; +import java.util.Locale; import org.hamcrest.Description; @@ -8,21 +9,39 @@ * A matcher that tests that the examined date is after the reference date * * @author Stewart Bissett + * + * @param the test type + * @param the expected type */ -public class IsAfter extends DateMatcher { - - private final TemporalWrapper expected; - private final TemporalFormatter describer; - - public IsAfter(final TemporalWrapper expected, final TemporalFormatter describer) { +public class IsAfter extends DateMatcher { + + private final TemporalProvider expected; + private final TemporalConverter converter; + private final TemporalFunction functions; + private final Locale locale; + private final ZoneId zone; + + public IsAfter(TemporalConverter converter, + TemporalProvider expected, + TemporalFunction functions, + ZoneId zone, + Locale locale) { this.expected = expected; - this.describer = describer; + this.converter = converter; + this.functions = functions; + this.locale = locale; + this.zone = zone; } + public IsAfter(TemporalConverter converter, TemporalProvider expected, TemporalFunction functions) { + this(converter, expected, functions, ZoneId.systemDefault(), Locale.getDefault(Locale.Category.FORMAT)); + } + @Override protected boolean matchesSafely(final T actual, final Description mismatchDescription) { - if (expected.isSame(actual) || expected.isAfter(actual)) { - mismatchDescription.appendText("date is " + describer.describe(actual)); + E expectedValue = expected.apply(zone), actualValue = converter.apply(actual, zone); + if (functions.isSame(expectedValue, actualValue) || functions.isAfter(expectedValue, actualValue)) { + mismatchDescription.appendText("date is " + functions.describe(actualValue, locale)); return false; } else { return true; @@ -31,12 +50,11 @@ protected boolean matchesSafely(final T actual, final Description mismatchDescri @Override public void describeTo(final Description description) { - description.appendText("the date is after " + describer.describe(expected.unwrap())); + description.appendText("the date is after " + functions.describe(expected.apply(zone), locale)); } @Override public DateMatcher atZone(ZoneId zone) { - return new IsAfter<>(expected.withZone(zone), describer); + return new IsAfter<>(converter, expected, functions, zone, locale); } - } \ No newline at end of file diff --git a/src/main/java/org/exparity/hamcrest/date/core/IsBefore.java b/src/main/java/org/exparity/hamcrest/date/core/IsBefore.java index ef82d30..2b3ea42 100644 --- a/src/main/java/org/exparity/hamcrest/date/core/IsBefore.java +++ b/src/main/java/org/exparity/hamcrest/date/core/IsBefore.java @@ -1,6 +1,7 @@ package org.exparity.hamcrest.date.core; import java.time.ZoneId; +import java.util.Locale; import org.hamcrest.Description; @@ -8,21 +9,39 @@ * A matcher that tests that the examined date is before the reference date * * @author Stewart Bissett + * + * @param the test type + * @param the expected type */ -public class IsBefore extends DateMatcher { - - private final TemporalWrapper expected; - private final TemporalFormatter describer; - - public IsBefore(final TemporalWrapper expected, final TemporalFormatter describer) { +public class IsBefore extends DateMatcher { + + private final TemporalProvider expected; + private final TemporalConverter converter; + private final TemporalFunction functions; + private final Locale locale; + private final ZoneId zone; + + public IsBefore(TemporalConverter converter, + TemporalProvider expected, + TemporalFunction functions, + ZoneId zone, + Locale locale) { this.expected = expected; - this.describer = describer; + this.converter = converter; + this.functions = functions; + this.locale = locale; + this.zone = zone; } + public IsBefore(TemporalConverter converter, TemporalProvider expected, TemporalFunction functions) { + this(converter, expected, functions, ZoneId.systemDefault(), Locale.getDefault(Locale.Category.FORMAT)); + } + @Override protected boolean matchesSafely(final T actual, final Description mismatchDescription) { - if (this.expected.isSame(actual) || this.expected.isBefore(actual)) { - mismatchDescription.appendText("date is " + this.describer.describe(actual)); + E expectedValue = expected.apply(zone), actualValue = converter.apply(actual, zone); + if (functions.isSame(expectedValue, actualValue) || functions.isBefore(expectedValue, actualValue)) { + mismatchDescription.appendText("date is " + functions.describe(actualValue, locale)); return false; } else { return true; @@ -31,12 +50,11 @@ protected boolean matchesSafely(final T actual, final Description mismatchDescri @Override public void describeTo(final Description description) { - description.appendText("the date is before " + this.describer.describe(this.expected.unwrap())); + description.appendText("the date is before " + functions.describe(expected.apply(zone), locale)); } @Override public DateMatcher atZone(ZoneId zone) { - return new IsBefore<>(expected.withZone(zone), describer); + return new IsBefore<>(converter, expected, functions, zone, locale); } - } \ No newline at end of file diff --git a/src/main/java/org/exparity/hamcrest/date/core/IsDayOfMonth.java b/src/main/java/org/exparity/hamcrest/date/core/IsDayOfMonth.java index 4a2ba4f..5e9d4ed 100644 --- a/src/main/java/org/exparity/hamcrest/date/core/IsDayOfMonth.java +++ b/src/main/java/org/exparity/hamcrest/date/core/IsDayOfMonth.java @@ -1,7 +1,9 @@ package org.exparity.hamcrest.date.core; import java.time.ZoneId; +import java.util.Locale; +import org.exparity.hamcrest.date.core.types.DayOfMonth; import org.hamcrest.Description; /** @@ -11,24 +13,30 @@ */ public class IsDayOfMonth extends DateMatcher { - private final TemporalFieldWrapper expected; - private final TemporalFieldAdapter accessor; + private final TemporalConverter converter; + private final TemporalProvider expected; + private final Locale locale; private final ZoneId zone; - private IsDayOfMonth(final TemporalFieldWrapper expected, final TemporalFieldAdapter accessor, final ZoneId zone) { + public IsDayOfMonth(TemporalConverter converter, + TemporalProvider expected, + ZoneId zone, + Locale locale) { this.expected = expected; - this.accessor = accessor; + this.converter = converter; + this.locale = locale; this.zone = zone; } - public IsDayOfMonth(final TemporalFieldWrapper expected, final TemporalFieldAdapter accessor) { - this(expected, accessor, ZoneId.systemDefault()); + public IsDayOfMonth(TemporalConverter converter, TemporalProvider expected) { + this(converter, expected, ZoneId.systemDefault(), Locale.getDefault(Locale.Category.FORMAT)); } @Override protected boolean matchesSafely(final T actual, final Description mismatchDescription) { - if (!this.expected.isSame(actual)) { - mismatchDescription.appendText("the date has the day of month " + accessor.asTemporalField(actual, zone)); + DayOfMonth expectedValue = expected.apply(zone), actualValue = converter.apply(actual, zone); + if (!expectedValue.equals(actualValue)) { + mismatchDescription.appendText("the date has the day of month " + actualValue); return false; } else { return true; @@ -37,12 +45,12 @@ protected boolean matchesSafely(final T actual, final Description mismatchDescri @Override public void describeTo(final Description description) { - description.appendText("the date has the day of month " + expected.unwrap()); + description.appendText("the date has the day of month " + expected.apply(zone)); } @Override public DateMatcher atZone(ZoneId zone) { - return new IsDayOfMonth<>(expected.withZone(zone), accessor); + return new IsDayOfMonth<>(converter, expected, zone, locale); } } \ No newline at end of file diff --git a/src/main/java/org/exparity/hamcrest/date/core/IsDayOfWeek.java b/src/main/java/org/exparity/hamcrest/date/core/IsDayOfWeek.java index 06cfd78..6148567 100644 --- a/src/main/java/org/exparity/hamcrest/date/core/IsDayOfWeek.java +++ b/src/main/java/org/exparity/hamcrest/date/core/IsDayOfWeek.java @@ -1,7 +1,12 @@ package org.exparity.hamcrest.date.core; +import static java.util.stream.Collectors.joining; + import java.time.DayOfWeek; import java.time.ZoneId; +import java.time.format.TextStyle; +import java.util.List; +import java.util.Locale; import org.hamcrest.Description; @@ -12,38 +17,58 @@ */ public class IsDayOfWeek extends DateMatcher { - private final TemporalFieldWrapper expected; - private final TemporalFieldAdapter accessor; - private final ZoneId zone; - - private IsDayOfWeek(final TemporalFieldWrapper expected, final TemporalFieldAdapter accessor, final ZoneId zone) { - this.expected = expected; - this.accessor = accessor; - this.zone = zone; - } - - public IsDayOfWeek(final TemporalFieldWrapper expected, final TemporalFieldAdapter accessor) { - this(expected, accessor, ZoneId.systemDefault()); - } - - @Override - protected boolean matchesSafely(final T actual, final Description mismatchDescription) { - if (!this.expected.isSame(actual)) { - mismatchDescription.appendText("the date is on a " + DayOfWeek.of(accessor.asTemporalField(actual, zone)).name().toLowerCase()); - return false; - } else { - return true; - } - } - - @Override - public void describeTo(final Description description) { - description.appendText("the date is on a " + DayOfWeek.of(expected.unwrap()).name().toLowerCase()); - } - - @Override - public DateMatcher atZone(ZoneId zone) { - return new IsDayOfWeek<>(expected.withZone(zone), accessor); - } + private final TemporalConverter converter; + private final TemporalProvider> expected; + private final Locale locale; + private final ZoneId zone; + + public IsDayOfWeek(TemporalConverter converter, + TemporalProvider> expected, + ZoneId zone, + Locale locale) { + this.expected = expected; + this.converter = converter; + this.locale = locale; + this.zone = zone; + } + + public IsDayOfWeek(TemporalConverter converter, TemporalProvider> expected) { + this(converter, expected, ZoneId.systemDefault(), Locale.getDefault(Locale.Category.FORMAT)); + } + + @Override + protected boolean matchesSafely(final T actual, final Description mismatchDescription) { + List expectedValues = expected.apply(zone); + DayOfWeek actualValue = converter.apply(actual, zone); + if (!expectedValues.contains(actualValue)) { + mismatchDescription.appendText("the date is on a " + describe(actualValue)); + return false; + } else { + return true; + } + } + + @Override + public void describeTo(final Description description) { + List expectedValues = expected.apply(zone); + if ( expectedValues.size() > 1 ) { + List headValues = expectedValues.subList(0, expectedValues.size() - 1); + DayOfWeek tailValue = expectedValues.get(expectedValues.size()-1); + description.appendText( + "the date is on a " + headValues.stream().map(this::describe).collect(joining(", ")) + " or " + describe(tailValue)); + } else { + description.appendText( + "the date is on a " + describe(expectedValues.get(0))); + } + } + + @Override + public DateMatcher atZone(ZoneId zone) { + return new IsDayOfWeek<>(converter, expected, zone, locale); + } + + private String describe(DayOfWeek actualValue) { + return actualValue.getDisplayName(TextStyle.FULL, locale); + } } \ No newline at end of file diff --git a/src/main/java/org/exparity/hamcrest/date/core/IsFirstDayOfMonth.java b/src/main/java/org/exparity/hamcrest/date/core/IsFirstDayOfMonth.java new file mode 100644 index 0000000..8492a80 --- /dev/null +++ b/src/main/java/org/exparity/hamcrest/date/core/IsFirstDayOfMonth.java @@ -0,0 +1,54 @@ +package org.exparity.hamcrest.date.core; + +import java.time.ZoneId; +import java.time.temporal.ChronoField; +import java.time.temporal.TemporalAccessor; +import java.time.temporal.ValueRange; +import java.util.Locale; + +import org.hamcrest.Description; + +/** + * A matcher that tests that the examined date is on the first day of the month + * + * @author Stewart Bissett + */ +public class IsFirstDayOfMonth extends DateMatcher { + + private final TemporalConverter converter; + private final Locale locale; + private final ZoneId zone; + + public IsFirstDayOfMonth(TemporalConverter converter, ZoneId zone, Locale locale) { + this.converter = converter; + this.locale = locale; + this.zone = zone; + } + + public IsFirstDayOfMonth(TemporalConverter converter) { + this(converter, ZoneId.systemDefault(), Locale.getDefault(Locale.Category.FORMAT)); + } + + @Override + protected boolean matchesSafely(final T actual, final Description mismatchDesc) { + TemporalAccessor actualTemporal = converter.apply(actual, zone); + ValueRange actualRange = ChronoField.DAY_OF_MONTH.rangeRefinedBy(actualTemporal); + long actualValue = ChronoField.DAY_OF_MONTH.getFrom(actualTemporal), expectedValue = actualRange.getMinimum(); + if (expectedValue != actualValue) { + mismatchDesc.appendText("date is the " + actualValue + " day of the month"); + return false; + } else { + return true; + } + } + + @Override + public void describeTo(final Description description) { + description.appendText("the date is the first day of the month"); + } + + @Override + public DateMatcher atZone(ZoneId zone) { + return new IsFirstDayOfMonth<>(converter, zone, locale); + } +} \ No newline at end of file diff --git a/src/main/java/org/exparity/hamcrest/date/core/IsHour.java b/src/main/java/org/exparity/hamcrest/date/core/IsHour.java index 55812e2..5681bd1 100644 --- a/src/main/java/org/exparity/hamcrest/date/core/IsHour.java +++ b/src/main/java/org/exparity/hamcrest/date/core/IsHour.java @@ -1,7 +1,9 @@ package org.exparity.hamcrest.date.core; import java.time.ZoneId; +import java.util.Locale; +import org.exparity.hamcrest.date.core.types.Hour; import org.hamcrest.Description; /** @@ -11,24 +13,30 @@ */ public class IsHour extends DateMatcher { - private final TemporalFieldWrapper expected; - private final TemporalFieldAdapter accessor; + private final TemporalConverter converter; + private final TemporalProvider expected; + private final Locale locale; private final ZoneId zone; - private IsHour(final TemporalFieldWrapper expected, final TemporalFieldAdapter accessor, final ZoneId zone) { + public IsHour(TemporalConverter converter, + TemporalProvider expected, + ZoneId zone, + Locale locale) { this.expected = expected; - this.accessor = accessor; + this.converter = converter; + this.locale = locale; this.zone = zone; } - public IsHour(final TemporalFieldWrapper expected, final TemporalFieldAdapter accessor) { - this(expected, accessor, ZoneId.systemDefault()); + public IsHour(TemporalConverter converter, TemporalProvider expected) { + this(converter, expected, ZoneId.systemDefault(), Locale.getDefault(Locale.Category.FORMAT)); } @Override protected boolean matchesSafely(final T actual, final Description mismatchDescription) { - if (!this.expected.isSame(actual)) { - mismatchDescription.appendText("the date has the hour " + accessor.asTemporalField(actual, zone)); + Hour expectedValue = expected.apply(zone), actualValue = converter.apply(actual, zone); + if (!expectedValue.equals(actualValue)) { + mismatchDescription.appendText("the date has the hour " + actualValue); return false; } else { return true; @@ -37,12 +45,11 @@ protected boolean matchesSafely(final T actual, final Description mismatchDescri @Override public void describeTo(final Description description) { - description.appendText("the date has the hour " + expected.unwrap()); + description.appendText("the date has the hour " + expected.apply(zone)); } @Override public DateMatcher atZone(ZoneId zone) { - return new IsHour<>(expected.withZone(zone), accessor); + return new IsHour<>(converter, expected, zone, locale); } - } \ No newline at end of file diff --git a/src/main/java/org/exparity/hamcrest/date/core/IsLastDayOfMonth.java b/src/main/java/org/exparity/hamcrest/date/core/IsLastDayOfMonth.java new file mode 100644 index 0000000..a5ca2d7 --- /dev/null +++ b/src/main/java/org/exparity/hamcrest/date/core/IsLastDayOfMonth.java @@ -0,0 +1,54 @@ +package org.exparity.hamcrest.date.core; + +import java.time.ZoneId; +import java.time.temporal.ChronoField; +import java.time.temporal.TemporalAccessor; +import java.time.temporal.ValueRange; +import java.util.Locale; + +import org.hamcrest.Description; + +/** + * A matcher that tests that the examined date is on the first day of the month + * + * @author Stewart Bissett + */ +public class IsLastDayOfMonth extends DateMatcher { + + private final TemporalConverter converter; + private final Locale locale; + private final ZoneId zone; + + public IsLastDayOfMonth(TemporalConverter converter, ZoneId zone, Locale locale) { + this.converter = converter; + this.locale = locale; + this.zone = zone; + } + + public IsLastDayOfMonth(TemporalConverter converter) { + this(converter, ZoneId.systemDefault(), Locale.getDefault(Locale.Category.FORMAT)); + } + + @Override + protected boolean matchesSafely(final T actual, final Description mismatchDesc) { + TemporalAccessor actualTemporal = converter.apply(actual, zone); + ValueRange actualRange = ChronoField.DAY_OF_MONTH.rangeRefinedBy(actualTemporal); + long actualValue = ChronoField.DAY_OF_MONTH.getFrom(actualTemporal), expectedValue = actualRange.getMaximum(); + if (expectedValue != actualValue) { + mismatchDesc.appendText("date is the " + actualValue + " day of the month"); + return false; + } else { + return true; + } + } + + @Override + public void describeTo(final Description description) { + description.appendText("the date is the last day of the month"); + } + + @Override + public DateMatcher atZone(ZoneId zone) { + return new IsLastDayOfMonth<>(converter, zone, locale); + } +} \ No newline at end of file diff --git a/src/main/java/org/exparity/hamcrest/date/core/IsLeapYear.java b/src/main/java/org/exparity/hamcrest/date/core/IsLeapYear.java index 896d455..f39190e 100644 --- a/src/main/java/org/exparity/hamcrest/date/core/IsLeapYear.java +++ b/src/main/java/org/exparity/hamcrest/date/core/IsLeapYear.java @@ -1,8 +1,8 @@ package org.exparity.hamcrest.date.core; -import static java.time.temporal.TemporalQueries.localDate; - +import java.time.Year; import java.time.ZoneId; +import java.util.Locale; import org.hamcrest.Description; @@ -13,26 +13,25 @@ */ public class IsLeapYear extends DateMatcher { - private final TemporalAdapter adapter; - private final TemporalFormatter formatter; + private final TemporalConverter converter; + private final Locale locale; private final ZoneId zone; - private IsLeapYear(final TemporalAdapter adapter, final TemporalFormatter formatter, final ZoneId zone) { - this.adapter = adapter; - this.formatter = formatter; + public IsLeapYear(TemporalConverter converter, ZoneId zone, Locale locale) { + this.converter = converter; + this.locale = locale; this.zone = zone; } - public IsLeapYear(final TemporalAdapter adapter, final TemporalFormatter formatter) { - this.adapter = adapter; - this.formatter = formatter; - this.zone = ZoneId.systemDefault(); + public IsLeapYear(TemporalConverter converter) { + this(converter, ZoneId.systemDefault(), Locale.getDefault(Locale.Category.FORMAT)); } @Override protected boolean matchesSafely(final T actual, final Description mismatchDesc) { - if (!this.adapter.asTemporal(actual, zone).query(localDate()).isLeapYear()) { - mismatchDesc.appendText("the date " + this.formatter.describe(actual) + " is not a leap year"); + Year actualValue = converter.apply(actual, zone); + if (!actualValue.isLeap()) { + mismatchDesc.appendText("the year " + actualValue + " is not a leap year"); return false; } else { return true; @@ -46,7 +45,7 @@ public void describeTo(final Description description) { @Override public DateMatcher atZone(ZoneId zone) { - return new IsLeapYear<>(adapter, formatter, zone); + return new IsLeapYear<>(converter, zone, locale); } } \ No newline at end of file diff --git a/src/main/java/org/exparity/hamcrest/date/core/IsMaximum.java b/src/main/java/org/exparity/hamcrest/date/core/IsMaximum.java index ba8f289..769e3ba 100644 --- a/src/main/java/org/exparity/hamcrest/date/core/IsMaximum.java +++ b/src/main/java/org/exparity/hamcrest/date/core/IsMaximum.java @@ -1,11 +1,14 @@ package org.exparity.hamcrest.date.core; +import static java.util.stream.Collectors.joining; + import java.time.ZoneId; import java.time.temporal.ChronoField; +import java.time.temporal.TemporalAccessor; import java.time.temporal.ValueRange; -import java.util.function.Supplier; +import java.util.Locale; +import java.util.stream.Stream; -import org.exparity.hamcrest.date.core.format.DatePartFormatter; import org.hamcrest.Description; /** @@ -15,80 +18,50 @@ */ public class IsMaximum extends DateMatcher { - private final ChronoField datePart; - private final TemporalFieldAdapter adapter; - private final TemporalFieldRangeAdapter rangeAdapter; - private final DatePartFormatter formatter; - private final Supplier descriptionSupplier; - private final ZoneId zone; - - private IsMaximum(final ChronoField datePart, - final TemporalFieldAdapter adapter, - final TemporalFieldRangeAdapter rangeAdapter, - final DatePartFormatter formatter, - final Supplier descriptionSupplier, - final ZoneId zone) { - this.datePart = datePart; - this.adapter = adapter; - this.rangeAdapter = rangeAdapter; - this.formatter = formatter; - this.descriptionSupplier = descriptionSupplier; - this.zone = zone; - } + private static final String SPLIT_ON_UPPERCASE_REGEX = "(?=[A-Z])"; - public IsMaximum(final ChronoField datePart, - final TemporalFieldAdapter adapter, - final TemporalFieldRangeAdapter rangeAdapter, - final DatePartFormatter formatter, - final Supplier descriptionSupplier) { - this.datePart = datePart; - this.adapter = adapter; - this.rangeAdapter = rangeAdapter; - this.formatter = formatter; - this.descriptionSupplier = descriptionSupplier; - this.zone = ZoneId.systemDefault(); - } + private final TemporalConverter converter; + private final ChronoField field; + private final Locale locale; + private final ZoneId zone; - public IsMaximum(final ChronoField datePart, - final TemporalFieldAdapter adapter, - final TemporalFieldRangeAdapter rangeAdapter, - final DatePartFormatter formatter) { - this(datePart, adapter, rangeAdapter, formatter, () -> "the date is the maximum value for " + formatter.describe(datePart)); - } + public IsMaximum(TemporalConverter converter, + ChronoField field, + ZoneId zone, + Locale locale) { + this.converter = converter; + this.field = field; + this.locale = locale; + this.zone = zone; + } - @Override - protected boolean matchesSafely(final T actual, final Description mismatchDesc) { - long actualValue = this.adapter.asTemporalField(actual, zone); - ValueRange range = this.rangeAdapter.asTemporalFieldRange(actual, zone); - if (range.getMaximum() != actualValue) { - mismatchDesc.appendText("date is the " + actualValue - + " " - + this.formatter.describe(this.datePart) - + " instead of " - + range.getMinimum() - + " " - + this.formatter.describe(this.datePart)); - return false; - } else { - return true; - } - } + public IsMaximum(TemporalConverter converter, ChronoField field) { + this(converter, field, ZoneId.systemDefault(), Locale.getDefault(Locale.Category.FORMAT)); + } - @Override - public void describeTo(final Description description) { - description.appendText(this.descriptionSupplier.get()); - } + @Override + protected boolean matchesSafely(final T actual, final Description mismatchDesc) { + TemporalAccessor actualTemporal = converter.apply(actual, zone); + ValueRange actualRange = field.rangeRefinedBy(actualTemporal); + long actualValue = field.getFrom(actualTemporal), expectedValue = actualRange.getMaximum(); + if (expectedValue != actualValue) { + mismatchDesc.appendText("date has the value " + actualValue + " instead of " + expectedValue); + return false; + } else { + return true; + } + } - @Override - public DateMatcher atZone(ZoneId zone) { - return new IsMaximum<>( - datePart, - adapter, - rangeAdapter, - formatter, - descriptionSupplier, - zone - ); - } + @Override + public void describeTo(final Description description) { + description.appendText("the date has the maximum value for " + + Stream.of(field.getDisplayName(locale).split(SPLIT_ON_UPPERCASE_REGEX)) + .map(String::toLowerCase) + .collect(joining(" "))); + } + @Override + public DateMatcher atZone(ZoneId zone) { + return new IsMaximum<>(converter, field, zone, locale); + } } \ No newline at end of file diff --git a/src/main/java/org/exparity/hamcrest/date/core/IsMillisecond.java b/src/main/java/org/exparity/hamcrest/date/core/IsMillisecond.java index fbedeb2..bed061b 100644 --- a/src/main/java/org/exparity/hamcrest/date/core/IsMillisecond.java +++ b/src/main/java/org/exparity/hamcrest/date/core/IsMillisecond.java @@ -1,7 +1,9 @@ package org.exparity.hamcrest.date.core; import java.time.ZoneId; +import java.util.Locale; +import org.exparity.hamcrest.date.core.types.Millisecond; import org.hamcrest.Description; /** @@ -11,24 +13,30 @@ */ public class IsMillisecond extends DateMatcher { - private final TemporalFieldWrapper expected; - private final TemporalFieldAdapter accessor; + private final TemporalConverter converter; + private final TemporalProvider expected; + private final Locale locale; private final ZoneId zone; - private IsMillisecond(final TemporalFieldWrapper expected, final TemporalFieldAdapter accessor, final ZoneId zone) { + public IsMillisecond(TemporalConverter converter, + TemporalProvider expected, + ZoneId zone, + Locale locale) { this.expected = expected; - this.accessor = accessor; + this.converter = converter; + this.locale = locale; this.zone = zone; } - public IsMillisecond(final TemporalFieldWrapper expected, final TemporalFieldAdapter accessor) { - this(expected, accessor, ZoneId.systemDefault()); + public IsMillisecond(TemporalConverter converter, TemporalProvider expected) { + this(converter, expected, ZoneId.systemDefault(), Locale.getDefault(Locale.Category.FORMAT)); } @Override protected boolean matchesSafely(final T actual, final Description mismatchDescription) { - if (!this.expected.isSame(actual)) { - mismatchDescription.appendText("the date has the millisecond " + accessor.asTemporalField(actual, zone)); + Millisecond expectedValue = expected.apply(zone), actualValue = converter.apply(actual, zone); + if (!expectedValue.equals(actualValue)) { + mismatchDescription.appendText("the date has the millisecond " + actualValue); return false; } else { return true; @@ -37,12 +45,12 @@ protected boolean matchesSafely(final T actual, final Description mismatchDescri @Override public void describeTo(final Description description) { - description.appendText("the date has the millisecond " + expected.unwrap()); + description.appendText("the date has the millisecond " + expected.apply(zone)); } @Override public DateMatcher atZone(ZoneId zone) { - return new IsMillisecond<>(expected.withZone(zone), accessor); + return new IsMillisecond<>(converter, expected, zone, locale); } } \ No newline at end of file diff --git a/src/main/java/org/exparity/hamcrest/date/core/IsMinimum.java b/src/main/java/org/exparity/hamcrest/date/core/IsMinimum.java index 88e23b0..1234ca9 100644 --- a/src/main/java/org/exparity/hamcrest/date/core/IsMinimum.java +++ b/src/main/java/org/exparity/hamcrest/date/core/IsMinimum.java @@ -1,11 +1,14 @@ package org.exparity.hamcrest.date.core; +import static java.util.stream.Collectors.joining; + import java.time.ZoneId; import java.time.temporal.ChronoField; +import java.time.temporal.TemporalAccessor; import java.time.temporal.ValueRange; -import java.util.function.Supplier; +import java.util.Locale; +import java.util.stream.Stream; -import org.exparity.hamcrest.date.core.format.DatePartFormatter; import org.hamcrest.Description; /** @@ -15,79 +18,47 @@ */ public class IsMinimum extends DateMatcher { - private final ChronoField datePart; - private final TemporalFieldAdapter adapter; - private final TemporalFieldRangeAdapter rangeAdapter; - private final DatePartFormatter formatter; - private final Supplier descriptionSupplier; - private final ZoneId zone; - - private IsMinimum(final ChronoField datePart, - final TemporalFieldAdapter adapter, - final TemporalFieldRangeAdapter rangeAdapter, - final DatePartFormatter formatter, - final Supplier descriptionSupplier, - final ZoneId zone) { - this.datePart = datePart; - this.adapter = adapter; - this.rangeAdapter = rangeAdapter; - this.formatter = formatter; - this.descriptionSupplier = descriptionSupplier; - this.zone = zone; - } - - public IsMinimum(final ChronoField datePart, - final TemporalFieldAdapter adapter, - final TemporalFieldRangeAdapter rangeAdapter, - final DatePartFormatter formatter, - final Supplier descriptionSupplier) { - this.datePart = datePart; - this.adapter = adapter; - this.rangeAdapter = rangeAdapter; - this.formatter = formatter; - this.descriptionSupplier = descriptionSupplier; - this.zone = ZoneId.systemDefault(); - } + private static final String SPLIT_ON_UPPERCASE_REGEX = "(?=[A-Z])"; + + private final TemporalConverter converter; + private final ChronoField field; + private final Locale locale; + private final ZoneId zone; - public IsMinimum(final ChronoField datePart, - final TemporalFieldAdapter adapter, - final TemporalFieldRangeAdapter minimumAdapter, - final DatePartFormatter formatter) { - this(datePart, adapter, minimumAdapter, formatter, () -> "the date is the minimum value for " + formatter.describe(datePart)); - } + public IsMinimum(TemporalConverter converter, ChronoField field, ZoneId zone, Locale locale) { + this.converter = converter; + this.field = field; + this.locale = locale; + this.zone = zone; + } - @Override - protected boolean matchesSafely(final T actual, final Description mismatchDesc) { - int actualValue = this.adapter.asTemporalField(actual, zone); - ValueRange range = this.rangeAdapter.asTemporalFieldRange(actual, zone); - if (range.getMinimum() != actualValue) { - mismatchDesc.appendText("date is the " + actualValue - + " " - + this.formatter.describe(this.datePart) - + " instead of " - + range.getMinimum() - + " " - + this.formatter.describe(this.datePart)); - return false; - } else { - return true; - } - } + public IsMinimum(TemporalConverter converter, ChronoField field) { + this(converter, field, ZoneId.systemDefault(), Locale.getDefault(Locale.Category.FORMAT)); + } - @Override - public void describeTo(final Description description) { - description.appendText(this.descriptionSupplier.get()); - } + @Override + protected boolean matchesSafely(final T actual, final Description mismatchDesc) { + TemporalAccessor actualTemporal = converter.apply(actual, zone); + ValueRange actualRange = field.rangeRefinedBy(actualTemporal); + long actualValue = field.getFrom(actualTemporal), expectedValue = actualRange.getMinimum(); + if (expectedValue != actualValue) { + mismatchDesc.appendText("date has the value " + actualValue + " instead of " + expectedValue); + return false; + } else { + return true; + } + } - @Override - public DateMatcher atZone(ZoneId zone) { - return new IsMinimum<>( - datePart, - adapter, - rangeAdapter, - formatter, - descriptionSupplier, - zone); - } + @Override + public void describeTo(final Description description) { + description.appendText("the date has the minimum value for " + + Stream.of(field.getDisplayName(locale).split(SPLIT_ON_UPPERCASE_REGEX)) + .map(String::toLowerCase) + .collect(joining(" "))); + } + @Override + public DateMatcher atZone(ZoneId zone) { + return new IsMinimum<>(converter, field, zone, locale); + } } \ No newline at end of file diff --git a/src/main/java/org/exparity/hamcrest/date/core/IsMinute.java b/src/main/java/org/exparity/hamcrest/date/core/IsMinute.java index 1ea46a6..192e6c6 100644 --- a/src/main/java/org/exparity/hamcrest/date/core/IsMinute.java +++ b/src/main/java/org/exparity/hamcrest/date/core/IsMinute.java @@ -1,7 +1,9 @@ package org.exparity.hamcrest.date.core; import java.time.ZoneId; +import java.util.Locale; +import org.exparity.hamcrest.date.core.types.Minute; import org.hamcrest.Description; /** @@ -11,24 +13,30 @@ */ public class IsMinute extends DateMatcher { - private final TemporalFieldWrapper expected; - private final TemporalFieldAdapter accessor; + private final TemporalConverter converter; + private final TemporalProvider expected; + private final Locale locale; private final ZoneId zone; - private IsMinute(final TemporalFieldWrapper expected, final TemporalFieldAdapter accessor, final ZoneId zone) { + public IsMinute(TemporalConverter converter, + TemporalProvider expected, + ZoneId zone, + Locale locale) { this.expected = expected; - this.accessor = accessor; + this.converter = converter; + this.locale = locale; this.zone = zone; } - public IsMinute(final TemporalFieldWrapper expected, final TemporalFieldAdapter accessor) { - this(expected, accessor, ZoneId.systemDefault()); + public IsMinute(TemporalConverter converter, TemporalProvider expected) { + this(converter, expected, ZoneId.systemDefault(), Locale.getDefault(Locale.Category.FORMAT)); } @Override protected boolean matchesSafely(final T actual, final Description mismatchDescription) { - if (!this.expected.isSame(actual)) { - mismatchDescription.appendText("the date has the minute " + accessor.asTemporalField(actual, zone)); + Minute expectedValue = expected.apply(zone), actualValue = converter.apply(actual, zone); + if (!expectedValue.equals(actualValue)) { + mismatchDescription.appendText("the date has the minute " + actualValue); return false; } else { return true; @@ -37,12 +45,11 @@ protected boolean matchesSafely(final T actual, final Description mismatchDescri @Override public void describeTo(final Description description) { - description.appendText("the date has the minute " + expected.unwrap()); + description.appendText("the date has the minute " + expected.apply(zone)); } @Override public DateMatcher atZone(ZoneId zone) { - return new IsMinute<>(expected.withZone(zone), accessor); + return new IsMinute<>(converter, expected, zone, locale); } - } \ No newline at end of file diff --git a/src/main/java/org/exparity/hamcrest/date/core/IsMonth.java b/src/main/java/org/exparity/hamcrest/date/core/IsMonth.java index 7a4fec5..703b7ed 100644 --- a/src/main/java/org/exparity/hamcrest/date/core/IsMonth.java +++ b/src/main/java/org/exparity/hamcrest/date/core/IsMonth.java @@ -1,35 +1,43 @@ package org.exparity.hamcrest.date.core; +import java.time.Month; import java.time.ZoneId; +import java.time.format.TextStyle; +import java.util.Locale; import org.hamcrest.Description; /** - * A matcher that tests that the examined date is on the same month of the year - * as the reference date + * A matcher that tests that the examined date is on the same month of the year as the reference date * * @author Stewart Bissett */ public class IsMonth extends DateMatcher { - private final TemporalFieldWrapper expected; - private final TemporalFieldAdapter accessor; + private final TemporalConverter converter; + private final TemporalProvider expected; + private final Locale locale; private final ZoneId zone; - private IsMonth(final TemporalFieldWrapper expected, final TemporalFieldAdapter accessor, final ZoneId zone) { + public IsMonth(TemporalConverter converter, + TemporalProvider expected, + ZoneId zone, + Locale locale) { this.expected = expected; - this.accessor = accessor; + this.converter = converter; + this.locale = locale; this.zone = zone; } - public IsMonth(final TemporalFieldWrapper expected, final TemporalFieldAdapter accessor) { - this(expected, accessor, ZoneId.systemDefault()); + public IsMonth(TemporalConverter converter, TemporalProvider expected) { + this(converter, expected, ZoneId.systemDefault(), Locale.getDefault(Locale.Category.FORMAT)); } @Override protected boolean matchesSafely(final T actual, final Description mismatchDescription) { - if (!this.expected.isSame(actual)) { - mismatchDescription.appendText("the date has the month " + accessor.asTemporalField(actual, zone)); + Month expectedValue = expected.apply(zone), actualValue = converter.apply(actual, zone); + if (!expectedValue.equals(actualValue)) { + mismatchDescription.appendText("the date has the month " + describe(actualValue)); return false; } else { return true; @@ -38,12 +46,16 @@ protected boolean matchesSafely(final T actual, final Description mismatchDescri @Override public void describeTo(final Description description) { - description.appendText("the date has the month " + expected.unwrap()); + description.appendText("the date has the month " + describe(expected.apply(zone))); } @Override public DateMatcher atZone(ZoneId zone) { - return new IsMonth<>(expected.withZone(zone), accessor); + return new IsMonth<>(converter, expected, zone, locale); + } + + private String describe(Month actualValue) { + return actualValue.getDisplayName(TextStyle.FULL, locale); } } \ No newline at end of file diff --git a/src/main/java/org/exparity/hamcrest/date/core/IsSame.java b/src/main/java/org/exparity/hamcrest/date/core/IsSame.java index dd56aa2..da1df7e 100644 --- a/src/main/java/org/exparity/hamcrest/date/core/IsSame.java +++ b/src/main/java/org/exparity/hamcrest/date/core/IsSame.java @@ -1,6 +1,7 @@ package org.exparity.hamcrest.date.core; import java.time.ZoneId; +import java.util.Locale; import org.hamcrest.Description; @@ -9,20 +10,35 @@ * * @author Stewart Bissett */ -public class IsSame extends DateMatcher { - - private final TemporalWrapper expected; - private final TemporalFormatter describer; - - public IsSame(final TemporalWrapper expected, final TemporalFormatter describer) { +public class IsSame extends DateMatcher { + + private final TemporalProvider expected; + private final TemporalConverter converter; + private final TemporalFunction functions; + private final Locale locale; + private final ZoneId zone; + + public IsSame(TemporalConverter converter, + TemporalProvider expected, + TemporalFunction functions, + ZoneId zone, + Locale locale) { this.expected = expected; - this.describer = describer; + this.converter = converter; + this.functions = functions; + this.locale = locale; + this.zone = zone; } + public IsSame(TemporalConverter converter, TemporalProvider expected, TemporalFunction functions) { + this(converter, expected, functions, ZoneId.systemDefault(), Locale.getDefault(Locale.Category.FORMAT)); + } + @Override protected boolean matchesSafely(final T actual, final Description mismatchDescription) { - if (!this.expected.isSame(actual)) { - mismatchDescription.appendText("the date is " + this.describer.describe(actual)); + E expectedValue = expected.apply(zone), actualValue = converter.apply(actual, zone); + if (!functions.isSame(expectedValue, actualValue)) { + mismatchDescription.appendText("the date is " + functions.describe(actualValue, locale)); return false; } else { return true; @@ -31,12 +47,12 @@ protected boolean matchesSafely(final T actual, final Description mismatchDescri @Override public void describeTo(final Description description) { - description.appendText("the same date as " + this.describer.describe(this.expected.unwrap())); + description.appendText("the same date as " + functions.describe(expected.apply(zone), locale)); } @Override public DateMatcher atZone(ZoneId zone) { - return new IsSame<>(expected.withZone(zone), describer); + return new IsSame<>(converter, expected, functions, zone, locale); } } \ No newline at end of file diff --git a/src/main/java/org/exparity/hamcrest/date/core/IsSameDay.java b/src/main/java/org/exparity/hamcrest/date/core/IsSameDay.java index fa05001..f726b51 100644 --- a/src/main/java/org/exparity/hamcrest/date/core/IsSameDay.java +++ b/src/main/java/org/exparity/hamcrest/date/core/IsSameDay.java @@ -1,6 +1,8 @@ package org.exparity.hamcrest.date.core; +import java.time.LocalDate; import java.time.ZoneId; +import java.util.Locale; import org.hamcrest.Description; @@ -12,18 +14,30 @@ */ public class IsSameDay extends DateMatcher { - private final TemporalWrapper expected; - private final TemporalFormatter describer; + private final TemporalProvider expected; + private final TemporalConverter converter; + private final Locale locale; + private final ZoneId zone; - public IsSameDay(final TemporalWrapper expected, final TemporalFormatter describer) { + public IsSameDay(TemporalConverter converter, + TemporalProvider expected, + ZoneId zone, + Locale locale) { this.expected = expected; - this.describer = describer; + this.converter = converter; + this.locale = locale; + this.zone = zone; } + public IsSameDay(TemporalConverter converter, TemporalProvider expected) { + this(converter, expected, ZoneId.systemDefault(), Locale.getDefault(Locale.Category.FORMAT)); + } + @Override - protected boolean matchesSafely(final T actual, final Description mismatchDesc) { - if (!this.expected.isSame(actual)) { - mismatchDesc.appendText("the day is " + this.describer.describeDate(actual)); + protected boolean matchesSafely(final T actual, final Description mismatchDescription) { + LocalDate expectedValue = expected.apply(zone), actualValue = converter.apply(actual, zone); + if (!expectedValue.isEqual(actualValue)) { + mismatchDescription.appendText("the day is " + describe(actualValue)); return false; } else { return true; @@ -32,12 +46,16 @@ protected boolean matchesSafely(final T actual, final Description mismatchDesc) @Override public void describeTo(final Description description) { - description.appendText("the same day as " + this.describer.describeDate(this.expected.unwrap())); + description.appendText("the same day as " + describe(expected.apply(zone))); } @Override public DateMatcher atZone(ZoneId zone) { - return new IsSameDay<>(expected.withZone(zone), describer); + return new IsSameDay<>(converter, expected, zone, locale); + } + + private String describe(LocalDate actualValue) { + return TemporalFunctions.LOCALDATE.describe(actualValue, locale); } } \ No newline at end of file diff --git a/src/main/java/org/exparity/hamcrest/date/core/IsSameOrAfter.java b/src/main/java/org/exparity/hamcrest/date/core/IsSameOrAfter.java index cd666b6..909efc0 100644 --- a/src/main/java/org/exparity/hamcrest/date/core/IsSameOrAfter.java +++ b/src/main/java/org/exparity/hamcrest/date/core/IsSameOrAfter.java @@ -1,6 +1,7 @@ package org.exparity.hamcrest.date.core; import java.time.ZoneId; +import java.util.Locale; import org.hamcrest.Description; @@ -10,20 +11,35 @@ * * @author Stewart Bissett */ -public class IsSameOrAfter extends DateMatcher { - - private final TemporalWrapper expected; - private final TemporalFormatter describer; - - public IsSameOrAfter(final TemporalWrapper expected, final TemporalFormatter describer) { +public class IsSameOrAfter extends DateMatcher { + + private final TemporalProvider expected; + private final TemporalConverter converter; + private final TemporalFunction functions; + private final Locale locale; + private final ZoneId zone; + + public IsSameOrAfter(TemporalConverter converter, + TemporalProvider expected, + TemporalFunction functions, + ZoneId zone, + Locale locale) { this.expected = expected; - this.describer = describer; + this.converter = converter; + this.functions = functions; + this.locale = locale; + this.zone = zone; } + public IsSameOrAfter(TemporalConverter converter, TemporalProvider expected, TemporalFunction functions) { + this(converter, expected, functions, ZoneId.systemDefault(), Locale.getDefault(Locale.Category.FORMAT)); + } + @Override protected boolean matchesSafely(final T actual, final Description mismatchDescription) { - if (this.expected.isAfter(actual)) { - mismatchDescription.appendText("the date is " + this.describer.describe(actual)); + E expectedValue = expected.apply(zone), actualValue = converter.apply(actual, zone); + if (functions.isAfter(expectedValue, actualValue) ) { + mismatchDescription.appendText("the date is " + functions.describe(actualValue, locale)); return false; } else { return true; @@ -32,12 +48,11 @@ protected boolean matchesSafely(final T actual, final Description mismatchDescri @Override public void describeTo(final Description description) { - description.appendText("the date is on the same date or after " + this.describer.describe(this.expected.unwrap())); + description.appendText("the date is on the same date or after " + functions.describe(expected.apply(zone), locale)); } @Override public DateMatcher atZone(ZoneId zone) { - return new IsSameOrAfter<>(expected.withZone(zone), describer); + return new IsSameOrAfter<>(converter, expected, functions, zone, locale); } - } \ No newline at end of file diff --git a/src/main/java/org/exparity/hamcrest/date/core/IsSameOrBefore.java b/src/main/java/org/exparity/hamcrest/date/core/IsSameOrBefore.java index 0f96224..f7308e8 100644 --- a/src/main/java/org/exparity/hamcrest/date/core/IsSameOrBefore.java +++ b/src/main/java/org/exparity/hamcrest/date/core/IsSameOrBefore.java @@ -1,28 +1,45 @@ package org.exparity.hamcrest.date.core; import java.time.ZoneId; +import java.util.Locale; import org.hamcrest.Description; /** - * A matcher that tests that the actual date is before or the same instant as the reference date + * A matcher that tests that the examined date is before or the same instant as + * the reference date * * @author Stewart Bissett */ -public class IsSameOrBefore extends DateMatcher { - - private final TemporalWrapper expected; - private final TemporalFormatter describer; - - public IsSameOrBefore(final TemporalWrapper expected, final TemporalFormatter describer) { +public class IsSameOrBefore extends DateMatcher { + + private final TemporalProvider expected; + private final TemporalConverter converter; + private final TemporalFunction functions; + private final Locale locale; + private final ZoneId zone; + + public IsSameOrBefore(TemporalConverter converter, + TemporalProvider expected, + TemporalFunction functions, + ZoneId zone, + Locale locale) { this.expected = expected; - this.describer = describer; + this.converter = converter; + this.functions = functions; + this.locale = locale; + this.zone = zone; } + public IsSameOrBefore(TemporalConverter converter, TemporalProvider expected, TemporalFunction functions) { + this(converter, expected, functions, ZoneId.systemDefault(), Locale.getDefault(Locale.Category.FORMAT)); + } + @Override protected boolean matchesSafely(final T actual, final Description mismatchDescription) { - if (this.expected.isBefore(actual)) { - mismatchDescription.appendText("the date is " + this.describer.describe(actual)); + E expectedValue = expected.apply(zone), actualValue = converter.apply(actual, zone); + if (functions.isBefore(expectedValue, actualValue) ) { + mismatchDescription.appendText("the date is " + functions.describe(actualValue, locale)); return false; } else { return true; @@ -31,12 +48,11 @@ protected boolean matchesSafely(final T actual, final Description mismatchDescri @Override public void describeTo(final Description description) { - description.appendText("the date is on the same date or before " + this.describer.describe(this.expected.unwrap())); + description.appendText("the date is on the same date or before " + functions.describe(expected.apply(zone), locale)); } @Override public DateMatcher atZone(ZoneId zone) { - return new IsSameOrBefore<>(expected.withZone(zone), describer); + return new IsSameOrBefore<>(converter, expected, functions, zone, locale); } - } \ No newline at end of file diff --git a/src/main/java/org/exparity/hamcrest/date/core/IsSecond.java b/src/main/java/org/exparity/hamcrest/date/core/IsSecond.java index 79635e0..4fc9fc0 100644 --- a/src/main/java/org/exparity/hamcrest/date/core/IsSecond.java +++ b/src/main/java/org/exparity/hamcrest/date/core/IsSecond.java @@ -1,7 +1,9 @@ package org.exparity.hamcrest.date.core; import java.time.ZoneId; +import java.util.Locale; +import org.exparity.hamcrest.date.core.types.Second; import org.hamcrest.Description; /** @@ -11,24 +13,30 @@ */ public class IsSecond extends DateMatcher { - private final TemporalFieldWrapper expected; - private final TemporalFieldAdapter accessor; + private final TemporalConverter converter; + private final TemporalProvider expected; + private final Locale locale; private final ZoneId zone; - private IsSecond(final TemporalFieldWrapper expected, final TemporalFieldAdapter accessor, final ZoneId zone) { + public IsSecond(TemporalConverter converter, + TemporalProvider expected, + ZoneId zone, + Locale locale) { this.expected = expected; - this.accessor = accessor; + this.converter = converter; + this.locale = locale; this.zone = zone; } - public IsSecond(final TemporalFieldWrapper expected, final TemporalFieldAdapter accessor) { - this(expected, accessor, ZoneId.systemDefault()); + public IsSecond(TemporalConverter converter, TemporalProvider expected) { + this(converter, expected, ZoneId.systemDefault(), Locale.getDefault(Locale.Category.FORMAT)); } @Override protected boolean matchesSafely(final T actual, final Description mismatchDescription) { - if (!this.expected.isSame(actual)) { - mismatchDescription.appendText("the date has the second " + accessor.asTemporalField(actual, zone)); + Second expectedValue = expected.apply(zone), actualValue = converter.apply(actual, zone); + if (!expectedValue.equals(actualValue)) { + mismatchDescription.appendText("the date has the second " + actualValue); return false; } else { return true; @@ -37,12 +45,11 @@ protected boolean matchesSafely(final T actual, final Description mismatchDescri @Override public void describeTo(final Description description) { - description.appendText("the date has the second " + expected.unwrap()); + description.appendText("the date has the second " + expected.apply(zone)); } @Override public DateMatcher atZone(ZoneId zone) { - return new IsSecond<>(expected.withZone(zone), accessor); + return new IsSecond<>(converter, expected, zone, locale); } - } \ No newline at end of file diff --git a/src/main/java/org/exparity/hamcrest/date/core/IsWithin.java b/src/main/java/org/exparity/hamcrest/date/core/IsWithin.java index df3e588..69e9347 100644 --- a/src/main/java/org/exparity/hamcrest/date/core/IsWithin.java +++ b/src/main/java/org/exparity/hamcrest/date/core/IsWithin.java @@ -1,40 +1,58 @@ package org.exparity.hamcrest.date.core; import java.time.ZoneId; -import java.time.temporal.ChronoUnit; +import java.util.Locale; +import org.exparity.hamcrest.date.core.types.Interval; import org.hamcrest.Description; /** - * A matcher that tests that the examined date is within a defined period of the - * reference date + * A matcher that tests that the examined date is within a defined period of the reference date * * @author Stewart Bissett */ -public class IsWithin extends DateMatcher { - - private final long period; - private final ChronoUnit unit; - private final TemporalWrapper expected; - private final TemporalFormatter describer; - - public IsWithin(final long period, final ChronoUnit unit, final TemporalWrapper expected, final TemporalFormatter describer) { - this.period = period; - this.unit = unit; - this.expected = expected; - this.describer = describer; +public class IsWithin extends DateMatcher { + + private final Interval expectedInterval; + private final TemporalProvider reference; + private final TemporalConverter converter; + private final TemporalFunction functions; + private final Locale locale; + private final ZoneId zone; + + public IsWithin(Interval interval, + TemporalConverter converter, + TemporalProvider reference, + TemporalFunction functions, + ZoneId zone, + Locale locale) { + this.expectedInterval = interval; + this.converter = converter; + this.reference = reference; + this.functions = functions; + this.locale = locale; + this.zone = zone; + } + + public IsWithin(Interval interval, + TemporalConverter converter, + TemporalProvider reference, + TemporalFunction functions) { + this(interval, + converter, + reference, + functions, + ZoneId.systemDefault(), + Locale.getDefault(Locale.Category.FORMAT)); } @Override - protected boolean matchesSafely(final T actual, final Description mismatchDesc) { - long actualDuration = this.expected.difference(actual, this.unit); - if (actualDuration > this.period) { - mismatchDesc.appendText("the date is " + this.describer.describe(actual) - + " and " - + actualDuration - + " " - + describeUnit() - + " different"); + protected boolean matchesSafely(final T actual, final Description mismatchDescription) { + E referenceValue = reference.apply(zone), actualValue = converter.apply(actual, zone); + Interval actualInterval = functions.interval(referenceValue, actualValue, expectedInterval.getUnit()); + if (actualInterval.longerThan(expectedInterval)) { + mismatchDescription.appendText("the date is " + functions.describe(actualValue, locale) + " and " + + actualInterval.describe(locale) + " different"); return false; } else { return true; @@ -43,16 +61,13 @@ protected boolean matchesSafely(final T actual, final Description mismatchDesc) @Override public void describeTo(final Description description) { - description.appendText("the date is within " + this.period + " " + describeUnit() + " of " + this.describer.describe(this.expected.unwrap())); - } - - private String describeUnit() { - return this.unit.toString().toLowerCase(); + description.appendText("the date is within " + expectedInterval.describe(locale) + " of " + + functions.describe(reference.apply(zone), locale)); } @Override public DateMatcher atZone(ZoneId zone) { - return new IsWithin<>(period, unit, expected.withZone(zone), describer); + return new IsWithin<>(expectedInterval, converter, reference, functions, zone, locale); } } \ No newline at end of file diff --git a/src/main/java/org/exparity/hamcrest/date/core/IsYear.java b/src/main/java/org/exparity/hamcrest/date/core/IsYear.java index 9c469cd..29cccc9 100644 --- a/src/main/java/org/exparity/hamcrest/date/core/IsYear.java +++ b/src/main/java/org/exparity/hamcrest/date/core/IsYear.java @@ -1,6 +1,8 @@ package org.exparity.hamcrest.date.core; +import java.time.Year; import java.time.ZoneId; +import java.util.Locale; import org.hamcrest.Description; @@ -12,24 +14,30 @@ */ public class IsYear extends DateMatcher { - private final TemporalFieldWrapper expected; - private final TemporalFieldAdapter accessor; + private final TemporalConverter converter; + private final TemporalProvider expected; + private final Locale locale; private final ZoneId zone; - private IsYear(final TemporalFieldWrapper expected, final TemporalFieldAdapter accessor, final ZoneId zone) { + public IsYear(TemporalConverter converter, + TemporalProvider expected, + ZoneId zone, + Locale locale) { this.expected = expected; - this.accessor = accessor; + this.converter = converter; + this.locale = locale; this.zone = zone; } - public IsYear(final TemporalFieldWrapper expected, final TemporalFieldAdapter accessor) { - this(expected, accessor, ZoneId.systemDefault()); + public IsYear(TemporalConverter converter, TemporalProvider expected) { + this(converter, expected, ZoneId.systemDefault(), Locale.getDefault(Locale.Category.FORMAT)); } @Override protected boolean matchesSafely(final T actual, final Description mismatchDescription) { - if (!this.expected.isSame(actual)) { - mismatchDescription.appendText("the date has the year " + accessor.asTemporalField(actual, zone)); + Year expectedValue = expected.apply(zone), actualValue = converter.apply(actual, zone); + if (!expectedValue.equals(actualValue)) { + mismatchDescription.appendText("the date has the year " + actualValue); return false; } else { return true; @@ -38,12 +46,11 @@ protected boolean matchesSafely(final T actual, final Description mismatchDescri @Override public void describeTo(final Description description) { - description.appendText("the date has the year " + expected.unwrap()); + description.appendText("the date has the year " + expected.apply(zone)); } @Override public DateMatcher atZone(ZoneId zone) { - return new IsYear<>(expected.withZone(zone), accessor); + return new IsYear<>(converter, expected, zone, locale); } - } \ No newline at end of file diff --git a/src/main/java/org/exparity/hamcrest/date/core/TemporalAdapter.java b/src/main/java/org/exparity/hamcrest/date/core/TemporalAdapter.java deleted file mode 100644 index d411e35..0000000 --- a/src/main/java/org/exparity/hamcrest/date/core/TemporalAdapter.java +++ /dev/null @@ -1,21 +0,0 @@ -package org.exparity.hamcrest.date.core; - -import java.time.ZoneId; -import java.time.temporal.Temporal; - -/** - * Adapter which can convert a value to a {@link Temporal} - * - * @author Stewart Bissett - */ -@FunctionalInterface -public interface TemporalAdapter { - - /** - * Return the value as a {@link Temporal} - * @param source the source value to convert - * @param zone the reference time zone - * @return the value of the field - */ - Temporal asTemporal(final T source, final ZoneId zone); -} diff --git a/src/main/java/org/exparity/hamcrest/date/core/TemporalConversionException.java b/src/main/java/org/exparity/hamcrest/date/core/TemporalConversionException.java new file mode 100644 index 0000000..38a9272 --- /dev/null +++ b/src/main/java/org/exparity/hamcrest/date/core/TemporalConversionException.java @@ -0,0 +1,13 @@ +package org.exparity.hamcrest.date.core; + +/** + * Exception thrown when a temporal type cannot be converted to another + */ +public class TemporalConversionException extends RuntimeException { + + private static final long serialVersionUID = 1L; + + public TemporalConversionException(String message) { + super(message); + } +} diff --git a/src/main/java/org/exparity/hamcrest/date/core/TemporalConverter.java b/src/main/java/org/exparity/hamcrest/date/core/TemporalConverter.java new file mode 100644 index 0000000..1634fb3 --- /dev/null +++ b/src/main/java/org/exparity/hamcrest/date/core/TemporalConverter.java @@ -0,0 +1,23 @@ +package org.exparity.hamcrest.date.core; + +import java.time.ZoneId; + +/** + * Convert one temporal type to another temporal type + * + * @author Stewart Bissett + * + * @param the source type + * @param the result type + */ +@FunctionalInterface +public interface TemporalConverter { + + /** + * Convert one temporal type to another temporal type + * @param source the source to convert + * @param zone the timezone to use + * @return the source value as the target value + */ + public R apply(T source, ZoneId zone); +} diff --git a/src/main/java/org/exparity/hamcrest/date/core/TemporalConverters.java b/src/main/java/org/exparity/hamcrest/date/core/TemporalConverters.java new file mode 100644 index 0000000..c084889 --- /dev/null +++ b/src/main/java/org/exparity/hamcrest/date/core/TemporalConverters.java @@ -0,0 +1,143 @@ +package org.exparity.hamcrest.date.core; + +import java.time.DayOfWeek; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.Month; +import java.time.Year; +import java.time.ZonedDateTime; +import java.time.temporal.TemporalAccessor; +import java.util.Date; + +import org.exparity.hamcrest.date.core.types.DayOfMonth; +import org.exparity.hamcrest.date.core.types.Hour; +import org.exparity.hamcrest.date.core.types.Millisecond; +import org.exparity.hamcrest.date.core.types.Minute; +import org.exparity.hamcrest.date.core.types.Second; + +/** + * Static repository of {@link TemporalConverter} instances which convert a temporal type from another temporal type + * e.g. given a {@link LocalDate} returns the hour, or given a {@link java.sql.Date} returns a {@link LocalDate}. + * No-operation conversions e.g. LocalDate to LocalDate are present to keep a consistent usage pattern in the + * {@link DateMatcher} implementations. + *

+ * The temporal converters generally "down-cast" a temporal type to another and are used to support testing the actual + * type against the reference type where the reference type is an equal or less accurate temporal unit e.g. comparing if + * a LocalDateTime is on a given year. There should not be "up-casting" converters because these would be making up + * absent information e.g. converting a {@link LocalDate} to a {@link LocalDateTime} + */ +public class TemporalConverters { + + private TemporalConverters() {}; + + public static final String UNSUPPORTED_SQL_DATE_UNIT = "java.sql.Date does not support time-based comparisons. Prefer SqlDateMatchers for java.sql.Date appropriate matchers"; + + /** + * SQL Date Converters + */ + public static TemporalConverter SQLDATE_AS_LOCALDATE = (date, zone) -> date.toLocalDate(); + public static TemporalConverter SQLDATE_AS_SQLDATE = (date, zone) -> date; + public static TemporalConverter SQLDATE_AS_YEAR = (date, zone) -> Year.from(SQLDATE_AS_LOCALDATE.apply(date, zone)); + public static TemporalConverter SQLDATE_AS_MONTH = (date, zone) -> SQLDATE_AS_LOCALDATE.apply(date, zone).getMonth(); + public static TemporalConverter SQLDATE_AS_DAYOFMONTH = (date, zone) -> DayOfMonth.from(SQLDATE_AS_LOCALDATE.apply(date, zone)); + public static TemporalConverter SQLDATE_AS_DAYOFWEEK = (date, zone) -> SQLDATE_AS_LOCALDATE.apply(date, zone).getDayOfWeek(); + + /** + * Java Date Converters + */ + public static TemporalConverter JAVADATE_AS_SQLDATE = (date, zone) -> { + if (date instanceof java.sql.Date) { + return (java.sql.Date) date; + } else { + return new java.sql.Date(date.getTime()); + } + }; + + public static TemporalConverter JAVADATE_AS_INSTANT = (date, zone) -> { + if (date instanceof java.sql.Date) { + throw new TemporalConversionException(UNSUPPORTED_SQL_DATE_UNIT); + } else { + return date.toInstant(); + } + }; + + public static TemporalConverter JAVADATE_AS_ZONEDDATETIME = (date, zone) -> JAVADATE_AS_INSTANT.apply(date, zone).atZone(zone); + public static TemporalConverter JAVADATE_AS_LOCALDATETIME = (date, zone) -> JAVADATE_AS_ZONEDDATETIME.apply(date, zone).toLocalDateTime(); + + public static TemporalConverter JAVADATE_AS_LOCALDATE = (date, zone) -> { + if (date instanceof java.sql.Date) { + return ((java.sql.Date) date).toLocalDate(); + } else { + return JAVADATE_AS_ZONEDDATETIME.apply(date, zone).toLocalDate(); + } + }; + + public static TemporalConverter JAVADATE_AS_TEMPORAL = (date, zone) -> { + if (date instanceof java.sql.Date) { + return JAVADATE_AS_LOCALDATE.apply(date, zone); + } else { + return JAVADATE_AS_ZONEDDATETIME.apply(date, zone); + } + }; + + + public static TemporalConverter JAVADATE_AS_JAVADATE = (date, zone) -> date; + public static TemporalConverter JAVADATE_AS_YEAR = (date, zone) -> Year.from(JAVADATE_AS_LOCALDATE.apply(date, zone)); + public static TemporalConverter JAVADATE_AS_MONTH = (date, zone) -> JAVADATE_AS_LOCALDATE.apply(date, zone).getMonth(); + public static TemporalConverter JAVADATE_AS_DAYOFMONTH = (date, zone) -> DayOfMonth.from(JAVADATE_AS_LOCALDATE.apply(date, zone)); + public static TemporalConverter JAVADATE_AS_DAYOFWEEK = (date, zone) -> JAVADATE_AS_LOCALDATE.apply(date, zone).getDayOfWeek(); + public static TemporalConverter JAVADATE_AS_HOUR = (date, zone) -> Hour.from(JAVADATE_AS_LOCALDATETIME.apply(date, zone)); + public static TemporalConverter JAVADATE_AS_MINUTE = (date, zone) -> Minute.from(JAVADATE_AS_LOCALDATETIME.apply(date, zone)); + public static TemporalConverter JAVADATE_AS_SECOND = (date, zone) -> Second.from(JAVADATE_AS_LOCALDATETIME.apply(date, zone)); + public static TemporalConverter JAVADATE_AS_MILLISECOND = (date, zone) -> Millisecond.from(JAVADATE_AS_INSTANT.apply(date, zone)); + + /** + * LocalTime Converters + */ + public static TemporalConverter LOCALTIME_AS_LOCALTIME = (time, zone) -> time; + public static TemporalConverter LOCALTIME_AS_HOUR = (time, zone) -> Hour.from(time); + public static TemporalConverter LOCALTIME_AS_MINUTE = (time, zone) -> Minute.from(time); + public static TemporalConverter LOCALTIME_AS_SECOND = (time, zone) -> Second.from(time); + + /** + * LocalDate Converters + */ + public static TemporalConverter LOCALDATE_AS_LOCALDATE = (date, zone) -> date; + public static TemporalConverter LOCALDATE_AS_YEAR = (date, zone) -> Year.from(date); + public static TemporalConverter LOCALDATE_AS_MONTH = (date, zone) -> date.getMonth(); + public static TemporalConverter LOCALDATE_AS_DAYOFMONTH = (date, zone) -> DayOfMonth.from(date); + public static TemporalConverter LOCALDATE_AS_DAYOFWEEK = (date, zone) -> date.getDayOfWeek(); + + /** + * LocalDateTime Converters + */ + public static TemporalConverter LOCALDATETIME_AS_LOCALDATETIME = (date, zone) -> date; + public static TemporalConverter LOCALDATETIME_AS_LOCALDATE = (date, zone) -> date.toLocalDate(); + public static TemporalConverter LOCALDATETIME_AS_YEAR = (date, zone) -> Year.from(date); + public static TemporalConverter LOCALDATETIME_AS_MONTH = (date, zone) -> date.getMonth(); + public static TemporalConverter LOCALDATETIME_AS_DAYOFMONTH = (date, zone) -> DayOfMonth.from(date); + public static TemporalConverter LOCALDATETIME_AS_DAYOFWEEK = (date, zone) -> date.getDayOfWeek(); + public static TemporalConverter LOCALDATETIME_AS_HOUR = (date, zone) -> Hour.from(date); + public static TemporalConverter LOCALDATETIME_AS_MINUTE = (date, zone) -> Minute.from(date); + public static TemporalConverter LOCALDATETIME_AS_SECOND = (date, zone) -> Second.from(date); + + /** + * LocalDateTime Converters + */ + public static TemporalConverter ZONEDDATETIME_AS_ZONEDDATETIME = (date, zone) -> date.withZoneSameInstant(zone); + public static TemporalConverter ZONEDDATETIME_AS_LOCALDATE = (date, zone) -> ZONEDDATETIME_AS_ZONEDDATETIME.apply(date, zone).toLocalDate(); + public static TemporalConverter ZONEDDATETIME_AS_YEAR = (date, zone) -> Year.from(ZONEDDATETIME_AS_LOCALDATE.apply(date, zone)); + public static TemporalConverter ZONEDDATETIME_AS_MONTH = (date, zone) -> ZONEDDATETIME_AS_LOCALDATE.apply(date, zone).getMonth(); + public static TemporalConverter ZONEDDATETIME_AS_DAYOFMONTH = (date, zone) -> DayOfMonth.from(ZONEDDATETIME_AS_LOCALDATE.apply(date, zone)); + public static TemporalConverter ZONEDDATETIME_AS_DAYOFWEEK = (date, zone) -> ZONEDDATETIME_AS_LOCALDATE.apply(date, zone).getDayOfWeek(); + public static TemporalConverter ZONEDDATETIME_AS_HOUR = (date, zone) -> Hour.from(ZONEDDATETIME_AS_ZONEDDATETIME.apply(date, zone)); + public static TemporalConverter ZONEDDATETIME_AS_MINUTE = (date, zone) -> Minute.from(ZONEDDATETIME_AS_ZONEDDATETIME.apply(date, zone)); + public static TemporalConverter ZONEDDATETIME_AS_SECOND = (date, zone) -> Second.from(ZONEDDATETIME_AS_ZONEDDATETIME.apply(date, zone)); + + /** + * DayOfWeek Converters + */ + public static TemporalConverter DAYOFWEEK_TO_DAYOFWEEK = (date, zone) -> date; +} diff --git a/src/main/java/org/exparity/hamcrest/date/core/TemporalFieldAdapter.java b/src/main/java/org/exparity/hamcrest/date/core/TemporalFieldAdapter.java deleted file mode 100644 index 2400867..0000000 --- a/src/main/java/org/exparity/hamcrest/date/core/TemporalFieldAdapter.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.exparity.hamcrest.date.core; - -import java.time.ZoneId; - -/** - * Adapter which can convert a value to a temporal field - * - * @see java.time.temporal.TemporalField - * @author Thomas Naskali - */ -@FunctionalInterface -public interface TemporalFieldAdapter { - - /** - * Return the value as a temporal field - * @param source the source value to convert - * @param zone the reference time zone - * @return the value of the temporal field - */ - int asTemporalField(final T source, final ZoneId zone); - -} diff --git a/src/main/java/org/exparity/hamcrest/date/core/TemporalFieldRangeAdapter.java b/src/main/java/org/exparity/hamcrest/date/core/TemporalFieldRangeAdapter.java deleted file mode 100644 index ae163c8..0000000 --- a/src/main/java/org/exparity/hamcrest/date/core/TemporalFieldRangeAdapter.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.exparity.hamcrest.date.core; - -import java.time.ZoneId; -import java.time.temporal.ValueRange; - -/** - * Adapter which can convert a value to a {@link ValueRange} - * - * @author Thomas Naskali - */ -@FunctionalInterface -public interface TemporalFieldRangeAdapter { - - /** - * Return the value as a {@link ValueRange} - * @param source the source value to convert - * @param zone the reference time zone - * @return the value of the range - */ - ValueRange asTemporalFieldRange(final T source, final ZoneId zone); - -} diff --git a/src/main/java/org/exparity/hamcrest/date/core/TemporalFieldWrapper.java b/src/main/java/org/exparity/hamcrest/date/core/TemporalFieldWrapper.java deleted file mode 100644 index 23f747f..0000000 --- a/src/main/java/org/exparity/hamcrest/date/core/TemporalFieldWrapper.java +++ /dev/null @@ -1,44 +0,0 @@ -package org.exparity.hamcrest.date.core; - -import java.time.ZoneId; - -/** - * Wrapper which wraps a temporal field so it can support the operations required - * by the matchers - * - * @author Thomas Naskali - */ -public interface TemporalFieldWrapper { - - /** - * @param other a temporal to test against - * @return true if this temporal field is after the other - */ - boolean isAfter(final T other); - - /** - * @param other a temporal to test against - * @return true if this temporal field is before the other - */ - boolean isBefore(final T other); - - /** - * @param other a temporal to test against - * @return true if this temporal field is the same as the other - */ - boolean isSame(final T other); - - /** - * @return a the wrapped value - */ - int unwrap(); - - /** - * Makes a copy of this wrapper based on a given time zone. - * - * @param zone a new reference time zone - * @return a copy of the wrapper based on the new reference time zone - */ - TemporalFieldWrapper withZone(ZoneId zone); - -} diff --git a/src/main/java/org/exparity/hamcrest/date/core/TemporalFormatter.java b/src/main/java/org/exparity/hamcrest/date/core/TemporalFormatter.java deleted file mode 100644 index 735c933..0000000 --- a/src/main/java/org/exparity/hamcrest/date/core/TemporalFormatter.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.exparity.hamcrest.date.core; - -/** - * Return the description of a temporal value - * - * @author Stewart Bissett - * @param The temporal type - */ -public interface TemporalFormatter { - - /** - * @param temporal the temporal value to describe - * @return a pretty description of the temporal value as a date - */ - String describe(T temporal); - - /** - * @param temporal the temporal value to describe - * @return a pretty description of the date portion of the temporal - */ - String describeDate(T temporal); -} diff --git a/src/main/java/org/exparity/hamcrest/date/core/TemporalFunction.java b/src/main/java/org/exparity/hamcrest/date/core/TemporalFunction.java new file mode 100644 index 0000000..8bc7d04 --- /dev/null +++ b/src/main/java/org/exparity/hamcrest/date/core/TemporalFunction.java @@ -0,0 +1,57 @@ +package org.exparity.hamcrest.date.core; + +import java.time.temporal.ChronoUnit; +import java.util.Locale; + +import org.exparity.hamcrest.date.core.types.Interval; + +/** + * Suite of temporal functions for a temporal type + * + * @author Stewart Bissett + * + * @param the temporal type + */ +public interface TemporalFunction { + + /** + * Describe a temporal type + * @param temporal the temporal instance to describe + * @param locale the locale to describe the temporal in + * @return a string description of the temporal + */ + String describe(T temporal, Locale locale); + + /** + * Test if a temporal is the same as another temporal + * @param temporal the temporal to test for + * @param other the temporal to test against + * @return a boolean + */ + boolean isSame(T temporal, T other); + + /** + * Test if a temporal is after another temporal + * @param temporal the temporal to test for + * @param other the temporal to test against + * @return a boolean + */ + boolean isAfter(T temporal, T other); + + /** + * Test if a temporal is before another temporal + * @param temporal the temporal to test for + * @param other the temporal to test against + * @return a boolean + */ + boolean isBefore(T temporal, T other); + + /** + * Return the interval between two temporals + * @param temporal the temporal to test for + * @param other the temporal to test against + * @param unit the unit to measure the interval in + * @return an {@link Interval} + */ + Interval interval(T temporal, T other, ChronoUnit unit); +} diff --git a/src/main/java/org/exparity/hamcrest/date/core/TemporalFunctions.java b/src/main/java/org/exparity/hamcrest/date/core/TemporalFunctions.java new file mode 100644 index 0000000..399bbd4 --- /dev/null +++ b/src/main/java/org/exparity/hamcrest/date/core/TemporalFunctions.java @@ -0,0 +1,33 @@ +package org.exparity.hamcrest.date.core; + +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.ZonedDateTime; +import java.util.Date; + +import org.exparity.hamcrest.date.core.function.DateFunction; +import org.exparity.hamcrest.date.core.function.InstantFunction; +import org.exparity.hamcrest.date.core.function.LocalDateFunction; +import org.exparity.hamcrest.date.core.function.LocalDateTimeFunction; +import org.exparity.hamcrest.date.core.function.LocalTimeFunction; +import org.exparity.hamcrest.date.core.function.SqlDateFunction; + +/** + * Static repository of {@link TemporalFunction} instances + * + * @author Stewart Bissett + */ +public class TemporalFunctions { + + private TemporalFunctions() {} + + public static TemporalFunction JAVADATE = new DateFunction(); + public static TemporalFunction SQLDATE = new SqlDateFunction(); + public static TemporalFunction LOCALDATE = new LocalDateFunction(); + public static TemporalFunction LOCALTIME = new LocalTimeFunction(); + public static TemporalFunction LOCALDATETIME = new LocalDateTimeFunction(); + public static TemporalFunction ZONEDDATETIME = new ZonedDateTimeFunction(); + public static TemporalFunction INSTANT = new InstantFunction(); +} diff --git a/src/main/java/org/exparity/hamcrest/date/core/TemporalProvider.java b/src/main/java/org/exparity/hamcrest/date/core/TemporalProvider.java new file mode 100644 index 0000000..91f464c --- /dev/null +++ b/src/main/java/org/exparity/hamcrest/date/core/TemporalProvider.java @@ -0,0 +1,10 @@ +package org.exparity.hamcrest.date.core; + +import java.time.ZoneId; + +@FunctionalInterface +public interface TemporalProvider { + + public S apply(final ZoneId zone); + +} diff --git a/src/main/java/org/exparity/hamcrest/date/core/TemporalProviders.java b/src/main/java/org/exparity/hamcrest/date/core/TemporalProviders.java new file mode 100644 index 0000000..f13b0c4 --- /dev/null +++ b/src/main/java/org/exparity/hamcrest/date/core/TemporalProviders.java @@ -0,0 +1,425 @@ +package org.exparity.hamcrest.date.core; + +import static org.exparity.hamcrest.date.core.TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT; + +import java.time.DayOfWeek; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.Month; +import java.time.Year; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.util.Arrays; +import java.util.Date; +import java.util.List; + +import org.exparity.hamcrest.date.core.types.DayOfMonth; +import org.exparity.hamcrest.date.core.types.Hour; +import org.exparity.hamcrest.date.core.types.Millisecond; +import org.exparity.hamcrest.date.core.types.Minute; +import org.exparity.hamcrest.date.core.types.Second; + +/** + * Static repository of {@link TemporalProvider} instances which provide a temporal type from another temporal type e.g. + * given a {@link LocalDate} returns the hour, or given a {@link java.sql.Date} returns a {@link LocalDate}. + * No-operation providers e.g. LocalDate -> LocalDate are present to keep a consistent usage pattern in the + * {@link DateMatcher} implementations + */ +public class TemporalProviders { + + private TemporalProviders() {} + + /** + * Factory to create a {@link TemporalProvider} for a {@link java.sql.Date} + */ + public static TemporalProvider sqlDate(java.util.Date date) { + if (date instanceof java.sql.Date) { + return sqlDate((java.sql.Date) date); + } else { + return (z) -> new java.sql.Date(date.getTime()); + } + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link java.sql.Date} + */ + public static TemporalProvider sqlDate(java.sql.Date date) { + return (z) -> (java.sql.Date) date; + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link Date} + */ + public static TemporalProvider javaDate(Date date) { + if (date instanceof java.sql.Date) { + return (zone) -> new Date(date.getTime()); + } else { + return (zone) -> Date.from(instant(date).apply(zone)); + } + } + + public static TemporalProvider javaDate(java.sql.Date date) { + throw new TemporalConversionException(UNSUPPORTED_SQL_DATE_UNIT); + } + + /** + * Factory to create a {@link TemporalProvider} for an {@link Instant} + */ + public static TemporalProvider instant(Date date) { + return (zone) -> zonedDateTime(date).apply(zone).toInstant(); + } + + /** + * Factory to create a {@link TemporalProvider} for an {@link Instant} + */ + public static TemporalProvider instant(Instant instant) { + return (zone) -> instant.atZone(zone).toInstant(); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link Date} + */ + public static TemporalProvider instant(java.sql.Date date) { + throw new TemporalConversionException(UNSUPPORTED_SQL_DATE_UNIT); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link LocalDate} + */ + public static TemporalProvider localDate(LocalDate date) { + return (zone) -> date; + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link LocalDate} + */ + public static TemporalProvider localDate(ZonedDateTime date) { + return (zone) -> date.toLocalDate(); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link LocalDate} + */ + public static TemporalProvider localDate(LocalDateTime date) { + return (zone) -> date.toLocalDate(); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link LocalDate} + */ + public static TemporalProvider localDate(Date date) { + if (date instanceof java.sql.Date) { + return (zone) -> ((java.sql.Date) date).toLocalDate(); + } else { + return (zone) -> zonedDateTime(date).apply(zone).toLocalDate(); + } + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link LocalDateTime} + */ + public static TemporalProvider localDateTime(LocalDateTime date) { + return (zone) -> date.atZone(zone).toLocalDateTime(); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link ZonedDateTime} + */ + public static TemporalProvider zonedDateTime(ZonedDateTime date) { + return (zone) -> date.withZoneSameInstant(zone); + } + + /** + * Factory to create a {@link TemporalProvider} for an {@link ZonedDateTime} + */ + public static TemporalProvider zonedDateTime(Date date) { + if (date instanceof java.sql.Date) { + return zonedDateTime((java.sql.Date) date); + } else { + return (zone) -> ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).withZoneSameInstant(zone); + } + } + + /** + * Factory to create a {@link TemporalProvider} for an {@link ZonedDateTime} + */ + public static TemporalProvider zonedDateTime(java.sql.Date date) { + throw new TemporalConversionException(UNSUPPORTED_SQL_DATE_UNIT); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link LocalTime} + */ + public static TemporalProvider localTime(LocalTime time) { + return (zone) -> time; + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link Year} + */ + public static TemporalProvider year(ZonedDateTime date) { + return (zone) -> Year.from(date.withZoneSameInstant(zone)); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link Year} + */ + public static TemporalProvider year(LocalDateTime date) { + return (zone) -> Year.from(date); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link Year} + */ + public static TemporalProvider year(LocalDate date) { + return (zone) -> Year.from(date); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link Year} + */ + public static TemporalProvider year(Integer year) { + return (zone) -> Year.of(year); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link Year} + */ + public static TemporalProvider year(Date date) { + return (zone) -> Year.from(localDate(date).apply(zone)); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link Month} + */ + public static TemporalProvider month(ZonedDateTime date) { + return (zone) -> date.withZoneSameInstant(zone).getMonth(); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link Month} + */ + public static TemporalProvider month(LocalDateTime date) { + return (zone) -> date.getMonth(); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link Month} + */ + public static TemporalProvider month(LocalDate date) { + return (zone) -> date.getMonth(); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link Month} + */ + public static TemporalProvider month(Month month) { + return (zone) -> month; + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link Month} + */ + public static TemporalProvider month(Date date) { + return (zone) -> localDate(date).apply(zone).getMonth(); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link DayOfWeek} + */ + public static TemporalProvider> dayOfWeek(ZonedDateTime date) { + return (zone) -> Arrays.asList(date.withZoneSameInstant(zone).getDayOfWeek()); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link DayOfWeek} + */ + public static TemporalProvider> daysOfWeek(LocalDateTime date) { + return (zone) -> Arrays.asList(date.getDayOfWeek()); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link DayOfWeek} + */ + public static TemporalProvider> daysOfWeek(LocalDate date) { + return (zone) -> Arrays.asList(date.getDayOfWeek()); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link DayOfWeek} + */ + public static TemporalProvider> daysOfWeek(DayOfWeek... dayOfWeek) { + return (zone) -> Arrays.asList(dayOfWeek); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link DayOfWeek} + */ + public static TemporalProvider> daysOfWeek(DayOfWeek dayOfWeek) { + return (zone) -> Arrays.asList(dayOfWeek); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link DayOfWeek} + */ + public static TemporalProvider> daysOfWeek(Date date) { + return (zone) -> Arrays.asList(localDate(date).apply(zone).getDayOfWeek()); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link DayOfMonth} + */ + public static TemporalProvider dayOfMonth(ZonedDateTime date) { + return (zone) -> DayOfMonth.from(date.withZoneSameInstant(zone)); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link DayOfMonth} + */ + public static TemporalProvider dayOfMonth(int dayOfMonth) { + return (zone) -> DayOfMonth.of(dayOfMonth); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link DayOfMonth} + */ + public static TemporalProvider dayOfMonth(LocalDateTime date) { + return (zone) -> DayOfMonth.from(date); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link DayOfMonth} + */ + public static TemporalProvider dayOfMonth(LocalDate date) { + return (zone) -> DayOfMonth.from(date); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link Integer} + */ + public static TemporalProvider dayOfMonth(Date date) { + return (zone) -> DayOfMonth.from(localDate(date).apply(zone)); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link Hour} + */ + public static TemporalProvider hour(Date date) { + return (zone) -> Hour.from(zonedDateTime(date).apply(zone)); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link Hour} + */ + public static TemporalProvider hour(int hour) { + return (zone) -> Hour.of(hour); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link Hour} + */ + public static TemporalProvider hour(LocalDateTime date) { + return (zone) -> Hour.from(date); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link Hour} + */ + public static TemporalProvider hour(ZonedDateTime date) { + return (zone) -> Hour.from(date.withZoneSameInstant(zone)); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link Hour} + */ + public static TemporalProvider hour(LocalTime time) { + return (zone) -> Hour.from(time); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link Minute} + */ + public static TemporalProvider minute(Date date) { + return (zone) -> Minute.from(zonedDateTime(date).apply(zone)); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link Minute} + */ + public static TemporalProvider minute(int minute) { + return (zone) -> Minute.of(minute); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link Minute} + */ + public static TemporalProvider minute(LocalDateTime date) { + return (zone) -> Minute.from(date); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link Minute} + */ + public static TemporalProvider minute(ZonedDateTime date) { + return (zone) -> Minute.from(date.withZoneSameInstant(zone)); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link Minute} + */ + public static TemporalProvider minute(LocalTime time) { + return (zone) -> Minute.from(time); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link Second} + */ + public static TemporalProvider second(Date date) { + return (zone) -> Second.from(zonedDateTime(date).apply(zone)); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link Second} + */ + public static TemporalProvider second(Integer second) { + return (zone) -> Second.of(second); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link Second} + */ + public static TemporalProvider second(LocalTime time) { + return (zone) -> Second.from(time); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link Second} + */ + public static TemporalProvider second(LocalDateTime date) { + return (zone) -> Second.from(date); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link Second} + */ + public static TemporalProvider second(ZonedDateTime date) { + return (zone) -> Second.from(date.withZoneSameInstant(zone)); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link Millisecond} + */ + public static TemporalProvider millisecondOfMinute(Date date) { + return (zone) -> Millisecond.from(instant(date).apply(zone)); + } + + /** + * Factory to create a {@link TemporalProvider} for a {@link Integer} + */ + public static TemporalProvider millisecondOfMinute(int millisecond) { + return (zone) -> Millisecond.of(millisecond); + } + +} diff --git a/src/main/java/org/exparity/hamcrest/date/core/TemporalWrapper.java b/src/main/java/org/exparity/hamcrest/date/core/TemporalWrapper.java deleted file mode 100644 index 7ec6cb8..0000000 --- a/src/main/java/org/exparity/hamcrest/date/core/TemporalWrapper.java +++ /dev/null @@ -1,50 +0,0 @@ -package org.exparity.hamcrest.date.core; - -import java.time.ZoneId; -import java.time.temporal.ChronoUnit; - -/** - * Wrapper which wraps a temporal time so it can support the operations required - * by the matchers - * - * @author Stewart Bissett - */ -public interface TemporalWrapper { - - /** - * @param other a temporal to test against - * @return true if this temporal is after the other - */ - boolean isAfter(final T other); - - /** - * @param other a temporal to test against - * @return true if this temporal is before the other - */ - boolean isBefore(final T other); - - /** - * @param other a temporal to test against - * @return true if this temporal is the same as the other - */ - boolean isSame(final T other); - - /** - * @return a the wrapped value - */ - T unwrap(); - - /** - * Return the difference in units between this time and the other time in the given units - */ - long difference(final T other, final ChronoUnit unit); - - /** - * Makes a copy of this wrapper based on a given time zone. - * - * @param zone a new reference time zone - * @return a copy of the wrapper based on the new reference time zone - */ - TemporalWrapper withZone(ZoneId zone); - -} diff --git a/src/main/java/org/exparity/hamcrest/date/core/ZonedDateTimeFunction.java b/src/main/java/org/exparity/hamcrest/date/core/ZonedDateTimeFunction.java new file mode 100644 index 0000000..3776184 --- /dev/null +++ b/src/main/java/org/exparity/hamcrest/date/core/ZonedDateTimeFunction.java @@ -0,0 +1,43 @@ +package org.exparity.hamcrest.date.core; + +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; +import java.util.Locale; + +import org.exparity.hamcrest.date.core.types.Interval; + +/** + * Implementation of {@link TemporalFunctions} for {@link ZonedDateTime} objects. + * + * @author Stewart Bissett + */ +public class ZonedDateTimeFunction implements TemporalFunction { + + private static final String DATE_TIME_PATTERN = "EEE, dd MMM yyyy hh:mm:ss.SSS a Z"; + + @Override + public boolean isAfter(final ZonedDateTime expected, final ZonedDateTime actual) { + return expected.isAfter(actual); + } + + @Override + public boolean isBefore(final ZonedDateTime expected, final ZonedDateTime actual) { + return expected.isBefore(actual); + } + + @Override + public boolean isSame(final ZonedDateTime expected, final ZonedDateTime actual) { + return expected.equals(actual); + } + + @Override + public Interval interval(ZonedDateTime expected, ZonedDateTime other, ChronoUnit unit) { + return Interval.of(expected.until(other, unit), unit); + } + + @Override + public String describe(final ZonedDateTime temporal, final Locale locale) { + return temporal.format(DateTimeFormatter.ofPattern(DATE_TIME_PATTERN, locale)); + } +} diff --git a/src/main/java/org/exparity/hamcrest/date/core/format/DateFormatter.java b/src/main/java/org/exparity/hamcrest/date/core/format/DateFormatter.java deleted file mode 100644 index 88fc0dd..0000000 --- a/src/main/java/org/exparity/hamcrest/date/core/format/DateFormatter.java +++ /dev/null @@ -1,43 +0,0 @@ -package org.exparity.hamcrest.date.core.format; - -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.Locale; - -import org.exparity.hamcrest.date.core.TemporalFormatter; - -/** - * Implementation of a {@link TemporalFormatter} to describe {@link Date} - * instances - * - * @author Stewart Bissett - */ -public class DateFormatter implements TemporalFormatter { - - private static final String DATE_TIME_FORMAT = "EEE, dd MMM yyyy hh:mm:ss.SSS a"; - private static final String DATE_FORMAT = "EEE, dd MMM yyyy"; - - private final Locale locale; - - public DateFormatter(Locale locale) { - this.locale = locale; - } - - public DateFormatter() { - this(Locale.getDefault(Locale.Category.FORMAT)); - } - - @Override - public String describe(final Date temporal) { - if ( temporal instanceof java.sql.Date ) { - return describeDate(temporal); - } else { - return new SimpleDateFormat(DATE_TIME_FORMAT, locale).format(temporal); - } - } - - @Override - public String describeDate(final Date temporal) { - return new SimpleDateFormat(DATE_FORMAT, locale).format(temporal); - } -} diff --git a/src/main/java/org/exparity/hamcrest/date/core/format/DatePartFormatter.java b/src/main/java/org/exparity/hamcrest/date/core/format/DatePartFormatter.java deleted file mode 100644 index 077dae6..0000000 --- a/src/main/java/org/exparity/hamcrest/date/core/format/DatePartFormatter.java +++ /dev/null @@ -1,32 +0,0 @@ -package org.exparity.hamcrest.date.core.format; - -import static java.util.Locale.getDefault; -import static java.util.stream.Collectors.joining; - -import java.time.temporal.ChronoField; -import java.util.stream.Stream; - -/** - * Return a human readable description of a date part - * - * @author Stewart Bissett - */ -public class DatePartFormatter { - - private static final String SPLIT_ON_UPPERCASE_REGEX = "(?=[A-Z])"; - - /** - * Return a human readable description of a date field - * @param field the field to describe - * @return the human readable description - */ - public String describe(final ChronoField field) { - switch (field) { - default: - return Stream.of(field.getDisplayName(getDefault()).split(SPLIT_ON_UPPERCASE_REGEX)) - .map(String::toLowerCase) - .collect(joining(" ")); - } - } - -} diff --git a/src/main/java/org/exparity/hamcrest/date/core/format/LocalDateFormatter.java b/src/main/java/org/exparity/hamcrest/date/core/format/LocalDateFormatter.java deleted file mode 100644 index f31a3cc..0000000 --- a/src/main/java/org/exparity/hamcrest/date/core/format/LocalDateFormatter.java +++ /dev/null @@ -1,40 +0,0 @@ -package org.exparity.hamcrest.date.core.format; - -import java.time.LocalDate; -import java.time.format.DateTimeFormatter; -import java.util.Locale; - -import org.exparity.hamcrest.date.core.TemporalFormatter; - -/** - * Implementation of a {@link TemporalFormatter} that can format a {@link LocalDate} - * - * @author Stewart Bissett - */ -public class LocalDateFormatter implements TemporalFormatter { - - private static final String DATE_PATTERN = "EEE, dd MMM yyyy"; - - private final DateTimeFormatter dateTimeFormatter; - private final DateTimeFormatter dateFormatter; - - public LocalDateFormatter(Locale locale) { - this.dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_PATTERN, locale); - this.dateFormatter = DateTimeFormatter.ofPattern(DATE_PATTERN, locale); - } - - public LocalDateFormatter() { - this(Locale.getDefault(Locale.Category.FORMAT)); - } - - @Override - public String describe(final LocalDate temporal) { - return temporal.format(dateTimeFormatter); - } - - @Override - public String describeDate(final LocalDate temporal) { - return temporal.format(dateFormatter); - } - -} diff --git a/src/main/java/org/exparity/hamcrest/date/core/format/LocalDateTimeFormatter.java b/src/main/java/org/exparity/hamcrest/date/core/format/LocalDateTimeFormatter.java deleted file mode 100644 index 9474701..0000000 --- a/src/main/java/org/exparity/hamcrest/date/core/format/LocalDateTimeFormatter.java +++ /dev/null @@ -1,40 +0,0 @@ -package org.exparity.hamcrest.date.core.format; - -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; -import java.util.Locale; - -import org.exparity.hamcrest.date.core.TemporalFormatter; - -/** - * Implementation of a {@link TemporalFormatter} that can format a - * {@link LocalDateTime} - * - * @author Stewart Bissett - */ -public class LocalDateTimeFormatter implements TemporalFormatter { - - private static final String DATE_FORMAT = "EEE, dd MMM yyyy"; - private static final String DATE_TIME_PATTERN = "EEE, dd MMM yyyy hh:mm:ss.SSS a"; - private final DateTimeFormatter dateTimeFormatter; - private final DateTimeFormatter dateFormat; - - public LocalDateTimeFormatter() { - this(Locale.getDefault(Locale.Category.FORMAT)); - } - - public LocalDateTimeFormatter(Locale locale) { - this.dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_TIME_PATTERN, locale); - this.dateFormat = DateTimeFormatter.ofPattern(DATE_FORMAT, locale); - } - - @Override - public String describe(final LocalDateTime temporal) { - return temporal.format(dateTimeFormatter); - } - - @Override - public String describeDate(final LocalDateTime temporal) { - return temporal.format(dateFormat); - } -} diff --git a/src/main/java/org/exparity/hamcrest/date/core/format/LocalTimeFormatter.java b/src/main/java/org/exparity/hamcrest/date/core/format/LocalTimeFormatter.java deleted file mode 100644 index 5be93c9..0000000 --- a/src/main/java/org/exparity/hamcrest/date/core/format/LocalTimeFormatter.java +++ /dev/null @@ -1,37 +0,0 @@ -package org.exparity.hamcrest.date.core.format; - -import java.time.LocalTime; -import java.time.format.DateTimeFormatter; -import java.util.Locale; - -import org.exparity.hamcrest.date.core.TemporalFormatter; - -/** - * Implementation of a {@link TemporalFormatter} that can format a - * {@link LocalTime} - * - * @author Stewart Bissett - */ -public class LocalTimeFormatter implements TemporalFormatter { - - private static final String TIME_PATTERN = "hh:mm:ss a"; - private final DateTimeFormatter formatter;; - - public LocalTimeFormatter(Locale locale) { - this.formatter = DateTimeFormatter.ofPattern(TIME_PATTERN, locale); - } - - public LocalTimeFormatter() { - this(Locale.getDefault(Locale.Category.FORMAT)); - } - - @Override - public String describe(final LocalTime temporal) { - return temporal.format(formatter); - } - - @Override - public String describeDate(final LocalTime temporal) { - throw new UnsupportedOperationException("describeDate is not supported against LocalTime"); - } -} diff --git a/src/main/java/org/exparity/hamcrest/date/core/format/SqlDateFormatter.java b/src/main/java/org/exparity/hamcrest/date/core/format/SqlDateFormatter.java deleted file mode 100644 index 0b84c07..0000000 --- a/src/main/java/org/exparity/hamcrest/date/core/format/SqlDateFormatter.java +++ /dev/null @@ -1,38 +0,0 @@ -package org.exparity.hamcrest.date.core.format; - -import java.text.SimpleDateFormat; -import java.sql.Date; -import java.util.Locale; - -import org.exparity.hamcrest.date.core.TemporalFormatter; - -/** - * Implementation of a {@link TemporalFormatter} to describe {@link Date} - * instances - * - * @author Stewart Bissett - */ -public class SqlDateFormatter implements TemporalFormatter { - - private static final String DATE_FORMAT = "EEE, dd MMM yyyy"; - - private final Locale locale; - - public SqlDateFormatter(Locale locale) { - this.locale = locale; - } - - public SqlDateFormatter() { - this(Locale.getDefault(Locale.Category.FORMAT)); - } - - @Override - public String describe(final Date temporal) { - return describeDate(temporal); - } - - @Override - public String describeDate(final Date temporal) { - return new SimpleDateFormat(DATE_FORMAT, locale).format(temporal); - } -} diff --git a/src/main/java/org/exparity/hamcrest/date/core/format/ZonedDateTimeFormatter.java b/src/main/java/org/exparity/hamcrest/date/core/format/ZonedDateTimeFormatter.java deleted file mode 100644 index 988c9b7..0000000 --- a/src/main/java/org/exparity/hamcrest/date/core/format/ZonedDateTimeFormatter.java +++ /dev/null @@ -1,43 +0,0 @@ -package org.exparity.hamcrest.date.core.format; - -import java.time.ZonedDateTime; -import java.time.format.DateTimeFormatter; -import java.util.Locale; - -import org.exparity.hamcrest.date.core.TemporalFormatter; - -/** - * Implementation of a {@link TemporalFormatter} that can format a - * {@link ZonedDateTime} - * - * @author Stewart Bissett - */ -public class ZonedDateTimeFormatter implements TemporalFormatter { - - private static final String DATE_PATTERN = "EEE, dd MMM yyyy"; - - private static final String DATE_TIME_PATTERN = "EEE, dd MMM yyyy hh:mm:ss.SSS a Z"; - - private final DateTimeFormatter dateTimeFormatter; - private final DateTimeFormatter dateFormat; - - public ZonedDateTimeFormatter() { - this(Locale.getDefault(Locale.Category.FORMAT)); - } - - public ZonedDateTimeFormatter(Locale locale) { - this.dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_TIME_PATTERN, locale); - this.dateFormat = DateTimeFormatter.ofPattern(DATE_PATTERN, locale); - } - - @Override - public String describe(final ZonedDateTime temporal) { - return temporal.format(dateTimeFormatter); - } - - @Override - public String describeDate(final ZonedDateTime temporal) { - return temporal.format(dateFormat); - } - -} diff --git a/src/main/java/org/exparity/hamcrest/date/core/function/DateFunction.java b/src/main/java/org/exparity/hamcrest/date/core/function/DateFunction.java new file mode 100644 index 0000000..e7fa3ae --- /dev/null +++ b/src/main/java/org/exparity/hamcrest/date/core/function/DateFunction.java @@ -0,0 +1,77 @@ +package org.exparity.hamcrest.date.core.function; + +import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.ZoneId; +import java.time.temporal.ChronoUnit; +import java.util.Date; +import java.util.Locale; + +import org.exparity.hamcrest.date.core.TemporalFunction; +import org.exparity.hamcrest.date.core.types.Interval; + +/** + * Implementation of {@link TemporalWrapper} to wrap {@link Date} objects. + * + * @author Stewart Bissett + */ +public class DateFunction implements TemporalFunction { + + public static final String JAVA_SQL_DATE_UNIT = "java.sql.Date does not support time-based units. Prefer SqlDateMatchers for java.sql.Date appropriate matchers"; + + private static final String DATE_TIME_FORMAT = "EEE, dd MMM yyyy hh:mm:ss.SSS a"; + private static final String DATE_FORMAT = "EEE, dd MMM yyyy"; + + @Override + public boolean isAfter(final Date expected, final Date actual) { + if (expected instanceof java.sql.Date || actual instanceof java.sql.Date) { + return toLocalDate(expected).isAfter(toLocalDate(actual)); + } else { + return expected.after(actual); + } + } + + @Override + public boolean isBefore(final Date expected, final Date actual) { + if (expected instanceof java.sql.Date || actual instanceof java.sql.Date) { + return toLocalDate(expected).isBefore(toLocalDate(actual)); + } else { + return expected.before(actual); + } + } + + @Override + public boolean isSame(final Date expected, final Date actual) { + if (expected instanceof java.sql.Date || actual instanceof java.sql.Date) { + return toLocalDate(expected).equals(toLocalDate(actual)); + } else { + return expected.equals(actual); + } + } + + @Override + public Interval interval(Date expected, Date actual, ChronoUnit unit) { + if (expected instanceof java.sql.Date || actual instanceof java.sql.Date) { + return Interval.of(toLocalDate(expected).until(toLocalDate(actual), unit), unit); + } else { + return Interval.of(expected.toInstant().until(actual.toInstant(), unit), unit); + } + } + + @Override + public String describe(final Date temporal, final Locale locale) { + if (temporal instanceof java.sql.Date) { + return new SimpleDateFormat(DATE_FORMAT, locale).format(temporal); + } else { + return new SimpleDateFormat(DATE_TIME_FORMAT, locale).format(temporal); + } + } + + private LocalDate toLocalDate(Date expected) { + if (expected instanceof java.sql.Date) { + return ((java.sql.Date) expected).toLocalDate(); + } else { + return expected.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); + } + } +} diff --git a/src/main/java/org/exparity/hamcrest/date/core/function/InstantFunction.java b/src/main/java/org/exparity/hamcrest/date/core/function/InstantFunction.java new file mode 100644 index 0000000..42c3ce9 --- /dev/null +++ b/src/main/java/org/exparity/hamcrest/date/core/function/InstantFunction.java @@ -0,0 +1,40 @@ +package org.exparity.hamcrest.date.core.function; + +import java.time.Instant; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; +import java.util.Locale; + +import org.exparity.hamcrest.date.core.TemporalFunction; +import org.exparity.hamcrest.date.core.types.Interval; + +/** + * Implementation of {@link TemporalFunction} for {@link Instant} + */ +public final class InstantFunction implements TemporalFunction { + + @Override + public boolean isSame(Instant temporal, Instant other) { + return temporal.equals(other); + } + + @Override + public boolean isAfter(Instant temporal, Instant other) { + return temporal.isAfter(other); + } + + @Override + public boolean isBefore(Instant temporal, Instant other) { + return temporal.isBefore(other); + } + + @Override + public Interval interval(Instant expected, Instant other, ChronoUnit unit) { + return Interval.of(expected.until(other, unit), unit); + } + + @Override + public String describe(Instant temporal, Locale locale) { + return DateTimeFormatter.ISO_INSTANT.format(temporal); + } +} \ No newline at end of file diff --git a/src/main/java/org/exparity/hamcrest/date/core/function/LocalDateFunction.java b/src/main/java/org/exparity/hamcrest/date/core/function/LocalDateFunction.java new file mode 100644 index 0000000..02ccae9 --- /dev/null +++ b/src/main/java/org/exparity/hamcrest/date/core/function/LocalDateFunction.java @@ -0,0 +1,44 @@ +package org.exparity.hamcrest.date.core.function; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; +import java.util.Locale; + +import org.exparity.hamcrest.date.core.TemporalFunction; +import org.exparity.hamcrest.date.core.types.Interval; + +/** + * Implementation of {@link TemporalFunction} to wrap {@link LocalDate} objects. + * + * @author Stewart Bissett + */ +public class LocalDateFunction implements TemporalFunction { + + private static final String DATE_PATTERN = "EEE, dd MMM yyyy"; + + @Override + public boolean isAfter(final LocalDate expected, final LocalDate actual) { + return expected.isAfter(actual); + } + + @Override + public boolean isBefore(final LocalDate expected, final LocalDate actual) { + return expected.isBefore(actual); + } + + @Override + public boolean isSame(final LocalDate expected, final LocalDate actual) { + return expected.equals(actual); + } + + @Override + public Interval interval(LocalDate expected, LocalDate other, ChronoUnit unit) { + return Interval.of(expected.until(other, unit), unit); + } + + @Override + public String describe(final LocalDate temporal, final Locale locale) { + return temporal.format(DateTimeFormatter.ofPattern(DATE_PATTERN, locale)); + } +} diff --git a/src/main/java/org/exparity/hamcrest/date/core/function/LocalDateTimeFunction.java b/src/main/java/org/exparity/hamcrest/date/core/function/LocalDateTimeFunction.java new file mode 100644 index 0000000..3a5d86d --- /dev/null +++ b/src/main/java/org/exparity/hamcrest/date/core/function/LocalDateTimeFunction.java @@ -0,0 +1,44 @@ +package org.exparity.hamcrest.date.core.function; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; +import java.util.Locale; + +import org.exparity.hamcrest.date.core.TemporalFunction; +import org.exparity.hamcrest.date.core.types.Interval; + +/** + * Implementation of {@link TemporalFunction} for {@link LocalDateTime} objects. + * + * @author Stewart Bissett + */ +public class LocalDateTimeFunction implements TemporalFunction { + + private static final String DATE_TIME_PATTERN = "EEE, dd MMM yyyy hh:mm:ss.SSS a"; + + @Override + public boolean isAfter(final LocalDateTime expected, final LocalDateTime actual) { + return expected.isAfter(actual); + } + + @Override + public boolean isBefore(final LocalDateTime expected, final LocalDateTime actual) { + return expected.isBefore(actual); + } + + @Override + public boolean isSame(final LocalDateTime expected, final LocalDateTime actual) { + return expected.equals(actual); + } + + @Override + public Interval interval(LocalDateTime expected, LocalDateTime other, ChronoUnit unit) { + return Interval.of(expected.until(other, unit), unit); + } + + @Override + public String describe(final LocalDateTime temporal, final Locale locale) { + return temporal.format(DateTimeFormatter.ofPattern(DATE_TIME_PATTERN, locale)); + } +} diff --git a/src/main/java/org/exparity/hamcrest/date/core/function/LocalTimeFunction.java b/src/main/java/org/exparity/hamcrest/date/core/function/LocalTimeFunction.java new file mode 100644 index 0000000..7de693f --- /dev/null +++ b/src/main/java/org/exparity/hamcrest/date/core/function/LocalTimeFunction.java @@ -0,0 +1,44 @@ +package org.exparity.hamcrest.date.core.function; + +import java.time.LocalTime; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; +import java.util.Locale; + +import org.exparity.hamcrest.date.core.TemporalFunction; +import org.exparity.hamcrest.date.core.types.Interval; + +/** + * Implementation of {@link TemporalFunction} to wrap {@link LocalTime} objects. + * + * @author Stewart Bissett + */ +public class LocalTimeFunction implements TemporalFunction { + + private static final String TIME_PATTERN = "hh:mm:ss a"; + + @Override + public boolean isAfter(final LocalTime expected, final LocalTime actual) { + return expected.isAfter(actual); + } + + @Override + public boolean isBefore(final LocalTime expected, final LocalTime actual) { + return expected.isBefore(actual); + } + + @Override + public boolean isSame(final LocalTime expected, final LocalTime actual) { + return expected.equals(actual); + } + + @Override + public Interval interval(LocalTime expected, LocalTime other, ChronoUnit unit) { + return Interval.of(expected.until(other, unit), unit); + } + + @Override + public String describe(final LocalTime temporal, final Locale locale) { + return temporal.format(DateTimeFormatter.ofPattern(TIME_PATTERN, locale)); + } +} diff --git a/src/main/java/org/exparity/hamcrest/date/core/function/SqlDateFunction.java b/src/main/java/org/exparity/hamcrest/date/core/function/SqlDateFunction.java new file mode 100644 index 0000000..c30376a --- /dev/null +++ b/src/main/java/org/exparity/hamcrest/date/core/function/SqlDateFunction.java @@ -0,0 +1,44 @@ +package org.exparity.hamcrest.date.core.function; + +import java.sql.Date; +import java.text.SimpleDateFormat; +import java.time.temporal.ChronoUnit; +import java.util.Locale; + +import org.exparity.hamcrest.date.core.TemporalFunction; +import org.exparity.hamcrest.date.core.types.Interval; + +/** + * Implementation of {@link TemporalWrapper} to wrap {@link Date} objects. + * + * @author Stewart Bissett + */ +public class SqlDateFunction implements TemporalFunction { + + private static final String DATE_FORMAT = "EEE, dd MMM yyyy"; + + @Override + public boolean isAfter(final Date expected, final Date actual) { + return expected.toLocalDate().isAfter(actual.toLocalDate()); + } + + @Override + public boolean isBefore(final Date expected, final Date actual) { + return expected.toLocalDate().isBefore(actual.toLocalDate()); + } + + @Override + public boolean isSame(final Date expected, final Date actual) { + return expected.toLocalDate().equals(actual.toLocalDate()); + } + + @Override + public Interval interval(Date temporal, Date other, ChronoUnit unit) { + return Interval.of(temporal.toLocalDate().until(other.toLocalDate(), unit), unit); + } + + @Override + public String describe(final Date temporal, final Locale locale) { + return new SimpleDateFormat(DATE_FORMAT, locale).format(temporal); + } +} diff --git a/src/main/java/org/exparity/hamcrest/date/core/types/DayOfMonth.java b/src/main/java/org/exparity/hamcrest/date/core/types/DayOfMonth.java new file mode 100644 index 0000000..d7db6ea --- /dev/null +++ b/src/main/java/org/exparity/hamcrest/date/core/types/DayOfMonth.java @@ -0,0 +1,54 @@ +package org.exparity.hamcrest.date.core.types; + +import java.time.temporal.ChronoField; +import java.time.temporal.TemporalAccessor; + +/** + * Value type to represent a day of a month. Instantiate via {@link DayOfMonth#of(int)} factory method. + * + * @author Stewart Bissett + */ +public class DayOfMonth { + + /** + * Create an instance of an {@link DayOfMonth} + */ + public static DayOfMonth of(int value) { + return new DayOfMonth(value); + } + + /** + * Create an instance of an {@link DayOfMonth} from a {@link TemporalAccessor} + */ + public static DayOfMonth from(TemporalAccessor temporal) { + return new DayOfMonth(temporal.get(ChronoField.DAY_OF_MONTH)); + } + + private final int value; + + private DayOfMonth(int value) { + this.value = value; + } + + public int getValue() { + return value; + } + + @Override + public int hashCode() { + return Integer.valueOf(value).hashCode(); + } + + @Override + public boolean equals(Object obj) { + if ( obj == this ) return true; + if ( obj == null || obj.getClass() != getClass()) return false; + DayOfMonth that = (DayOfMonth) obj; + return this.value == that.value; + } + + @Override + public String toString() { + return String.valueOf(value); + } +} diff --git a/src/main/java/org/exparity/hamcrest/date/core/types/Hour.java b/src/main/java/org/exparity/hamcrest/date/core/types/Hour.java new file mode 100644 index 0000000..a9a77c3 --- /dev/null +++ b/src/main/java/org/exparity/hamcrest/date/core/types/Hour.java @@ -0,0 +1,54 @@ +package org.exparity.hamcrest.date.core.types; + +import java.time.temporal.ChronoField; +import java.time.temporal.TemporalAccessor; + +/** + * Value type to represent an hour of the day. Instantiate via {@link Hour#of(int)} factory method. + * + * @author Stewart Bissett + */ +public class Hour { + + /** + * Create an instance of an {@link Hour} + */ + public static Hour of(int value) { + return new Hour(value); + } + + /** + * Create an instance of an {@link Hour} from a {@link TemporalAccessor} + */ + public static Hour from(TemporalAccessor temporal) { + return new Hour(temporal.get(ChronoField.HOUR_OF_DAY)); + } + + private final int value; + + private Hour(int value) { + this.value = value; + } + + public int getValue() { + return value; + } + + @Override + public int hashCode() { + return Integer.valueOf(value).hashCode(); + } + + @Override + public boolean equals(Object obj) { + if ( obj == this ) return true; + if ( obj == null || obj.getClass() != getClass()) return false; + Hour that = (Hour) obj; + return this.value == that.value; + } + + @Override + public String toString() { + return String.valueOf(value); + } +} diff --git a/src/main/java/org/exparity/hamcrest/date/core/types/Interval.java b/src/main/java/org/exparity/hamcrest/date/core/types/Interval.java new file mode 100644 index 0000000..4461d32 --- /dev/null +++ b/src/main/java/org/exparity/hamcrest/date/core/types/Interval.java @@ -0,0 +1,61 @@ +package org.exparity.hamcrest.date.core.types; + +import java.time.Duration; +import java.time.Period; +import java.time.temporal.ChronoUnit; +import java.util.Locale; + +/** + * Encapsulate a time or date interval. Equivalent to {@link Duration} and {@link Period} but supporting both time and + * date elements + * + * @author Stewart Bissett + */ +public class Interval { + + /** + * Create an {@link Interval} from a {@link Period} using a unit of {@link ChronoUnit#DAYS} + */ + public static Interval of(Period period) { + return new Interval(period.getDays(), ChronoUnit.DAYS); + } + + /** + * Create an {@link Interval} from a {@link Period} using a unit of {@link ChronoUnit#DAYS} + */ + public static Interval of(Duration duration) { + return new Interval(duration.getSeconds(), ChronoUnit.SECONDS); + } + + /** + * Create an {@link Interval} from an explicit period and {@link ChronoUnit} + */ + public static Interval of(long period, ChronoUnit unit) { + return new Interval(period, unit); + } + + private final long period; + private final ChronoUnit unit; + + private Interval(long period, ChronoUnit unit) { + this.period = Math.abs(period); + this.unit = unit; + } + + public ChronoUnit getUnit() { + return unit; + } + + public boolean longerThan(Interval other) { + if ( unit == other.unit ) { + return period > other.period; + } else { + throw new UnsupportedOperationException("Units are different. " + unit + " vs " + other.unit); + } + } + + public String describe(Locale locale) { + return period + " " + unit.toString(); + } + +} diff --git a/src/main/java/org/exparity/hamcrest/date/core/types/Millisecond.java b/src/main/java/org/exparity/hamcrest/date/core/types/Millisecond.java new file mode 100644 index 0000000..c35f26b --- /dev/null +++ b/src/main/java/org/exparity/hamcrest/date/core/types/Millisecond.java @@ -0,0 +1,63 @@ +package org.exparity.hamcrest.date.core.types; + +import java.time.temporal.ChronoField; +import java.time.temporal.TemporalAccessor; + +/** + * Value type to represent a millisecond of a minute. Instantiate via {@link Millisecond#of(int)} factory method. + * + * @author Stewart Bissett + */ +public class Millisecond { + + private static final int NANOS_PER_MILLISECOND = 1000000; + + /** + * Create an instance of an {@link Millisecond} + */ + public static Millisecond of(int value) { + return new Millisecond(value); + } + + /** + * Create an instance of an {@link Millisecond} from a number of nanoseconds + */ + public static Millisecond ofNanos(int nano) { + return Millisecond.of(nano/NANOS_PER_MILLISECOND); + } + + /** + * Create an instance of an {@link Millisecond} from a {@link TemporalAccessor} + */ + public static Millisecond from(TemporalAccessor temporal) { + return new Millisecond(temporal.get(ChronoField.MILLI_OF_SECOND)); + } + + private final int value; + + private Millisecond(int value) { + this.value = value; + } + + public int getValue() { + return value; + } + + @Override + public int hashCode() { + return Integer.valueOf(value).hashCode(); + } + + @Override + public boolean equals(Object obj) { + if ( obj == this ) return true; + if ( obj == null || obj.getClass() != getClass()) return false; + Millisecond that = (Millisecond) obj; + return this.value == that.value; + } + + @Override + public String toString() { + return String.valueOf(value); + } +} diff --git a/src/main/java/org/exparity/hamcrest/date/core/types/Minute.java b/src/main/java/org/exparity/hamcrest/date/core/types/Minute.java new file mode 100644 index 0000000..6a4b183 --- /dev/null +++ b/src/main/java/org/exparity/hamcrest/date/core/types/Minute.java @@ -0,0 +1,54 @@ +package org.exparity.hamcrest.date.core.types; + +import java.time.temporal.ChronoField; +import java.time.temporal.TemporalAccessor; + +/** + * Value type to represent an minute of an hour. Instantiate via {@link Minute#of(int)} factory method. + * + * @author Stewart Bissett + */ +public class Minute { + + /** + * Create an instance of an {@link Minute} + */ + public static Minute of(int value) { + return new Minute(value); + } + + /** + * Create an instance of an {@link Minute} from a {@link TemporalAccessor} + */ + public static Minute from(TemporalAccessor temporal) { + return new Minute(temporal.get(ChronoField.MINUTE_OF_HOUR)); + } + + private final int value; + + private Minute(int value) { + this.value = value; + } + + public int getValue() { + return value; + } + + @Override + public int hashCode() { + return Integer.valueOf(value).hashCode(); + } + + @Override + public boolean equals(Object obj) { + if ( obj == this ) return true; + if ( obj == null || obj.getClass() != getClass()) return false; + Minute that = (Minute) obj; + return this.value == that.value; + } + + @Override + public String toString() { + return String.valueOf(value); + } +} diff --git a/src/main/java/org/exparity/hamcrest/date/core/types/Second.java b/src/main/java/org/exparity/hamcrest/date/core/types/Second.java new file mode 100644 index 0000000..95feab1 --- /dev/null +++ b/src/main/java/org/exparity/hamcrest/date/core/types/Second.java @@ -0,0 +1,54 @@ +package org.exparity.hamcrest.date.core.types; + +import java.time.temporal.ChronoField; +import java.time.temporal.TemporalAccessor; + +/** + * Value type to represent an second of a minute. Instantiate via {@link Second#of(int)} factory method. + * + * @author Stewart Bissett + */ +public class Second { + + /** + * Create an instance of an {@link Second} + */ + public static Second of(int value) { + return new Second(value); + } + + /** + * Create an instance of an {@link Second} from a {@link TemporalAccessor} + */ + public static Second from(TemporalAccessor temporal) { + return new Second(temporal.get(ChronoField.SECOND_OF_MINUTE)); + } + + private final int value; + + private Second(int value) { + this.value = value; + } + + public int getValue() { + return value; + } + + @Override + public int hashCode() { + return Integer.valueOf(value).hashCode(); + } + + @Override + public boolean equals(Object obj) { + if ( obj == this ) return true; + if ( obj == null || obj.getClass() != getClass()) return false; + Second that = (Second) obj; + return this.value == that.value; + } + + @Override + public String toString() { + return String.valueOf(value); + } +} diff --git a/src/main/java/org/exparity/hamcrest/date/core/wrapper/DateWrapper.java b/src/main/java/org/exparity/hamcrest/date/core/wrapper/DateWrapper.java deleted file mode 100644 index 48489d6..0000000 --- a/src/main/java/org/exparity/hamcrest/date/core/wrapper/DateWrapper.java +++ /dev/null @@ -1,148 +0,0 @@ -package org.exparity.hamcrest.date.core.wrapper; - -import java.time.Instant; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.Month; -import java.time.ZoneId; -import java.time.ZonedDateTime; -import java.time.temporal.ChronoUnit; -import java.time.temporal.Temporal; -import java.time.temporal.TemporalUnit; -import java.util.Date; -import java.util.function.Function; - -import org.exparity.hamcrest.date.core.TemporalWrapper; - -/** - * Implementation of {@link TemporalWrapper} to wrap {@link Date} objects. - * - * @author Stewart Bissett - */ -public class DateWrapper implements TemporalWrapper { - - public static final String JAVA_SQL_DATE_UNIT = "java.sql.Date does not support time-based units. Prefer SqlDateMatchers for java.sql.Date appropriate matchers"; - - private final Function wrapped; - private final ZoneId zone; - private final TemporalUnit accuracy; - - private DateWrapper(final Function wrapped, final ZoneId zone, final TemporalUnit accuracy) { - this.wrapped = wrapped; - this.zone = zone; - this.accuracy = accuracy; - } - - public DateWrapper(final Date date) { - this(date, ChronoUnit.MILLIS); - } - - public DateWrapper(final java.sql.Date date) { - this(z -> date.toLocalDate().atStartOfDay(z), ZoneId.systemDefault(), ChronoUnit.DAYS); - } - - public DateWrapper(final Date date, final TemporalUnit accuracy) { - this(z -> date.toInstant().atZone(z), ZoneId.systemDefault(), accuracy); - } - - public DateWrapper(final LocalDate date) { - this(date, ChronoUnit.DAYS); - } - - public DateWrapper(final LocalDate date, final TemporalUnit accuracy) { - this.zone = ZoneId.systemDefault(); - this.wrapped = date::atStartOfDay; - this.accuracy = accuracy; - } - - public DateWrapper(final int year, final Month month, final int dayOfMonth) { - this(LocalDate.of(year, month, dayOfMonth)); - } - - public DateWrapper(final int year, final Month month, final int dayOfMonth, final int hour, final int minute, - final int second) { - this.zone = ZoneId.systemDefault(); - this.wrapped = z -> LocalDateTime.of(year, month, dayOfMonth, hour, minute, second).atZone(z); - this.accuracy = ChronoUnit.SECONDS; - } - - public DateWrapper(final int year, final Month month, final int dayOfMonth, final int hour, final int minute, - final int second, final int millis) { - this.zone = ZoneId.systemDefault(); - this.wrapped = z -> LocalDateTime.of(year, month, dayOfMonth, hour, minute, second, millis * 1000000).atZone(z); - this.accuracy = ChronoUnit.MILLIS; - } - - @Override - public long difference(final Date other, final ChronoUnit unit) { - if ( other instanceof java.sql.Date) { - return difference(thisLocalDate(), otherLocalDate(other), requireDateBased(unit)); - } else { - return difference(thisInstant(), otherInstant(other), unit); - } - } - - @Override - public boolean isAfter(final Date other) { - if ( other instanceof java.sql.Date) { - return thisLocalDate().isAfter(otherLocalDate(other)); - } else { - return thisInstant().isAfter(otherInstant(other)); - } - } - - @Override - public boolean isBefore(final Date other) { - if ( other instanceof java.sql.Date) { - return thisLocalDate().isBefore(otherLocalDate(other)); - } else { - return thisInstant().isBefore(otherInstant(other)); - } - } - - @Override - public boolean isSame(final Date other) { - if ( other instanceof java.sql.Date) { - return thisLocalDate().isEqual(otherLocalDate(other)); - } else { - return thisInstant().equals(otherInstant(other)); - } - } - - @Override - public Date unwrap() { - return new Date(wrapped.apply(zone).toInstant().toEpochMilli()); - } - - @Override - public DateWrapper withZone(final ZoneId zone) { - return new DateWrapper(wrapped, zone, accuracy); - } - - private Instant otherInstant(final Date other) { - return other.toInstant().atZone(zone).truncatedTo(accuracy).toInstant(); - } - - private Instant thisInstant() { - return wrapped.apply(zone).truncatedTo(accuracy).toInstant(); - } - - private LocalDate otherLocalDate(final Date other) { - return ((java.sql.Date) other).toLocalDate(); - } - - private LocalDate thisLocalDate() { - return wrapped.apply(zone).toLocalDate(); - } - - private long difference(Temporal from, Temporal to, final ChronoUnit unit) { - return Math.abs(from.until(to, unit)); - } - - private ChronoUnit requireDateBased(ChronoUnit unit) { - if ( !unit.isDateBased() ) { - throw new IllegalArgumentException(JAVA_SQL_DATE_UNIT); - } - return unit; - } -} diff --git a/src/main/java/org/exparity/hamcrest/date/core/wrapper/FieldDateAdaptor.java b/src/main/java/org/exparity/hamcrest/date/core/wrapper/FieldDateAdaptor.java deleted file mode 100644 index 7090897..0000000 --- a/src/main/java/org/exparity/hamcrest/date/core/wrapper/FieldDateAdaptor.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.exparity.hamcrest.date.core.wrapper; - -import java.time.ZoneId; -import java.time.temporal.ChronoField; -import java.util.Date; - -import org.exparity.hamcrest.date.core.TemporalFieldAdapter; - -/** - * Implementation of a {@link TemporalFieldAdapter} for {@link Date} - */ -public class FieldDateAdaptor implements TemporalFieldAdapter { - - private final ChronoField field; - - public FieldDateAdaptor(ChronoField field) { - this.field = field; - } - - @Override - public int asTemporalField(Date source, ZoneId zone) { - if (source instanceof java.sql.Date) { - return ((java.sql.Date) source).toLocalDate().get(field); - } else { - return source.toInstant().atZone(zone).get(field); - } - } -} \ No newline at end of file diff --git a/src/main/java/org/exparity/hamcrest/date/core/wrapper/FieldDateRangeAdaptor.java b/src/main/java/org/exparity/hamcrest/date/core/wrapper/FieldDateRangeAdaptor.java deleted file mode 100644 index 9b364bf..0000000 --- a/src/main/java/org/exparity/hamcrest/date/core/wrapper/FieldDateRangeAdaptor.java +++ /dev/null @@ -1,29 +0,0 @@ -package org.exparity.hamcrest.date.core.wrapper; - -import java.time.ZoneId; -import java.time.temporal.ChronoField; -import java.time.temporal.ValueRange; -import java.util.Date; - -import org.exparity.hamcrest.date.core.TemporalFieldRangeAdapter; - -/** - * Implementation of a {@link TemporalFieldRangeAdapter} for {@link Date} - */ -public class FieldDateRangeAdaptor implements TemporalFieldRangeAdapter { - - private final ChronoField field; - - public FieldDateRangeAdaptor(ChronoField field) { - this.field = field; - } - - @Override - public ValueRange asTemporalFieldRange(Date source, ZoneId zone) { - if (source instanceof java.sql.Date) { - return field.rangeRefinedBy(((java.sql.Date) source).toLocalDate()); - } else { - return field.rangeRefinedBy(source.toInstant().atZone(zone)); - } - } -} \ No newline at end of file diff --git a/src/main/java/org/exparity/hamcrest/date/core/wrapper/FieldDateWrapper.java b/src/main/java/org/exparity/hamcrest/date/core/wrapper/FieldDateWrapper.java deleted file mode 100644 index 8be2c20..0000000 --- a/src/main/java/org/exparity/hamcrest/date/core/wrapper/FieldDateWrapper.java +++ /dev/null @@ -1,91 +0,0 @@ -package org.exparity.hamcrest.date.core.wrapper; - -import java.time.ZoneId; -import java.time.temporal.ChronoField; -import java.time.temporal.UnsupportedTemporalTypeException; -import java.util.Date; -import java.util.function.ToIntFunction; - -import org.exparity.hamcrest.date.DateMatchers; -import org.exparity.hamcrest.date.core.TemporalFieldWrapper; - -/** - * Implementation of {@link TemporalFieldWrapper} which wraps a temporal field of a {@link Date} instance. - * - * @author Thomas Naskali - */ -public class FieldDateWrapper implements TemporalFieldWrapper { - - public static final String JAVA_SQL_DATE_UNIT = "java.sql.Date does not support time-based units. Prefer SqlDateMatchers for java.sql.Date appropriate matchers"; - - private final ToIntFunction wrapped; - private final ChronoField field; - private final ZoneId zone; - - private FieldDateWrapper(final ToIntFunction wrapped, final ChronoField field, final ZoneId zone) { - this.wrapped = wrapped; - this.field = field; - this.zone = zone; - } - - public FieldDateWrapper(final int value, final ChronoField field) { - this((ignored) -> value, field, ZoneId.systemDefault()); - } - - public FieldDateWrapper(Date date, ChronoField field) { - this(z -> date.toInstant().atZone(z).get(field), field, ZoneId.systemDefault()); - } - - public FieldDateWrapper(java.sql.Date date, ChronoField field) { - this(z -> date.toLocalDate().atStartOfDay(z).get(field), requireDateBased(field), ZoneId.systemDefault()); - } - - @Override - public boolean isAfter(Date other) { - return selfField() > otherField(other); - } - - private int selfField() { - return wrapped.applyAsInt(zone); - } - - @Override - public boolean isBefore(Date other) { - return selfField() < otherField(other); - } - - @Override - public boolean isSame(Date other) { - return selfField() == otherField(other); - } - - @Override - public int unwrap() { - return selfField(); - } - - @Override - public TemporalFieldWrapper withZone(ZoneId zone) { - return new FieldDateWrapper(wrapped, field, zone); - } - - private int otherField(Date other) { - if (other instanceof java.sql.Date) { - try { - return ((java.sql.Date) other).toLocalDate().get(field); - } catch (UnsupportedTemporalTypeException e) { - throw new IllegalArgumentException(DateMatchers.UNSUPPORTED_SQL_DATE_UNIT); - } - } else { - return other.toInstant().atZone(zone).get(field); - } - } - - private static ChronoField requireDateBased(ChronoField field) { - if ( !field.isDateBased() ) { - throw new IllegalArgumentException(DateMatchers.UNSUPPORTED_SQL_DATE_UNIT); - } - return field; - } - -} diff --git a/src/main/java/org/exparity/hamcrest/date/core/wrapper/FieldLocalDateTimeWrapper.java b/src/main/java/org/exparity/hamcrest/date/core/wrapper/FieldLocalDateTimeWrapper.java deleted file mode 100644 index 1d9073a..0000000 --- a/src/main/java/org/exparity/hamcrest/date/core/wrapper/FieldLocalDateTimeWrapper.java +++ /dev/null @@ -1,64 +0,0 @@ -package org.exparity.hamcrest.date.core.wrapper; - -import java.time.LocalDateTime; -import java.time.ZoneId; -import java.time.temporal.ChronoField; -import java.util.function.ToIntFunction; - -import org.exparity.hamcrest.date.core.TemporalFieldWrapper; - -/** - * Implementation of {@link TemporalFieldWrapper} which wraps a temporal field of a {@link LocalDateTime} instance. - * - * @author Thomas Naskali - */ -public class FieldLocalDateTimeWrapper implements TemporalFieldWrapper { - - private final ToIntFunction wrapped; - private final ChronoField field; - private final ZoneId zone; - - private FieldLocalDateTimeWrapper(final ToIntFunction wrapped, final ChronoField field, final ZoneId zone) { - this.wrapped = wrapped; - this.field = field; - this.zone = zone; - } - - public FieldLocalDateTimeWrapper(final int value, final ChronoField field) { - this.wrapped = (ignored) -> value; - this.field = field; - this.zone = ZoneId.systemDefault(); - } - - public FieldLocalDateTimeWrapper(final LocalDateTime date, final ChronoField field) { - this.wrapped = z -> date.atZone(z).get(field); - this.field = field; - this.zone = ZoneId.systemDefault(); - } - - @Override - public boolean isAfter(final LocalDateTime other) { - return wrapped.applyAsInt(zone) > other.atZone(zone).get(field); - } - - @Override - public boolean isBefore(final LocalDateTime other) { - return wrapped.applyAsInt(zone) < other.atZone(zone).get(field); - } - - @Override - public boolean isSame(final LocalDateTime other) { - return wrapped.applyAsInt(zone) == other.atZone(zone).get(field); - } - - @Override - public int unwrap() { - return wrapped.applyAsInt(zone); - } - - @Override - public TemporalFieldWrapper withZone(final ZoneId zone) { - return new FieldLocalDateTimeWrapper(wrapped, field, zone); - } - -} diff --git a/src/main/java/org/exparity/hamcrest/date/core/wrapper/FieldLocalDateWrapper.java b/src/main/java/org/exparity/hamcrest/date/core/wrapper/FieldLocalDateWrapper.java deleted file mode 100644 index 34bf3f6..0000000 --- a/src/main/java/org/exparity/hamcrest/date/core/wrapper/FieldLocalDateWrapper.java +++ /dev/null @@ -1,64 +0,0 @@ -package org.exparity.hamcrest.date.core.wrapper; - -import java.time.LocalDate; -import java.time.ZoneId; -import java.time.temporal.ChronoField; -import java.util.function.ToIntFunction; - -import org.exparity.hamcrest.date.core.TemporalFieldWrapper; - -/** - * Implementation of {@link TemporalFieldWrapper} which wraps a temporal field of a {@link LocalDate} instance. - * - * @author Thomas Naskali - */ -public class FieldLocalDateWrapper implements TemporalFieldWrapper { - - private final ToIntFunction wrapped; - private final ChronoField field; - private final ZoneId zone; - - private FieldLocalDateWrapper(final ToIntFunction wrapped, final ChronoField field, final ZoneId zone) { - this.wrapped = wrapped; - this.field = field; - this.zone = zone; - } - - public FieldLocalDateWrapper(final int value, final ChronoField field) { - this.wrapped = (ignored) -> value; - this.field = field; - this.zone = ZoneId.systemDefault(); - } - - public FieldLocalDateWrapper(final LocalDate date, final ChronoField field) { - this.wrapped = z -> date.atStartOfDay(z).get(field); - this.field = field; - this.zone = ZoneId.systemDefault(); - } - - @Override - public boolean isAfter(final LocalDate other) { - return wrapped.applyAsInt(zone) > other.atStartOfDay(zone).get(field); - } - - @Override - public boolean isBefore(final LocalDate other) { - return wrapped.applyAsInt(zone) < other.atStartOfDay(zone).get(field); - } - - @Override - public boolean isSame(final LocalDate other) { - return wrapped.applyAsInt(zone) == other.atStartOfDay(zone).get(field); - } - - @Override - public int unwrap() { - return wrapped.applyAsInt(zone); - } - - @Override - public TemporalFieldWrapper withZone(final ZoneId zone) { - return new FieldLocalDateWrapper(wrapped, field, zone); - } - -} diff --git a/src/main/java/org/exparity/hamcrest/date/core/wrapper/FieldLocalTimeWrapper.java b/src/main/java/org/exparity/hamcrest/date/core/wrapper/FieldLocalTimeWrapper.java deleted file mode 100644 index 569c780..0000000 --- a/src/main/java/org/exparity/hamcrest/date/core/wrapper/FieldLocalTimeWrapper.java +++ /dev/null @@ -1,64 +0,0 @@ -package org.exparity.hamcrest.date.core.wrapper; - -import java.time.LocalTime; -import java.time.ZoneId; -import java.time.temporal.ChronoField; -import java.util.function.ToIntFunction; - -import org.exparity.hamcrest.date.core.TemporalFieldWrapper; - -/** - * Implementation of {@link TemporalFieldWrapper} which wraps a temporal field of a {@link LocalTime} instance. - * - * @author Thomas Naskali - */ -public class FieldLocalTimeWrapper implements TemporalFieldWrapper { - - private final ToIntFunction wrapped; - private final ChronoField field; - private final ZoneId zone; - - private FieldLocalTimeWrapper(final ToIntFunction wrapped, final ChronoField field, final ZoneId zone) { - this.wrapped = wrapped; - this.field = field; - this.zone = zone; - } - - public FieldLocalTimeWrapper(final int value, final ChronoField field) { - this.wrapped = (ignored) -> value; - this.field = field; - this.zone = ZoneId.systemDefault(); - } - - public FieldLocalTimeWrapper(final LocalTime date, final ChronoField field) { - this.wrapped = (ignored) -> date.get(field); - this.field = field; - this.zone = ZoneId.systemDefault(); - } - - @Override - public boolean isAfter(final LocalTime other) { - return wrapped.applyAsInt(zone) > other.get(field); - } - - @Override - public boolean isBefore(final LocalTime other) { - return wrapped.applyAsInt(zone) < other.get(field); - } - - @Override - public boolean isSame(final LocalTime other) { - return wrapped.applyAsInt(zone) == other.get(field); - } - - @Override - public int unwrap() { - return wrapped.applyAsInt(zone); - } - - @Override - public TemporalFieldWrapper withZone(final ZoneId zone) { - return new FieldLocalTimeWrapper(wrapped, field, zone); - } - -} diff --git a/src/main/java/org/exparity/hamcrest/date/core/wrapper/FieldSqlDateAdaptor.java b/src/main/java/org/exparity/hamcrest/date/core/wrapper/FieldSqlDateAdaptor.java deleted file mode 100644 index a837b88..0000000 --- a/src/main/java/org/exparity/hamcrest/date/core/wrapper/FieldSqlDateAdaptor.java +++ /dev/null @@ -1,24 +0,0 @@ -package org.exparity.hamcrest.date.core.wrapper; - -import java.sql.Date; -import java.time.ZoneId; -import java.time.temporal.ChronoField; - -import org.exparity.hamcrest.date.core.TemporalFieldAdapter; - -/** - * Implementation of a {@link TemporalFieldAdapter} for {@link Date} - */ -public class FieldSqlDateAdaptor implements TemporalFieldAdapter { - - private final ChronoField field; - - public FieldSqlDateAdaptor(ChronoField field) { - this.field = field; - } - - @Override - public int asTemporalField(Date source, ZoneId zone) { - return source.toLocalDate().get(field); - } -} \ No newline at end of file diff --git a/src/main/java/org/exparity/hamcrest/date/core/wrapper/FieldSqlDateRangeAdaptor.java b/src/main/java/org/exparity/hamcrest/date/core/wrapper/FieldSqlDateRangeAdaptor.java deleted file mode 100644 index 96dc742..0000000 --- a/src/main/java/org/exparity/hamcrest/date/core/wrapper/FieldSqlDateRangeAdaptor.java +++ /dev/null @@ -1,25 +0,0 @@ -package org.exparity.hamcrest.date.core.wrapper; - -import java.sql.Date; -import java.time.ZoneId; -import java.time.temporal.ChronoField; -import java.time.temporal.ValueRange; - -import org.exparity.hamcrest.date.core.TemporalFieldRangeAdapter; - -/** - * Implementation of a {@link TemporalFieldRangeAdapter} for {@link Date} - */ -public class FieldSqlDateRangeAdaptor implements TemporalFieldRangeAdapter { - - private final ChronoField field; - - public FieldSqlDateRangeAdaptor(ChronoField field) { - this.field = field; - } - - @Override - public ValueRange asTemporalFieldRange(Date source, ZoneId zone) { - return field.rangeRefinedBy(source.toLocalDate()); - } -} \ No newline at end of file diff --git a/src/main/java/org/exparity/hamcrest/date/core/wrapper/FieldSqlDateWrapper.java b/src/main/java/org/exparity/hamcrest/date/core/wrapper/FieldSqlDateWrapper.java deleted file mode 100644 index d54ddab..0000000 --- a/src/main/java/org/exparity/hamcrest/date/core/wrapper/FieldSqlDateWrapper.java +++ /dev/null @@ -1,63 +0,0 @@ -package org.exparity.hamcrest.date.core.wrapper; - -import java.sql.Date; -import java.time.ZoneId; -import java.time.temporal.ChronoField; -import java.util.function.ToIntFunction; - -import org.exparity.hamcrest.date.core.TemporalFieldWrapper; - -/** - * Implementation of {@link TemporalFieldWrapper} which wraps a temporal field of a {@link Date} instance. - * - * @author Thomas Naskali - */ -public class FieldSqlDateWrapper implements TemporalFieldWrapper { - - private final ToIntFunction wrapped; - private final ChronoField field; - private final ZoneId zone; - - private FieldSqlDateWrapper(final ToIntFunction wrapped, final ChronoField field, final ZoneId zone) { - this.wrapped = wrapped; - this.field = field; - this.zone = zone; - } - - public FieldSqlDateWrapper(final int value, final ChronoField field) { - this((ignored) -> value, field, ZoneId.systemDefault()); - } - - public FieldSqlDateWrapper(Date date, ChronoField field) { - this(z -> date.toLocalDate().get(field), field, ZoneId.systemDefault()); - } - - public FieldSqlDateWrapper(java.util.Date date, ChronoField field) { - this(z -> date.toInstant().atZone(z).get(field), field, ZoneId.systemDefault()); - } - - @Override - public boolean isAfter(Date other) { - return wrapped.applyAsInt(zone) > other.toLocalDate().get(field); - } - - @Override - public boolean isBefore(Date other) { - return wrapped.applyAsInt(zone) < other.toLocalDate().get(field); - } - - @Override - public boolean isSame(Date other) { - return wrapped.applyAsInt(zone) == other.toLocalDate().get(field); - } - - @Override - public int unwrap() { - return wrapped.applyAsInt(zone); - } - - @Override - public TemporalFieldWrapper withZone(ZoneId zone) { - return new FieldSqlDateWrapper(wrapped, field, zone); - } -} diff --git a/src/main/java/org/exparity/hamcrest/date/core/wrapper/FieldZonedDateTimeWrapper.java b/src/main/java/org/exparity/hamcrest/date/core/wrapper/FieldZonedDateTimeWrapper.java deleted file mode 100644 index dde3d4c..0000000 --- a/src/main/java/org/exparity/hamcrest/date/core/wrapper/FieldZonedDateTimeWrapper.java +++ /dev/null @@ -1,64 +0,0 @@ -package org.exparity.hamcrest.date.core.wrapper; - -import java.time.ZoneId; -import java.time.ZonedDateTime; -import java.time.temporal.ChronoField; -import java.util.function.ToIntFunction; - -import org.exparity.hamcrest.date.core.TemporalFieldWrapper; - -/** - * Implementation of {@link TemporalFieldWrapper} which wraps a temporal field of a {@link ZonedDateTime} instance. - * - * @author Thomas Naskali - */ -public class FieldZonedDateTimeWrapper implements TemporalFieldWrapper { - - private final ToIntFunction wrapped; - private final ChronoField field; - private final ZoneId zone; - - private FieldZonedDateTimeWrapper(final ToIntFunction wrapped, final ChronoField field, final ZoneId zone) { - this.wrapped = wrapped; - this.field = field; - this.zone = zone; - } - - public FieldZonedDateTimeWrapper(final int value, final ChronoField field) { - this.wrapped = (ignored) -> value; - this.field = field; - this.zone = ZoneId.systemDefault(); - } - - public FieldZonedDateTimeWrapper(final ZonedDateTime date, final ChronoField field) { - this.wrapped = z -> date.withZoneSameInstant(z).get(field); - this.field = field; - this.zone = ZoneId.systemDefault(); - } - - @Override - public boolean isAfter(final ZonedDateTime other) { - return wrapped.applyAsInt(zone) > other.withZoneSameInstant(zone).get(field); - } - - @Override - public boolean isBefore(final ZonedDateTime other) { - return wrapped.applyAsInt(zone) < other.withZoneSameInstant(zone).get(field); - } - - @Override - public boolean isSame(final ZonedDateTime other) { - return wrapped.applyAsInt(zone) == other.withZoneSameInstant(zone).get(field); - } - - @Override - public int unwrap() { - return wrapped.applyAsInt(zone); - } - - @Override - public TemporalFieldWrapper withZone(final ZoneId zone) { - return new FieldZonedDateTimeWrapper(wrapped, field, zone); - } - -} diff --git a/src/main/java/org/exparity/hamcrest/date/core/wrapper/LocalDateTimeWrapper.java b/src/main/java/org/exparity/hamcrest/date/core/wrapper/LocalDateTimeWrapper.java deleted file mode 100644 index 596994d..0000000 --- a/src/main/java/org/exparity/hamcrest/date/core/wrapper/LocalDateTimeWrapper.java +++ /dev/null @@ -1,100 +0,0 @@ -package org.exparity.hamcrest.date.core.wrapper; - -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.Month; -import java.time.ZoneId; -import java.time.temporal.ChronoUnit; -import java.time.temporal.TemporalUnit; -import java.util.Date; -import java.util.function.Supplier; - -import org.exparity.hamcrest.date.core.TemporalWrapper; - -/** - * Implementation of a {@link TemporalWrapper} which wraps a - * {@link LocalDateTime} instance - * - * @author Stewart Bissett - */ -public class LocalDateTimeWrapper implements TemporalWrapper { - - private final Supplier wrapped; - private final ZoneId zone; - private final TemporalUnit accuracy; - - private LocalDateTimeWrapper(final Supplier wrapped, final ZoneId zone, final TemporalUnit accuracy) { - this.wrapped = wrapped; - this.zone = zone; - this.accuracy = accuracy; - } - - public LocalDateTimeWrapper(final Date date) { - this(date, ChronoUnit.MILLIS); - } - - public LocalDateTimeWrapper(final Date date, final TemporalUnit accuracy) { - this.zone = ZoneId.systemDefault(); - this.wrapped = () -> date.toInstant().atZone(zone).toLocalDateTime(); - this.accuracy = accuracy; - } - - public LocalDateTimeWrapper(final LocalDateTime date) { - this(date, ChronoUnit.NANOS); - } - - public LocalDateTimeWrapper(final LocalDateTime date, final TemporalUnit accuracy) { - this.zone = ZoneId.systemDefault(); - this.wrapped = () -> date; - this.accuracy = accuracy; - } - - public LocalDateTimeWrapper(final int year, final Month month, final int dayOfMonth) { - this.zone = ZoneId.systemDefault(); - this.wrapped = () -> LocalDate.of(year, month, dayOfMonth).atStartOfDay(); - this.accuracy = ChronoUnit.DAYS; - } - - public LocalDateTimeWrapper(final int year, final Month month, final int dayOfMonth, final int hour, final int minute, final int second) { - this.zone = ZoneId.systemDefault(); - this.wrapped = () -> LocalDateTime.of(year, month, dayOfMonth, hour, minute, second); - this.accuracy = ChronoUnit.SECONDS; - } - - public LocalDateTimeWrapper(final int year, final Month month, final int dayOfMonth, final int hour, final int minute, final int second, final int nanos) { - this.zone = ZoneId.systemDefault(); - this.wrapped = () -> LocalDateTime.of(year, month, dayOfMonth, hour, minute, second, nanos); - this.accuracy = ChronoUnit.NANOS; - } - - @Override - public long difference(final LocalDateTime other, final ChronoUnit unit) { - return Math.abs(wrapped.get().truncatedTo(accuracy).until(other, unit)); - } - - @Override - public boolean isAfter(final LocalDateTime other) { - return wrapped.get().truncatedTo(accuracy).isAfter(other.truncatedTo(accuracy)); - } - - @Override - public boolean isBefore(final LocalDateTime other) { - return wrapped.get().truncatedTo(accuracy).isBefore(other.truncatedTo(accuracy)); - } - - @Override - public boolean isSame(final LocalDateTime other) { - return wrapped.get().truncatedTo(accuracy).isEqual(other.truncatedTo(accuracy)); - } - - @Override - public LocalDateTime unwrap() { - return wrapped.get(); - } - - @Override - public LocalDateTimeWrapper withZone(final ZoneId zone) { - return new LocalDateTimeWrapper(wrapped, zone, accuracy); - } - -} diff --git a/src/main/java/org/exparity/hamcrest/date/core/wrapper/LocalDateWrapper.java b/src/main/java/org/exparity/hamcrest/date/core/wrapper/LocalDateWrapper.java deleted file mode 100644 index 36c2bdd..0000000 --- a/src/main/java/org/exparity/hamcrest/date/core/wrapper/LocalDateWrapper.java +++ /dev/null @@ -1,73 +0,0 @@ -package org.exparity.hamcrest.date.core.wrapper; - -import java.time.LocalDate; -import java.time.ZoneId; -import java.time.temporal.ChronoUnit; -import java.util.Date; -import java.util.function.Supplier; - -import org.exparity.hamcrest.date.core.TemporalWrapper; - -/** - * Implementation of a {@link TemporalWrapper} which wraps a {@link LocalDate} - * instance - * - * @author Stewart Bissett - */ -public class LocalDateWrapper implements TemporalWrapper { - - private final Supplier wrapped; - private final ZoneId zone; - - private LocalDateWrapper(final Supplier wrapped, final ZoneId zone) { - this.wrapped = wrapped; - this.zone = zone; - } - - public LocalDateWrapper(final Date date) { - this.zone = ZoneId.systemDefault(); - this.wrapped = () -> date.toInstant().atZone(zone).toLocalDate(); - } - - public LocalDateWrapper(final LocalDate date) { - this.zone = ZoneId.systemDefault(); - this.wrapped = () -> date; - } - - @Override - public long difference(final LocalDate other, ChronoUnit unit) { - return Math.abs(wrapped.get().until(other, unit)); - } - - @Override - public boolean isAfter(final LocalDate other) { - return wrapped.get().isAfter(other); - } - - @Override - public boolean isBefore(final LocalDate other) { - return wrapped.get().isBefore(other); - } - - @Override - public boolean isSame(final LocalDate other) { - return wrapped.get().isEqual(other); - } - - - @Override - public LocalDate unwrap() { - return wrapped.get(); - } - - @Override - public String toString() { - return wrapped.get().toString(); - } - - @Override - public TemporalWrapper withZone(final ZoneId zone) { - return new LocalDateWrapper(this.wrapped, zone); - } - -} diff --git a/src/main/java/org/exparity/hamcrest/date/core/wrapper/LocalTimeWrapper.java b/src/main/java/org/exparity/hamcrest/date/core/wrapper/LocalTimeWrapper.java deleted file mode 100644 index 5ed9e7e..0000000 --- a/src/main/java/org/exparity/hamcrest/date/core/wrapper/LocalTimeWrapper.java +++ /dev/null @@ -1,57 +0,0 @@ -package org.exparity.hamcrest.date.core.wrapper; - -import java.time.LocalTime; -import java.time.ZoneId; -import java.time.temporal.ChronoUnit; - -import org.exparity.hamcrest.date.core.TemporalWrapper; - -/** - * Implementation of a {@link TemporalWrapper} which wraps a {@link LocalTime} - * instance - * - * @author Stewart Bissett - */ -public class LocalTimeWrapper implements TemporalWrapper { - - private final LocalTime wrapped; - - public LocalTimeWrapper(final LocalTime date) { - this.wrapped = date; - } - - public LocalTimeWrapper(final int hour, final int minute, final int second) { - this(LocalTime.of(hour, minute, second)); - } - - @Override - public long difference(final LocalTime other, final ChronoUnit unit) { - return Math.abs(wrapped.until(other, unit)); - } - - @Override - public boolean isAfter(final LocalTime other) { - return wrapped.isAfter(other); - } - - @Override - public boolean isBefore(final LocalTime other) { - return wrapped.isBefore(other); - } - - @Override - public boolean isSame(final LocalTime other) { - return wrapped.equals(other); - } - - @Override - public LocalTime unwrap() { - return wrapped; - } - - @Override - public TemporalWrapper withZone(final ZoneId zone) { - return this; - } - -} diff --git a/src/main/java/org/exparity/hamcrest/date/core/wrapper/SqlDateWrapper.java b/src/main/java/org/exparity/hamcrest/date/core/wrapper/SqlDateWrapper.java deleted file mode 100644 index 3dda264..0000000 --- a/src/main/java/org/exparity/hamcrest/date/core/wrapper/SqlDateWrapper.java +++ /dev/null @@ -1,84 +0,0 @@ -package org.exparity.hamcrest.date.core.wrapper; - -import java.sql.Date; -import java.time.LocalDate; -import java.time.Month; -import java.time.ZoneId; -import java.time.temporal.ChronoUnit; -import java.util.function.Function; - -import org.exparity.hamcrest.date.core.TemporalWrapper; - -/** - * Implementation of {@link TemporalWrapper} to wrap {@link Date} objects. - * - * @author Stewart Bissett - */ -public class SqlDateWrapper implements TemporalWrapper { - - private final Function wrapped; - private ZoneId zone; - - public SqlDateWrapper(final LocalDate date) { - this((zoneId) -> date, ZoneId.systemDefault()); - } - - public SqlDateWrapper(final int year, final Month month, final int dayOfMonth) { - this(LocalDate.of(year, month, dayOfMonth)); - } - - public SqlDateWrapper(final Date date) { - this(date.toLocalDate()); - } - - public SqlDateWrapper(final java.util.Date date) { - this(date, ZoneId.systemDefault()); - } - - public SqlDateWrapper(final java.util.Date date, ZoneId zone) { - this((zoneId) -> date.toInstant().atZone(zoneId).toLocalDate(), zone); - } - - private SqlDateWrapper(Function wrapped, ZoneId zone) { - this.wrapped = wrapped; - this.zone = zone; - } - - @Override - public long difference(final Date other, final ChronoUnit unit) { - return Math.abs((thisLocalDate().until(otherLocalDate(other), unit))); - } - - @Override - public boolean isAfter(final Date other) { - return thisLocalDate().isAfter(otherLocalDate(other)); - } - - @Override - public boolean isBefore(final Date other) { - return thisLocalDate().isBefore(otherLocalDate(other)); - } - - @Override - public boolean isSame(final Date other) { - return thisLocalDate().isEqual(otherLocalDate(other)); - } - - @Override - public Date unwrap() { - return Date.valueOf(wrapped.apply(zone)); - } - - @Override - public SqlDateWrapper withZone(final ZoneId zone) { - return new SqlDateWrapper(wrapped, zone); - } - - private LocalDate otherLocalDate(final Date other) { - return other.toLocalDate(); - } - - private LocalDate thisLocalDate() { - return wrapped.apply(zone); - } -} diff --git a/src/main/java/org/exparity/hamcrest/date/core/wrapper/ZonedDateTimeWrapper.java b/src/main/java/org/exparity/hamcrest/date/core/wrapper/ZonedDateTimeWrapper.java deleted file mode 100644 index d26b8f3..0000000 --- a/src/main/java/org/exparity/hamcrest/date/core/wrapper/ZonedDateTimeWrapper.java +++ /dev/null @@ -1,109 +0,0 @@ -package org.exparity.hamcrest.date.core.wrapper; - -import java.time.*; -import java.time.temporal.ChronoUnit; -import java.time.temporal.TemporalUnit; -import java.util.Date; -import java.util.function.Function; - -import org.exparity.hamcrest.date.core.TemporalWrapper; - -/** - * Implementation of a {@link TemporalWrapper} which wraps a - * {@link ZonedDateTime} instance - * - * @author Stewart Bissett - */ -public class ZonedDateTimeWrapper implements TemporalWrapper { - - private final Function wrapped; - private final ZoneId zone; - private final TemporalUnit accuracy; - - private ZonedDateTimeWrapper(final Function wrapped, final ZoneId zone, final TemporalUnit accuracy) { - this.wrapped = wrapped; - this.zone = zone; - this.accuracy = accuracy; - } - - public ZonedDateTimeWrapper(final Date date) { - this(date, ChronoUnit.MILLIS); - } - - public ZonedDateTimeWrapper(final Date date, final TemporalUnit accuracy) { - this.zone = ZoneId.systemDefault(); - this.wrapped = z -> date.toInstant().atZone(z); - this.accuracy = accuracy; - } - - public ZonedDateTimeWrapper(final ZonedDateTime date) { - this(date, ChronoUnit.NANOS); - } - - public ZonedDateTimeWrapper(final ZonedDateTime date, final TemporalUnit accuracy) { - this.zone = ZoneId.systemDefault(); - this.wrapped = date::withZoneSameInstant; - this.accuracy = accuracy; - } - - public ZonedDateTimeWrapper(final LocalDateTime date, final ZoneId zone) { - this(date, zone, ChronoUnit.NANOS); - } - - public ZonedDateTimeWrapper(final LocalDateTime date, final ZoneId zone, final TemporalUnit accuracy) { - this.zone = zone; - this.wrapped = date::atZone; - this.accuracy = accuracy; - } - - public ZonedDateTimeWrapper(final int year, final Month month, final int dayOfMonth, final int hour, - final int minute, final int second, final ZoneId zone) { - this.zone = zone; - this.wrapped = z -> LocalDateTime.of(year, month, dayOfMonth, hour, minute, second).atZone(zone); - this.accuracy = ChronoUnit.SECONDS; - } - - public ZonedDateTimeWrapper(final int year, final Month month, final int dayOfMonth, final int hour, - final int minute, final int second, final int nanos, final ZoneId zone) { - this.zone = zone; - this.wrapped = z -> LocalDateTime.of(year, month, dayOfMonth, hour, minute, second, nanos).atZone(zone); - this.accuracy = ChronoUnit.NANOS; - } - - public ZonedDateTimeWrapper(final int year, final Month month, final int dayOfMonth, final ZoneId zone) { - this.zone = zone; - this.wrapped = z -> LocalDate.of(year, month, dayOfMonth).atStartOfDay().atZone(zone); - this.accuracy = ChronoUnit.DAYS; - } - - @Override - public long difference(final ZonedDateTime other, final ChronoUnit unit) { - return Math.abs(wrapped.apply(zone).truncatedTo(accuracy).toInstant().until(other.toInstant(), unit)); - } - - @Override - public boolean isAfter(final ZonedDateTime other) { - return wrapped.apply(zone).truncatedTo(accuracy).toInstant().isAfter(other.truncatedTo(accuracy).toInstant()); - } - - @Override - public boolean isBefore(final ZonedDateTime other) { - return wrapped.apply(zone).truncatedTo(accuracy).toInstant().isBefore(other.truncatedTo(accuracy).toInstant()); - } - - @Override - public boolean isSame(final ZonedDateTime other) { - return wrapped.apply(zone).truncatedTo(accuracy).toInstant().equals(other.truncatedTo(accuracy).toInstant()); - } - - @Override - public ZonedDateTime unwrap() { - return wrapped.apply(zone); - } - - @Override - public TemporalWrapper withZone(final ZoneId zone) { - return new ZonedDateTimeWrapper(wrapped, zone, accuracy); - } - -} diff --git a/src/test/java/org/exparity/hamcrest/date/core/IsDayOfWeekTest.java b/src/test/java/org/exparity/hamcrest/date/core/IsDayOfWeekTest.java index a8352f2..1c6cc71 100644 --- a/src/test/java/org/exparity/hamcrest/date/core/IsDayOfWeekTest.java +++ b/src/test/java/org/exparity/hamcrest/date/core/IsDayOfWeekTest.java @@ -20,8 +20,8 @@ @SuppressWarnings("deprecation") public class IsDayOfWeekTest { - private static final String ASSERTION_PATTERN = "\\sExpected: the date is on a \\p{IsAlphabetic}+?\\s but: the date is on a \\p{IsAlphabetic}+"; - private static final String ASSERTION_PATTERN_ANYOF = "\\sExpected: \\(the date is on a \\p{IsAlphabetic}+?( or the date is on a \\p{IsAlphabetic}+?)+?\\)\\s but: the date is on a \\p{IsAlphabetic}+"; + private static final String ASSERTION_PATTERN = "\\sExpected: the date is on a [A-Z][a-z]+?\\s but: the date is on a [A-Z][a-z]+"; + private static final String ASSERTION_PATTERN_MULTIPLE_DAYS = "\\sExpected: the date is on a [A-Za-z, ]+?\\s but: the date is on a [A-Z][a-z]+"; // Date Matchers @Test @@ -70,27 +70,27 @@ public void isDateWeekend() { assertThat(JAN_07_2012_11AM_UTC_AS_DATE, DateMatchers.isWeekend()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isDateMondayIsNotWeekend() { assertThat(JAN_02_2012_11AM_UTC_AS_DATE, DateMatchers.isWeekend()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isDateTuesdayIsNotWeekend() { assertThat(JAN_03_2012_11AM_UTC_AS_DATE, DateMatchers.isWeekend()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isDateWednesdayIsNotWeekend() { assertThat(JAN_04_2012_11AM_UTC_AS_DATE, DateMatchers.isWeekend()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isDateThursdayIsNotWeekend() { assertThat(JAN_05_2012_11AM_UTC_AS_DATE, DateMatchers.isWeekend()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isDateFridayIsNotWeekend() { assertThat(JAN_06_2012_11AM_UTC_AS_DATE, DateMatchers.isWeekend()); } @@ -104,12 +104,12 @@ public void isDateWeekday() { assertThat(JAN_06_2012_11AM_UTC_AS_DATE, DateMatchers.isWeekday()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isDateSaturdayIsNotWeekday() { assertThat(JAN_07_2012_11AM_UTC_AS_DATE, DateMatchers.isWeekday()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isDateSundayIsNotWeekday() { assertThat(JAN_01_2012_11AM_UTC_AS_DATE, DateMatchers.isWeekday()); } @@ -211,27 +211,27 @@ public void isSqlDateWeekend() { assertThat(JAN_07_2012_AS_SQL, SqlDateMatchers.isWeekend()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isSqlDateMondayIsNotWeekend() { assertThat(JAN_02_2012_AS_SQL, SqlDateMatchers.isWeekend()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isSqlDateTuesdayIsNotWeekend() { assertThat(JAN_03_2012_AS_SQL, SqlDateMatchers.isWeekend()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isSqlDateWednesdayIsNotWeekend() { assertThat(JAN_04_2012_AS_SQL, SqlDateMatchers.isWeekend()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isSqlDateThursdayIsNotWeekend() { assertThat(JAN_05_2012_AS_SQL, SqlDateMatchers.isWeekend()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isSqlDateFridayIsNotWeekend() { assertThat(JAN_06_2012_AS_SQL, SqlDateMatchers.isWeekend()); } @@ -245,12 +245,12 @@ public void isSqlDateWeekday() { assertThat(JAN_06_2012_AS_SQL, SqlDateMatchers.isWeekday()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isSqlDateSaturdayIsNotWeekday() { assertThat(JAN_07_2012_AS_SQL, SqlDateMatchers.isWeekday()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isSqlDateSundayIsNotWeekday() { assertThat(JAN_01_2012_AS_SQL, SqlDateMatchers.isWeekday()); } @@ -351,27 +351,27 @@ public void isSqlDateWeekendUsingDateMatchers() { assertThat(JAN_07_2012_AS_SQL, DateMatchers.isWeekend()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isSqlDateMondayIsNotWeekendUsingDateMatchers() { assertThat(JAN_02_2012_AS_SQL, DateMatchers.isWeekend()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isSqlDateTuesdayIsNotWeekendUsingDateMatchers() { assertThat(JAN_03_2012_AS_SQL, DateMatchers.isWeekend()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isSqlDateWednesdayIsNotWeekendUsingDateMatchers() { assertThat(JAN_04_2012_AS_SQL, DateMatchers.isWeekend()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isSqlDateThursdayIsNotWeekendUsingDateMatchers() { assertThat(JAN_05_2012_AS_SQL, DateMatchers.isWeekend()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isSqlDateFridayIsNotWeekendUsingDateMatchers() { assertThat(JAN_06_2012_AS_SQL, DateMatchers.isWeekend()); } @@ -385,12 +385,12 @@ public void isSqlDateWeekdayUsingDateMatchers() { assertThat(JAN_06_2012_AS_SQL, DateMatchers.isWeekday()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isSqlDateSaturdayIsNotWeekdayUsingDateMatchers() { assertThat(JAN_07_2012_AS_SQL, DateMatchers.isWeekday()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isSqlDateSundayIsNotWeekdayUsingDateMatchers() { assertThat(JAN_01_2012_AS_SQL, DateMatchers.isWeekday()); } @@ -535,12 +535,12 @@ public void isLocalDateWeekday() { assertThat(AUG_07_2015, LocalDateMatchers.isWeekday()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isLocalDateNotWeekdayOnSaturday() { assertThat(AUG_08_2015, LocalDateMatchers.isWeekday()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isLocalDateNotWeekdayOnSunday() { assertThat(AUG_09_2015, LocalDateMatchers.isWeekday()); } @@ -551,27 +551,27 @@ public void isLocalDateWeekend() { assertThat(AUG_09_2015, LocalDateMatchers.isWeekend()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isLocalDateNotWeekendOnMonday() { assertThat(AUG_03_2015, LocalDateMatchers.isWeekend()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isLocalDateNotWeekendOnTuesday() { assertThat(AUG_04_2015, LocalDateMatchers.isWeekend()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isLocalDateNotWeekendOnWednesday() { assertThat(AUG_05_2015, LocalDateMatchers.isWeekend()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isLocalDateNotWeekendOnThursday() { assertThat(AUG_06_2015, LocalDateMatchers.isWeekend()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isLocalDateNotWeekendOnFriday() { assertThat(AUG_07_2015, LocalDateMatchers.isWeekend()); } @@ -666,12 +666,12 @@ public void isLocalDateTimeWeekday() { assertThat(AUG_07_2015_NOON, LocalDateTimeMatchers.isWeekday()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isLocalDateTimeNotWeekdayOnSaturday() { assertThat(AUG_08_2015_NOON, LocalDateTimeMatchers.isWeekday()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isLocalDateTimeNotWeekdayOnSunday() { assertThat(AUG_09_2015_NOON, LocalDateTimeMatchers.isWeekday()); } @@ -682,27 +682,27 @@ public void isLocalDateTimeWeekend() { assertThat(AUG_09_2015_NOON, LocalDateTimeMatchers.isWeekend()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isLocalDateTimeNotWeekendOnMonday() { assertThat(AUG_03_2015_NOON, LocalDateTimeMatchers.isWeekend()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isLocalDateTimeNotWeekendOnTuesday() { assertThat(AUG_04_2015_NOON, LocalDateTimeMatchers.isWeekend()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isLocalDateTimeNotWeekendOnWednesday() { assertThat(AUG_05_2015_NOON, LocalDateTimeMatchers.isWeekend()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isLocalDateTimeNotWeekendOnThursday() { assertThat(AUG_06_2015_NOON, LocalDateTimeMatchers.isWeekend()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isLocalDateTimeNotWeekendOnFriday() { assertThat(AUG_07_2015_NOON, LocalDateTimeMatchers.isWeekend()); } @@ -797,12 +797,12 @@ public void isZonedDateTimeWeekday() { assertThat(AUG_07_2015_NOON_UTC, ZonedDateTimeMatchers.isWeekday()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isZonedDateTimeNotWeekdayOnSaturday() { assertThat(AUG_08_2015_NOON_UTC, ZonedDateTimeMatchers.isWeekday()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isZonedDateTimeNotWeekdayOnSunday() { assertThat(AUG_09_2015_NOON_UTC, ZonedDateTimeMatchers.isWeekday()); } @@ -813,27 +813,27 @@ public void isZonedDateTimeWeekend() { assertThat(AUG_09_2015_NOON_UTC, ZonedDateTimeMatchers.isWeekend()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isZonedDateTimeNotWeekendOnMonday() { assertThat(AUG_03_2015_NOON_UTC, ZonedDateTimeMatchers.isWeekend()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isZonedDateTimeNotWeekendOnTuesday() { assertThat(AUG_04_2015_NOON_UTC, ZonedDateTimeMatchers.isWeekend()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isZonedDateTimeNotWeekendOnWednesday() { assertThat(AUG_05_2015_NOON_UTC, ZonedDateTimeMatchers.isWeekend()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isZonedDateTimeNotWeekendOnThursday() { assertThat(AUG_06_2015_NOON_UTC, ZonedDateTimeMatchers.isWeekend()); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_ANYOF) + @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN_MULTIPLE_DAYS) public void isZonedDateTimeNotWeekendOnFriday() { assertThat(AUG_07_2015_NOON_UTC, ZonedDateTimeMatchers.isWeekend()); } diff --git a/src/test/java/org/exparity/hamcrest/date/core/IsFirstDayOfMonthTest.java b/src/test/java/org/exparity/hamcrest/date/core/IsFirstDayOfMonthTest.java index 735de21..c5cc38d 100644 --- a/src/test/java/org/exparity/hamcrest/date/core/IsFirstDayOfMonthTest.java +++ b/src/test/java/org/exparity/hamcrest/date/core/IsFirstDayOfMonthTest.java @@ -15,7 +15,7 @@ */ public class IsFirstDayOfMonthTest { - private static final String ASSERTION_PATTERN = "\\sExpected: the date is the first day of the month\\s but: date is the [0-9]+? day of month instead of [0-9]+? day of month"; + private static final String ASSERTION_PATTERN = "\\sExpected: the date is the first day of the month\\s but: date is the [0-9]+? day of the month"; // Date Matchers @Test diff --git a/src/test/java/org/exparity/hamcrest/date/core/IsHourTest.java b/src/test/java/org/exparity/hamcrest/date/core/IsHourTest.java index 108a60a..144d6ee 100644 --- a/src/test/java/org/exparity/hamcrest/date/core/IsHourTest.java +++ b/src/test/java/org/exparity/hamcrest/date/core/IsHourTest.java @@ -34,7 +34,7 @@ public void isDateNotHour() { assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.isHour(11).atZone(UTC)); } - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = DateMatchers.UNSUPPORTED_SQL_DATE_UNIT) + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isSqlDateHour() { assertThat(AUG_04_2015_AS_SQL, DateMatchers.isHour(12).atZone(UTC)); } @@ -49,7 +49,7 @@ public void isDateNotSameHour() { assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.sameHour(11).atZone(UTC)); } - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = DateMatchers.UNSUPPORTED_SQL_DATE_UNIT) + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isSqlDateSameHour() { assertThat(AUG_04_2015_AS_SQL, DateMatchers.sameHour(12).atZone(UTC)); } diff --git a/src/test/java/org/exparity/hamcrest/date/core/IsLastDayOfMonthTest.java b/src/test/java/org/exparity/hamcrest/date/core/IsLastDayOfMonthTest.java index c76f699..2323fe1 100644 --- a/src/test/java/org/exparity/hamcrest/date/core/IsLastDayOfMonthTest.java +++ b/src/test/java/org/exparity/hamcrest/date/core/IsLastDayOfMonthTest.java @@ -15,7 +15,7 @@ */ public class IsLastDayOfMonthTest { - private static final String ASSERTION_PATTERN = "\\sExpected: the date is the last day of the month\\s but: date is the [0-9]+? day of month instead of [0-9]+? day of month"; + private static final String ASSERTION_PATTERN = "\\sExpected: the date is the last day of the month\\s but: date is the [0-9]+? day of the month"; // Date Matchers @Test diff --git a/src/test/java/org/exparity/hamcrest/date/core/IsLeapYearTest.java b/src/test/java/org/exparity/hamcrest/date/core/IsLeapYearTest.java index 404fda4..5497c14 100644 --- a/src/test/java/org/exparity/hamcrest/date/core/IsLeapYearTest.java +++ b/src/test/java/org/exparity/hamcrest/date/core/IsLeapYearTest.java @@ -19,7 +19,7 @@ */ public class IsLeapYearTest { - private static final String ASSERTION_PATTERN = "\\sExpected: a leap year\\s but: the date (?s:.)+? is not a leap year"; + private static final String ASSERTION_PATTERN = "\\sExpected: a leap year\\s but: the year (?s:.)+? is not a leap year"; // Date Matchers @Test diff --git a/src/test/java/org/exparity/hamcrest/date/core/IsMaximumTest.java b/src/test/java/org/exparity/hamcrest/date/core/IsMaximumTest.java index a3f9323..c530530 100644 --- a/src/test/java/org/exparity/hamcrest/date/core/IsMaximumTest.java +++ b/src/test/java/org/exparity/hamcrest/date/core/IsMaximumTest.java @@ -19,7 +19,7 @@ */ public class IsMaximumTest { - private static final String ASSERTION_PATTERN = "\\sExpected: the date is the maximum value for [\\w ]+?\\s but: date is the [0-9]+? [\\w ]+? instead of [0-9]+? [\\w ]+"; + private static final String ASSERTION_PATTERN = "\\sExpected: the date has the maximum value for [\\w ]+?\\s but: date has the value [0-9]+? instead of [0-9]+?"; // Date Matchers @Test diff --git a/src/test/java/org/exparity/hamcrest/date/core/IsMillisecondTest.java b/src/test/java/org/exparity/hamcrest/date/core/IsMillisecondTest.java index 5fca260..39f0951 100644 --- a/src/test/java/org/exparity/hamcrest/date/core/IsMillisecondTest.java +++ b/src/test/java/org/exparity/hamcrest/date/core/IsMillisecondTest.java @@ -27,7 +27,7 @@ public void isDateNotMillisecond() { assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.isMillisecond(1)); } - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = DateMatchers.UNSUPPORTED_SQL_DATE_UNIT) + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isSqlDateMillisecond() { assertThat(AUG_04_2015_AS_SQL, DateMatchers.isMillisecond(0)); } @@ -42,7 +42,7 @@ public void isDateNotSameMillisecond() { assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.sameMillisecond(1)); } - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = DateMatchers.UNSUPPORTED_SQL_DATE_UNIT) + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isSqlDateSameMillisecond() { assertThat(AUG_04_2015_AS_SQL, DateMatchers.sameMillisecond(0)); } diff --git a/src/test/java/org/exparity/hamcrest/date/core/IsMinimumTest.java b/src/test/java/org/exparity/hamcrest/date/core/IsMinimumTest.java index b2eb423..30f8422 100644 --- a/src/test/java/org/exparity/hamcrest/date/core/IsMinimumTest.java +++ b/src/test/java/org/exparity/hamcrest/date/core/IsMinimumTest.java @@ -19,7 +19,7 @@ */ public class IsMinimumTest { - private static final String ASSERTION_PATTERN = "\\sExpected: the date is the minimum value for [\\w ]+?\\s but: date is the [0-9]+? [\\w ]+? instead of [0-9]+? [\\w ]+?"; + private static final String ASSERTION_PATTERN = "\\sExpected: the date has the minimum value for [\\w ]+?\\s but: date has the value [0-9]+? instead of [0-9]+?"; // Date Matchers @Test diff --git a/src/test/java/org/exparity/hamcrest/date/core/IsMinuteTest.java b/src/test/java/org/exparity/hamcrest/date/core/IsMinuteTest.java index ea2892f..9a65e13 100644 --- a/src/test/java/org/exparity/hamcrest/date/core/IsMinuteTest.java +++ b/src/test/java/org/exparity/hamcrest/date/core/IsMinuteTest.java @@ -32,7 +32,7 @@ public void isDateNotMinute() { assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.isMinute(1)); } - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = DateMatchers.UNSUPPORTED_SQL_DATE_UNIT) + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isSqlDateMinute() { assertThat(AUG_04_2015_AS_SQL, DateMatchers.isMinute(0)); } @@ -47,7 +47,7 @@ public void isDateNotSameMinute() { assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.sameMinute(1)); } - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = DateMatchers.UNSUPPORTED_SQL_DATE_UNIT) + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isSqlDateSameMinute() { assertThat(AUG_04_2015_AS_SQL, DateMatchers.sameMinute(0)); } diff --git a/src/test/java/org/exparity/hamcrest/date/core/IsMonthTest.java b/src/test/java/org/exparity/hamcrest/date/core/IsMonthTest.java index b781935..ecf2c51 100644 --- a/src/test/java/org/exparity/hamcrest/date/core/IsMonthTest.java +++ b/src/test/java/org/exparity/hamcrest/date/core/IsMonthTest.java @@ -19,7 +19,7 @@ */ public class IsMonthTest { - private static final String ASSERTION_PATTERN = "\\sExpected: the date has the month [0-9]+?\\s but: the date has the month [0-9]+?"; + private static final String ASSERTION_PATTERN = "\\sExpected: the date has the month [A-Z][a-z]+?\\s but: the date has the month [A-Z][a-z]+?"; // Date Matchers @Test diff --git a/src/test/java/org/exparity/hamcrest/date/core/IsSameDayTest.java b/src/test/java/org/exparity/hamcrest/date/core/IsSameDayTest.java index 6b57899..f26391e 100644 --- a/src/test/java/org/exparity/hamcrest/date/core/IsSameDayTest.java +++ b/src/test/java/org/exparity/hamcrest/date/core/IsSameDayTest.java @@ -242,5 +242,4 @@ public void isZonedDateTimeSameDayLaterDay() { public void isZonedDateTimeSameDayLaterSameDay() { assertThat(AUG_04_2015_NOON_UTC, ZonedDateTimeMatchers.isDay(2015, AUGUST, 4, UTC).atZone(UTC)); } - } diff --git a/src/test/java/org/exparity/hamcrest/date/core/IsSameHourOfDayTest.java b/src/test/java/org/exparity/hamcrest/date/core/IsSameHourOfDayTest.java index 7ca2082..74d0ea5 100644 --- a/src/test/java/org/exparity/hamcrest/date/core/IsSameHourOfDayTest.java +++ b/src/test/java/org/exparity/hamcrest/date/core/IsSameHourOfDayTest.java @@ -47,13 +47,13 @@ public void isDateSameMonthDifferentDay() { assertThat(other, DateMatchers.sameHour(date)); } - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = DateMatchers.UNSUPPORTED_SQL_DATE_UNIT) + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isSqlDateSameHour() { Date date = new Date(), other = new java.sql.Date(date.getTime()); assertThat(other, DateMatchers.sameHour(date)); } - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = DateMatchers.UNSUPPORTED_SQL_DATE_UNIT) + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isDateSameHourSqlDate() { Date date = new Date(); java.sql.Date other = new java.sql.Date(date.getTime()); @@ -88,7 +88,7 @@ public void isDateSameHourOfDayMidnightLocallyDifferentTimeZoneHigherOffsetPersp assertThat(JAN_01_2012_MIDNIGHT_GMT_AS_DATE, DateMatchers.sameHourOfDay(JAN_01_2012_MIDNIGHT_CET_AS_DATE).atZone(ZoneIds.CET)); } - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = DateMatchers.UNSUPPORTED_SQL_DATE_UNIT) + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isDateSameHourOfDaySqlDate() { Date date = new Date(); java.sql.Date other = new java.sql.Date(date.getTime()); diff --git a/src/test/java/org/exparity/hamcrest/date/core/IsSameInstantTest.java b/src/test/java/org/exparity/hamcrest/date/core/IsSameInstantTest.java index 6443515..e01c706 100644 --- a/src/test/java/org/exparity/hamcrest/date/core/IsSameInstantTest.java +++ b/src/test/java/org/exparity/hamcrest/date/core/IsSameInstantTest.java @@ -98,32 +98,32 @@ public void isDateSameInstantSameDeprecatedDateValue() { // java.sql.Date Matchers - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = DateMatchers.UNSUPPORTED_SQL_DATE_UNIT) + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isDateSameInstantEarlierSqlDate() { assertThat(JUN_15_2012_11PM_UTC_AS_DATE, DateMatchers.sameInstant(JUN_14_2012_AS_SQL).atZone(UTC)); } - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = DateMatchers.UNSUPPORTED_SQL_DATE_UNIT) + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isDateSameInstantLaterSqlDate() { assertThat(JUN_15_2012_11AM_UTC_AS_DATE, DateMatchers.sameInstant(JUN_16_2012_AS_SQL).atZone(UTC)); } - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = DateMatchers.UNSUPPORTED_SQL_DATE_UNIT) + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isDateSameInstantSameSqlDate() { assertThat(JUN_15_2012_11PM_UTC_AS_DATE, DateMatchers.sameInstant(JUN_15_2012_AS_SQL).atZone(UTC)); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isSqlDateSameInstantLaterDate() { assertThat(AUG_04_2015_AS_SQL, DateMatchers.sameInstant(AUG_05_2015_NOON_UTC_AS_DATE).atZone(UTC)); } - @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isSqlDateSameInstantEarlierDate() { assertThat(AUG_04_2015_AS_SQL, DateMatchers.sameInstant(AUG_03_2015_NOON_UTC_AS_DATE).atZone(UTC)); } - @Test + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isSqlDateSameInstantSameDate() { assertThat(AUG_04_2015_AS_SQL, DateMatchers.sameInstant(AUG_04_2015_NOON_UTC_AS_DATE).atZone(UTC)); } diff --git a/src/test/java/org/exparity/hamcrest/date/core/IsSameMillisecondOfSecondTest.java b/src/test/java/org/exparity/hamcrest/date/core/IsSameMillisecondOfSecondTest.java index d62dd32..3aab985 100644 --- a/src/test/java/org/exparity/hamcrest/date/core/IsSameMillisecondOfSecondTest.java +++ b/src/test/java/org/exparity/hamcrest/date/core/IsSameMillisecondOfSecondTest.java @@ -38,13 +38,13 @@ public void isDateSameMillisecondDifferentSecond() { assertThat(other, DateMatchers.sameMillisecond(date)); } - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = DateMatchers.UNSUPPORTED_SQL_DATE_UNIT) + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isSqlDateSameMillisecond() { Date date = new Date(), other = new java.sql.Date(date.getTime()); assertThat(other, DateMatchers.sameMillisecond(date)); } - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = DateMatchers.UNSUPPORTED_SQL_DATE_UNIT) + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isDateSameMillisecondSqlDate() { Date date = new Date(); java.sql.Date other = new java.sql.Date(date.getTime()); @@ -69,13 +69,13 @@ public void isDateSameMillisecondOfSecondDifferentSecond() { assertThat(other, DateMatchers.sameMillisecondOfSecond(date)); } - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = DateMatchers.UNSUPPORTED_SQL_DATE_UNIT) + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isSqlDateSameMillisecondOfSecond() { Date date = new Date(), other = new java.sql.Date(date.getTime()); assertThat(other, DateMatchers.sameMillisecondOfSecond(date)); } - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = DateMatchers.UNSUPPORTED_SQL_DATE_UNIT) + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isDateSameMillisecondOfSecondSqlDate() { Date date = new Date(); java.sql.Date other = new java.sql.Date(date.getTime()); diff --git a/src/test/java/org/exparity/hamcrest/date/core/IsSameMinuteOfHourTest.java b/src/test/java/org/exparity/hamcrest/date/core/IsSameMinuteOfHourTest.java index e210a18..45dc8fe 100644 --- a/src/test/java/org/exparity/hamcrest/date/core/IsSameMinuteOfHourTest.java +++ b/src/test/java/org/exparity/hamcrest/date/core/IsSameMinuteOfHourTest.java @@ -44,13 +44,13 @@ public void isDateSameMonthDifferentHour() { assertThat(other, DateMatchers.sameMinute(date)); } - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = DateMatchers.UNSUPPORTED_SQL_DATE_UNIT) + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isSqlDateSameMinute() { Date date = new Date(), other = new java.sql.Date(date.getTime()); assertThat(other, DateMatchers.sameMinute(date)); } - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = DateMatchers.UNSUPPORTED_SQL_DATE_UNIT) + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isDateSameMinuteSqlDate() { Date date = new Date(); java.sql.Date other = new java.sql.Date(date.getTime()); @@ -75,13 +75,13 @@ public void isDateSameMinuteOfHourDifferentHour() { assertThat(other, DateMatchers.sameMinuteOfHour(date)); } - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = DateMatchers.UNSUPPORTED_SQL_DATE_UNIT) + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isSqlDateSameMinuteOfHour() { Date date = new Date(), other = new java.sql.Date(date.getTime()); assertThat(other, DateMatchers.sameMinuteOfHour(date)); } - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = DateMatchers.UNSUPPORTED_SQL_DATE_UNIT) + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isSameMinuteOfHourSqlDate() { Date date = new Date(); java.sql.Date other = new java.sql.Date(date.getTime()); diff --git a/src/test/java/org/exparity/hamcrest/date/core/IsSameMonthOfYearTest.java b/src/test/java/org/exparity/hamcrest/date/core/IsSameMonthOfYearTest.java index da4596d..269fe96 100644 --- a/src/test/java/org/exparity/hamcrest/date/core/IsSameMonthOfYearTest.java +++ b/src/test/java/org/exparity/hamcrest/date/core/IsSameMonthOfYearTest.java @@ -20,7 +20,7 @@ @SuppressWarnings("deprecation") public class IsSameMonthOfYearTest { - private static final String ASSERTION_PATTERN = "\\sExpected: the date has the month [0-9]+?\\s but: the date has the month [0-9]+?"; + private static final String ASSERTION_PATTERN = "\\sExpected: the date has the month [A-Z][a-z]+?\\s but: the date has the month [A-Z][a-z]+?"; // Date Matchers @Test diff --git a/src/test/java/org/exparity/hamcrest/date/core/IsSameOrAfterTest.java b/src/test/java/org/exparity/hamcrest/date/core/IsSameOrAfterTest.java index 2ae3eb7..c71e6b9 100644 --- a/src/test/java/org/exparity/hamcrest/date/core/IsSameOrAfterTest.java +++ b/src/test/java/org/exparity/hamcrest/date/core/IsSameOrAfterTest.java @@ -128,9 +128,9 @@ public void isSqlDateSameOrAfterLaterDate() { assertThat(JUN_15_2012_11AM_UTC_AS_DATE, DateMatchers.sameOrAfter(JUN_16_2012_11PM_UTC_AS_DATE).atZone(UTC)); } - @Test + @Test public void isSqlDateSameOrAfterSameDate() { - assertThat(JUN_15_2012_AS_SQL, DateMatchers.sameOrAfter(JUN_15_2012_11PM_UTC_AS_DATE).atZone(UTC)); + assertThat(JUN_15_2012_AS_SQL, DateMatchers.sameOrAfter(JUN_15_2012_11AM_UTC_AS_DATE).atZone(UTC)); } @Test @@ -178,17 +178,17 @@ public void isSqlDateSameOrAfterSameDayMonthYear() { assertThat(JUN_15_2012_AS_SQL, DateMatchers.sameOrAfter(new DayMonthYear(15, Months.JUNE, 2012)).atZone(UTC)); } - @Test + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isSqlDateSameOrAfterEarlierDateTime() { assertThat(JUN_15_2012_AS_SQL, DateMatchers.sameOrAfter(2012, Months.JUNE, 15, 10, 59, 59).atZone(UTC)); } - @Test + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isSqlDateSameOrAfterLaterDateTimeOnSameDay() { assertThat(JUN_15_2012_AS_SQL, DateMatchers.sameOrAfter(2012, Months.JUNE, 15, 11, 0, 1).atZone(UTC)); } - @Test + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isSqlDateSameOrAfterSameDateTime() { assertThat(JUN_15_2012_AS_SQL, DateMatchers.sameOrAfter(2012, Months.JUNE, 15, 11, 0, 0).atZone(UTC)); } diff --git a/src/test/java/org/exparity/hamcrest/date/core/IsSameOrBeforeTest.java b/src/test/java/org/exparity/hamcrest/date/core/IsSameOrBeforeTest.java index d090f9b..bebcdbe 100644 --- a/src/test/java/org/exparity/hamcrest/date/core/IsSameOrBeforeTest.java +++ b/src/test/java/org/exparity/hamcrest/date/core/IsSameOrBeforeTest.java @@ -177,17 +177,17 @@ public void isSqlDateSameOrBeforeSameDayMonthYear() { assertThat(JUN_15_2012_AS_SQL, DateMatchers.sameOrBefore(new DayMonthYear(15, Months.JUNE, 2012)).atZone(UTC)); } - @Test + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isSqlDateSameOrBeforeLaterDateTimeOnSameDay() { assertThat(JUN_15_2012_AS_SQL, DateMatchers.sameOrBefore(2012, Months.JUNE, 15, 10, 59, 59).atZone(UTC)); } - @Test + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isSqlDateSameOrBeforeDateTime() { assertThat(JUN_15_2012_AS_SQL, DateMatchers.sameOrBefore(2012, Months.JUNE, 15, 11, 0, 1).atZone(UTC)); } - @Test + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isSqlDateSameOrBeforeSameDateTime() { assertThat(JUN_15_2012_AS_SQL, DateMatchers.sameOrBefore(2012, Months.JUNE, 15, 11, 0, 0).atZone(UTC)); } diff --git a/src/test/java/org/exparity/hamcrest/date/core/IsSameSecondOfMinuteTest.java b/src/test/java/org/exparity/hamcrest/date/core/IsSameSecondOfMinuteTest.java index b315e7a..a4b5e84 100644 --- a/src/test/java/org/exparity/hamcrest/date/core/IsSameSecondOfMinuteTest.java +++ b/src/test/java/org/exparity/hamcrest/date/core/IsSameSecondOfMinuteTest.java @@ -44,13 +44,13 @@ public void isDateSameSecondDifferentMinute() { assertThat(other, DateMatchers.sameSecond(date)); } - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = DateMatchers.UNSUPPORTED_SQL_DATE_UNIT) + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isSqlDateSameSecond() { Date date = new Date(), other = new java.sql.Date(date.getTime()); assertThat(other, DateMatchers.sameSecond(date)); } - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = DateMatchers.UNSUPPORTED_SQL_DATE_UNIT) + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isDateSameSecondSqlDate() { Date date = new Date(); java.sql.Date other = new java.sql.Date(date.getTime()); @@ -75,13 +75,13 @@ public void isDateSameSecondOfMinuteDifferentMinute() { assertThat(other, DateMatchers.sameSecondOfMinute(date)); } - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = DateMatchers.UNSUPPORTED_SQL_DATE_UNIT) + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isSqlDateSameSecondOfMinute() { Date date = new Date(), other = new java.sql.Date(date.getTime()); assertThat(other, DateMatchers.sameSecondOfMinute(date)); } - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = DateMatchers.UNSUPPORTED_SQL_DATE_UNIT) + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isDateSameSecondOfMinuteSqlDate() { Date date = new Date(); java.sql.Date other = new java.sql.Date(date.getTime()); diff --git a/src/test/java/org/exparity/hamcrest/date/core/IsSecondTest.java b/src/test/java/org/exparity/hamcrest/date/core/IsSecondTest.java index 6039d77..5cf430e 100644 --- a/src/test/java/org/exparity/hamcrest/date/core/IsSecondTest.java +++ b/src/test/java/org/exparity/hamcrest/date/core/IsSecondTest.java @@ -32,7 +32,7 @@ public void isDateNotSecond() { assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.isSecond(1)); } - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = DateMatchers.UNSUPPORTED_SQL_DATE_UNIT) + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isSqlDateSecond() { assertThat(AUG_04_2015_AS_SQL, DateMatchers.isSecond(0)); } @@ -47,7 +47,7 @@ public void isDateNotSameSecond() { assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.sameSecond(1)); } - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = DateMatchers.UNSUPPORTED_SQL_DATE_UNIT) + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isSqlDateSameSecond() { assertThat(AUG_04_2015_AS_SQL, DateMatchers.sameSecond(0)); } diff --git a/src/test/java/org/exparity/hamcrest/date/core/IsWithinTest.java b/src/test/java/org/exparity/hamcrest/date/core/IsWithinTest.java index 9651b1c..bfa88f4 100644 --- a/src/test/java/org/exparity/hamcrest/date/core/IsWithinTest.java +++ b/src/test/java/org/exparity/hamcrest/date/core/IsWithinTest.java @@ -14,7 +14,6 @@ import org.exparity.hamcrest.date.LocalTimeMatchers; import org.exparity.hamcrest.date.SqlDateMatchers; import org.exparity.hamcrest.date.ZonedDateTimeMatchers; -import org.exparity.hamcrest.date.core.wrapper.DateWrapper; import org.exparity.hamcrest.date.testutils.ZoneIds; import org.testng.annotations.Test; @@ -273,25 +272,25 @@ public void isSqlDateWithinDayMonthYearOutsideLimitUsingDateMatchers() { assertThat(AUG_04_2015_AS_SQL, DateMatchers.within(2, ChronoUnit.DAYS, 2015, Month.AUGUST, 7).atZone(UTC)); } - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = DateWrapper.JAVA_SQL_DATE_UNIT) + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isSqlDateWithinSameDayMonthYearTimeUsingDateMatchers() { assertThat(AUG_04_2015_AS_SQL, DateMatchers.within(2, ChronoUnit.MILLIS, 2015, Month.AUGUST, 4, 12, 0, 0, 0).atZone(UTC)); } - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = DateWrapper.JAVA_SQL_DATE_UNIT) + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isSqlDateWithinDayMonthYearTimeInsideLimitUsingDateMatchers() { assertThat(AUG_04_2015_AS_SQL, DateMatchers.within(2, ChronoUnit.MILLIS, 2015, Month.AUGUST, 4, 12, 0, 0, 1).atZone(UTC)); } - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = DateWrapper.JAVA_SQL_DATE_UNIT) + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isSqlDateWithinDayMonthYearTimeEqualLimitUsingDateMatchers() { assertThat(AUG_04_2015_AS_SQL, DateMatchers.within(2, ChronoUnit.MILLIS, 2015, Month.AUGUST, 4, 12, 0, 0, 2).atZone(UTC)); } - @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = DateWrapper.JAVA_SQL_DATE_UNIT) + @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) public void isSqlDateWithinDayMonthYearTimeOutsideLimitUsingDateMatchers() { assertThat(AUG_04_2015_AS_SQL, DateMatchers.within(2, ChronoUnit.MILLIS, 2015, Month.AUGUST, 4, 12, 0, 0, 3).atZone(UTC)); diff --git a/src/test/java/org/exparity/hamcrest/date/core/format/DateFormatterTest.java b/src/test/java/org/exparity/hamcrest/date/core/format/DateFormatterTest.java deleted file mode 100644 index 141d84e..0000000 --- a/src/test/java/org/exparity/hamcrest/date/core/format/DateFormatterTest.java +++ /dev/null @@ -1,42 +0,0 @@ -package org.exparity.hamcrest.date.core.format; - -import static org.hamcrest.MatcherAssert.assertThat; - -import java.util.Locale; - -import org.exparity.hamcrest.date.testutils.Dates; -import org.hamcrest.Matchers; -import org.testng.annotations.Test; - -/** - * Unit test for the {@link DateFormatter} class - * - * @author Stewart Bissett - */ -public class DateFormatterTest { - - @Test - public void canDescribe() { - String description = new DateFormatter(Locale.US).describe(Dates.AUG_04_2015_1159_DEFAULT_AS_DATE); - assertThat(description, Matchers.equalTo("Tue, 04 Aug 2015 11:59:00.000 AM")); - } - - @Test - public void canDescribeDate() { - String description = new DateFormatter(Locale.US).describeDate(Dates.AUG_04_2015_1159_DEFAULT_AS_DATE); - assertThat(description, Matchers.equalTo("Tue, 04 Aug 2015")); - } - - @Test - public void canDescribeSqlDate() { - String description = new DateFormatter(Locale.US).describe(Dates.AUG_04_2015_AS_SQL); - assertThat(description, Matchers.equalTo("Tue, 04 Aug 2015")); - } - - @Test - public void canDescribeDateSqlDate() { - String description = new DateFormatter(Locale.US).describeDate(Dates.AUG_04_2015_AS_SQL); - assertThat(description, Matchers.equalTo("Tue, 04 Aug 2015")); - } - -} diff --git a/src/test/java/org/exparity/hamcrest/date/core/format/LocalDateFormatterTest.java b/src/test/java/org/exparity/hamcrest/date/core/format/LocalDateFormatterTest.java deleted file mode 100644 index 647a66c..0000000 --- a/src/test/java/org/exparity/hamcrest/date/core/format/LocalDateFormatterTest.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.exparity.hamcrest.date.core.format; - -import static org.hamcrest.MatcherAssert.assertThat; - -import java.util.Locale; - -import org.exparity.hamcrest.date.testutils.Dates; -import org.hamcrest.Matchers; -import org.testng.annotations.Test; - -/** - * Unit test for the {@link LocalDateFormatter} class - * - * @author Stewart Bissett - */ -public class LocalDateFormatterTest { - - @Test - public void canDescribe() { - String description = new LocalDateFormatter(Locale.US).describe(Dates.AUG_04_2015); - assertThat(description, Matchers.equalTo("Tue, 04 Aug 2015")); - } - - @Test - public void canDescribeDate() { - String description = new LocalDateFormatter(Locale.US).describeDate(Dates.AUG_04_2015); - assertThat(description, Matchers.equalTo("Tue, 04 Aug 2015")); - } - -} diff --git a/src/test/java/org/exparity/hamcrest/date/core/format/LocalDateTimeFormatterTest.java b/src/test/java/org/exparity/hamcrest/date/core/format/LocalDateTimeFormatterTest.java deleted file mode 100644 index 9b43799..0000000 --- a/src/test/java/org/exparity/hamcrest/date/core/format/LocalDateTimeFormatterTest.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.exparity.hamcrest.date.core.format; - -import static org.hamcrest.MatcherAssert.assertThat; - -import java.util.Locale; - -import org.exparity.hamcrest.date.testutils.Dates; -import org.hamcrest.Matchers; -import org.testng.annotations.Test; - -/** - * Unit test for the {@link LocalDateTimeFormatter} class - * - * @author Stewart Bissett - */ -public class LocalDateTimeFormatterTest { - - @Test - public void canDescribe() { - String description = new LocalDateTimeFormatter(Locale.US).describe(Dates.AUG_04_2015_1159); - assertThat(description, Matchers.equalTo("Tue, 04 Aug 2015 11:59:00.000 AM")); - } - - @Test - public void canDescribeDate() { - String description = new LocalDateTimeFormatter(Locale.UK).describeDate(Dates.AUG_04_2015_1159); - assertThat(description, Matchers.equalTo("Tue, 04 Aug 2015")); - } - -} diff --git a/src/test/java/org/exparity/hamcrest/date/core/format/LocalTimeFormatterTest.java b/src/test/java/org/exparity/hamcrest/date/core/format/LocalTimeFormatterTest.java deleted file mode 100644 index 54cf5fa..0000000 --- a/src/test/java/org/exparity/hamcrest/date/core/format/LocalTimeFormatterTest.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.exparity.hamcrest.date.core.format; - -import static org.hamcrest.MatcherAssert.assertThat; - -import java.time.LocalTime; -import java.time.temporal.ChronoUnit; -import java.util.Locale; - -import org.hamcrest.Matchers; -import org.testng.annotations.Test; - -/** - * Unit test for the {@link LocalTimeFormatter} class - * - * @author Stewart Bissett - */ -public class LocalTimeFormatterTest { - - @Test - public void canDescribe() { - String description = new LocalTimeFormatter(Locale.US).describe(LocalTime.NOON.minus(1, ChronoUnit.SECONDS)); - assertThat(description, Matchers.equalTo("11:59:59 AM")); - } - - @Test(expectedExceptions = UnsupportedOperationException.class) - public void canDescribeDate() { - new LocalTimeFormatter(Locale.UK).describeDate(LocalTime.NOON.minus(1, ChronoUnit.SECONDS)); - } - -} diff --git a/src/test/java/org/exparity/hamcrest/date/core/format/SqlDateFormatterTest.java b/src/test/java/org/exparity/hamcrest/date/core/format/SqlDateFormatterTest.java deleted file mode 100644 index 4afc17e..0000000 --- a/src/test/java/org/exparity/hamcrest/date/core/format/SqlDateFormatterTest.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.exparity.hamcrest.date.core.format; - -import static org.hamcrest.MatcherAssert.assertThat; - -import java.util.Locale; - -import org.exparity.hamcrest.date.testutils.Dates; -import org.hamcrest.Matchers; -import org.testng.annotations.Test; - -/** - * Unit test for the {@link SqlDateFormatter} class - * - * @author Stewart Bissett - */ -public class SqlDateFormatterTest { - - @Test - public void canDescribe() { - String description = new SqlDateFormatter(Locale.US).describe(Dates.AUG_04_2015_AS_SQL); - assertThat(description, Matchers.equalTo("Tue, 04 Aug 2015")); - } - - @Test - public void canDescribeDate() { - String description = new SqlDateFormatter(Locale.US).describeDate(Dates.AUG_04_2015_AS_SQL); - assertThat(description, Matchers.equalTo("Tue, 04 Aug 2015")); - } - -} diff --git a/src/test/java/org/exparity/hamcrest/date/core/format/ZonedDateTimeFormatterTest.java b/src/test/java/org/exparity/hamcrest/date/core/format/ZonedDateTimeFormatterTest.java deleted file mode 100644 index 2bd3cf8..0000000 --- a/src/test/java/org/exparity/hamcrest/date/core/format/ZonedDateTimeFormatterTest.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.exparity.hamcrest.date.core.format; - -import static org.hamcrest.MatcherAssert.assertThat; - -import java.util.Locale; - -import org.exparity.hamcrest.date.testutils.Dates; -import org.hamcrest.Matchers; -import org.testng.annotations.Test; - -/** - * Unit test for the {@link ZonedDateTimeFormatter} class - * - * @author Stewart Bissett - */ -public class ZonedDateTimeFormatterTest { - - @Test - public void canDescribe() { - String description = new ZonedDateTimeFormatter(Locale.US).describe(Dates.AUG_04_2015_11AM_UTC); - assertThat(description, Matchers.equalTo("Tue, 04 Aug 2015 11:59:00.000 AM +0000")); - } - - @Test - public void canDescribeDate() { - String description = new ZonedDateTimeFormatter(Locale.US).describeDate(Dates.AUG_04_2015_11AM_UTC); - assertThat(description, Matchers.equalTo("Tue, 04 Aug 2015")); - } - -}