├── .gitignore ├── .travis.yml ├── LICENSE.txt ├── README.md ├── pom.xml └── src ├── main └── java │ └── org │ └── exparity │ └── hamcrest │ └── date │ ├── DateMatchers.java │ ├── DayMonthYear.java │ ├── InstantMatchers.java │ ├── LocalDateMatchers.java │ ├── LocalDateTimeMatchers.java │ ├── LocalTimeMatchers.java │ ├── Moments.java │ ├── Months.java │ ├── OffsetDateTimeMatchers.java │ ├── SqlDateMatchers.java │ ├── Weekdays.java │ ├── ZonedDateTimeMatchers.java │ ├── core │ ├── IsAfter.java │ ├── IsBefore.java │ ├── IsDayOfMonth.java │ ├── IsDayOfWeek.java │ ├── IsFirstDayOfMonth.java │ ├── IsHour.java │ ├── IsLastDayOfMonth.java │ ├── IsLeapYear.java │ ├── IsMaximum.java │ ├── IsMillisecond.java │ ├── IsMinimum.java │ ├── IsMinute.java │ ├── IsMonth.java │ ├── IsSame.java │ ├── IsSameDay.java │ ├── IsSameOrAfter.java │ ├── IsSameOrBefore.java │ ├── IsSecond.java │ ├── IsWithin.java │ ├── IsYear.java │ ├── TemporalConversionException.java │ ├── TemporalConverter.java │ ├── TemporalConverters.java │ ├── TemporalFunction.java │ ├── TemporalFunctions.java │ ├── TemporalMatcher.java │ ├── TemporalProvider.java │ ├── TemporalProviders.java │ ├── function │ │ ├── DateFunction.java │ │ ├── InstantFunction.java │ │ ├── LocalDateFunction.java │ │ ├── LocalDateTimeFunction.java │ │ ├── LocalTimeFunction.java │ │ ├── OffsetDateTimeFunction.java │ │ ├── SqlDateFunction.java │ │ └── ZonedDateTimeFunction.java │ └── types │ │ ├── DayOfMonth.java │ │ ├── Hour.java │ │ ├── Interval.java │ │ ├── Millisecond.java │ │ ├── Minute.java │ │ └── Second.java │ └── package.html └── test └── java └── org └── exparity └── hamcrest └── date ├── MomentsTest.java ├── MonthsTest.java ├── WeekDaysTest.java ├── core ├── IsAfterTest.java ├── IsBeforeTest.java ├── IsDayOfMonthTest.java ├── IsDayOfWeekTest.java ├── IsDayTest.java ├── IsFirstDayOfMonthTest.java ├── IsHourTest.java ├── IsLastDayOfMonthTest.java ├── IsLeapYearTest.java ├── IsMaximumTest.java ├── IsMillisecondTest.java ├── IsMinimumTest.java ├── IsMinuteTest.java ├── IsMonthTest.java ├── IsSameDayOfMonthTest.java ├── IsSameDayOfWeekTest.java ├── IsSameDayTest.java ├── IsSameHourOfDayTest.java ├── IsSameInstantTest.java ├── IsSameMillisecondOfSecondTest.java ├── IsSameMinuteOfHourTest.java ├── IsSameMonthOfYearTest.java ├── IsSameOrAfterTest.java ├── IsSameOrBeforeTest.java ├── IsSameSecondOfMinuteTest.java ├── IsSameYearTest.java ├── IsSecondTest.java ├── IsWithinTest.java └── IsYearTest.java └── testutils ├── DateMatcherTestUtils.java ├── Dates.java ├── TimeZones.java ├── ZoneIds.java └── ZoneOffsets.java /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Eclipse Files # 4 | .classpath 5 | .project 6 | 7 | # IDEA Files # 8 | .idea 9 | 10 | # Package Files # 11 | *.jar 12 | *.war 13 | *.ear 14 | /.settings 15 | /target 16 | /test-output 17 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | sudo: false 3 | jdk: 4 | - openjdk8 5 | after_success: 6 | - mvn clean cobertura:cobertura coveralls:cobertura 7 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2019, eXparity Limited 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, 8 | this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in the 11 | documentation and/or other materials provided with the distribution. 12 | * Neither the name of copyright holder nor the names of its contributors 13 | may be used to endorse or promote products derived from this software 14 | without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 20 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 | POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Hamcrest Date [![Build Status](https://travis-ci.org/eXparity/hamcrest-date.svg?branch=master)](https://travis-ci.org/eXparity/hamcrest-date) [![Coverage Status](https://coveralls.io/repos/eXparity/hamcrest-date/badge.png?branch=master)](https://coveralls.io/r/eXparity/hamcrest-date?branch=master) 2 | ============= 3 | 4 | A date matching library for [Java Hamcrest][] 5 | 6 | Licensed under [BSD License][]. 7 | 8 | What is Hamcrest Date? 9 | ----------------- 10 | Hamcrest Date is an extension library for the [Java Hamcrest][] matcher library which provides Matcher implementations for Java date types including LocalDate, LocalTime, LocalDateTime, ZonedDateTime, and Date. 11 | 12 | Downloads 13 | --------- 14 | You can obtain Hamcrest Date binaries from [maven central][]. To include in your project: 15 | 16 | A maven project 17 | 18 | ```xml 19 | 20 | org.exparity 21 | hamcrest-date 22 | 2.0.8 23 | 24 | ``` 25 | 26 | Versions 2.x.x onwards require Java 8. If you are using an earlier version of Java 8 then include version 27 | 28 | ```xml 29 | 30 | org.exparity 31 | hamcrest-date 32 | 1.1.0 33 | 34 | ``` 35 | 36 | Binaries 37 | -------- 38 | Hamcrest Date has a single binary, `hamcrest-date.jar`, which contains all the date matchers. Sources and JavaDoc jars are available. 39 | 40 | Usage 41 | ------------- 42 | 43 | The matchers are exposed as static methods on the LocalDateMatchers, LocalTimeMatchers, LocalDateTimeMatchers, ZonedDateTimeMatchers, OffsetDateTimeMatchers, SqlDateMatchers and DateMatchers class. 44 | 45 | Units of time are imported from the `ChronoUnit` class: 46 | 47 | ```java 48 | import static java.time.temporal.ChronoUnit.*; 49 | ``` 50 | 51 | Each matcher can be imported statically (preferred) or normally: 52 | ```java 53 | // static, makes sameDay, within, etc available 54 | import static org.exparity.hamcrest.date.LocalDateMatchers.*; 55 | // non-static, must use qualified LocalDateMatchers.sameDay, etc. 56 | import org.exparity.hamcrest.date.LocalDateMatchers; 57 | ``` 58 | 59 | For example, with non-static imports: 60 | 61 | ```java 62 | LocalDate today = LocalDate.now(); myBirthday = LocalDate.of(2015, AUGUST, 9); 63 | MatcherAssert.assertThat(today, LocalDateMatchers.sameDay(myBirthday)); 64 | ``` 65 | 66 | or to test if you're getting closer to your birthday: 67 | 68 | ```java 69 | LocalDate today = LocalDate.now(); myBirthday = LocalDate.of(2015, AUGUST, 9); 70 | MatcherAssert.assertThat(today, LocalDateMatchers.within(1, ChronoUnit.DAY, myBirthday)); 71 | ``` 72 | 73 | or with static importing: 74 | 75 | ```java 76 | LocalDate today = LocalDate.now(); myBirthday = LocalDate.of(2015, AUGUST, 9); 77 | assertThat(today, within(1, DAY, myBirthday)); 78 | ``` 79 | 80 | The same matchers are available for all date types so to match LocalDateTime values: 81 | 82 | ```java 83 | LocalDateTime myAppointment = LocalDateTime.of(2015, AUGUST, 9, 10, 30, 0); 84 | assertThat(LocalDateTime.now(), within(15, MINUTES, myAppointment)); 85 | ``` 86 | 87 | or to match ZonedDateTime values: 88 | 89 | ```java 90 | ZonedDateTime myAppointment = ZonedDateTime.of(LocalDateTime.of(2015, AUGUST, 9, 10, 30, 0), ZoneId.of("UTC")); 91 | assertThat(ZonedDateTime.now(), within(15, MINUTES, myAppointment)); 92 | ``` 93 | 94 | or to match Instant values: 95 | 96 | ```java 97 | Instant instant = Instant.now(); 98 | assertThat(instant, within(1, SECONDS, Instant.now()); 99 | ``` 100 | 101 | or to match OffsetDateTime values: 102 | 103 | ```java 104 | OffsetDateTime myAppointment = OffsetDateTime.of(LocalDateTime.of(2015, AUGUST, 9, 10, 30, 0), ZoneOffset.UTC); 105 | assertThat(OffsetDateTime.now(), within(15, MINUTES, myAppointment)); 106 | ``` 107 | 108 | or to match LocalTime values: 109 | 110 | ```java 111 | LocalTime myAppointment = LocalTime.NOON; 112 | assertThat(LocalTime.now(), within(15, MINUTES, myAppointment)); 113 | ``` 114 | 115 | or to match java.sql.Date values: 116 | 117 | ```java 118 | java.sql.Date myAppointment = java.sql.Date.valueOf(LocalDate.of(2015, AUGUST, 9); 119 | assertThat(new java.sql.Date(), within(15, MINUTES, myAppointment)); 120 | ``` 121 | 122 | The library includes date matchers for: 123 | 124 | * __after__ - Test if the actual date is after the reference date 125 | * __before__ - Test if the actual date is before the reference date 126 | * __within__ - Test if the actual date is within a given period (before or after) of the reference date 127 | * __sameDay__ - Test if the actual date is on the same day as the reference date 128 | * __sameHourOfDay__ - Test if the actual date is on the same hour of the day as the reference date 129 | * __sameInstant__ - Test if the actual date at the same instance as the reference date 130 | * __sameOrBefore__ - Test if the actual date is the same or before the reference date 131 | * __sameOrAfter__ - Test if the actual date is the same or after the reference date 132 | * __sameMinuteOfHour__ - Test if the actual date is on the same minute of the hour as the reference date 133 | * __sameMonthOfYear__ - Test if the actual date is on the same month of the year as the reference date 134 | * __sameSecondOfMinute__ - Test if the actual date is on the same second of the minute as the reference date 135 | * __sameDayOfWeek__ - Test if the actual date is on the same week day as the reference date 136 | * __sameYear__ - Test if the actual date is on the same year as the reference date 137 | * __isInstance__ - Test if the actual date is at the exact instant 138 | * __isSecond__ - Test if the actual date is on the given second 139 | * __isMinute__ - Test if the actual date is on the given minute 140 | * __isHour__ - Test if the actual date is on the given hour 141 | * __isDayOfWeek__ - Test if the actual date is on the given day of the week 142 | * __isDayOfMonth__ - Test if the actual date is on the given day of the month 143 | * __isMonth__ - Test if the actual date is on the given month 144 | * __isYear__ - Test if the actual date is on the given year 145 | * __isYesterday__ - Test if the actual date is yesterday 146 | * __isToday__ - Test if the actual date is today 147 | * __isTomorrow__ - Test if the actual date is tomorrow 148 | * __isMonday__ - Test if the actual date is on a monday 149 | * __isTuesday__ - Test if the actual date is on a tuesday 150 | * __isWednesday__ - Test if the actual date is on a wednesday 151 | * __isThursday__ - Test if the actual date is on a thursday 152 | * __isFriday__ - Test if the actual date is on a friday 153 | * __isSaturday__ - Test if the actual date is on a saturday 154 | * __isSunday__ - Test if the actual date is on a sunday 155 | * __isWeekday__ - Test if the actual date is on a weekday 156 | * __isWeekend__ - Test if the actual date is on a weekend 157 | * __isJanuary__ - Test if the actual date is in january 158 | * __isFebruary__ - Test if the actual date is in february 159 | * __isMarch__ - Test if the actual date is in march 160 | * __isApril__ - Test if the actual date is in april 161 | * __isMay__ - Test if the actual date is in may 162 | * __isJune__ - Test if the actual date is in june 163 | * __isJuly__ - Test if the actual date is in july 164 | * __isAugust__ - Test if the actual date is in august 165 | * __isSeptember__ - Test if the actual date is in september 166 | * __isOctober__ - Test if the actual date is in october 167 | * __isNovember__ - Test if the actual date is in november 168 | * __isDecember__ - Test if the actual date is in december 169 | * __isLeapYear__ - Test if the actual date is on a leap year 170 | 171 | The Javadocs include examples on all methods so you can look there for examples for specific methods 172 | 173 | Source 174 | ------ 175 | The source is structured along the lines of the maven standard folder structure for a jar project. 176 | 177 | * Core classes [src/main/java] 178 | * Unit tests [src/test/java] 179 | 180 | The source includes a pom.xml for building with Maven 181 | 182 | Release Notes 183 | ------------- 184 | Changes 2.0.7 -> 2.0.8 185 | * Fix Issue 37 - Add support for InstantMatchers 186 | 187 | Changes 2.0.6 -> 2.0.7 188 | * Fix Issue 26 - Use licence consistently in release jar files 189 | * Fix Issue 32 - Fix assertion errors for temporals with timezones 190 | 191 | Changes 2.0.5 -> 2.0.6 192 | * Fix Issue 21 - Add support for changing Locale 193 | * Fix Issue 24 - Add OffsetDateTimeMatchers 194 | * Fix Issue 27 - (UnsupportedOperationException with java.sql.Date) 195 | * Handle java.sql.Date in DateMatchers 196 | * Add SqlDateMatchers 197 | 198 | Changes 2.0.4 -> 2.0.5 199 | * Fix Issue 20 - (DateMatchers.isDay(int, Month, int) not adapted for non-zero time zones) 200 | * Support for time zones in temporal fields matchers 201 | 202 | Changes 2.0.3 -> 2.0.4 203 | * Fix Issue 18 - Add test scope to testng 204 | 205 | Changes 2.0.2 -> 2.0.3 206 | * Fix Issue 16 - Incorrect assertion message for before 207 | * Fix Issue 17 - AM/PM Indicator missing in assertion message 208 | 209 | Changes 2.0.1 -> 2.0.2 210 | * Add Support for LocalTime 211 | 212 | Changes 1.1.0 -> 2.0.0 213 | * Add Support for Java 8 date types. 214 | * Add new is{*} matchers 215 | * Move matcher classes to .core package 216 | 217 | Changes 1.0.1 -> 1.1 218 | * Remove deprecated uk.co.it.modular.hamcrest.date.DateMatchers. 219 | 220 | Changes 1.0.0 -> 1.0.1 221 | * Restore and deprecate uk.co.it.modular.hamcrest.date.DateMatchers for backwards compatibility with previous package structure. 222 | 223 | Changes 0.9.5 -> 1.0.0 224 | * Package change to new organisation org.exparity from uk.co.it.modular 225 | * Fixup flaky tests by using local timezone in non-timezone tests 226 | 227 | Acknowledgements 228 | ---------------- 229 | Developers: 230 | * Stewart Bissett 231 | 232 | Thanks to the developers at [Java Hamcrest][]. Without their hardwork and core libraries there'd be nothing to be extend and we be stuck with old school, non-declarative, non-reusable, assertions. 233 | 234 | [BSD License]: http://opensource.org/licenses/BSD-3-Clause 235 | [Maven central]: http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22hamcrest-date%22 236 | [Java Hamcrest]: http://github.com/hamcrest/JavaHamcrest 237 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | org.exparity 5 | hamcrest-date 6 | 2.0.9-SNAPSHOT 7 | 8 | Hamcrest Date 9 | https://github.com/exparity/hamcrest-date 10 | Hamcrest Date matchers for Java 11 | 12 | 13 | org.sonatype.oss 14 | oss-parent 15 | 9 16 | 17 | 18 | 19 | 20 | BSD-3-Clause 21 | https://opensource.org/licenses/BSD-3-Clause 22 | repo 23 | 24 | 25 | 26 | 27 | scm:git:@github.com:exparity/hamcrest-date.git 28 | scm:git:git@github.com:exparity/hamcrest-date.git 29 | git@github.com:exparity/hamcrest-date.git 30 | 31 | 32 | 33 | eXparity Limited 34 | 35 | 36 | 37 | 38 | stewbis 39 | Stewart Bissett 40 | 41 | Developer 42 | 43 | 44 | 45 | 46 | 47 | 1.8 48 | 1.8 49 | 50 | 51 | 52 | 53 | org.hamcrest 54 | hamcrest 55 | 2.2 56 | 57 | 58 | org.testng 59 | testng 60 | 7.7.1 61 | test 62 | 63 | 64 | junit 65 | junit 66 | 4.13.2 67 | test 68 | 69 | 70 | org.hamcrest 71 | hamcrest-core 72 | 73 | 74 | 75 | 76 | org.exparity 77 | fluent-date 78 | 2.0.0 79 | test 80 | 81 | 82 | 83 | 84 | 85 | 86 | org.apache.maven.plugins 87 | maven-jar-plugin 88 | 3.3.0 89 | 90 | 91 | ${project.build.outputDirectory}/META-INF/MANIFEST.MF 92 | 93 | 94 | 95 | 96 | org.apache.maven.plugins 97 | maven-javadoc-plugin 98 | 3.5.0 99 | 100 | -Xdoclint:none 101 | 8 102 | 103 | 104 | 105 | org.eluder.coveralls 106 | coveralls-maven-plugin 107 | 2.2.0 108 | 109 | MjyqJXIjgudLEv2xAp6s8OBZzuRerEjzh 110 | 111 | 112 | 113 | org.codehaus.mojo 114 | cobertura-maven-plugin 115 | 2.7 116 | 117 | xml 118 | 256m 119 | true 120 | 121 | 122 | 123 | 124 | org.apache.felix 125 | maven-bundle-plugin 126 | 4.2.1 127 | 128 | 129 | org.exparity.hamcrest.date.* 130 | 131 | 132 | 133 | 134 | bundle-manifest 135 | process-classes 136 | 137 | manifest 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | ${basedir} 146 | META-INF 147 | 148 | LICENSE.txt 149 | 150 | 151 | 152 | 153 | 154 | -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/DayMonthYear.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date; 2 | 3 | import java.time.LocalDate; 4 | 5 | /** 6 | * Value object to store a day, month, and year tuple 7 | * 8 | * @author Stewart Bissett 9 | * 10 | * @deprecated Use {@link LocalDate} 11 | */ 12 | @Deprecated 13 | public class DayMonthYear { 14 | 15 | private final int day, year; 16 | private final Months month; 17 | 18 | public DayMonthYear(final int day, final Months month, final int year) { 19 | this.day = day; 20 | this.month = month; 21 | this.year = year; 22 | } 23 | 24 | public int getDay() { 25 | return day; 26 | } 27 | 28 | public Months getMonth() { 29 | return month; 30 | } 31 | 32 | public int getYear() { 33 | return year; 34 | } 35 | 36 | public LocalDate toLocalDate() { 37 | return LocalDate.of(year, month.month(), day); 38 | } 39 | 40 | @Override 41 | public boolean equals(final Object obj) { 42 | if (obj == this) { 43 | return true; 44 | } 45 | if (!(obj instanceof DayMonthYear)) { 46 | return false; 47 | } 48 | DayMonthYear rhs = (DayMonthYear) obj; 49 | return year == rhs.year && month == rhs.month && day == rhs.day; 50 | } 51 | 52 | @Override 53 | public int hashCode() { 54 | final int prime = 31; 55 | int result = 1; 56 | result = prime * result + day; 57 | result = prime * result + ((month == null) ? 0 : month.hashCode()); 58 | result = prime * result + year; 59 | return result; 60 | } 61 | 62 | @Override 63 | public String toString() { 64 | return "DayMonthYear[" + year + ":" + month + ":" + day + "]"; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/Moments.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date; 2 | 3 | import static org.exparity.hamcrest.date.Months.fromCalendar; 4 | 5 | import java.util.Calendar; 6 | import java.util.Date; 7 | 8 | /** 9 | * Static factory to create moments in time such as now, today, tomorrow, etc 10 | * 11 | * @author Stewart Bissett 12 | */ 13 | @Deprecated 14 | public abstract class Moments { 15 | 16 | /** 17 | * Return a {@link Date} instance representing now down to millisecond 18 | * accuracy 19 | */ 20 | public static Date now() { 21 | return new Date(); 22 | } 23 | 24 | /** 25 | * Return a {@link DayMonthYear} instance representing yesterday 26 | */ 27 | public static DayMonthYear yesterday() { 28 | return aRelativeDayMonthYear(-1, Calendar.DAY_OF_MONTH); 29 | } 30 | 31 | /** 32 | * Return a {@link DayMonthYear} instance representing today 33 | */ 34 | public static DayMonthYear today() { 35 | return aRelativeDayMonthYear(0, Calendar.DAY_OF_MONTH); 36 | } 37 | 38 | /** 39 | * Return a {@link DayMonthYear} instance representing tomorrow 40 | */ 41 | public static DayMonthYear tomorrow() { 42 | return aRelativeDayMonthYear(1, Calendar.DAY_OF_MONTH); 43 | } 44 | 45 | private static DayMonthYear aRelativeDayMonthYear(final int adjustment, final int datePart) { 46 | Calendar calendar = Calendar.getInstance(); 47 | calendar.add(datePart, adjustment); 48 | return new DayMonthYear( 49 | calendar.get(datePart), 50 | fromCalendar(calendar.get(Calendar.MONTH)), 51 | calendar.get(Calendar.YEAR)); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/Months.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date; 2 | 3 | import java.time.Month; 4 | import java.util.Calendar; 5 | 6 | /** 7 | * Enumeration of months in a year 8 | * 9 | * @author Stewart Bissett 10 | * @deprecated Use {@link Month} enumeration 11 | */ 12 | @Deprecated 13 | public enum Months { 14 | 15 | JAN(Calendar.JANUARY, Month.JANUARY, "January"), 16 | JANUARY(Calendar.JANUARY, Month.JANUARY, "January"), 17 | FEB(Calendar.FEBRUARY, Month.FEBRUARY, "February"), 18 | FEBRUARY(Calendar.FEBRUARY, Month.FEBRUARY, "February"), 19 | MAR(Calendar.MARCH, Month.MARCH, "March"), 20 | MARCH(Calendar.MARCH, Month.MARCH, "March"), 21 | APR(Calendar.APRIL, Month.APRIL, "April"), 22 | APRIL(Calendar.APRIL, Month.APRIL, "April"), 23 | MAY(Calendar.MAY, Month.MAY, "May"), 24 | JUN(Calendar.JUNE, Month.JUNE, "June"), 25 | JUNE(Calendar.JUNE, Month.JUNE, "June"), 26 | JUL(Calendar.JULY, Month.JULY, "July"), 27 | JULY(Calendar.JULY, Month.JULY, "July"), 28 | AUG(Calendar.AUGUST, Month.AUGUST, "August"), 29 | AUGUST(Calendar.AUGUST, Month.AUGUST, "August"), 30 | SEP(Calendar.SEPTEMBER, Month.SEPTEMBER, "September"), 31 | SEPTEMBER(Calendar.SEPTEMBER, Month.SEPTEMBER, "September"), 32 | OCT(Calendar.OCTOBER, Month.OCTOBER, "October"), 33 | OCTOBER(Calendar.OCTOBER, Month.OCTOBER, "October"), 34 | NOV(Calendar.NOVEMBER, Month.NOVEMBER, "November"), 35 | NOVEMBER(Calendar.NOVEMBER, Month.NOVEMBER, "November"), 36 | DEC(Calendar.DECEMBER, Month.DECEMBER, "December"), 37 | DECEMBER(Calendar.DECEMBER, Month.DECEMBER, "December"); 38 | 39 | /** 40 | * Factory method to create a Months instance from a java calendar month value 41 | */ 42 | public static Months fromCalendar(final int calendarMonth) { 43 | for (Months month : values()) { 44 | if (calendarMonth == month.calendarMonth) { 45 | return month; 46 | } 47 | } 48 | throw new IllegalArgumentException("Unknown calendar month value '" + calendarMonth + "'"); 49 | } 50 | 51 | private final int calendarMonth; 52 | private final Month month; 53 | private final String description; 54 | 55 | Months(final int calendarMonth, final Month month, final String description) { 56 | this.calendarMonth = calendarMonth; 57 | this.month = month; 58 | this.description = description; 59 | } 60 | 61 | public int calendarConstant() { 62 | return calendarMonth; 63 | } 64 | 65 | public String describe() { 66 | return description; 67 | } 68 | 69 | public Month month() { 70 | return month; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/Weekdays.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date; 2 | 3 | import java.time.DayOfWeek; 4 | import java.time.format.TextStyle; 5 | import java.util.Calendar; 6 | import java.util.Locale; 7 | 8 | /** 9 | * Enumeration of days in a week 10 | * 11 | * @author Stewart Bissett 12 | * 13 | * @deprecated Use {@link DayOfWeek} 14 | */ 15 | public enum Weekdays { 16 | 17 | MONDAY(Calendar.MONDAY, DayOfWeek.MONDAY), 18 | TUESDAY(Calendar.TUESDAY, DayOfWeek.TUESDAY), 19 | WEDNESDAY(Calendar.WEDNESDAY, DayOfWeek.WEDNESDAY), 20 | THURSDAY(Calendar.THURSDAY, DayOfWeek.THURSDAY), 21 | FRIDAY(Calendar.FRIDAY, DayOfWeek.FRIDAY), 22 | SATURDAY(Calendar.SATURDAY, DayOfWeek.SATURDAY), 23 | SUNDAY(Calendar.SUNDAY, DayOfWeek.SUNDAY); 24 | 25 | private final int calendarDay; 26 | private final DayOfWeek dayOfWeek; 27 | 28 | Weekdays(final int calendarDay, final DayOfWeek dayOfWeek) { 29 | this.calendarDay = calendarDay; 30 | this.dayOfWeek = dayOfWeek; 31 | } 32 | 33 | public int getAsCalendarConstant() { 34 | return calendarDay; 35 | } 36 | 37 | public String describe() { 38 | return dayOfWeek.getDisplayName(TextStyle.FULL, Locale.ENGLISH); 39 | } 40 | 41 | public DayOfWeek getAsDayOfWeek() { 42 | return dayOfWeek; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/IsAfter.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import java.time.ZoneId; 4 | import java.util.Locale; 5 | import java.util.Optional; 6 | 7 | import org.hamcrest.Description; 8 | 9 | /** 10 | * A matcher that tests that the examined date is after the reference date 11 | * 12 | * @author Stewart Bissett 13 | * 14 | * @param the test type 15 | * @param the expected type 16 | */ 17 | public class IsAfter extends TemporalMatcher { 18 | 19 | private final TemporalProvider expected; 20 | private final TemporalConverter converter; 21 | private final TemporalFunction functions; 22 | private final Locale locale; 23 | private final Optional zone; 24 | 25 | public IsAfter(TemporalConverter converter, 26 | TemporalProvider expected, 27 | TemporalFunction functions, 28 | Optional zone, 29 | Locale locale) { 30 | this.expected = expected; 31 | this.converter = converter; 32 | this.functions = functions; 33 | this.locale = locale; 34 | this.zone = zone; 35 | } 36 | 37 | public IsAfter(TemporalConverter converter, TemporalProvider expected, TemporalFunction functions) { 38 | this(converter, expected, functions, Optional.empty(), Locale.getDefault(Locale.Category.FORMAT)); 39 | } 40 | 41 | @Override 42 | protected boolean matchesSafely(final T actual, final Description mismatchDescription) { 43 | E expectedValue = expected.apply(zone), actualValue = converter.apply(actual, zone); 44 | if (functions.isSame(expectedValue, actualValue) || functions.isAfter(expectedValue, actualValue)) { 45 | mismatchDescription.appendText("date is " + functions.describe(actualValue, locale)); 46 | return false; 47 | } else { 48 | return true; 49 | } 50 | } 51 | 52 | @Override 53 | public void describeTo(final Description description) { 54 | description.appendText("the date is after " + functions.describe(expected.apply(zone), locale)); 55 | } 56 | 57 | @Override 58 | public TemporalMatcher atZone(ZoneId zone) { 59 | return new IsAfter<>(converter, expected, functions, Optional.of(zone), locale); 60 | } 61 | 62 | @Override 63 | public TemporalMatcher atLocale(Locale locale) { 64 | return new IsAfter<>(converter, expected, functions, zone, locale); 65 | } 66 | 67 | } -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/IsBefore.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import java.time.ZoneId; 4 | import java.util.Locale; 5 | import java.util.Optional; 6 | 7 | import org.hamcrest.Description; 8 | 9 | /** 10 | * A matcher that tests that the examined date is before the reference date 11 | * 12 | * @author Stewart Bissett 13 | * 14 | * @param the test type 15 | * @param the expected type 16 | */ 17 | public class IsBefore extends TemporalMatcher { 18 | 19 | private final TemporalProvider expected; 20 | private final TemporalConverter converter; 21 | private final TemporalFunction functions; 22 | private final Locale locale; 23 | private final Optional zone; 24 | 25 | public IsBefore(TemporalConverter converter, 26 | TemporalProvider expected, 27 | TemporalFunction functions, 28 | Optional zone, 29 | Locale locale) { 30 | this.expected = expected; 31 | this.converter = converter; 32 | this.functions = functions; 33 | this.locale = locale; 34 | this.zone = zone; 35 | } 36 | 37 | public IsBefore(TemporalConverter converter, TemporalProvider expected, TemporalFunction functions) { 38 | this(converter, expected, functions, Optional.empty(), Locale.getDefault(Locale.Category.FORMAT)); 39 | } 40 | 41 | @Override 42 | protected boolean matchesSafely(final T actual, final Description mismatchDescription) { 43 | E expectedValue = expected.apply(zone), actualValue = converter.apply(actual, zone); 44 | if (functions.isSame(expectedValue, actualValue) || functions.isBefore(expectedValue, actualValue)) { 45 | mismatchDescription.appendText("date is " + functions.describe(actualValue, locale)); 46 | return false; 47 | } else { 48 | return true; 49 | } 50 | } 51 | 52 | @Override 53 | public void describeTo(final Description description) { 54 | description.appendText("the date is before " + functions.describe(expected.apply(zone), locale)); 55 | } 56 | 57 | @Override 58 | public TemporalMatcher atZone(ZoneId zone) { 59 | return new IsBefore<>(converter, expected, functions, Optional.of(zone), locale); 60 | } 61 | 62 | @Override 63 | public TemporalMatcher atLocale(Locale locale) { 64 | return new IsBefore<>(converter, expected, functions, zone, locale); 65 | } 66 | } -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/IsDayOfMonth.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import java.time.ZoneId; 4 | import java.util.Locale; 5 | import java.util.Optional; 6 | 7 | import org.exparity.hamcrest.date.core.types.DayOfMonth; 8 | import org.hamcrest.Description; 9 | 10 | /** 11 | * A matcher that tests that the examined date is on the specified hour 12 | * 13 | * @author Stewart Bissett 14 | */ 15 | public class IsDayOfMonth extends TemporalMatcher { 16 | 17 | private final TemporalConverter converter; 18 | private final TemporalProvider expected; 19 | private final Locale locale; 20 | private final Optional zone; 21 | 22 | public IsDayOfMonth(TemporalConverter converter, 23 | TemporalProvider expected, 24 | Optional zone, 25 | Locale locale) { 26 | this.expected = expected; 27 | this.converter = converter; 28 | this.locale = locale; 29 | this.zone = zone; 30 | } 31 | 32 | public IsDayOfMonth(TemporalConverter converter, TemporalProvider expected) { 33 | this(converter, expected, Optional.empty(), Locale.getDefault(Locale.Category.FORMAT)); 34 | } 35 | 36 | @Override 37 | protected boolean matchesSafely(final T actual, final Description mismatchDescription) { 38 | DayOfMonth expectedValue = expected.apply(zone), actualValue = converter.apply(actual, zone); 39 | if (!expectedValue.equals(actualValue)) { 40 | mismatchDescription.appendText("the date has the day of month " + actualValue); 41 | return false; 42 | } else { 43 | return true; 44 | } 45 | } 46 | 47 | @Override 48 | public void describeTo(final Description description) { 49 | description.appendText("the date has the day of month " + expected.apply(zone)); 50 | } 51 | 52 | @Override 53 | public TemporalMatcher atZone(ZoneId zone) { 54 | return new IsDayOfMonth<>(converter, expected, Optional.of(zone), locale); 55 | } 56 | 57 | @Override 58 | public TemporalMatcher atLocale(Locale locale) { 59 | return new IsDayOfMonth<>(converter, expected, zone, locale); 60 | } 61 | 62 | } -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/IsDayOfWeek.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import static java.util.stream.Collectors.joining; 4 | 5 | import java.time.DayOfWeek; 6 | import java.time.ZoneId; 7 | import java.time.format.TextStyle; 8 | import java.util.List; 9 | import java.util.Locale; 10 | import java.util.Optional; 11 | 12 | import org.hamcrest.Description; 13 | 14 | /** 15 | * A matcher that tests that the examined date is on the same day of the week as the reference date 16 | * 17 | * @author Stewart Bissett 18 | */ 19 | public class IsDayOfWeek extends TemporalMatcher { 20 | 21 | private final TemporalConverter converter; 22 | private final TemporalProvider> expected; 23 | private final Locale locale; 24 | private final Optional zone; 25 | 26 | public IsDayOfWeek(TemporalConverter converter, 27 | TemporalProvider> expected, 28 | Optional zone, 29 | Locale locale) { 30 | this.expected = expected; 31 | this.converter = converter; 32 | this.locale = locale; 33 | this.zone = zone; 34 | } 35 | 36 | public IsDayOfWeek(TemporalConverter converter, TemporalProvider> expected) { 37 | this(converter, expected, Optional.empty(), Locale.getDefault(Locale.Category.FORMAT)); 38 | } 39 | 40 | @Override 41 | protected boolean matchesSafely(final T actual, final Description mismatchDescription) { 42 | List expectedValues = expected.apply(zone); 43 | DayOfWeek actualValue = converter.apply(actual, zone); 44 | if (!expectedValues.contains(actualValue)) { 45 | mismatchDescription.appendText("the date is on a " + describe(actualValue)); 46 | return false; 47 | } else { 48 | return true; 49 | } 50 | } 51 | 52 | @Override 53 | public void describeTo(final Description description) { 54 | List expectedValues = expected.apply(zone); 55 | if ( expectedValues.size() > 1 ) { 56 | List headValues = expectedValues.subList(0, expectedValues.size() - 1); 57 | DayOfWeek tailValue = expectedValues.get(expectedValues.size()-1); 58 | description.appendText( 59 | "the date is on a " + headValues.stream().map(this::describe).collect(joining(", ")) + " or " + describe(tailValue)); 60 | } else { 61 | description.appendText( 62 | "the date is on a " + describe(expectedValues.get(0))); 63 | } 64 | } 65 | 66 | @Override 67 | public TemporalMatcher atZone(ZoneId zone) { 68 | return new IsDayOfWeek<>(converter, expected, Optional.of(zone), locale); 69 | } 70 | 71 | @Override 72 | public TemporalMatcher atLocale(Locale locale) { 73 | return new IsDayOfWeek<>(converter, expected, zone, locale); 74 | } 75 | 76 | private String describe(DayOfWeek actualValue) { 77 | return actualValue.getDisplayName(TextStyle.FULL, locale); 78 | } 79 | 80 | } -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/IsFirstDayOfMonth.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import java.time.ZoneId; 4 | import java.time.temporal.ChronoField; 5 | import java.time.temporal.TemporalAccessor; 6 | import java.time.temporal.ValueRange; 7 | import java.util.Locale; 8 | import java.util.Optional; 9 | 10 | import org.hamcrest.Description; 11 | 12 | /** 13 | * A matcher that tests that the examined date is on the first day of the month 14 | * 15 | * @author Stewart Bissett 16 | */ 17 | public class IsFirstDayOfMonth extends TemporalMatcher { 18 | 19 | private final TemporalConverter converter; 20 | private final Locale locale; 21 | private final Optional zone; 22 | 23 | public IsFirstDayOfMonth(TemporalConverter converter, Optional zone, Locale locale) { 24 | this.converter = converter; 25 | this.locale = locale; 26 | this.zone = zone; 27 | } 28 | 29 | public IsFirstDayOfMonth(TemporalConverter converter) { 30 | this(converter, Optional.empty(), Locale.getDefault(Locale.Category.FORMAT)); 31 | } 32 | 33 | @Override 34 | protected boolean matchesSafely(final T actual, final Description mismatchDesc) { 35 | TemporalAccessor actualTemporal = converter.apply(actual, zone); 36 | ValueRange actualRange = ChronoField.DAY_OF_MONTH.rangeRefinedBy(actualTemporal); 37 | long actualValue = ChronoField.DAY_OF_MONTH.getFrom(actualTemporal), expectedValue = actualRange.getMinimum(); 38 | if (expectedValue != actualValue) { 39 | mismatchDesc.appendText("date is the " + actualValue + " day of the month"); 40 | return false; 41 | } else { 42 | return true; 43 | } 44 | } 45 | 46 | @Override 47 | public void describeTo(final Description description) { 48 | description.appendText("the date is the first day of the month"); 49 | } 50 | 51 | @Override 52 | public TemporalMatcher atZone(ZoneId zone) { 53 | return new IsFirstDayOfMonth<>(converter, Optional.of(zone), locale); 54 | } 55 | 56 | @Override 57 | public TemporalMatcher atLocale(Locale locale) { 58 | return new IsFirstDayOfMonth<>(converter, zone, locale); 59 | } 60 | } -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/IsHour.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import java.time.ZoneId; 4 | import java.util.Locale; 5 | import java.util.Optional; 6 | 7 | import org.exparity.hamcrest.date.core.types.Hour; 8 | import org.hamcrest.Description; 9 | 10 | /** 11 | * A matcher that tests that the examined date is on the specified hour 12 | * 13 | * @author Stewart Bissett 14 | */ 15 | public class IsHour extends TemporalMatcher { 16 | 17 | private final TemporalConverter converter; 18 | private final TemporalProvider expected; 19 | private final Locale locale; 20 | private final Optional zone; 21 | 22 | public IsHour(TemporalConverter converter, 23 | TemporalProvider expected, 24 | Optional zone, 25 | Locale locale) { 26 | this.expected = expected; 27 | this.converter = converter; 28 | this.locale = locale; 29 | this.zone = zone; 30 | } 31 | 32 | public IsHour(TemporalConverter converter, TemporalProvider expected) { 33 | this(converter, expected, Optional.empty(), Locale.getDefault(Locale.Category.FORMAT)); 34 | } 35 | 36 | @Override 37 | protected boolean matchesSafely(final T actual, final Description mismatchDescription) { 38 | Hour expectedValue = expected.apply(zone), actualValue = converter.apply(actual, zone); 39 | if (!expectedValue.equals(actualValue)) { 40 | mismatchDescription.appendText("the date has the hour " + actualValue); 41 | return false; 42 | } else { 43 | return true; 44 | } 45 | } 46 | 47 | @Override 48 | public void describeTo(final Description description) { 49 | description.appendText("the date has the hour " + expected.apply(zone)); 50 | } 51 | 52 | @Override 53 | public TemporalMatcher atZone(ZoneId zone) { 54 | return new IsHour<>(converter, expected, Optional.of(zone), locale); 55 | } 56 | 57 | @Override 58 | public TemporalMatcher atLocale(Locale locale) { 59 | return new IsHour<>(converter, expected, zone, locale); 60 | } 61 | } -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/IsLastDayOfMonth.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import java.time.ZoneId; 4 | import java.time.temporal.ChronoField; 5 | import java.time.temporal.TemporalAccessor; 6 | import java.time.temporal.ValueRange; 7 | import java.util.Locale; 8 | import java.util.Optional; 9 | 10 | import org.hamcrest.Description; 11 | 12 | /** 13 | * A matcher that tests that the examined date is on the first day of the month 14 | * 15 | * @author Stewart Bissett 16 | */ 17 | public class IsLastDayOfMonth extends TemporalMatcher { 18 | 19 | private final TemporalConverter converter; 20 | private final Locale locale; 21 | private final Optional zone; 22 | 23 | public IsLastDayOfMonth(TemporalConverter converter, Optional zone, Locale locale) { 24 | this.converter = converter; 25 | this.locale = locale; 26 | this.zone = zone; 27 | } 28 | 29 | public IsLastDayOfMonth(TemporalConverter converter) { 30 | this(converter, Optional.empty(), Locale.getDefault(Locale.Category.FORMAT)); 31 | } 32 | 33 | @Override 34 | protected boolean matchesSafely(final T actual, final Description mismatchDesc) { 35 | TemporalAccessor actualTemporal = converter.apply(actual, zone); 36 | ValueRange actualRange = ChronoField.DAY_OF_MONTH.rangeRefinedBy(actualTemporal); 37 | long actualValue = ChronoField.DAY_OF_MONTH.getFrom(actualTemporal), expectedValue = actualRange.getMaximum(); 38 | if (expectedValue != actualValue) { 39 | mismatchDesc.appendText("date is the " + actualValue + " day of the month"); 40 | return false; 41 | } else { 42 | return true; 43 | } 44 | } 45 | 46 | @Override 47 | public void describeTo(final Description description) { 48 | description.appendText("the date is the last day of the month"); 49 | } 50 | 51 | @Override 52 | public TemporalMatcher atZone(ZoneId zone) { 53 | return new IsLastDayOfMonth<>(converter, Optional.of(zone), locale); 54 | } 55 | 56 | @Override 57 | public TemporalMatcher atLocale(Locale locale) { 58 | return new IsLastDayOfMonth<>(converter, zone, locale); 59 | } 60 | } -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/IsLeapYear.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import java.time.Year; 4 | import java.time.ZoneId; 5 | import java.util.Locale; 6 | import java.util.Optional; 7 | 8 | import org.hamcrest.Description; 9 | 10 | /** 11 | * A matcher that tests that the examined date is a leap year 12 | * 13 | * @author Stewart Bissett 14 | */ 15 | public class IsLeapYear extends TemporalMatcher { 16 | 17 | private final TemporalConverter converter; 18 | private final Locale locale; 19 | private final Optional zone; 20 | 21 | public IsLeapYear(TemporalConverter converter, Optional zone, Locale locale) { 22 | this.converter = converter; 23 | this.locale = locale; 24 | this.zone = zone; 25 | } 26 | 27 | public IsLeapYear(TemporalConverter converter) { 28 | this(converter, Optional.empty(), Locale.getDefault(Locale.Category.FORMAT)); 29 | } 30 | 31 | @Override 32 | protected boolean matchesSafely(final T actual, final Description mismatchDesc) { 33 | Year actualValue = converter.apply(actual, zone); 34 | if (!actualValue.isLeap()) { 35 | mismatchDesc.appendText("the year " + actualValue + " is not a leap year"); 36 | return false; 37 | } else { 38 | return true; 39 | } 40 | } 41 | 42 | @Override 43 | public void describeTo(final Description description) { 44 | description.appendText("a leap year"); 45 | } 46 | 47 | @Override 48 | public TemporalMatcher atZone(ZoneId zone) { 49 | return new IsLeapYear<>(converter, Optional.of(zone), locale); 50 | } 51 | 52 | @Override 53 | public TemporalMatcher atLocale(Locale locale) { 54 | return new IsLeapYear<>(converter, zone, locale); 55 | } 56 | } -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/IsMaximum.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import static java.util.stream.Collectors.joining; 4 | 5 | import java.time.ZoneId; 6 | import java.time.temporal.ChronoField; 7 | import java.time.temporal.TemporalAccessor; 8 | import java.time.temporal.ValueRange; 9 | import java.util.Locale; 10 | import java.util.Optional; 11 | import java.util.stream.Stream; 12 | 13 | import org.hamcrest.Description; 14 | 15 | /** 16 | * A base matcher that tests that the examined date has the maximum value for the given date part 17 | * 18 | * @author Stewart Bissett 19 | */ 20 | public class IsMaximum extends TemporalMatcher { 21 | 22 | private static final String SPLIT_ON_UPPERCASE_REGEX = "(?=[A-Z])"; 23 | 24 | private final TemporalConverter converter; 25 | private final ChronoField field; 26 | private final Locale locale; 27 | private final Optional zone; 28 | 29 | public IsMaximum(TemporalConverter converter, 30 | ChronoField field, 31 | Optional zone, 32 | Locale locale) { 33 | this.converter = converter; 34 | this.field = field; 35 | this.locale = locale; 36 | this.zone = zone; 37 | } 38 | 39 | public IsMaximum(TemporalConverter converter, ChronoField field) { 40 | this(converter, field, Optional.empty(), Locale.getDefault(Locale.Category.FORMAT)); 41 | } 42 | 43 | @Override 44 | protected boolean matchesSafely(final T actual, final Description mismatchDesc) { 45 | TemporalAccessor actualTemporal = converter.apply(actual, zone); 46 | ValueRange actualRange = field.rangeRefinedBy(actualTemporal); 47 | long actualValue = field.getFrom(actualTemporal), expectedValue = actualRange.getMaximum(); 48 | if (expectedValue != actualValue) { 49 | mismatchDesc.appendText("date has the value " + actualValue + " instead of " + expectedValue); 50 | return false; 51 | } else { 52 | return true; 53 | } 54 | } 55 | 56 | @Override 57 | public void describeTo(final Description description) { 58 | description.appendText("the date has the maximum value for " 59 | + Stream.of(field.getDisplayName(locale).split(SPLIT_ON_UPPERCASE_REGEX)) 60 | .map(String::toLowerCase) 61 | .collect(joining(" "))); 62 | } 63 | 64 | @Override 65 | public TemporalMatcher atZone(ZoneId zone) { 66 | return new IsMaximum<>(converter, field, Optional.of(zone), locale); 67 | } 68 | 69 | @Override 70 | public TemporalMatcher atLocale(Locale locale) { 71 | return new IsMaximum<>(converter, field, zone, locale); 72 | } 73 | } -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/IsMillisecond.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import java.time.ZoneId; 4 | import java.util.Locale; 5 | import java.util.Optional; 6 | 7 | import org.exparity.hamcrest.date.core.types.Millisecond; 8 | import org.hamcrest.Description; 9 | 10 | /** 11 | * A matcher that tests that the examined date is on the specified minute 12 | * 13 | * @author Stewart Bissett 14 | */ 15 | public class IsMillisecond extends TemporalMatcher { 16 | 17 | private final TemporalConverter converter; 18 | private final TemporalProvider expected; 19 | private final Locale locale; 20 | private final Optional zone; 21 | 22 | public IsMillisecond(TemporalConverter converter, 23 | TemporalProvider expected, 24 | Optional zone, 25 | Locale locale) { 26 | this.expected = expected; 27 | this.converter = converter; 28 | this.locale = locale; 29 | this.zone = zone; 30 | } 31 | 32 | public IsMillisecond(TemporalConverter converter, TemporalProvider expected) { 33 | this(converter, expected, Optional.empty(), Locale.getDefault(Locale.Category.FORMAT)); 34 | } 35 | 36 | @Override 37 | protected boolean matchesSafely(final T actual, final Description mismatchDescription) { 38 | Millisecond expectedValue = expected.apply(zone), actualValue = converter.apply(actual, zone); 39 | if (!expectedValue.equals(actualValue)) { 40 | mismatchDescription.appendText("the date has the millisecond " + actualValue); 41 | return false; 42 | } else { 43 | return true; 44 | } 45 | } 46 | 47 | @Override 48 | public void describeTo(final Description description) { 49 | description.appendText("the date has the millisecond " + expected.apply(zone)); 50 | } 51 | 52 | @Override 53 | public TemporalMatcher atZone(ZoneId zone) { 54 | return new IsMillisecond<>(converter, expected, Optional.of(zone), locale); 55 | } 56 | 57 | @Override 58 | public TemporalMatcher atLocale(Locale locale) { 59 | return new IsMillisecond<>(converter, expected, zone, locale); 60 | } 61 | } -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/IsMinimum.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import static java.util.stream.Collectors.joining; 4 | 5 | import java.time.ZoneId; 6 | import java.time.temporal.ChronoField; 7 | import java.time.temporal.TemporalAccessor; 8 | import java.time.temporal.ValueRange; 9 | import java.util.Locale; 10 | import java.util.Optional; 11 | import java.util.stream.Stream; 12 | 13 | import org.hamcrest.Description; 14 | 15 | /** 16 | * A base matcher that tests that the examined date has the maximum value for the given date part 17 | * 18 | * @author Stewart Bissett 19 | */ 20 | public class IsMinimum extends TemporalMatcher { 21 | 22 | private static final String SPLIT_ON_UPPERCASE_REGEX = "(?=[A-Z])"; 23 | 24 | private final TemporalConverter converter; 25 | private final ChronoField field; 26 | private final Locale locale; 27 | private final Optional zone; 28 | 29 | public IsMinimum(TemporalConverter converter, ChronoField field, Optional zone, Locale locale) { 30 | this.converter = converter; 31 | this.field = field; 32 | this.locale = locale; 33 | this.zone = zone; 34 | } 35 | 36 | public IsMinimum(TemporalConverter converter, ChronoField field) { 37 | this(converter, field, Optional.empty(), Locale.getDefault(Locale.Category.FORMAT)); 38 | } 39 | 40 | @Override 41 | protected boolean matchesSafely(final T actual, final Description mismatchDesc) { 42 | TemporalAccessor actualTemporal = converter.apply(actual, zone); 43 | ValueRange actualRange = field.rangeRefinedBy(actualTemporal); 44 | long actualValue = field.getFrom(actualTemporal), expectedValue = actualRange.getMinimum(); 45 | if (expectedValue != actualValue) { 46 | mismatchDesc.appendText("date has the value " + actualValue + " instead of " + expectedValue); 47 | return false; 48 | } else { 49 | return true; 50 | } 51 | } 52 | 53 | @Override 54 | public void describeTo(final Description description) { 55 | description.appendText("the date has the minimum value for " 56 | + Stream.of(field.getDisplayName(locale).split(SPLIT_ON_UPPERCASE_REGEX)) 57 | .map(String::toLowerCase) 58 | .collect(joining(" "))); 59 | } 60 | 61 | @Override 62 | public TemporalMatcher atZone(ZoneId zone) { 63 | return new IsMinimum<>(converter, field, Optional.of(zone), locale); 64 | } 65 | 66 | @Override 67 | public TemporalMatcher atLocale(Locale locale) { 68 | return new IsMinimum<>(converter, field, zone, locale); 69 | } 70 | } -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/IsMinute.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import java.time.ZoneId; 4 | import java.util.Locale; 5 | import java.util.Optional; 6 | 7 | import org.exparity.hamcrest.date.core.types.Minute; 8 | import org.hamcrest.Description; 9 | 10 | /** 11 | * A matcher that tests that the examined date is on the specified minute 12 | * 13 | * @author Stewart Bissett 14 | */ 15 | public class IsMinute extends TemporalMatcher { 16 | 17 | private final TemporalConverter converter; 18 | private final TemporalProvider expected; 19 | private final Locale locale; 20 | private final Optional zone; 21 | 22 | public IsMinute(TemporalConverter converter, 23 | TemporalProvider expected, 24 | Optional zone, 25 | Locale locale) { 26 | this.expected = expected; 27 | this.converter = converter; 28 | this.locale = locale; 29 | this.zone = zone; 30 | } 31 | 32 | public IsMinute(TemporalConverter converter, TemporalProvider expected) { 33 | this(converter, expected, Optional.empty(), Locale.getDefault(Locale.Category.FORMAT)); 34 | } 35 | 36 | @Override 37 | protected boolean matchesSafely(final T actual, final Description mismatchDescription) { 38 | Minute expectedValue = expected.apply(zone), actualValue = converter.apply(actual, zone); 39 | if (!expectedValue.equals(actualValue)) { 40 | mismatchDescription.appendText("the date has the minute " + actualValue); 41 | return false; 42 | } else { 43 | return true; 44 | } 45 | } 46 | 47 | @Override 48 | public void describeTo(final Description description) { 49 | description.appendText("the date has the minute " + expected.apply(zone)); 50 | } 51 | 52 | @Override 53 | public TemporalMatcher atZone(ZoneId zone) { 54 | return new IsMinute<>(converter, expected, Optional.of(zone), locale); 55 | } 56 | 57 | @Override 58 | public TemporalMatcher atLocale(Locale locale) { 59 | return new IsMinute<>(converter, expected, zone, locale); 60 | } 61 | } -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/IsMonth.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import java.time.Month; 4 | import java.time.ZoneId; 5 | import java.time.format.TextStyle; 6 | import java.util.Locale; 7 | import java.util.Optional; 8 | 9 | import org.hamcrest.Description; 10 | 11 | /** 12 | * A matcher that tests that the examined date is on the same month of the year as the reference date 13 | * 14 | * @author Stewart Bissett 15 | */ 16 | public class IsMonth extends TemporalMatcher { 17 | 18 | private final TemporalConverter converter; 19 | private final TemporalProvider expected; 20 | private final Locale locale; 21 | private final Optional zone; 22 | 23 | public IsMonth(TemporalConverter converter, 24 | TemporalProvider expected, 25 | Optional zone, 26 | Locale locale) { 27 | this.expected = expected; 28 | this.converter = converter; 29 | this.locale = locale; 30 | this.zone = zone; 31 | } 32 | 33 | public IsMonth(TemporalConverter converter, TemporalProvider expected) { 34 | this(converter, expected, Optional.empty(), Locale.getDefault(Locale.Category.FORMAT)); 35 | } 36 | 37 | @Override 38 | protected boolean matchesSafely(final T actual, final Description mismatchDescription) { 39 | Month expectedValue = expected.apply(zone), actualValue = converter.apply(actual, zone); 40 | if (!expectedValue.equals(actualValue)) { 41 | mismatchDescription.appendText("the date has the month " + describe(actualValue)); 42 | return false; 43 | } else { 44 | return true; 45 | } 46 | } 47 | 48 | @Override 49 | public void describeTo(final Description description) { 50 | description.appendText("the date has the month " + describe(expected.apply(zone))); 51 | } 52 | 53 | @Override 54 | public TemporalMatcher atZone(ZoneId zone) { 55 | return new IsMonth<>(converter, expected, Optional.of(zone), locale); 56 | } 57 | 58 | @Override 59 | public TemporalMatcher atLocale(Locale locale) { 60 | return new IsMonth<>(converter, expected, zone, locale); 61 | } 62 | 63 | private String describe(Month actualValue) { 64 | return actualValue.getDisplayName(TextStyle.FULL, locale); 65 | } 66 | 67 | } -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/IsSame.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import java.time.ZoneId; 4 | import java.util.Locale; 5 | import java.util.Optional; 6 | 7 | import org.hamcrest.Description; 8 | 9 | /** 10 | * A matcher that tests that the examined date is the same as the reference date 11 | * 12 | * @author Stewart Bissett 13 | */ 14 | public class IsSame extends TemporalMatcher { 15 | 16 | private final TemporalProvider expected; 17 | private final TemporalConverter converter; 18 | private final TemporalFunction functions; 19 | private final Locale locale; 20 | private final Optional zone; 21 | 22 | public IsSame(TemporalConverter converter, 23 | TemporalProvider expected, 24 | TemporalFunction functions, 25 | Optional zone, 26 | Locale locale) { 27 | this.expected = expected; 28 | this.converter = converter; 29 | this.functions = functions; 30 | this.locale = locale; 31 | this.zone = zone; 32 | } 33 | 34 | public IsSame(TemporalConverter converter, TemporalProvider expected, TemporalFunction functions) { 35 | this(converter, expected, functions, Optional.empty(), Locale.getDefault(Locale.Category.FORMAT)); 36 | } 37 | 38 | @Override 39 | protected boolean matchesSafely(final T actual, final Description mismatchDescription) { 40 | E expectedValue = expected.apply(zone), actualValue = converter.apply(actual, zone); 41 | if (!functions.isSame(expectedValue, actualValue)) { 42 | mismatchDescription.appendText("the date is " + functions.describe(actualValue, locale)); 43 | return false; 44 | } else { 45 | return true; 46 | } 47 | } 48 | 49 | @Override 50 | public void describeTo(final Description description) { 51 | description.appendText("the same date as " + functions.describe(expected.apply(zone), locale)); 52 | } 53 | 54 | @Override 55 | public TemporalMatcher atZone(ZoneId zone) { 56 | return new IsSame<>(converter, expected, functions, Optional.of(zone), locale); 57 | } 58 | 59 | @Override 60 | public TemporalMatcher atLocale(Locale locale) { 61 | return new IsSame<>(converter, expected, functions, zone, locale); 62 | } 63 | } -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/IsSameDay.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import java.time.LocalDate; 4 | import java.time.ZoneId; 5 | import java.util.Locale; 6 | import java.util.Optional; 7 | 8 | import org.hamcrest.Description; 9 | 10 | /** 11 | * A matcher that tests that the examined date is the same date as the reference 12 | * date 13 | * 14 | * @author Stewart Bissett 15 | */ 16 | public class IsSameDay extends TemporalMatcher { 17 | 18 | private final TemporalProvider expected; 19 | private final TemporalConverter converter; 20 | private final Locale locale; 21 | private final Optional zone; 22 | 23 | public IsSameDay(TemporalConverter converter, 24 | TemporalProvider expected, 25 | Optional zone, 26 | Locale locale) { 27 | this.expected = expected; 28 | this.converter = converter; 29 | this.locale = locale; 30 | this.zone = zone; 31 | } 32 | 33 | public IsSameDay(TemporalConverter converter, TemporalProvider expected) { 34 | this(converter, expected, Optional.empty(), Locale.getDefault(Locale.Category.FORMAT)); 35 | } 36 | 37 | @Override 38 | protected boolean matchesSafely(final T actual, final Description mismatchDescription) { 39 | LocalDate expectedValue = expected.apply(zone), actualValue = converter.apply(actual, zone); 40 | if (!expectedValue.isEqual(actualValue)) { 41 | mismatchDescription.appendText("the day is " + describe(actualValue)); 42 | return false; 43 | } else { 44 | return true; 45 | } 46 | } 47 | 48 | @Override 49 | public void describeTo(final Description description) { 50 | description.appendText("the same day as " + describe(expected.apply(zone))); 51 | } 52 | 53 | @Override 54 | public TemporalMatcher atZone(ZoneId zone) { 55 | return new IsSameDay<>(converter, expected, Optional.of(zone), locale); 56 | } 57 | 58 | @Override 59 | public TemporalMatcher atLocale(Locale locale) { 60 | return new IsSameDay<>(converter, expected, zone, locale); 61 | } 62 | 63 | private String describe(LocalDate actualValue) { 64 | return TemporalFunctions.LOCALDATE.describe(actualValue, locale); 65 | } 66 | 67 | } -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/IsSameOrAfter.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import java.time.ZoneId; 4 | import java.util.Locale; 5 | import java.util.Optional; 6 | 7 | import org.hamcrest.Description; 8 | 9 | /** 10 | * A matcher that tests that the examined date is before or the same instant as 11 | * the reference date 12 | * 13 | * @author Stewart Bissett 14 | */ 15 | public class IsSameOrAfter extends TemporalMatcher { 16 | 17 | private final TemporalProvider expected; 18 | private final TemporalConverter converter; 19 | private final TemporalFunction functions; 20 | private final Locale locale; 21 | private final Optional zone; 22 | 23 | public IsSameOrAfter(TemporalConverter converter, 24 | TemporalProvider expected, 25 | TemporalFunction functions, 26 | Optional zone, 27 | Locale locale) { 28 | this.expected = expected; 29 | this.converter = converter; 30 | this.functions = functions; 31 | this.locale = locale; 32 | this.zone = zone; 33 | } 34 | 35 | public IsSameOrAfter(TemporalConverter converter, TemporalProvider expected, TemporalFunction functions) { 36 | this(converter, expected, functions, Optional.empty(), Locale.getDefault(Locale.Category.FORMAT)); 37 | } 38 | 39 | @Override 40 | protected boolean matchesSafely(final T actual, final Description mismatchDescription) { 41 | E expectedValue = expected.apply(zone), actualValue = converter.apply(actual, zone); 42 | if (functions.isAfter(expectedValue, actualValue) ) { 43 | mismatchDescription.appendText("the date is " + functions.describe(actualValue, locale)); 44 | return false; 45 | } else { 46 | return true; 47 | } 48 | } 49 | 50 | @Override 51 | public void describeTo(final Description description) { 52 | description.appendText("the date is on the same date or after " + functions.describe(expected.apply(zone), locale)); 53 | } 54 | 55 | @Override 56 | public TemporalMatcher atZone(ZoneId zone) { 57 | return new IsSameOrAfter<>(converter, expected, functions, Optional.of(zone), locale); 58 | } 59 | 60 | @Override 61 | public TemporalMatcher atLocale(Locale locale) { 62 | return new IsSameOrAfter<>(converter, expected, functions, zone, locale); 63 | } 64 | } -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/IsSameOrBefore.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import java.time.ZoneId; 4 | import java.util.Locale; 5 | import java.util.Optional; 6 | 7 | import org.hamcrest.Description; 8 | 9 | /** 10 | * A matcher that tests that the examined date is before or the same instant as 11 | * the reference date 12 | * 13 | * @author Stewart Bissett 14 | */ 15 | public class IsSameOrBefore extends TemporalMatcher { 16 | 17 | private final TemporalProvider expected; 18 | private final TemporalConverter converter; 19 | private final TemporalFunction functions; 20 | private final Locale locale; 21 | private final Optional zone; 22 | 23 | public IsSameOrBefore(TemporalConverter converter, 24 | TemporalProvider expected, 25 | TemporalFunction functions, 26 | Optional zone, 27 | Locale locale) { 28 | this.expected = expected; 29 | this.converter = converter; 30 | this.functions = functions; 31 | this.locale = locale; 32 | this.zone = zone; 33 | } 34 | 35 | public IsSameOrBefore(TemporalConverter converter, TemporalProvider expected, TemporalFunction functions) { 36 | this(converter, expected, functions, Optional.empty(), Locale.getDefault(Locale.Category.FORMAT)); 37 | } 38 | 39 | @Override 40 | protected boolean matchesSafely(final T actual, final Description mismatchDescription) { 41 | E expectedValue = expected.apply(zone), actualValue = converter.apply(actual, zone); 42 | if (functions.isBefore(expectedValue, actualValue) ) { 43 | mismatchDescription.appendText("the date is " + functions.describe(actualValue, locale)); 44 | return false; 45 | } else { 46 | return true; 47 | } 48 | } 49 | 50 | @Override 51 | public void describeTo(final Description description) { 52 | description.appendText("the date is on the same date or before " + functions.describe(expected.apply(zone), locale)); 53 | } 54 | 55 | @Override 56 | public TemporalMatcher atZone(ZoneId zone) { 57 | return new IsSameOrBefore<>(converter, expected, functions, Optional.of(zone), locale); 58 | } 59 | 60 | @Override 61 | public TemporalMatcher atLocale(Locale locale) { 62 | return new IsSameOrBefore<>(converter, expected, functions, zone, locale); 63 | } 64 | } -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/IsSecond.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import java.time.ZoneId; 4 | import java.util.Locale; 5 | import java.util.Optional; 6 | 7 | import org.exparity.hamcrest.date.core.types.Second; 8 | import org.hamcrest.Description; 9 | 10 | /** 11 | * A matcher that tests that the examined date is on the specified second 12 | * 13 | * @author Stewart Bissett 14 | */ 15 | public class IsSecond extends TemporalMatcher { 16 | 17 | private final TemporalConverter converter; 18 | private final TemporalProvider expected; 19 | private final Locale locale; 20 | private final Optional zone; 21 | 22 | public IsSecond(TemporalConverter converter, 23 | TemporalProvider expected, 24 | Optional zone, 25 | Locale locale) { 26 | this.expected = expected; 27 | this.converter = converter; 28 | this.locale = locale; 29 | this.zone = zone; 30 | } 31 | 32 | public IsSecond(TemporalConverter converter, TemporalProvider expected) { 33 | this(converter, expected, Optional.empty(), Locale.getDefault(Locale.Category.FORMAT)); 34 | } 35 | 36 | @Override 37 | protected boolean matchesSafely(final T actual, final Description mismatchDescription) { 38 | Second expectedValue = expected.apply(zone), actualValue = converter.apply(actual, zone); 39 | if (!expectedValue.equals(actualValue)) { 40 | mismatchDescription.appendText("the date has the second " + actualValue); 41 | return false; 42 | } else { 43 | return true; 44 | } 45 | } 46 | 47 | @Override 48 | public void describeTo(final Description description) { 49 | description.appendText("the date has the second " + expected.apply(zone)); 50 | } 51 | 52 | @Override 53 | public TemporalMatcher atZone(ZoneId zone) { 54 | return new IsSecond<>(converter, expected, Optional.of(zone), locale); 55 | } 56 | 57 | @Override 58 | public TemporalMatcher atLocale(Locale locale) { 59 | return new IsSecond<>(converter, expected, zone, locale); 60 | } 61 | } -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/IsWithin.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import java.time.ZoneId; 4 | import java.util.Locale; 5 | import java.util.Optional; 6 | 7 | import org.exparity.hamcrest.date.core.types.Interval; 8 | import org.hamcrest.Description; 9 | 10 | /** 11 | * A matcher that tests that the examined date is within a defined period of the reference date 12 | * 13 | * @author Stewart Bissett 14 | */ 15 | public class IsWithin extends TemporalMatcher { 16 | 17 | private final Interval expectedInterval; 18 | private final TemporalProvider reference; 19 | private final TemporalConverter converter; 20 | private final TemporalFunction functions; 21 | private final Locale locale; 22 | private final Optional zone; 23 | 24 | public IsWithin(Interval interval, 25 | TemporalConverter converter, 26 | TemporalProvider reference, 27 | TemporalFunction functions, 28 | Optional zone, 29 | Locale locale) { 30 | this.expectedInterval = interval; 31 | this.converter = converter; 32 | this.reference = reference; 33 | this.functions = functions; 34 | this.locale = locale; 35 | this.zone = zone; 36 | } 37 | 38 | public IsWithin(Interval interval, 39 | TemporalConverter converter, 40 | TemporalProvider reference, 41 | TemporalFunction functions) { 42 | this(interval, 43 | converter, 44 | reference, 45 | functions, 46 | Optional.empty(), 47 | Locale.getDefault(Locale.Category.FORMAT)); 48 | } 49 | 50 | @Override 51 | protected boolean matchesSafely(final T actual, final Description mismatchDescription) { 52 | E referenceValue = reference.apply(zone), actualValue = converter.apply(actual, zone); 53 | Interval actualInterval = functions.interval(referenceValue, actualValue, expectedInterval.getUnit()); 54 | if (actualInterval.longerThan(expectedInterval)) { 55 | mismatchDescription.appendText("the date is " + functions.describe(actualValue, locale) + " and " 56 | + actualInterval.describe(locale) + " different"); 57 | return false; 58 | } else { 59 | return true; 60 | } 61 | } 62 | 63 | @Override 64 | public void describeTo(final Description description) { 65 | description.appendText("the date is within " + expectedInterval.describe(locale) + " of " 66 | + functions.describe(reference.apply(zone), locale)); 67 | } 68 | 69 | @Override 70 | public TemporalMatcher atZone(ZoneId zone) { 71 | return new IsWithin<>(expectedInterval, converter, reference, functions, Optional.of(zone), locale); 72 | } 73 | 74 | @Override 75 | public TemporalMatcher atLocale(Locale locale) { 76 | return new IsWithin<>(expectedInterval, converter, reference, functions, zone, locale); 77 | } 78 | } -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/IsYear.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import java.time.Year; 4 | import java.time.ZoneId; 5 | import java.util.Locale; 6 | import java.util.Optional; 7 | 8 | import org.hamcrest.Description; 9 | 10 | /** 11 | * A matcher that tests that the examined date is on the same year as the 12 | * reference date 13 | * 14 | * @author Stewart Bissett 15 | */ 16 | public class IsYear extends TemporalMatcher { 17 | 18 | private final TemporalConverter converter; 19 | private final TemporalProvider expected; 20 | private final Locale locale; 21 | private final Optional zone; 22 | 23 | public IsYear(TemporalConverter converter, 24 | TemporalProvider expected, 25 | Optional zone, 26 | Locale locale) { 27 | this.expected = expected; 28 | this.converter = converter; 29 | this.locale = locale; 30 | this.zone = zone; 31 | } 32 | 33 | public IsYear(TemporalConverter converter, TemporalProvider expected) { 34 | this(converter, expected, Optional.empty(), Locale.getDefault(Locale.Category.FORMAT)); 35 | } 36 | 37 | @Override 38 | protected boolean matchesSafely(final T actual, final Description mismatchDescription) { 39 | Year expectedValue = expected.apply(zone), actualValue = converter.apply(actual, zone); 40 | if (!expectedValue.equals(actualValue)) { 41 | mismatchDescription.appendText("the date has the year " + actualValue); 42 | return false; 43 | } else { 44 | return true; 45 | } 46 | } 47 | 48 | @Override 49 | public void describeTo(final Description description) { 50 | description.appendText("the date has the year " + expected.apply(zone)); 51 | } 52 | 53 | @Override 54 | public TemporalMatcher atZone(ZoneId zone) { 55 | return new IsYear<>(converter, expected, Optional.of(zone), locale); 56 | } 57 | 58 | @Override 59 | public TemporalMatcher atLocale(Locale locale) { 60 | return new IsYear<>(converter, expected, zone, locale); 61 | } 62 | } -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/TemporalConversionException.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | /** 4 | * Exception thrown when a temporal type cannot be converted to another 5 | */ 6 | public class TemporalConversionException extends RuntimeException { 7 | 8 | private static final long serialVersionUID = 1L; 9 | 10 | public TemporalConversionException(String message) { 11 | super(message); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/TemporalConverter.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import java.time.ZoneId; 4 | import java.util.Optional; 5 | 6 | /** 7 | * Convert one temporal type to another temporal type 8 | * 9 | * @author Stewart Bissett 10 | * 11 | * @param the source type 12 | * @param the result type 13 | */ 14 | @FunctionalInterface 15 | public interface TemporalConverter { 16 | 17 | /** 18 | * Convert one temporal type to another temporal type 19 | * @param source the source to convert 20 | * @param zone TODO 21 | * @return the source value as the target value 22 | */ 23 | public R apply(T source, Optional zone); 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/TemporalFunction.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import java.time.temporal.ChronoUnit; 4 | import java.util.Locale; 5 | 6 | import org.exparity.hamcrest.date.core.types.Interval; 7 | 8 | /** 9 | * Suite of temporal functions for a temporal type 10 | * 11 | * @author Stewart Bissett 12 | * 13 | * @param the temporal type 14 | */ 15 | public interface TemporalFunction { 16 | 17 | /** 18 | * Describe a temporal type 19 | * @param temporal the temporal instance to describe 20 | * @param locale the locale to describe the temporal in 21 | * @return a string description of the temporal 22 | */ 23 | String describe(T temporal, Locale locale); 24 | 25 | /** 26 | * Test if a temporal is the same as another temporal 27 | * @param temporal the temporal to test for 28 | * @param other the temporal to test against 29 | * @return a boolean 30 | */ 31 | boolean isSame(T temporal, T other); 32 | 33 | /** 34 | * Test if a temporal is after another temporal 35 | * @param temporal the temporal to test for 36 | * @param other the temporal to test against 37 | * @return a boolean 38 | */ 39 | boolean isAfter(T temporal, T other); 40 | 41 | /** 42 | * Test if a temporal is before another temporal 43 | * @param temporal the temporal to test for 44 | * @param other the temporal to test against 45 | * @return a boolean 46 | */ 47 | boolean isBefore(T temporal, T other); 48 | 49 | /** 50 | * Return the interval between two temporals 51 | * @param temporal the temporal to test for 52 | * @param other the temporal to test against 53 | * @param unit the unit to measure the interval in 54 | * @return an {@link Interval} 55 | */ 56 | Interval interval(T temporal, T other, ChronoUnit unit); 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/TemporalFunctions.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import java.time.Instant; 4 | import java.time.LocalDate; 5 | import java.time.LocalDateTime; 6 | import java.time.LocalTime; 7 | import java.time.OffsetDateTime; 8 | import java.time.ZonedDateTime; 9 | import java.util.Date; 10 | 11 | import org.exparity.hamcrest.date.core.function.DateFunction; 12 | import org.exparity.hamcrest.date.core.function.InstantFunction; 13 | import org.exparity.hamcrest.date.core.function.LocalDateFunction; 14 | import org.exparity.hamcrest.date.core.function.LocalDateTimeFunction; 15 | import org.exparity.hamcrest.date.core.function.LocalTimeFunction; 16 | import org.exparity.hamcrest.date.core.function.OffsetDateTimeFunction; 17 | import org.exparity.hamcrest.date.core.function.SqlDateFunction; 18 | import org.exparity.hamcrest.date.core.function.ZonedDateTimeFunction; 19 | 20 | /** 21 | * Static repository of {@link TemporalFunction} instances 22 | * 23 | * @author Stewart Bissett 24 | */ 25 | public class TemporalFunctions { 26 | 27 | private TemporalFunctions() {} 28 | 29 | public static TemporalFunction JAVADATE = new DateFunction(); 30 | public static TemporalFunction SQLDATE = new SqlDateFunction(); 31 | public static TemporalFunction LOCALDATE = new LocalDateFunction(); 32 | public static TemporalFunction LOCALTIME = new LocalTimeFunction(); 33 | public static TemporalFunction LOCALDATETIME = new LocalDateTimeFunction(); 34 | public static TemporalFunction ZONEDDATETIME = new ZonedDateTimeFunction(); 35 | public static TemporalFunction OFFSETDATETIME = new OffsetDateTimeFunction(); 36 | public static TemporalFunction INSTANT = new InstantFunction(); 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/TemporalMatcher.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import java.time.ZoneId; 4 | import java.time.ZoneOffset; 5 | import java.util.Locale; 6 | 7 | import org.hamcrest.TypeSafeDiagnosingMatcher; 8 | 9 | /** 10 | * Abstract {@link org.hamcrest.Matcher} for temporal objects allowing for time zone manipulation. 11 | * 12 | * @param the type of objects handled by this matcher 13 | * 14 | * @author Thomas Naskali 15 | */ 16 | public abstract class TemporalMatcher extends TypeSafeDiagnosingMatcher { 17 | 18 | /** 19 | * Creates a copy of this matcher using a specific time zone. 20 | * 21 | * @param zone a {@link ZoneId} 22 | * @return a copy of this matcher based on the new time zone 23 | */ 24 | public abstract TemporalMatcher atZone(ZoneId zone); 25 | 26 | /** 27 | * Creates a copy of this matcher using a specific locale. 28 | * 29 | * @param locale a {@link Locale} 30 | * @return a copy of this matcher using the new locale 31 | */ 32 | public abstract TemporalMatcher atLocale(Locale locale); 33 | 34 | /** 35 | * Creates a copy of this matcher using a specific time offset. 36 | * 37 | * @param offset the new time offset 38 | * @return a copy of this matcher based on the new time offset 39 | */ 40 | public TemporalMatcher atOffset(ZoneOffset offset) { 41 | return atZone(ZoneId.of(offset.getId())); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/TemporalProvider.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import java.time.ZoneId; 4 | import java.util.Optional; 5 | 6 | @FunctionalInterface 7 | public interface TemporalProvider { 8 | 9 | public S apply(final Optional zone); 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/function/DateFunction.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core.function; 2 | 3 | import java.text.SimpleDateFormat; 4 | import java.time.LocalDate; 5 | import java.time.ZoneId; 6 | import java.time.temporal.ChronoUnit; 7 | import java.util.Date; 8 | import java.util.Locale; 9 | 10 | import org.exparity.hamcrest.date.core.TemporalFunction; 11 | import org.exparity.hamcrest.date.core.types.Interval; 12 | 13 | /** 14 | * Implementation of {@link TemporalFunction} to wrap {@link Date} objects. 15 | * 16 | * @author Stewart Bissett 17 | */ 18 | public class DateFunction implements TemporalFunction { 19 | 20 | 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"; 21 | 22 | private static final String DATE_TIME_FORMAT = "EEE, dd MMM yyyy hh:mm:ss.SSS a"; 23 | private static final String DATE_FORMAT = "EEE, dd MMM yyyy"; 24 | 25 | @Override 26 | public boolean isAfter(final Date expected, final Date actual) { 27 | if (expected instanceof java.sql.Date || actual instanceof java.sql.Date) { 28 | return toLocalDate(expected).isAfter(toLocalDate(actual)); 29 | } else { 30 | return expected.after(actual); 31 | } 32 | } 33 | 34 | @Override 35 | public boolean isBefore(final Date expected, final Date actual) { 36 | if (expected instanceof java.sql.Date || actual instanceof java.sql.Date) { 37 | return toLocalDate(expected).isBefore(toLocalDate(actual)); 38 | } else { 39 | return expected.before(actual); 40 | } 41 | } 42 | 43 | @Override 44 | public boolean isSame(final Date expected, final Date actual) { 45 | if (expected instanceof java.sql.Date || actual instanceof java.sql.Date) { 46 | return toLocalDate(expected).equals(toLocalDate(actual)); 47 | } else { 48 | return expected.equals(actual); 49 | } 50 | } 51 | 52 | @Override 53 | public Interval interval(Date expected, Date actual, ChronoUnit unit) { 54 | if (expected instanceof java.sql.Date || actual instanceof java.sql.Date) { 55 | return Interval.of(toLocalDate(expected).until(toLocalDate(actual), unit), unit); 56 | } else { 57 | return Interval.of(expected.toInstant().until(actual.toInstant(), unit), unit); 58 | } 59 | } 60 | 61 | @Override 62 | public String describe(final Date temporal, final Locale locale) { 63 | if (temporal instanceof java.sql.Date) { 64 | return new SimpleDateFormat(DATE_FORMAT, locale).format(temporal); 65 | } else { 66 | return new SimpleDateFormat(DATE_TIME_FORMAT, locale).format(temporal); 67 | } 68 | } 69 | 70 | private LocalDate toLocalDate(Date expected) { 71 | if (expected instanceof java.sql.Date) { 72 | return ((java.sql.Date) expected).toLocalDate(); 73 | } else { 74 | return expected.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/function/InstantFunction.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core.function; 2 | 3 | import java.time.Instant; 4 | import java.time.format.DateTimeFormatter; 5 | import java.time.temporal.ChronoUnit; 6 | import java.util.Locale; 7 | 8 | import org.exparity.hamcrest.date.core.TemporalFunction; 9 | import org.exparity.hamcrest.date.core.types.Interval; 10 | 11 | /** 12 | * Implementation of {@link TemporalFunction} for {@link Instant} 13 | */ 14 | public final class InstantFunction implements TemporalFunction { 15 | 16 | @Override 17 | public boolean isSame(Instant temporal, Instant other) { 18 | return temporal.equals(other); 19 | } 20 | 21 | @Override 22 | public boolean isAfter(Instant temporal, Instant other) { 23 | return temporal.isAfter(other); 24 | } 25 | 26 | @Override 27 | public boolean isBefore(Instant temporal, Instant other) { 28 | return temporal.isBefore(other); 29 | } 30 | 31 | @Override 32 | public Interval interval(Instant expected, Instant other, ChronoUnit unit) { 33 | return Interval.of(expected.until(other, unit), unit); 34 | } 35 | 36 | @Override 37 | public String describe(Instant temporal, Locale locale) { 38 | return DateTimeFormatter.ISO_INSTANT.format(temporal); 39 | } 40 | } -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/function/LocalDateFunction.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core.function; 2 | 3 | import java.time.LocalDate; 4 | import java.time.format.DateTimeFormatter; 5 | import java.time.temporal.ChronoUnit; 6 | import java.util.Locale; 7 | 8 | import org.exparity.hamcrest.date.core.TemporalFunction; 9 | import org.exparity.hamcrest.date.core.types.Interval; 10 | 11 | /** 12 | * Implementation of {@link TemporalFunction} to wrap {@link LocalDate} objects. 13 | * 14 | * @author Stewart Bissett 15 | */ 16 | public class LocalDateFunction implements TemporalFunction { 17 | 18 | private static final String DATE_PATTERN = "EEE, dd MMM yyyy"; 19 | 20 | @Override 21 | public boolean isAfter(final LocalDate expected, final LocalDate actual) { 22 | return expected.isAfter(actual); 23 | } 24 | 25 | @Override 26 | public boolean isBefore(final LocalDate expected, final LocalDate actual) { 27 | return expected.isBefore(actual); 28 | } 29 | 30 | @Override 31 | public boolean isSame(final LocalDate expected, final LocalDate actual) { 32 | return expected.isEqual(actual); 33 | } 34 | 35 | @Override 36 | public Interval interval(LocalDate expected, LocalDate other, ChronoUnit unit) { 37 | return Interval.of(expected.until(other, unit), unit); 38 | } 39 | 40 | @Override 41 | public String describe(final LocalDate temporal, final Locale locale) { 42 | return temporal.format(DateTimeFormatter.ofPattern(DATE_PATTERN, locale)); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/function/LocalDateTimeFunction.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core.function; 2 | 3 | import java.time.LocalDateTime; 4 | import java.time.format.DateTimeFormatter; 5 | import java.time.temporal.ChronoUnit; 6 | import java.util.Locale; 7 | 8 | import org.exparity.hamcrest.date.core.TemporalFunction; 9 | import org.exparity.hamcrest.date.core.types.Interval; 10 | 11 | /** 12 | * Implementation of {@link TemporalFunction} for {@link LocalDateTime} objects. 13 | * 14 | * @author Stewart Bissett 15 | */ 16 | public class LocalDateTimeFunction implements TemporalFunction { 17 | 18 | private static final String DATE_TIME_PATTERN = "EEE, dd MMM yyyy hh:mm:ss.SSS a"; 19 | 20 | @Override 21 | public boolean isAfter(final LocalDateTime expected, final LocalDateTime actual) { 22 | return expected.isAfter(actual); 23 | } 24 | 25 | @Override 26 | public boolean isBefore(final LocalDateTime expected, final LocalDateTime actual) { 27 | return expected.isBefore(actual); 28 | } 29 | 30 | @Override 31 | public boolean isSame(final LocalDateTime expected, final LocalDateTime actual) { 32 | return expected.isEqual(actual); 33 | } 34 | 35 | @Override 36 | public Interval interval(LocalDateTime expected, LocalDateTime other, ChronoUnit unit) { 37 | return Interval.of(expected.until(other, unit), unit); 38 | } 39 | 40 | @Override 41 | public String describe(final LocalDateTime temporal, final Locale locale) { 42 | return temporal.format(DateTimeFormatter.ofPattern(DATE_TIME_PATTERN, locale)); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/function/LocalTimeFunction.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core.function; 2 | 3 | import java.time.LocalTime; 4 | import java.time.format.DateTimeFormatter; 5 | import java.time.temporal.ChronoUnit; 6 | import java.util.Locale; 7 | 8 | import org.exparity.hamcrest.date.core.TemporalFunction; 9 | import org.exparity.hamcrest.date.core.types.Interval; 10 | 11 | /** 12 | * Implementation of {@link TemporalFunction} to wrap {@link LocalTime} objects. 13 | * 14 | * @author Stewart Bissett 15 | */ 16 | public class LocalTimeFunction implements TemporalFunction { 17 | 18 | private static final String TIME_PATTERN = "hh:mm:ss a"; 19 | 20 | @Override 21 | public boolean isAfter(final LocalTime expected, final LocalTime actual) { 22 | return expected.isAfter(actual); 23 | } 24 | 25 | @Override 26 | public boolean isBefore(final LocalTime expected, final LocalTime actual) { 27 | return expected.isBefore(actual); 28 | } 29 | 30 | @Override 31 | public boolean isSame(final LocalTime expected, final LocalTime actual) { 32 | return expected.equals(actual); 33 | } 34 | 35 | @Override 36 | public Interval interval(LocalTime expected, LocalTime other, ChronoUnit unit) { 37 | return Interval.of(expected.until(other, unit), unit); 38 | } 39 | 40 | @Override 41 | public String describe(final LocalTime temporal, final Locale locale) { 42 | return temporal.format(DateTimeFormatter.ofPattern(TIME_PATTERN, locale)); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/function/OffsetDateTimeFunction.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core.function; 2 | 3 | import java.time.OffsetDateTime; 4 | import java.time.format.DateTimeFormatter; 5 | import java.time.temporal.ChronoUnit; 6 | import java.util.Locale; 7 | 8 | import org.exparity.hamcrest.date.core.TemporalFunction; 9 | import org.exparity.hamcrest.date.core.TemporalFunctions; 10 | import org.exparity.hamcrest.date.core.types.Interval; 11 | 12 | /** 13 | * Implementation of {@link TemporalFunctions} for {@link OffsetDateTime} objects. 14 | * 15 | * @author Stewart Bissett 16 | */ 17 | public class OffsetDateTimeFunction implements TemporalFunction { 18 | 19 | private static final String DATE_TIME_PATTERN = "EEE, dd MMM yyyy hh:mm:ss.SSS a Z"; 20 | 21 | @Override 22 | public boolean isAfter(final OffsetDateTime expected, final OffsetDateTime actual) { 23 | return expected.isAfter(actual); 24 | } 25 | 26 | @Override 27 | public boolean isBefore(final OffsetDateTime expected, final OffsetDateTime actual) { 28 | return expected.isBefore(actual); 29 | } 30 | 31 | @Override 32 | public boolean isSame(final OffsetDateTime expected, final OffsetDateTime actual) { 33 | return expected.isEqual(actual); 34 | } 35 | 36 | @Override 37 | public Interval interval(OffsetDateTime expected, OffsetDateTime other, ChronoUnit unit) { 38 | return Interval.of(expected.until(other, unit), unit); 39 | } 40 | 41 | @Override 42 | public String describe(final OffsetDateTime temporal, final Locale locale) { 43 | return temporal.format(DateTimeFormatter.ofPattern(DATE_TIME_PATTERN, locale)); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/function/SqlDateFunction.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core.function; 2 | 3 | import java.sql.Date; 4 | import java.text.SimpleDateFormat; 5 | import java.time.temporal.ChronoUnit; 6 | import java.util.Locale; 7 | 8 | import org.exparity.hamcrest.date.core.TemporalFunction; 9 | import org.exparity.hamcrest.date.core.types.Interval; 10 | 11 | /** 12 | * Implementation of {@link TemporalFunction} to wrap {@link Date} objects. 13 | * 14 | * @author Stewart Bissett 15 | */ 16 | public class SqlDateFunction implements TemporalFunction { 17 | 18 | private static final String DATE_FORMAT = "EEE, dd MMM yyyy"; 19 | 20 | @Override 21 | public boolean isAfter(final Date expected, final Date actual) { 22 | return expected.toLocalDate().isAfter(actual.toLocalDate()); 23 | } 24 | 25 | @Override 26 | public boolean isBefore(final Date expected, final Date actual) { 27 | return expected.toLocalDate().isBefore(actual.toLocalDate()); 28 | } 29 | 30 | @Override 31 | public boolean isSame(final Date expected, final Date actual) { 32 | return expected.toLocalDate().equals(actual.toLocalDate()); 33 | } 34 | 35 | @Override 36 | public Interval interval(Date temporal, Date other, ChronoUnit unit) { 37 | return Interval.of(temporal.toLocalDate().until(other.toLocalDate(), unit), unit); 38 | } 39 | 40 | @Override 41 | public String describe(final Date temporal, final Locale locale) { 42 | return new SimpleDateFormat(DATE_FORMAT, locale).format(temporal); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/function/ZonedDateTimeFunction.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core.function; 2 | 3 | import java.time.ZonedDateTime; 4 | import java.time.format.DateTimeFormatter; 5 | import java.time.temporal.ChronoUnit; 6 | import java.util.Locale; 7 | 8 | import org.exparity.hamcrest.date.core.TemporalFunction; 9 | import org.exparity.hamcrest.date.core.TemporalFunctions; 10 | import org.exparity.hamcrest.date.core.types.Interval; 11 | 12 | /** 13 | * Implementation of {@link TemporalFunctions} for {@link ZonedDateTime} objects. 14 | * 15 | * @author Stewart Bissett 16 | */ 17 | public class ZonedDateTimeFunction implements TemporalFunction { 18 | 19 | private static final String DATE_TIME_PATTERN = "EEE, dd MMM yyyy hh:mm:ss.SSS a Z"; 20 | 21 | @Override 22 | public boolean isAfter(final ZonedDateTime expected, final ZonedDateTime actual) { 23 | return expected.isAfter(actual); 24 | } 25 | 26 | @Override 27 | public boolean isBefore(final ZonedDateTime expected, final ZonedDateTime actual) { 28 | return expected.isBefore(actual); 29 | } 30 | 31 | @Override 32 | public boolean isSame(final ZonedDateTime expected, final ZonedDateTime actual) { 33 | return expected.isEqual(actual); 34 | } 35 | 36 | @Override 37 | public Interval interval(ZonedDateTime expected, ZonedDateTime other, ChronoUnit unit) { 38 | return Interval.of(expected.until(other, unit), unit); 39 | } 40 | 41 | @Override 42 | public String describe(final ZonedDateTime temporal, final Locale locale) { 43 | return temporal.format(DateTimeFormatter.ofPattern(DATE_TIME_PATTERN, locale)); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/types/DayOfMonth.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core.types; 2 | 3 | import java.time.temporal.ChronoField; 4 | import java.time.temporal.TemporalAccessor; 5 | 6 | /** 7 | * Value type to represent a day of a month. Instantiate via {@link DayOfMonth#of(int)} factory method. 8 | * 9 | * @author Stewart Bissett 10 | */ 11 | public class DayOfMonth { 12 | 13 | /** 14 | * Create an instance of an {@link DayOfMonth} 15 | */ 16 | public static DayOfMonth of(int value) { 17 | return new DayOfMonth(value); 18 | } 19 | 20 | /** 21 | * Create an instance of an {@link DayOfMonth} from a {@link TemporalAccessor} 22 | */ 23 | public static DayOfMonth from(TemporalAccessor temporal) { 24 | return new DayOfMonth(temporal.get(ChronoField.DAY_OF_MONTH)); 25 | } 26 | 27 | private final int value; 28 | 29 | private DayOfMonth(int value) { 30 | this.value = value; 31 | } 32 | 33 | public int getValue() { 34 | return value; 35 | } 36 | 37 | @Override 38 | public int hashCode() { 39 | return Integer.valueOf(value).hashCode(); 40 | } 41 | 42 | @Override 43 | public boolean equals(Object obj) { 44 | if ( obj == this ) return true; 45 | if ( obj == null || obj.getClass() != getClass()) return false; 46 | DayOfMonth that = (DayOfMonth) obj; 47 | return this.value == that.value; 48 | } 49 | 50 | @Override 51 | public String toString() { 52 | return String.valueOf(value); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/types/Hour.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core.types; 2 | 3 | import java.time.temporal.ChronoField; 4 | import java.time.temporal.TemporalAccessor; 5 | 6 | /** 7 | * Value type to represent an hour of the day. Instantiate via {@link Hour#of(int)} factory method. 8 | * 9 | * @author Stewart Bissett 10 | */ 11 | public class Hour { 12 | 13 | /** 14 | * Create an instance of an {@link Hour} 15 | */ 16 | public static Hour of(int value) { 17 | return new Hour(value); 18 | } 19 | 20 | /** 21 | * Create an instance of an {@link Hour} from a {@link TemporalAccessor} 22 | */ 23 | public static Hour from(TemporalAccessor temporal) { 24 | return new Hour(temporal.get(ChronoField.HOUR_OF_DAY)); 25 | } 26 | 27 | private final int value; 28 | 29 | private Hour(int value) { 30 | this.value = value; 31 | } 32 | 33 | public int getValue() { 34 | return value; 35 | } 36 | 37 | @Override 38 | public int hashCode() { 39 | return Integer.valueOf(value).hashCode(); 40 | } 41 | 42 | @Override 43 | public boolean equals(Object obj) { 44 | if ( obj == this ) return true; 45 | if ( obj == null || obj.getClass() != getClass()) return false; 46 | Hour that = (Hour) obj; 47 | return this.value == that.value; 48 | } 49 | 50 | @Override 51 | public String toString() { 52 | return String.valueOf(value); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/types/Interval.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core.types; 2 | 3 | import java.time.Duration; 4 | import java.time.Period; 5 | import java.time.temporal.ChronoUnit; 6 | import java.util.Locale; 7 | 8 | /** 9 | * Encapsulate a time or date interval. Equivalent to {@link Duration} and {@link Period} but supporting both time and 10 | * date elements 11 | * 12 | * @author Stewart Bissett 13 | */ 14 | public class Interval { 15 | 16 | /** 17 | * Create an {@link Interval} from a {@link Period} using a unit of {@link ChronoUnit#DAYS} 18 | */ 19 | public static Interval of(Period period) { 20 | return new Interval(period.getDays(), ChronoUnit.DAYS); 21 | } 22 | 23 | /** 24 | * Create an {@link Interval} from a {@link Period} using a unit of {@link ChronoUnit#DAYS} 25 | */ 26 | public static Interval of(Duration duration) { 27 | return new Interval(duration.getSeconds(), ChronoUnit.SECONDS); 28 | } 29 | 30 | /** 31 | * Create an {@link Interval} from an explicit period and {@link ChronoUnit} 32 | */ 33 | public static Interval of(long period, ChronoUnit unit) { 34 | return new Interval(period, unit); 35 | } 36 | 37 | private final long period; 38 | private final ChronoUnit unit; 39 | 40 | private Interval(long period, ChronoUnit unit) { 41 | this.period = Math.abs(period); 42 | this.unit = unit; 43 | } 44 | 45 | public ChronoUnit getUnit() { 46 | return unit; 47 | } 48 | 49 | public boolean longerThan(Interval other) { 50 | if ( unit == other.unit ) { 51 | return period > other.period; 52 | } else { 53 | throw new UnsupportedOperationException("Units are different. " + unit + " vs " + other.unit); 54 | } 55 | } 56 | 57 | public String describe(Locale locale) { 58 | return period + " " + unit.toString(); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/types/Millisecond.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core.types; 2 | 3 | import java.time.temporal.ChronoField; 4 | import java.time.temporal.TemporalAccessor; 5 | 6 | /** 7 | * Value type to represent a millisecond of a minute. Instantiate via {@link Millisecond#of(int)} factory method. 8 | * 9 | * @author Stewart Bissett 10 | */ 11 | public class Millisecond { 12 | 13 | private static final int NANOS_PER_MILLISECOND = 1000000; 14 | 15 | /** 16 | * Create an instance of an {@link Millisecond} 17 | */ 18 | public static Millisecond of(int value) { 19 | return new Millisecond(value); 20 | } 21 | 22 | /** 23 | * Create an instance of an {@link Millisecond} from a number of nanoseconds 24 | */ 25 | public static Millisecond ofNanos(int nano) { 26 | return Millisecond.of(nano/NANOS_PER_MILLISECOND); 27 | } 28 | 29 | /** 30 | * Create an instance of an {@link Millisecond} from a {@link TemporalAccessor} 31 | */ 32 | public static Millisecond from(TemporalAccessor temporal) { 33 | return new Millisecond(temporal.get(ChronoField.MILLI_OF_SECOND)); 34 | } 35 | 36 | private final int value; 37 | 38 | private Millisecond(int value) { 39 | this.value = value; 40 | } 41 | 42 | public int getValue() { 43 | return value; 44 | } 45 | 46 | @Override 47 | public int hashCode() { 48 | return Integer.valueOf(value).hashCode(); 49 | } 50 | 51 | @Override 52 | public boolean equals(Object obj) { 53 | if ( obj == this ) return true; 54 | if ( obj == null || obj.getClass() != getClass()) return false; 55 | Millisecond that = (Millisecond) obj; 56 | return this.value == that.value; 57 | } 58 | 59 | @Override 60 | public String toString() { 61 | return String.valueOf(value); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/types/Minute.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core.types; 2 | 3 | import java.time.temporal.ChronoField; 4 | import java.time.temporal.TemporalAccessor; 5 | 6 | /** 7 | * Value type to represent an minute of an hour. Instantiate via {@link Minute#of(int)} factory method. 8 | * 9 | * @author Stewart Bissett 10 | */ 11 | public class Minute { 12 | 13 | /** 14 | * Create an instance of an {@link Minute} 15 | */ 16 | public static Minute of(int value) { 17 | return new Minute(value); 18 | } 19 | 20 | /** 21 | * Create an instance of an {@link Minute} from a {@link TemporalAccessor} 22 | */ 23 | public static Minute from(TemporalAccessor temporal) { 24 | return new Minute(temporal.get(ChronoField.MINUTE_OF_HOUR)); 25 | } 26 | 27 | private final int value; 28 | 29 | private Minute(int value) { 30 | this.value = value; 31 | } 32 | 33 | public int getValue() { 34 | return value; 35 | } 36 | 37 | @Override 38 | public int hashCode() { 39 | return Integer.valueOf(value).hashCode(); 40 | } 41 | 42 | @Override 43 | public boolean equals(Object obj) { 44 | if ( obj == this ) return true; 45 | if ( obj == null || obj.getClass() != getClass()) return false; 46 | Minute that = (Minute) obj; 47 | return this.value == that.value; 48 | } 49 | 50 | @Override 51 | public String toString() { 52 | return String.valueOf(value); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/core/types/Second.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core.types; 2 | 3 | import java.time.temporal.ChronoField; 4 | import java.time.temporal.TemporalAccessor; 5 | 6 | /** 7 | * Value type to represent an second of a minute. Instantiate via {@link Second#of(int)} factory method. 8 | * 9 | * @author Stewart Bissett 10 | */ 11 | public class Second { 12 | 13 | /** 14 | * Create an instance of an {@link Second} 15 | */ 16 | public static Second of(int value) { 17 | return new Second(value); 18 | } 19 | 20 | /** 21 | * Create an instance of an {@link Second} from a {@link TemporalAccessor} 22 | */ 23 | public static Second from(TemporalAccessor temporal) { 24 | return new Second(temporal.get(ChronoField.SECOND_OF_MINUTE)); 25 | } 26 | 27 | private final int value; 28 | 29 | private Second(int value) { 30 | this.value = value; 31 | } 32 | 33 | public int getValue() { 34 | return value; 35 | } 36 | 37 | @Override 38 | public int hashCode() { 39 | return Integer.valueOf(value).hashCode(); 40 | } 41 | 42 | @Override 43 | public boolean equals(Object obj) { 44 | if ( obj == this ) return true; 45 | if ( obj == null || obj.getClass() != getClass()) return false; 46 | Second that = (Second) obj; 47 | return this.value == that.value; 48 | } 49 | 50 | @Override 51 | public String toString() { 52 | return String.valueOf(value); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/org/exparity/hamcrest/date/package.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |

Matchers of Dates

6 | 7 | 8 | -------------------------------------------------------------------------------- /src/test/java/org/exparity/hamcrest/date/MomentsTest.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date; 2 | 3 | import static org.exparity.hamcrest.date.DateMatchers.isToday; 4 | import static org.hamcrest.MatcherAssert.assertThat; 5 | import static org.hamcrest.Matchers.equalTo; 6 | 7 | import java.time.LocalDate; 8 | 9 | import org.testng.annotations.Test; 10 | 11 | /** 12 | * @author Stewart Bissett 13 | */ 14 | @SuppressWarnings("deprecation") 15 | public class MomentsTest { 16 | 17 | @Test 18 | public void canDefineNow() { 19 | assertThat(Moments.now(), isToday()); 20 | } 21 | 22 | @Test 23 | public void canDefineToday() { 24 | LocalDate today = LocalDate.now(); 25 | assertThat( 26 | Moments.today(), 27 | equalTo( 28 | new DayMonthYear( 29 | today.getDayOfMonth(), 30 | Months.fromCalendar(today.getMonthValue()-1), 31 | today.getYear()))); 32 | } 33 | 34 | @Test 35 | public void canDefineTomorrow() { 36 | LocalDate tomorrow = LocalDate.now().plusDays(1); 37 | assertThat( 38 | Moments.tomorrow(), 39 | equalTo( 40 | new DayMonthYear( 41 | tomorrow.getDayOfMonth(), 42 | Months.fromCalendar(tomorrow.getMonthValue()-1), 43 | tomorrow.getYear()))); 44 | } 45 | 46 | @Test 47 | public void canDefineYesterday() { 48 | LocalDate yesterday = LocalDate.now().minusDays(1); 49 | assertThat( 50 | Moments.yesterday(), 51 | equalTo( 52 | new DayMonthYear( 53 | yesterday.getDayOfMonth(), 54 | Months.fromCalendar(yesterday.getMonthValue()-1), 55 | yesterday.getYear()))); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/test/java/org/exparity/hamcrest/date/MonthsTest.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date; 2 | 3 | import static org.junit.Assert.assertThat; 4 | 5 | import org.hamcrest.Matchers; 6 | import org.testng.annotations.Test; 7 | 8 | /** 9 | * @author Stewart Bissett 10 | */ 11 | @SuppressWarnings("deprecation") 12 | public class MonthsTest { 13 | 14 | @Test 15 | public void canCreateJanuaryFromCalendar() { 16 | assertThat(Months.fromCalendar(0), Matchers.equalTo(Months.JAN)); 17 | } 18 | 19 | @Test 20 | public void canCreateFebruaryFromCalendar() { 21 | assertThat(Months.fromCalendar(1), Matchers.equalTo(Months.FEB)); 22 | } 23 | 24 | @Test 25 | public void canCreateMarchFromCalendar() { 26 | assertThat(Months.fromCalendar(2), Matchers.equalTo(Months.MAR)); 27 | } 28 | 29 | @Test 30 | public void canCreateAprilFromCalendar() { 31 | assertThat(Months.fromCalendar(3), Matchers.equalTo(Months.APR)); 32 | } 33 | 34 | @Test 35 | public void canCreateMayFromCalendar() { 36 | assertThat(Months.fromCalendar(4), Matchers.equalTo(Months.MAY)); 37 | } 38 | 39 | @Test 40 | public void canCreateJuneFromCalendar() { 41 | assertThat(Months.fromCalendar(5), Matchers.equalTo(Months.JUN)); 42 | } 43 | 44 | @Test 45 | public void canCreateJulyFromCalendar() { 46 | assertThat(Months.fromCalendar(6), Matchers.equalTo(Months.JUL)); 47 | } 48 | 49 | @Test 50 | public void canCreateAugustFromCalendar() { 51 | assertThat(Months.fromCalendar(7), Matchers.equalTo(Months.AUG)); 52 | } 53 | 54 | @Test 55 | public void canCreateSeptembeFromCalendar() { 56 | assertThat(Months.fromCalendar(8), Matchers.equalTo(Months.SEP)); 57 | } 58 | 59 | @Test 60 | public void canCreateOctoberFromCalendar() { 61 | assertThat(Months.fromCalendar(9), Matchers.equalTo(Months.OCT)); 62 | } 63 | 64 | @Test 65 | public void canCreateNovembeFromCalendar() { 66 | assertThat(Months.fromCalendar(10), Matchers.equalTo(Months.NOV)); 67 | } 68 | 69 | @Test 70 | public void canCreateDecemberFromCalendar() { 71 | assertThat(Months.fromCalendar(11), Matchers.equalTo(Months.DEC)); 72 | } 73 | 74 | @Test(expectedExceptions = IllegalArgumentException.class) 75 | public void canErrorIfMonthLow() { 76 | Months.fromCalendar(-1); 77 | } 78 | 79 | @Test(expectedExceptions = IllegalArgumentException.class) 80 | public void canErrorIfMonthHigh() { 81 | Months.fromCalendar(12); 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /src/test/java/org/exparity/hamcrest/date/WeekDaysTest.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date; 2 | 3 | import static org.exparity.hamcrest.date.Weekdays.*; 4 | import static org.junit.Assert.assertThat; 5 | 6 | import java.time.DayOfWeek; 7 | import java.util.Calendar; 8 | 9 | import org.hamcrest.Matchers; 10 | import org.testng.annotations.Test; 11 | 12 | /** 13 | * @author Stewart Bissett 14 | */ 15 | @SuppressWarnings("deprecation") 16 | public class WeekDaysTest { 17 | 18 | @Test 19 | public void canDefineMonday() { 20 | assertThat(MONDAY.getAsCalendarConstant(), Matchers.equalTo(Calendar.MONDAY)); 21 | assertThat(MONDAY.getAsDayOfWeek(), Matchers.equalTo(DayOfWeek.MONDAY)); 22 | assertThat(MONDAY.describe(), Matchers.equalTo("Monday")); 23 | } 24 | 25 | @Test 26 | public void canDefineTuesday() { 27 | assertThat(TUESDAY.getAsCalendarConstant(), Matchers.equalTo(Calendar.TUESDAY)); 28 | assertThat(TUESDAY.getAsDayOfWeek(), Matchers.equalTo(DayOfWeek.TUESDAY)); 29 | assertThat(TUESDAY.describe(), Matchers.equalTo("Tuesday")); 30 | } 31 | 32 | @Test 33 | public void canDefineWednesday() { 34 | assertThat(WEDNESDAY.getAsCalendarConstant(), Matchers.equalTo(Calendar.WEDNESDAY)); 35 | assertThat(WEDNESDAY.getAsDayOfWeek(), Matchers.equalTo(DayOfWeek.WEDNESDAY)); 36 | assertThat(WEDNESDAY.describe(), Matchers.equalTo("Wednesday")); 37 | } 38 | 39 | @Test 40 | public void canDefineThursday() { 41 | assertThat(THURSDAY.getAsCalendarConstant(), Matchers.equalTo(Calendar.THURSDAY)); 42 | assertThat(THURSDAY.getAsDayOfWeek(), Matchers.equalTo(DayOfWeek.THURSDAY)); 43 | assertThat(THURSDAY.describe(), Matchers.equalTo("Thursday")); 44 | } 45 | 46 | @Test 47 | public void canDefineFriday() { 48 | assertThat(FRIDAY.getAsCalendarConstant(), Matchers.equalTo(Calendar.FRIDAY)); 49 | assertThat(FRIDAY.getAsDayOfWeek(), Matchers.equalTo(DayOfWeek.FRIDAY)); 50 | assertThat(FRIDAY.describe(), Matchers.equalTo("Friday")); 51 | } 52 | 53 | @Test 54 | public void canDefineSaturday() { 55 | assertThat(SATURDAY.getAsCalendarConstant(), Matchers.equalTo(Calendar.SATURDAY)); 56 | assertThat(SATURDAY.getAsDayOfWeek(), Matchers.equalTo(DayOfWeek.SATURDAY)); 57 | assertThat(SATURDAY.describe(), Matchers.equalTo("Saturday")); 58 | } 59 | 60 | @Test 61 | public void canDefineSunday() { 62 | assertThat(SUNDAY.getAsCalendarConstant(), Matchers.equalTo(Calendar.SUNDAY)); 63 | assertThat(SUNDAY.getAsDayOfWeek(), Matchers.equalTo(DayOfWeek.SUNDAY)); 64 | assertThat(SUNDAY.describe(), Matchers.equalTo("Sunday")); 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /src/test/java/org/exparity/hamcrest/date/core/IsDayOfMonthTest.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import static org.exparity.hamcrest.date.testutils.Dates.*; 4 | import static org.hamcrest.MatcherAssert.assertThat; 5 | 6 | import org.exparity.hamcrest.date.DateMatchers; 7 | import org.exparity.hamcrest.date.InstantMatchers; 8 | import org.exparity.hamcrest.date.LocalDateMatchers; 9 | import org.exparity.hamcrest.date.LocalDateTimeMatchers; 10 | import org.exparity.hamcrest.date.OffsetDateTimeMatchers; 11 | import org.exparity.hamcrest.date.SqlDateMatchers; 12 | import org.exparity.hamcrest.date.ZonedDateTimeMatchers; 13 | import org.testng.annotations.Test; 14 | 15 | /** 16 | * @author Stewart Bissett 17 | */ 18 | public class IsDayOfMonthTest { 19 | 20 | private static final String ASSERTION_PATTERN = "\\sExpected: the date has the day of month [0-9]+?\\s but: the date has the day of month [0-9]+?"; 21 | 22 | // Date Matchers 23 | @Test 24 | public void isDateFirstDayOfMonth() { 25 | assertThat(AUG_01_2015_NOON_UTC_AS_DATE, DateMatchers.isDayOfMonth(1)); 26 | } 27 | 28 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 29 | public void isDateNotFirstDayOfMonth() { 30 | assertThat(AUG_31_2015_NOON_UTC_AS_DATE, DateMatchers.isDayOfMonth(1)); 31 | } 32 | 33 | @Test 34 | public void isDateLastDayOfMonth() { 35 | assertThat(AUG_31_2015_NOON_UTC_AS_DATE, DateMatchers.isDayOfMonth(31)); 36 | } 37 | 38 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 39 | public void isDateNotLastDayOfMonth() { 40 | assertThat(AUG_01_2015_NOON_UTC_AS_DATE, DateMatchers.isDayOfMonth(31)); 41 | } 42 | 43 | // java.sql.Date Matchers 44 | @Test 45 | public void isSqlDateFirstDayOfMonth() { 46 | assertThat(AUG_01_2015_AS_SQL, SqlDateMatchers.isDayOfMonth(1)); 47 | } 48 | 49 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 50 | public void isSqlDateNotFirstDayOfMonth() { 51 | assertThat(AUG_31_2015_AS_SQL, SqlDateMatchers.isDayOfMonth(1)); 52 | } 53 | 54 | @Test 55 | public void isSqlDateLastDayOfMonth() { 56 | assertThat(AUG_31_2015_AS_SQL, SqlDateMatchers.isDayOfMonth(31)); 57 | } 58 | 59 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 60 | public void isSqlDateNotLastDayOfMonth() { 61 | assertThat(AUG_01_2015_AS_SQL, SqlDateMatchers.isDayOfMonth(31)); 62 | } 63 | 64 | @Test 65 | public void isSqlDateFirstDayOfMonthUsingDateMatcher() { 66 | assertThat(AUG_01_2015_AS_SQL, DateMatchers.isDayOfMonth(1)); 67 | } 68 | 69 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 70 | public void isSqlDateNotFirstDayOfMonthUsingDateMatcher() { 71 | assertThat(AUG_31_2015_AS_SQL, DateMatchers.isDayOfMonth(1)); 72 | } 73 | 74 | @Test 75 | public void isSqlDateLastDayOfMonthUsingDateMatcher() { 76 | assertThat(AUG_31_2015_AS_SQL, DateMatchers.isDayOfMonth(31)); 77 | } 78 | 79 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 80 | public void isSqlDateNotLastDayOfMonthUsingDateMatcher() { 81 | assertThat(AUG_01_2015_AS_SQL, DateMatchers.isDayOfMonth(31)); 82 | } 83 | 84 | // LocalDate Matchers 85 | @Test 86 | public void isLocalDateFirstDayOfMonth() { 87 | assertThat(AUG_01_2015, LocalDateMatchers.isDayOfMonth(1)); 88 | } 89 | 90 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 91 | public void isLocalDateNotFirstDayOfMonth() { 92 | assertThat(AUG_31_2015, LocalDateMatchers.isDayOfMonth(1)); 93 | } 94 | 95 | @Test 96 | public void isLocalDateLastDayOfMonth() { 97 | assertThat(AUG_31_2015, LocalDateMatchers.isDayOfMonth(31)); 98 | } 99 | 100 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 101 | public void isLocalDateNotLastDayOfMonth() { 102 | assertThat(AUG_01_2015, LocalDateMatchers.isDayOfMonth(31)); 103 | } 104 | 105 | // LocalDateTime Matchers 106 | @Test 107 | public void isLocalDateTimeFirstDayOfMonth() { 108 | assertThat(AUG_01_2015_NOON, LocalDateTimeMatchers.isDayOfMonth(1)); 109 | } 110 | 111 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 112 | public void isLocalDateTimeNotFirstDayOfMonth() { 113 | assertThat(AUG_31_2015_NOON, LocalDateTimeMatchers.isDayOfMonth(1)); 114 | } 115 | 116 | @Test 117 | public void isLocalDateTimeLastDayOfMonth() { 118 | assertThat(AUG_31_2015_NOON, LocalDateTimeMatchers.isDayOfMonth(31)); 119 | } 120 | 121 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 122 | public void isLocalDateTimeNotLastDayOfMonth() { 123 | assertThat(AUG_01_2015_NOON, LocalDateTimeMatchers.isDayOfMonth(31)); 124 | } 125 | 126 | // ZonedDateTime Matchers 127 | @Test 128 | public void isZonedDateTimeFirstDayOfMonth() { 129 | assertThat(AUG_01_2015_NOON_UTC, ZonedDateTimeMatchers.isDayOfMonth(1)); 130 | } 131 | 132 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 133 | public void isZonedDateTimeNotFirstDayOfMonth() { 134 | assertThat(AUG_31_2015_NOON_UTC, ZonedDateTimeMatchers.isDayOfMonth(1)); 135 | } 136 | 137 | @Test 138 | public void isZonedDateTimeLastDayOfMonth() { 139 | assertThat(AUG_31_2015_NOON_UTC, ZonedDateTimeMatchers.isDayOfMonth(31)); 140 | } 141 | 142 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 143 | public void isZonedDateTimeNotLastDayOfMonth() { 144 | assertThat(AUG_01_2015_NOON_UTC, ZonedDateTimeMatchers.isDayOfMonth(31)); 145 | } 146 | 147 | // OffsetDateTime Matchers 148 | @Test 149 | public void isOffsetDateTimeFirstDayOfMonth() { 150 | assertThat(AUG_01_2015_NOON_OFFSET_UTC, OffsetDateTimeMatchers.isDayOfMonth(1)); 151 | } 152 | 153 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 154 | public void isOffsetDateTimeNotFirstDayOfMonth() { 155 | assertThat(AUG_31_2015_NOON_OFFSET_UTC, OffsetDateTimeMatchers.isDayOfMonth(1)); 156 | } 157 | 158 | @Test 159 | public void isOffsetDateTimeLastDayOfMonth() { 160 | assertThat(AUG_31_2015_NOON_OFFSET_UTC, OffsetDateTimeMatchers.isDayOfMonth(31)); 161 | } 162 | 163 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 164 | public void isOffsetDateTimeNotLastDayOfMonth() { 165 | assertThat(AUG_01_2015_NOON_OFFSET_UTC, OffsetDateTimeMatchers.isDayOfMonth(31)); 166 | } 167 | 168 | // Instant Matchers 169 | @Test 170 | public void isInstantFirstDayOfMonth() { 171 | assertThat(AUG_01_2015_NOON_INSTANT_UTC, InstantMatchers.isDayOfMonth(1)); 172 | } 173 | 174 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 175 | public void isInstantNotFirstDayOfMonth() { 176 | assertThat(AUG_31_2015_NOON_INSTANT_UTC, InstantMatchers.isDayOfMonth(1)); 177 | } 178 | 179 | @Test 180 | public void isInstantLastDayOfMonth() { 181 | assertThat(AUG_31_2015_NOON_INSTANT_UTC, InstantMatchers.isDayOfMonth(31)); 182 | } 183 | 184 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 185 | public void isInstantNotLastDayOfMonth() { 186 | assertThat(AUG_01_2015_NOON_INSTANT_UTC, InstantMatchers.isDayOfMonth(31)); 187 | } 188 | 189 | } 190 | -------------------------------------------------------------------------------- /src/test/java/org/exparity/hamcrest/date/core/IsFirstDayOfMonthTest.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import static org.exparity.hamcrest.date.testutils.Dates.*; 4 | import static org.hamcrest.MatcherAssert.assertThat; 5 | 6 | import org.exparity.hamcrest.date.DateMatchers; 7 | import org.exparity.hamcrest.date.InstantMatchers; 8 | import org.exparity.hamcrest.date.LocalDateMatchers; 9 | import org.exparity.hamcrest.date.LocalDateTimeMatchers; 10 | import org.exparity.hamcrest.date.OffsetDateTimeMatchers; 11 | import org.exparity.hamcrest.date.SqlDateMatchers; 12 | import org.exparity.hamcrest.date.ZonedDateTimeMatchers; 13 | import org.testng.annotations.Test; 14 | 15 | /** 16 | * @author Stewart Bissett 17 | */ 18 | public class IsFirstDayOfMonthTest { 19 | 20 | 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"; 21 | 22 | // Date Matchers 23 | @Test 24 | public void isDateFirstDayOfMonth() { 25 | assertThat(AUG_01_2015_NOON_UTC_AS_DATE, DateMatchers.isFirstDayOfMonth()); 26 | } 27 | 28 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 29 | public void isDateNotFirstDayOfMonth() { 30 | assertThat(AUG_31_2015_NOON_UTC_AS_DATE, DateMatchers.isFirstDayOfMonth()); 31 | } 32 | 33 | // java.sql.Date Matchers 34 | @Test 35 | public void isSqlDateFirstDayOfMonth() { 36 | assertThat(AUG_01_2015_AS_SQL, SqlDateMatchers.isFirstDayOfMonth()); 37 | } 38 | 39 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 40 | public void isSqlDateNotFirstDayOfMonth() { 41 | assertThat(AUG_31_2015_AS_SQL, SqlDateMatchers.isFirstDayOfMonth()); 42 | } 43 | 44 | // java.sql.Date Matchers 45 | @Test 46 | public void isSqlDateFirstDayOfMonthUsingDateMatchers() { 47 | assertThat(AUG_01_2015_AS_SQL, DateMatchers.isFirstDayOfMonth()); 48 | } 49 | 50 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 51 | public void isSqlDateNotFirstDayOfMonthUsingDateMatchers() { 52 | assertThat(AUG_31_2015_AS_SQL, DateMatchers.isFirstDayOfMonth()); 53 | } 54 | 55 | // LocalDate Matchers 56 | @Test 57 | public void isLocalDateFirstDayOfMonth() { 58 | assertThat(AUG_01_2015, LocalDateMatchers.isFirstDayOfMonth()); 59 | } 60 | 61 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 62 | public void isLocalDateNotFirstDayOfMonth() { 63 | assertThat(AUG_31_2015, LocalDateMatchers.isFirstDayOfMonth()); 64 | } 65 | 66 | // LocalDateTime Matchers 67 | @Test 68 | public void isLocalDateTimeFirstDayOfMonth() { 69 | assertThat(AUG_01_2015_NOON, LocalDateTimeMatchers.isFirstDayOfMonth()); 70 | } 71 | 72 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 73 | public void isLocalDateTimeNotFirstDayOfMonth() { 74 | assertThat(AUG_31_2015_NOON, LocalDateTimeMatchers.isFirstDayOfMonth()); 75 | } 76 | 77 | // ZonedDateTime Matchers 78 | @Test 79 | public void isZonedDateTimeFirstDayOfMonth() { 80 | assertThat(AUG_01_2015_NOON_UTC, ZonedDateTimeMatchers.isFirstDayOfMonth()); 81 | } 82 | 83 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 84 | public void isZonedDateTimeNotFirstDayOfMonth() { 85 | assertThat(AUG_31_2015_NOON_UTC, ZonedDateTimeMatchers.isFirstDayOfMonth()); 86 | } 87 | 88 | // OffsetDateTime Matchers 89 | @Test 90 | public void isOffsetDateTimeFirstDayOfMonth() { 91 | assertThat(AUG_01_2015_NOON_OFFSET_UTC, OffsetDateTimeMatchers.isFirstDayOfMonth()); 92 | } 93 | 94 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 95 | public void isOffsetDateTimeNotFirstDayOfMonth() { 96 | assertThat(AUG_31_2015_NOON_OFFSET_UTC, OffsetDateTimeMatchers.isFirstDayOfMonth()); 97 | } 98 | 99 | // Instant Matchers 100 | @Test 101 | public void isInstantFirstDayOfMonth() { 102 | assertThat(AUG_01_2015_NOON_INSTANT_UTC, InstantMatchers.isFirstDayOfMonth()); 103 | } 104 | 105 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 106 | public void isInstantNotFirstDayOfMonth() { 107 | assertThat(AUG_31_2015_NOON_INSTANT_UTC, InstantMatchers.isFirstDayOfMonth()); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /src/test/java/org/exparity/hamcrest/date/core/IsHourTest.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import static org.exparity.hamcrest.date.testutils.Dates.*; 4 | import static org.exparity.hamcrest.date.testutils.ZoneIds.UTC; 5 | import static org.hamcrest.MatcherAssert.assertThat; 6 | 7 | import java.time.LocalTime; 8 | 9 | import org.exparity.hamcrest.date.DateMatchers; 10 | import org.exparity.hamcrest.date.InstantMatchers; 11 | import org.exparity.hamcrest.date.LocalDateTimeMatchers; 12 | import org.exparity.hamcrest.date.LocalTimeMatchers; 13 | import org.exparity.hamcrest.date.OffsetDateTimeMatchers; 14 | import org.exparity.hamcrest.date.ZonedDateTimeMatchers; 15 | import org.exparity.hamcrest.date.testutils.ZoneIds; 16 | import org.testng.annotations.Test; 17 | 18 | /** 19 | * Unit Tests for the {@link IsHour} class 20 | * 21 | * @author Stewart Bissett 22 | */ 23 | @SuppressWarnings("deprecation") 24 | public class IsHourTest { 25 | 26 | private static final String ASSERTION_PATTERN = "\\sExpected: the date has the hour [0-9]+?\\s but: the date has the hour [0-9]+"; 27 | 28 | // Date Matchers 29 | @Test 30 | public void isDateHour() { 31 | assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.isHour(12).atZone(UTC)); 32 | } 33 | 34 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 35 | public void isDateNotHour() { 36 | assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.isHour(11).atZone(UTC)); 37 | } 38 | 39 | @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) 40 | public void isSqlDateHour() { 41 | assertThat(AUG_04_2015_AS_SQL, DateMatchers.isHour(12).atZone(UTC)); 42 | } 43 | 44 | @Test 45 | public void isDateSameHour() { 46 | assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.sameHour(12).atZone(UTC)); 47 | } 48 | 49 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 50 | public void isDateNotSameHour() { 51 | assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.sameHour(11).atZone(UTC)); 52 | } 53 | 54 | @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) 55 | public void isSqlDateSameHour() { 56 | assertThat(AUG_04_2015_AS_SQL, DateMatchers.sameHour(12).atZone(UTC)); 57 | } 58 | 59 | // LocalDateTime Matchers 60 | @Test 61 | public void isLocalDateTimeHour() { 62 | assertThat(AUG_04_2015_NOON, LocalDateTimeMatchers.isHour(12)); 63 | } 64 | 65 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 66 | public void isLocalDateTimeNotHour() { 67 | assertThat(AUG_04_2015_NOON, LocalDateTimeMatchers.isHour(11)); 68 | } 69 | 70 | // LocalTime Matchers 71 | @Test 72 | public void isLocalTimeHour() { 73 | assertThat(LocalTime.NOON, LocalTimeMatchers.isHour(12)); 74 | } 75 | 76 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 77 | public void isLocalTimeNotHour() { 78 | assertThat(LocalTime.NOON, LocalTimeMatchers.isHour(11)); 79 | } 80 | 81 | // ZonedDateTime Matchers 82 | 83 | @Test 84 | public void isZonedDateTimeHour() { 85 | assertThat(AUG_04_2015_NOON_UTC, ZonedDateTimeMatchers.isHour(12).atZone(ZoneIds.UTC)); 86 | } 87 | 88 | @Test 89 | public void isZonedDateTimeHourOtherZone() { 90 | assertThat(AUG_04_2015_NOON_UTC, ZonedDateTimeMatchers.isHour(7).atZone(ZoneIds.EST)); 91 | } 92 | 93 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 94 | public void isZonedDateTimeNotHourOtherZone() { 95 | assertThat(AUG_04_2015_NOON_UTC, ZonedDateTimeMatchers.isHour(12).atZone(ZoneIds.CET)); 96 | } 97 | 98 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 99 | public void isZonedDateTimeNotHour() { 100 | assertThat(AUG_04_2015_NOON_UTC, ZonedDateTimeMatchers.isHour(11).atZone(ZoneIds.UTC)); 101 | } 102 | 103 | // OffsetDateTime Matchers 104 | 105 | @Test 106 | public void isOffsetDateTimeHour() { 107 | assertThat(AUG_04_2015_NOON_OFFSET_UTC, OffsetDateTimeMatchers.isHour(12).atZone(ZoneIds.UTC)); 108 | } 109 | 110 | @Test 111 | public void isOffsetDateTimeHourOtherZone() { 112 | assertThat(AUG_04_2015_NOON_OFFSET_UTC, OffsetDateTimeMatchers.isHour(7).atZone(ZoneIds.EST)); 113 | } 114 | 115 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 116 | public void isOffsetDateTimeNotHourOtherZone() { 117 | assertThat(AUG_04_2015_NOON_OFFSET_UTC, OffsetDateTimeMatchers.isHour(12).atZone(ZoneIds.CET)); 118 | } 119 | 120 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 121 | public void isOffsetDateTimeNotHour() { 122 | assertThat(AUG_04_2015_NOON_OFFSET_UTC, OffsetDateTimeMatchers.isHour(11).atZone(ZoneIds.UTC)); 123 | } 124 | 125 | // Instant Matchers 126 | 127 | @Test 128 | public void isInstantHour() { 129 | assertThat(AUG_04_2015_NOON_INSTANT_UTC, InstantMatchers.isHour(12).atZone(ZoneIds.UTC)); 130 | } 131 | 132 | @Test 133 | public void isInstantHourOtherZone() { 134 | assertThat(AUG_04_2015_NOON_INSTANT_UTC, InstantMatchers.isHour(7).atZone(ZoneIds.EST)); 135 | } 136 | 137 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 138 | public void isInstantNotHourOtherZone() { 139 | assertThat(AUG_04_2015_NOON_INSTANT_UTC, InstantMatchers.isHour(12).atZone(ZoneIds.CET)); 140 | } 141 | 142 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 143 | public void isInstantNotHour() { 144 | assertThat(AUG_04_2015_NOON_INSTANT_UTC, InstantMatchers.isHour(11).atZone(ZoneIds.UTC)); 145 | } 146 | } 147 | -------------------------------------------------------------------------------- /src/test/java/org/exparity/hamcrest/date/core/IsLastDayOfMonthTest.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import static org.exparity.hamcrest.date.testutils.Dates.*; 4 | import static org.hamcrest.MatcherAssert.assertThat; 5 | 6 | import org.exparity.hamcrest.date.DateMatchers; 7 | import org.exparity.hamcrest.date.InstantMatchers; 8 | import org.exparity.hamcrest.date.LocalDateMatchers; 9 | import org.exparity.hamcrest.date.LocalDateTimeMatchers; 10 | import org.exparity.hamcrest.date.OffsetDateTimeMatchers; 11 | import org.exparity.hamcrest.date.SqlDateMatchers; 12 | import org.exparity.hamcrest.date.ZonedDateTimeMatchers; 13 | import org.testng.annotations.Test; 14 | 15 | /** 16 | * @author Stewart Bissett 17 | */ 18 | public class IsLastDayOfMonthTest { 19 | 20 | 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"; 21 | 22 | // Date Matchers 23 | @Test 24 | public void isDateLastDayOfMonth() { 25 | assertThat(AUG_31_2015_NOON_UTC_AS_DATE, DateMatchers.isLastDayOfMonth()); 26 | } 27 | 28 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 29 | public void isDateNotLastDayOfMonth() { 30 | assertThat(AUG_01_2015_NOON_UTC_AS_DATE, DateMatchers.isLastDayOfMonth()); 31 | } 32 | 33 | // java.sql.Date Matchers 34 | public void isSqlDateLastDayOfMonth() { 35 | assertThat(AUG_31_2015_AS_SQL, SqlDateMatchers.isLastDayOfMonth()); 36 | } 37 | 38 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 39 | public void isSqlDateNotLastDayOfMonth() { 40 | assertThat(AUG_01_2015_AS_SQL, SqlDateMatchers.isLastDayOfMonth()); 41 | } 42 | 43 | public void isSqlDateLastDayOfMonthUsingDateMatchers() { 44 | assertThat(AUG_31_2015_AS_SQL, DateMatchers.isLastDayOfMonth()); 45 | } 46 | 47 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 48 | public void isSqlDateNotLastDayOfMonthUsingDateMatchers() { 49 | assertThat(AUG_01_2015_AS_SQL, DateMatchers.isLastDayOfMonth()); 50 | } 51 | 52 | // LocalDate Matchers 53 | @Test 54 | public void isLocalDateLastDayOfMonth() { 55 | assertThat(AUG_31_2015, LocalDateMatchers.isLastDayOfMonth()); 56 | } 57 | 58 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 59 | public void isLocalDateNotLastDayOfMonth() { 60 | assertThat(AUG_01_2015, LocalDateMatchers.isLastDayOfMonth()); 61 | } 62 | 63 | // LocalDateTime Matchers 64 | @Test 65 | public void isLocalDateTimeLastDayOfMonth() { 66 | assertThat(AUG_31_2015_NOON, LocalDateTimeMatchers.isLastDayOfMonth()); 67 | } 68 | 69 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 70 | public void isLocalDateTimeNotLastDayOfMonth() { 71 | assertThat(AUG_01_2015_NOON, LocalDateTimeMatchers.isLastDayOfMonth()); 72 | } 73 | 74 | // ZonedDateTime Matchers 75 | 76 | @Test 77 | public void isZonedDateTimeLastDayOfMonth() { 78 | assertThat(AUG_31_2015_NOON_UTC, ZonedDateTimeMatchers.isLastDayOfMonth()); 79 | } 80 | 81 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 82 | public void isZonedDateTimeNotLastDayOfMonth() { 83 | assertThat(AUG_01_2015_NOON_UTC, ZonedDateTimeMatchers.isLastDayOfMonth()); 84 | } 85 | 86 | // OffsetDateTime Matchers 87 | 88 | @Test 89 | public void isOffsetDateTimeLastDayOfMonth() { 90 | assertThat(AUG_31_2015_NOON_OFFSET_UTC, OffsetDateTimeMatchers.isLastDayOfMonth()); 91 | } 92 | 93 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 94 | public void isOffsetDateTimeNotLastDayOfMonth() { 95 | assertThat(AUG_01_2015_NOON_OFFSET_UTC, OffsetDateTimeMatchers.isLastDayOfMonth()); 96 | } 97 | 98 | // Instant Matchers 99 | 100 | @Test 101 | public void isInstantLastDayOfMonth() { 102 | assertThat(AUG_31_2015_NOON_INSTANT_UTC, InstantMatchers.isLastDayOfMonth()); 103 | } 104 | 105 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 106 | public void isInstantNotLastDayOfMonth() { 107 | assertThat(AUG_01_2015_NOON_INSTANT_UTC, InstantMatchers.isLastDayOfMonth()); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /src/test/java/org/exparity/hamcrest/date/core/IsLeapYearTest.java: -------------------------------------------------------------------------------- 1 | 2 | package org.exparity.hamcrest.date.core; 3 | 4 | import static org.exparity.hamcrest.date.testutils.Dates.*; 5 | import static org.junit.Assert.assertThat; 6 | 7 | import org.exparity.hamcrest.date.DateMatchers; 8 | import org.exparity.hamcrest.date.InstantMatchers; 9 | import org.exparity.hamcrest.date.LocalDateMatchers; 10 | import org.exparity.hamcrest.date.LocalDateTimeMatchers; 11 | import org.exparity.hamcrest.date.OffsetDateTimeMatchers; 12 | import org.exparity.hamcrest.date.SqlDateMatchers; 13 | import org.exparity.hamcrest.date.ZonedDateTimeMatchers; 14 | import org.exparity.hamcrest.date.testutils.ZoneIds; 15 | import org.testng.annotations.Test; 16 | 17 | /** 18 | * Unit test for {@link IsLeapYear} 19 | * 20 | * @author Stewart Bisett 21 | */ 22 | public class IsLeapYearTest { 23 | 24 | private static final String ASSERTION_PATTERN = "\\sExpected: a leap year\\s but: the year (?s:.)+? is not a leap year"; 25 | 26 | // Date Matchers 27 | @Test 28 | public void isDateLeapYear() { 29 | assertThat(AUG_04_2016_MIDNIGHT_UTC_AS_DATE, DateMatchers.isLeapYear()); 30 | } 31 | 32 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 33 | public void isDateNotLeapYear() { 34 | assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.isLeapYear()); 35 | } 36 | 37 | @Test 38 | public void isDateLeapYearStartOfYearSameZone() { 39 | assertThat(JAN_01_2012_MIDNIGHT_CET_AS_DATE, DateMatchers.isLeapYear().atZone(ZoneIds.CET)); 40 | } 41 | 42 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 43 | public void isDateNotLeapYearStartOfYearLaterZone() { 44 | assertThat(JAN_01_2012_MIDNIGHT_CET_AS_DATE, DateMatchers.isLeapYear().atZone(ZoneIds.UTC)); 45 | } 46 | 47 | // java.sql.Date Matchers 48 | @Test 49 | public void isSqlDateLeapYear() { 50 | assertThat(AUG_04_2016_AS_SQL, SqlDateMatchers.isLeapYear()); 51 | } 52 | 53 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 54 | public void isSqlDateNotLeapYear() { 55 | assertThat(AUG_04_2015_AS_SQL, SqlDateMatchers.isLeapYear()); 56 | } 57 | 58 | @Test 59 | public void isSqlDateLeapYearUsingDateMatchers() { 60 | assertThat(AUG_04_2016_AS_SQL, DateMatchers.isLeapYear()); 61 | } 62 | 63 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 64 | public void isSqlDateNotLeapYearUsingDateMatchers() { 65 | assertThat(AUG_04_2015_AS_SQL, DateMatchers.isLeapYear()); 66 | } 67 | 68 | // LocalDate Matchers 69 | @Test 70 | public void isLocalDateLeapYear() { 71 | assertThat(AUG_04_2016, LocalDateMatchers.isLeapYear()); 72 | } 73 | 74 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 75 | public void isLocalDateNotLeapYear() { 76 | assertThat(AUG_04_2015, LocalDateMatchers.isLeapYear()); 77 | } 78 | 79 | // LocalDateTime Matchers 80 | @Test 81 | public void isLocalDateTimeLeapYear() { 82 | assertThat(AUG_04_2016_NOON, LocalDateTimeMatchers.isLeapYear()); 83 | } 84 | 85 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 86 | public void isLocalDateTimeNotLeapYear() { 87 | assertThat(AUG_04_2015_NOON, LocalDateTimeMatchers.isLeapYear()); 88 | } 89 | 90 | // ZonedDateTime Matchers 91 | @Test 92 | public void isZonedDateTimeLeapYear() { 93 | assertThat(AUG_04_2016_NOON_UTC, ZonedDateTimeMatchers.isLeapYear()); 94 | } 95 | 96 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 97 | public void isZonedDateTimeNotLeapYear() { 98 | assertThat(AUG_04_2015_NOON_UTC, ZonedDateTimeMatchers.isLeapYear()); 99 | } 100 | 101 | @Test 102 | public void isZonedDateTimeLeapYearStartOfYearSameZone() { 103 | assertThat(JAN_01_2012_MIDNIGHT_CET, ZonedDateTimeMatchers.isLeapYear().atZone(ZoneIds.CET)); 104 | } 105 | 106 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 107 | public void isZonedDateTimeNotLeapYearStartOfYearLaterZone() { 108 | assertThat(JAN_01_2012_MIDNIGHT_CET, ZonedDateTimeMatchers.isLeapYear().atZone(ZoneIds.UTC)); 109 | } 110 | 111 | // OffsetDateTime Matchers 112 | 113 | @Test 114 | public void isOffsetDateTimeLeapYear() { 115 | assertThat(AUG_04_2016_NOON_OFFSET_UTC, OffsetDateTimeMatchers.isLeapYear()); 116 | } 117 | 118 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 119 | public void isOffsetDateTimeNotLeapYear() { 120 | assertThat(AUG_04_2015_NOON_OFFSET_UTC, OffsetDateTimeMatchers.isLeapYear()); 121 | } 122 | 123 | @Test 124 | public void isOffsetDateTimeLeapYearStartOfYearSameZone() { 125 | assertThat(JAN_01_2012_MIDNIGHT_OFFSET_CET, OffsetDateTimeMatchers.isLeapYear().atZone(ZoneIds.CET)); 126 | } 127 | 128 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 129 | public void isOffsetDateTimeNotLeapYearStartOfYearLaterZone() { 130 | assertThat(JAN_01_2012_MIDNIGHT_OFFSET_CET, OffsetDateTimeMatchers.isLeapYear().atZone(ZoneIds.UTC)); 131 | } 132 | 133 | // Instant Matchers 134 | 135 | @Test 136 | public void isInstantLeapYear() { 137 | assertThat(AUG_04_2016_NOON_INSTANT_UTC, InstantMatchers.isLeapYear()); 138 | } 139 | 140 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 141 | public void isInstantNotLeapYear() { 142 | assertThat(AUG_04_2015_NOON_INSTANT_UTC, InstantMatchers.isLeapYear()); 143 | } 144 | 145 | @Test 146 | public void isInstantLeapYearStartOfYearSameZone() { 147 | assertThat(JAN_01_2012_MIDNIGHT_INSTANT_CET, InstantMatchers.isLeapYear().atZone(ZoneIds.CET)); 148 | } 149 | 150 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 151 | public void isInstantNotLeapYearStartOfYearLaterZone() { 152 | assertThat(JAN_01_2012_MIDNIGHT_INSTANT_CET, InstantMatchers.isLeapYear().atZone(ZoneIds.UTC)); 153 | } 154 | } 155 | -------------------------------------------------------------------------------- /src/test/java/org/exparity/hamcrest/date/core/IsMaximumTest.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import static org.exparity.hamcrest.date.testutils.Dates.*; 4 | import static org.hamcrest.MatcherAssert.assertThat; 5 | 6 | import java.time.LocalTime; 7 | import java.time.temporal.ChronoField; 8 | 9 | import org.exparity.hamcrest.date.DateMatchers; 10 | import org.exparity.hamcrest.date.InstantMatchers; 11 | import org.exparity.hamcrest.date.LocalDateMatchers; 12 | import org.exparity.hamcrest.date.LocalDateTimeMatchers; 13 | import org.exparity.hamcrest.date.LocalTimeMatchers; 14 | import org.exparity.hamcrest.date.OffsetDateTimeMatchers; 15 | import org.exparity.hamcrest.date.SqlDateMatchers; 16 | import org.exparity.hamcrest.date.ZonedDateTimeMatchers; 17 | import org.testng.annotations.Test; 18 | 19 | /** 20 | * @author Stewart Bissett 21 | */ 22 | public class IsMaximumTest { 23 | 24 | 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]+?"; 25 | 26 | // Date Matchers 27 | @Test 28 | public void isDateLastDayOfMonth() { 29 | assertThat(AUG_31_2015_NOON_UTC_AS_DATE, DateMatchers.isMaximum(ChronoField.DAY_OF_MONTH)); 30 | } 31 | 32 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 33 | public void isDateNotLastDayOfMonth() { 34 | assertThat(AUG_01_2015_NOON_UTC_AS_DATE, DateMatchers.isMaximum(ChronoField.DAY_OF_MONTH)); 35 | } 36 | 37 | // java.sql.Date Matchers 38 | @Test 39 | public void isSqlDateLastDayOfMonth() { 40 | assertThat(AUG_31_2015_AS_SQL, SqlDateMatchers.isMaximum(ChronoField.DAY_OF_MONTH)); 41 | } 42 | 43 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 44 | public void isSqlDateNotLastDayOfMonth() { 45 | assertThat(AUG_01_2015_AS_SQL, SqlDateMatchers.isMaximum(ChronoField.DAY_OF_MONTH)); 46 | } 47 | 48 | @Test 49 | public void isSqlDateLastDayOfMonthUsingDateMatchers() { 50 | assertThat(AUG_31_2015_AS_SQL, DateMatchers.isMaximum(ChronoField.DAY_OF_MONTH)); 51 | } 52 | 53 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 54 | public void isSqlDateNotLastDayOfMonthUsingDateMatchers() { 55 | assertThat(AUG_01_2015_AS_SQL, DateMatchers.isMaximum(ChronoField.DAY_OF_MONTH)); 56 | } 57 | 58 | // LocalDate Matchers 59 | @Test 60 | public void isLocalDateLastDayOfMonth() { 61 | assertThat(AUG_31_2015, LocalDateMatchers.isMaximum(ChronoField.DAY_OF_MONTH)); 62 | } 63 | 64 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 65 | public void isLocalDateNotLastDayOfMonth() { 66 | assertThat(AUG_01_2015, LocalDateMatchers.isMaximum(ChronoField.DAY_OF_MONTH)); 67 | } 68 | 69 | // LocalDateTime Matchers 70 | @Test 71 | public void isLocalDateTimeLastDayOfMonth() { 72 | assertThat(AUG_31_2015_NOON, LocalDateTimeMatchers.isMaximum(ChronoField.DAY_OF_MONTH)); 73 | } 74 | 75 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 76 | public void isLocalDateTimeNotLastDayOfMonth() { 77 | assertThat(AUG_01_2015_NOON, LocalDateTimeMatchers.isMaximum(ChronoField.DAY_OF_MONTH)); 78 | } 79 | 80 | // ZonedDateTime Matchers 81 | @Test 82 | public void isZonedDateTimeLastDayOfMonth() { 83 | assertThat(AUG_31_2015_NOON_UTC, ZonedDateTimeMatchers.isMaximum(ChronoField.DAY_OF_MONTH)); 84 | } 85 | 86 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 87 | public void isZonedDateTimeNotLastDayOfMonth() { 88 | assertThat(AUG_01_2015_NOON_UTC, ZonedDateTimeMatchers.isMaximum(ChronoField.DAY_OF_MONTH)); 89 | } 90 | 91 | // LocalTime Matchers 92 | @Test 93 | public void isLocalTimeLastHourOfDay() { 94 | assertThat(LocalTime.of(23, 0, 0), LocalTimeMatchers.isMaximum(ChronoField.HOUR_OF_DAY)); 95 | } 96 | 97 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 98 | public void isLocalTimeNotLastHourOfDay() { 99 | assertThat(LocalTime.of(22, 0, 0), LocalTimeMatchers.isMaximum(ChronoField.HOUR_OF_DAY)); 100 | } 101 | 102 | // OffsetDateTime Matchers 103 | 104 | @Test 105 | public void isOffsetDateTimeLastDayOfMonth() { 106 | assertThat(AUG_31_2015_NOON_OFFSET_UTC, OffsetDateTimeMatchers.isMaximum(ChronoField.DAY_OF_MONTH)); 107 | } 108 | 109 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 110 | public void isOffsetDateTimeNotLastDayOfMonth() { 111 | assertThat(AUG_01_2015_NOON_OFFSET_UTC, OffsetDateTimeMatchers.isMaximum(ChronoField.DAY_OF_MONTH)); 112 | } 113 | 114 | // Instant Matchers 115 | 116 | @Test 117 | public void isInstantLastDayOfMonth() { 118 | assertThat(AUG_31_2015_NOON_INSTANT_UTC, InstantMatchers.isMaximum(ChronoField.DAY_OF_MONTH)); 119 | } 120 | 121 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 122 | public void isInstantNotLastDayOfMonth() { 123 | assertThat(AUG_01_2015_NOON_INSTANT_UTC, InstantMatchers.isMaximum(ChronoField.DAY_OF_MONTH)); 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /src/test/java/org/exparity/hamcrest/date/core/IsMillisecondTest.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import static org.exparity.hamcrest.date.testutils.Dates.*; 4 | import static org.hamcrest.MatcherAssert.assertThat; 5 | 6 | import org.exparity.hamcrest.date.DateMatchers; 7 | import org.testng.annotations.Test; 8 | 9 | /** 10 | * Unit Tests for the {@link IsMillisecond} class 11 | * 12 | * @author Stewart Bissett 13 | */ 14 | @SuppressWarnings("deprecation") 15 | public class IsMillisecondTest { 16 | 17 | private static final String ASSERTION_PATTERN = "\\sExpected: the date has the millisecond [0-9]+?\\s but: the date has the millisecond [0-9]+"; 18 | 19 | // Date Matchers 20 | @Test 21 | public void isDateMillisecond() { 22 | assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.isMillisecond(0)); 23 | } 24 | 25 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 26 | public void isDateNotMillisecond() { 27 | assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.isMillisecond(1)); 28 | } 29 | 30 | @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) 31 | public void isSqlDateMillisecond() { 32 | assertThat(AUG_04_2015_AS_SQL, DateMatchers.isMillisecond(0)); 33 | } 34 | 35 | @Test 36 | public void isDateSameMillisecond() { 37 | assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.sameMillisecond(0)); 38 | } 39 | 40 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 41 | public void isDateNotSameMillisecond() { 42 | assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.sameMillisecond(1)); 43 | } 44 | 45 | @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) 46 | public void isSqlDateSameMillisecond() { 47 | assertThat(AUG_04_2015_AS_SQL, DateMatchers.sameMillisecond(0)); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/test/java/org/exparity/hamcrest/date/core/IsMinimumTest.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import static org.exparity.hamcrest.date.testutils.Dates.*; 4 | import static org.hamcrest.MatcherAssert.assertThat; 5 | 6 | import java.time.LocalTime; 7 | import java.time.temporal.ChronoField; 8 | 9 | import org.exparity.hamcrest.date.DateMatchers; 10 | import org.exparity.hamcrest.date.InstantMatchers; 11 | import org.exparity.hamcrest.date.LocalDateMatchers; 12 | import org.exparity.hamcrest.date.LocalDateTimeMatchers; 13 | import org.exparity.hamcrest.date.LocalTimeMatchers; 14 | import org.exparity.hamcrest.date.OffsetDateTimeMatchers; 15 | import org.exparity.hamcrest.date.SqlDateMatchers; 16 | import org.exparity.hamcrest.date.ZonedDateTimeMatchers; 17 | import org.testng.annotations.Test; 18 | 19 | /** 20 | * @author Stewart Bissett 21 | */ 22 | public class IsMinimumTest { 23 | 24 | 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]+?"; 25 | 26 | // Date Matchers 27 | @Test 28 | public void isDateFirstDayOfMonth() { 29 | assertThat(AUG_01_2015_NOON_UTC_AS_DATE, DateMatchers.isMinimum(ChronoField.DAY_OF_MONTH)); 30 | } 31 | 32 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 33 | public void isDateNotFirstDayOfMonth() { 34 | assertThat(AUG_31_2015_NOON_UTC_AS_DATE, DateMatchers.isMinimum(ChronoField.DAY_OF_MONTH)); 35 | } 36 | 37 | // java.sql.Date Matchers 38 | @Test 39 | public void isSqlDateFirstDayOfMonth() { 40 | assertThat(AUG_01_2015_AS_SQL, SqlDateMatchers.isMinimum(ChronoField.DAY_OF_MONTH)); 41 | } 42 | 43 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 44 | public void isSqlDateNotFirstDayOfMonth() { 45 | assertThat(AUG_31_2015_AS_SQL, SqlDateMatchers.isMinimum(ChronoField.DAY_OF_MONTH)); 46 | } 47 | 48 | @Test 49 | public void isSqlDateFirstDayOfMonthUsingDateMatchers() { 50 | assertThat(AUG_01_2015_AS_SQL, DateMatchers.isMinimum(ChronoField.DAY_OF_MONTH)); 51 | } 52 | 53 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 54 | public void isSqlDateNotFirstDayOfMonthUsingDateMatchers() { 55 | assertThat(AUG_31_2015_AS_SQL, DateMatchers.isMinimum(ChronoField.DAY_OF_MONTH)); 56 | } 57 | 58 | // LocalDate Matchers 59 | @Test 60 | public void isLocalDateFirstDayOfMonth() { 61 | assertThat(AUG_01_2015, LocalDateMatchers.isMinimum(ChronoField.DAY_OF_MONTH)); 62 | } 63 | 64 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 65 | public void isLocalDateNotFirstDayOfMonth() { 66 | assertThat(AUG_31_2015, LocalDateMatchers.isMinimum(ChronoField.DAY_OF_MONTH)); 67 | } 68 | 69 | // LocalDateTime Matchers 70 | @Test 71 | public void isLocalDateTimeFirstDayOfMonth() { 72 | assertThat(AUG_01_2015_NOON, LocalDateTimeMatchers.isMinimum(ChronoField.DAY_OF_MONTH)); 73 | } 74 | 75 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 76 | public void isLocalDateTimeNotFirstDayOfMonth() { 77 | assertThat(AUG_31_2015_NOON, LocalDateTimeMatchers.isMinimum(ChronoField.DAY_OF_MONTH)); 78 | } 79 | 80 | // ZonedDateTime Matchers 81 | @Test 82 | public void isZonedDateTimeFirstDayOfMonth() { 83 | assertThat(AUG_01_2015_NOON_UTC, ZonedDateTimeMatchers.isMinimum(ChronoField.DAY_OF_MONTH)); 84 | } 85 | 86 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 87 | public void isZonedDateTimeNotFirstDayOfMonth() { 88 | assertThat(AUG_31_2015_NOON_UTC, ZonedDateTimeMatchers.isMinimum(ChronoField.DAY_OF_MONTH)); 89 | } 90 | 91 | // LocalTime Matchers 92 | @Test 93 | public void isLocalTimeFirstHourOfDay() { 94 | assertThat(LocalTime.of(0, 0, 0), LocalTimeMatchers.isMinimum(ChronoField.HOUR_OF_DAY)); 95 | } 96 | 97 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 98 | public void isLocalTimeNotFirstHourOfDay() { 99 | assertThat(LocalTime.of(1, 0, 0), LocalTimeMatchers.isMinimum(ChronoField.HOUR_OF_DAY)); 100 | } 101 | 102 | // OffsetDateTime Matchers 103 | @Test 104 | public void isOffsetDateTimeFirstDayOfMonth() { 105 | assertThat(AUG_01_2015_NOON_OFFSET_UTC, OffsetDateTimeMatchers.isMinimum(ChronoField.DAY_OF_MONTH)); 106 | } 107 | 108 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 109 | public void isOffsetDateTimeNotFirstDayOfMonth() { 110 | assertThat(AUG_31_2015_NOON_OFFSET_UTC, OffsetDateTimeMatchers.isMinimum(ChronoField.DAY_OF_MONTH)); 111 | } 112 | 113 | // Instant Matchers 114 | 115 | @Test 116 | public void isInstantFirstDayOfMonth() { 117 | assertThat(AUG_01_2015_NOON_INSTANT_UTC, InstantMatchers.isMinimum(ChronoField.DAY_OF_MONTH)); 118 | } 119 | 120 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 121 | public void isInstantNotFirstDayOfMonth() { 122 | assertThat(AUG_31_2015_NOON_INSTANT_UTC, InstantMatchers.isMinimum(ChronoField.DAY_OF_MONTH)); 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /src/test/java/org/exparity/hamcrest/date/core/IsMinuteTest.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import static org.exparity.hamcrest.date.testutils.Dates.*; 4 | import static org.hamcrest.MatcherAssert.assertThat; 5 | 6 | import java.time.LocalTime; 7 | 8 | import org.exparity.hamcrest.date.DateMatchers; 9 | import org.exparity.hamcrest.date.InstantMatchers; 10 | import org.exparity.hamcrest.date.LocalDateTimeMatchers; 11 | import org.exparity.hamcrest.date.LocalTimeMatchers; 12 | import org.exparity.hamcrest.date.OffsetDateTimeMatchers; 13 | import org.exparity.hamcrest.date.ZonedDateTimeMatchers; 14 | import org.testng.annotations.Test; 15 | 16 | /** 17 | * Unit Tests for the {@link IsMinute} class 18 | * 19 | * @author Stewart Bissett 20 | */ 21 | @SuppressWarnings("deprecation") 22 | public class IsMinuteTest { 23 | 24 | private static final String ASSERTION_PATTERN = "\\sExpected: the date has the minute [0-9]+?\\s but: the date has the minute [0-9]+"; 25 | 26 | // Date Matchers 27 | @Test 28 | public void isDateMinute() { 29 | assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.isMinute(0)); 30 | } 31 | 32 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 33 | public void isDateNotMinute() { 34 | assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.isMinute(1)); 35 | } 36 | 37 | @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) 38 | public void isSqlDateMinute() { 39 | assertThat(AUG_04_2015_AS_SQL, DateMatchers.isMinute(0)); 40 | } 41 | 42 | @Test 43 | public void isDateSameMinute() { 44 | assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.sameMinute(0)); 45 | } 46 | 47 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 48 | public void isDateNotSameMinute() { 49 | assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.sameMinute(1)); 50 | } 51 | 52 | @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) 53 | public void isSqlDateSameMinute() { 54 | assertThat(AUG_04_2015_AS_SQL, DateMatchers.sameMinute(0)); 55 | } 56 | 57 | // LocalDateTime Matchers 58 | @Test 59 | public void isLocalDateTimeMinute() { 60 | assertThat(AUG_04_2015_NOON, LocalDateTimeMatchers.isMinute(0)); 61 | } 62 | 63 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 64 | public void isLocalDateTimeNotMinute() { 65 | assertThat(AUG_04_2015_NOON, LocalDateTimeMatchers.isMinute(1)); 66 | } 67 | 68 | // LocalTime Matchers 69 | @Test 70 | public void isLocalTimeMinute() { 71 | assertThat(LocalTime.NOON, LocalTimeMatchers.isMinute(0)); 72 | } 73 | 74 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 75 | public void isLocalTimeNotMinute() { 76 | assertThat(LocalTime.NOON, LocalTimeMatchers.isMinute(1)); 77 | } 78 | 79 | // ZonedDateTime Matchers 80 | @Test 81 | public void isZonedDateTimeMinute() { 82 | assertThat(AUG_04_2015_NOON_UTC, ZonedDateTimeMatchers.isMinute(0)); 83 | } 84 | 85 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 86 | public void isZonedDateTimeNotMinute() { 87 | assertThat(AUG_04_2015_NOON_UTC, ZonedDateTimeMatchers.isMinute(1)); 88 | } 89 | 90 | // OffsetDateTime Matchers 91 | 92 | @Test 93 | public void isOffsetDateTimeMinute() { 94 | assertThat(AUG_04_2015_NOON_OFFSET_UTC, OffsetDateTimeMatchers.isMinute(0)); 95 | } 96 | 97 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 98 | public void isOffsetDateTimeNotMinute() { 99 | assertThat(AUG_04_2015_NOON_OFFSET_UTC, OffsetDateTimeMatchers.isMinute(1)); 100 | } 101 | 102 | // Instant Matchers 103 | 104 | @Test 105 | public void isInstantMinute() { 106 | assertThat(AUG_04_2015_NOON_INSTANT_UTC, InstantMatchers.isMinute(0)); 107 | } 108 | 109 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 110 | public void isInstantNotMinute() { 111 | assertThat(AUG_04_2015_NOON_INSTANT_UTC, InstantMatchers.isMinute(1)); 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /src/test/java/org/exparity/hamcrest/date/core/IsSameDayOfMonthTest.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import static org.exparity.hamcrest.date.testutils.Dates.*; 4 | import static org.hamcrest.MatcherAssert.assertThat; 5 | 6 | import org.exparity.hamcrest.date.DateMatchers; 7 | import org.exparity.hamcrest.date.InstantMatchers; 8 | import org.exparity.hamcrest.date.LocalDateMatchers; 9 | import org.exparity.hamcrest.date.LocalDateTimeMatchers; 10 | import org.exparity.hamcrest.date.OffsetDateTimeMatchers; 11 | import org.exparity.hamcrest.date.SqlDateMatchers; 12 | import org.exparity.hamcrest.date.ZonedDateTimeMatchers; 13 | import org.testng.annotations.Test; 14 | 15 | /** 16 | * @author Stewart Bissett 17 | */ 18 | public class IsSameDayOfMonthTest { 19 | 20 | private static final String ASSERTION_PATTERN = "\\sExpected: the date has the day of month [0-9]+?\\s but: the date has the day of month [0-9]+?"; 21 | 22 | // Date Matchers 23 | @Test 24 | public void isDateSameDayOfMonth() { 25 | assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.sameDayOfMonth(AUG_04_2015_NOON_UTC_AS_DATE)); 26 | } 27 | 28 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 29 | public void isDateNotSameDayOfMonth() { 30 | assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.sameDayOfMonth(AUG_01_2015_NOON_UTC_AS_DATE)); 31 | } 32 | 33 | @Test 34 | public void isDateSameDayOfMonthDifferentMonth() { 35 | assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.sameDayOfMonth(SEP_04_2015_NOON_UTC_AS_DATE)); 36 | } 37 | 38 | // LocalDate Matchers 39 | @Test 40 | public void isSqlDateSameDayOfMonth() { 41 | assertThat(AUG_04_2015_AS_SQL, SqlDateMatchers.sameDayOfMonth(AUG_04_2015_AS_SQL)); 42 | } 43 | 44 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 45 | public void isSqlDateNotSameDayOfMonth() { 46 | assertThat(AUG_04_2015_AS_SQL, SqlDateMatchers.sameDayOfMonth(AUG_01_2015_AS_SQL)); 47 | } 48 | 49 | @Test 50 | public void isSqlDateSameDayOfMonthDifferentMonth() { 51 | assertThat(AUG_04_2015_AS_SQL, SqlDateMatchers.sameDayOfMonth(SEP_04_2015_AS_SQL)); 52 | } 53 | 54 | @Test 55 | public void isSqlDateSameDayOfMonthUsingDateMatchers() { 56 | assertThat(AUG_04_2015_AS_SQL, DateMatchers.sameDayOfMonth(AUG_04_2015_NOON_UTC_AS_DATE)); 57 | } 58 | 59 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 60 | public void isSqlDateNotSameDayOfMonthUsingDateMatchers() { 61 | assertThat(AUG_04_2015_AS_SQL, DateMatchers.sameDayOfMonth(AUG_01_2015_NOON_UTC_AS_DATE)); 62 | } 63 | 64 | @Test 65 | public void isSqlDateSameDayOfMonthDifferentMonthUsingDateMatchers() { 66 | assertThat(AUG_04_2015_AS_SQL, DateMatchers.sameDayOfMonth(SEP_04_2015_NOON_UTC_AS_DATE)); 67 | } 68 | 69 | // LocalDate Matchers 70 | @Test 71 | public void isLocalDateSameDayOfMonth() { 72 | assertThat(AUG_04_2015, LocalDateMatchers.sameDayOfMonth(AUG_04_2015)); 73 | } 74 | 75 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 76 | public void isLocalDateNotSameDayOfMonth() { 77 | assertThat(AUG_04_2015, LocalDateMatchers.sameDayOfMonth(AUG_01_2015)); 78 | } 79 | 80 | @Test 81 | public void isLocalDateSameDayOfMonthDifferentMonth() { 82 | assertThat(AUG_04_2015, LocalDateMatchers.sameDayOfMonth(SEP_04_2015)); 83 | } 84 | 85 | // LocalDateTime Matchers 86 | @Test 87 | public void isLocalDateTimeSameDayOfMonth() { 88 | assertThat(AUG_04_2015_NOON, LocalDateTimeMatchers.sameDayOfMonth(AUG_04_2015_NOON)); 89 | } 90 | 91 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 92 | public void isLocalDateTimeNotSameDayOfMonth() { 93 | assertThat(AUG_04_2015_NOON, LocalDateTimeMatchers.sameDayOfMonth(AUG_01_2015_NOON)); 94 | } 95 | 96 | @Test 97 | public void isLocalDateTimeSameDayOfMonthDifferentMonth() { 98 | assertThat(AUG_04_2015_NOON, LocalDateTimeMatchers.sameDayOfMonth(SEP_04_2015_NOON)); 99 | } 100 | 101 | // ZonedDateTime Matchers 102 | @Test 103 | public void isZonedDateTimeSameDayOfMonth() { 104 | assertThat(AUG_04_2015_NOON_UTC, ZonedDateTimeMatchers.sameDayOfMonth(AUG_04_2015_NOON_UTC)); 105 | } 106 | 107 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 108 | public void isZonedDateTimeNotSameDayOfMonth() { 109 | assertThat(AUG_04_2015_NOON_UTC, ZonedDateTimeMatchers.sameDayOfMonth(AUG_01_2015_NOON_UTC)); 110 | } 111 | 112 | @Test 113 | public void isZonedDateTimeSameDayOfMonthDifferentMonth() { 114 | assertThat(AUG_04_2015_NOON_UTC, ZonedDateTimeMatchers.sameDayOfMonth(SEP_04_2015_NOON_UTC)); 115 | } 116 | 117 | // OffsetDateTime Matchers 118 | 119 | @Test 120 | public void isOffsetDateTimeSameDayOfMonth() { 121 | assertThat(AUG_04_2015_NOON_OFFSET_UTC, OffsetDateTimeMatchers.sameDayOfMonth(AUG_04_2015_NOON_OFFSET_UTC)); 122 | } 123 | 124 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 125 | public void isOffsetDateTimeNotSameDayOfMonth() { 126 | assertThat(AUG_04_2015_NOON_OFFSET_UTC, OffsetDateTimeMatchers.sameDayOfMonth(AUG_01_2015_NOON_OFFSET_UTC)); 127 | } 128 | 129 | @Test 130 | public void isOffsetDateTimeSameDayOfMonthDifferentMonth() { 131 | assertThat(AUG_04_2015_NOON_OFFSET_UTC, OffsetDateTimeMatchers.sameDayOfMonth(SEP_04_2015_NOON_OFFSET_UTC)); 132 | } 133 | 134 | // Instant Matchers 135 | 136 | @Test 137 | public void isInstantSameDayOfMonth() { 138 | assertThat(AUG_04_2015_NOON_INSTANT_UTC, InstantMatchers.sameDayOfMonth(AUG_04_2015_NOON_INSTANT_UTC)); 139 | } 140 | 141 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 142 | public void isInstantNotSameDayOfMonth() { 143 | assertThat(AUG_04_2015_NOON_INSTANT_UTC, InstantMatchers.sameDayOfMonth(AUG_01_2015_NOON_INSTANT_UTC)); 144 | } 145 | 146 | @Test 147 | public void isInstantSameDayOfMonthDifferentMonth() { 148 | assertThat(AUG_04_2015_NOON_INSTANT_UTC, InstantMatchers.sameDayOfMonth(SEP_04_2015_NOON_INSTANT_UTC)); 149 | } 150 | } 151 | -------------------------------------------------------------------------------- /src/test/java/org/exparity/hamcrest/date/core/IsSameDayOfWeekTest.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import static org.exparity.hamcrest.date.testutils.Dates.*; 4 | import static org.hamcrest.MatcherAssert.assertThat; 5 | 6 | import org.exparity.hamcrest.date.DateMatchers; 7 | import org.exparity.hamcrest.date.InstantMatchers; 8 | import org.exparity.hamcrest.date.LocalDateMatchers; 9 | import org.exparity.hamcrest.date.LocalDateTimeMatchers; 10 | import org.exparity.hamcrest.date.OffsetDateTimeMatchers; 11 | import org.exparity.hamcrest.date.SqlDateMatchers; 12 | import org.exparity.hamcrest.date.ZonedDateTimeMatchers; 13 | import org.testng.annotations.Test; 14 | 15 | /** 16 | * @author Stewart Bissett 17 | */ 18 | public class IsSameDayOfWeekTest { 19 | 20 | private static final String ASSERTION_PATTERN = "\\sExpected: the date is on a \\p{IsAlphabetic}+?\\s but: the date is on a \\p{IsAlphabetic}+"; 21 | 22 | // Date Matchers 23 | @Test 24 | public void isDateSameDayOfWeek() { 25 | assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.sameDayOfWeek(AUG_04_2015_NOON_UTC_AS_DATE)); 26 | } 27 | 28 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 29 | public void isDateNotSameDayOfWeek() { 30 | assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.sameDayOfWeek(AUG_01_2015_NOON_UTC_AS_DATE)); 31 | } 32 | 33 | @Test 34 | public void isDateSameDayOfWeekDifferentMonth() { 35 | assertThat(AUG_07_2015_NOON_UTC_AS_DATE, DateMatchers.sameDayOfWeek(SEP_04_2015_NOON_UTC_AS_DATE)); 36 | } 37 | 38 | @Test 39 | public void isDateSameDayOfWeekAsSqlDate() { 40 | assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.sameDayOfWeek(AUG_04_2015_AS_SQL)); 41 | } 42 | 43 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 44 | public void isDateNotSameDayOfWeekAsSqlDate() { 45 | assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.sameDayOfWeek(AUG_01_2015_AS_SQL)); 46 | } 47 | 48 | @Test 49 | public void isDateSameDayOfWeekDifferentMonthAsSqlDate() { 50 | assertThat(AUG_07_2015_NOON_UTC_AS_DATE, DateMatchers.sameDayOfWeek(SEP_04_2015_AS_SQL)); 51 | } 52 | 53 | // java.sql.Date Matchers 54 | @Test 55 | public void isSqlDateSameDayOfWeek() { 56 | assertThat(AUG_04_2015_AS_SQL, SqlDateMatchers.sameDayOfWeek(AUG_04_2015_AS_SQL)); 57 | } 58 | 59 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 60 | public void isSqlDateNotSameDayOfWeek() { 61 | assertThat(AUG_04_2015_AS_SQL, SqlDateMatchers.sameDayOfWeek(AUG_01_2015_AS_SQL)); 62 | } 63 | 64 | @Test 65 | public void isSqlDateSameDayOfWeekDifferentMonth() { 66 | assertThat(AUG_07_2015_AS_SQL, SqlDateMatchers.sameDayOfWeek(SEP_04_2015_AS_SQL)); 67 | } 68 | 69 | @Test 70 | public void isSqlDateSameDayOfWeekAsDate() { 71 | assertThat(AUG_04_2015_AS_SQL, SqlDateMatchers.sameDayOfWeek(AUG_04_2015_NOON_UTC_AS_DATE)); 72 | } 73 | 74 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 75 | public void isSqlDateNotSameDayOfWeekAsDate() { 76 | assertThat(AUG_04_2015_AS_SQL, SqlDateMatchers.sameDayOfWeek(AUG_01_2015_NOON_UTC_AS_DATE)); 77 | } 78 | 79 | @Test 80 | public void isSqlDateSameDayOfWeekDifferentMonthAsDate() { 81 | assertThat(AUG_07_2015_AS_SQL, SqlDateMatchers.sameDayOfWeek(SEP_04_2015_NOON_UTC_AS_DATE)); 82 | } 83 | 84 | @Test 85 | public void isSqlDateSameDayOfWeekUsingDateMatchers() { 86 | assertThat(AUG_04_2015_AS_SQL, DateMatchers.sameDayOfWeek(AUG_04_2015_NOON_UTC_AS_DATE)); 87 | } 88 | 89 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 90 | public void isSqlDateNotSameDayOfWeekUsingDateMatchers() { 91 | assertThat(AUG_04_2015_AS_SQL, DateMatchers.sameDayOfWeek(AUG_01_2015_NOON_UTC_AS_DATE)); 92 | } 93 | 94 | @Test 95 | public void isSqlDateSameDayOfWeekDifferentMonthUsingDateMatchers() { 96 | assertThat(AUG_07_2015_AS_SQL, DateMatchers.sameDayOfWeek(SEP_04_2015_NOON_UTC_AS_DATE)); 97 | } 98 | 99 | // LocalDate Matchers 100 | @Test 101 | public void isLocalDateSameDayOfWeek() { 102 | assertThat(AUG_04_2015, LocalDateMatchers.sameDayOfWeek(AUG_04_2015)); 103 | } 104 | 105 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 106 | public void isLocalDateNotSameDayOfWeek() { 107 | assertThat(AUG_04_2015, LocalDateMatchers.sameDayOfWeek(AUG_03_2015)); 108 | } 109 | 110 | @Test 111 | public void isLocalDateSameDayOfWeekDifferentMonth() { 112 | assertThat(AUG_07_2015, LocalDateMatchers.sameDayOfWeek(SEP_04_2015)); 113 | } 114 | 115 | // LocalDateTime Matchers 116 | @Test 117 | public void isLocalDateTimeSameDayOfWeek() { 118 | assertThat(AUG_04_2015_NOON, LocalDateTimeMatchers.sameDayOfWeek(AUG_04_2015_NOON)); 119 | } 120 | 121 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 122 | public void isLocalDateTimeNotSameDayOfWeek() { 123 | assertThat(AUG_04_2015_NOON, LocalDateTimeMatchers.sameDayOfWeek(AUG_01_2015_NOON)); 124 | } 125 | 126 | @Test 127 | public void isLocalDateTimeSameDayOfWeekDifferentMonth() { 128 | assertThat(AUG_07_2015_NOON, LocalDateTimeMatchers.sameDayOfWeek(SEP_04_2015_NOON)); 129 | } 130 | 131 | // ZonedDateTime Matchers 132 | @Test 133 | public void isZonedDateTimeSameDayOfWeek() { 134 | assertThat(AUG_04_2015_NOON_UTC, ZonedDateTimeMatchers.sameDayOfWeek(AUG_04_2015_NOON_UTC)); 135 | } 136 | 137 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 138 | public void isZonedDateTimeNotSameDayOfWeek() { 139 | assertThat(AUG_04_2015_NOON_UTC, ZonedDateTimeMatchers.sameDayOfWeek(AUG_01_2015_NOON_UTC)); 140 | } 141 | 142 | @Test 143 | public void isZonedDateTimeSameDayOfWeekDifferentMonth() { 144 | assertThat(AUG_07_2015_NOON_UTC, ZonedDateTimeMatchers.sameDayOfWeek(SEP_04_2015_NOON_UTC)); 145 | } 146 | 147 | // OffsetDateTime Matchers 148 | @Test 149 | public void isOffsetDateTimeSameDayOfWeek() { 150 | assertThat(AUG_04_2015_NOON_OFFSET_UTC, OffsetDateTimeMatchers.sameDayOfWeek(AUG_04_2015_NOON_OFFSET_UTC)); 151 | } 152 | 153 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 154 | public void isOffsetDateTimeNotSameDayOfWeek() { 155 | assertThat(AUG_04_2015_NOON_OFFSET_UTC, OffsetDateTimeMatchers.sameDayOfWeek(AUG_01_2015_NOON_OFFSET_UTC)); 156 | } 157 | 158 | @Test 159 | public void isOffsetDateTimeSameDayOfWeekDifferentMonth() { 160 | assertThat(AUG_07_2015_NOON_OFFSET_UTC, OffsetDateTimeMatchers.sameDayOfWeek(SEP_04_2015_NOON_OFFSET_UTC)); 161 | } 162 | 163 | // Instant Matchers 164 | @Test 165 | public void isInstantSameDayOfWeek() { 166 | assertThat(AUG_04_2015_NOON_INSTANT_UTC, InstantMatchers.sameDayOfWeek(AUG_04_2015_NOON_INSTANT_UTC)); 167 | } 168 | 169 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 170 | public void isInstantNotSameDayOfWeek() { 171 | assertThat(AUG_04_2015_NOON_INSTANT_UTC, InstantMatchers.sameDayOfWeek(AUG_01_2015_NOON_INSTANT_UTC)); 172 | } 173 | 174 | @Test 175 | public void isInstantSameDayOfWeekDifferentMonth() { 176 | assertThat(AUG_07_2015_NOON_INSTANT_UTC, InstantMatchers.sameDayOfWeek(SEP_04_2015_NOON_INSTANT_UTC)); 177 | } 178 | 179 | } 180 | -------------------------------------------------------------------------------- /src/test/java/org/exparity/hamcrest/date/core/IsSameHourOfDayTest.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import static org.exparity.hamcrest.date.testutils.DateMatcherTestUtils.addDateField; 4 | import static org.exparity.hamcrest.date.testutils.Dates.JAN_01_2012_MIDNIGHT_CET_AS_DATE; 5 | import static org.exparity.hamcrest.date.testutils.Dates.JAN_01_2012_MIDNIGHT_GMT_AS_DATE; 6 | import static org.hamcrest.MatcherAssert.assertThat; 7 | 8 | import java.time.Instant; 9 | import java.time.LocalDateTime; 10 | import java.time.LocalTime; 11 | import java.time.OffsetDateTime; 12 | import java.time.temporal.ChronoUnit; 13 | import java.util.Calendar; 14 | import java.util.Date; 15 | 16 | import org.exparity.hamcrest.date.DateMatchers; 17 | import org.exparity.hamcrest.date.InstantMatchers; 18 | import org.exparity.hamcrest.date.LocalDateTimeMatchers; 19 | import org.exparity.hamcrest.date.LocalTimeMatchers; 20 | import org.exparity.hamcrest.date.OffsetDateTimeMatchers; 21 | import org.exparity.hamcrest.date.testutils.ZoneIds; 22 | import org.testng.annotations.Test; 23 | 24 | /** 25 | * Unit Tests for the {@link DateMatchers} class 26 | * 27 | * @author Stewart Bissett 28 | */ 29 | @SuppressWarnings("deprecation") 30 | public class IsSameHourOfDayTest { 31 | 32 | private static final String ASSERTION_PATTERN = "\\sExpected: the date has the hour [0-9]+?\\s but: the date has the hour [0-9]+"; 33 | 34 | // Date Matchers 35 | @Test 36 | public void isDateSameHour() { 37 | Date date = new Date(), other = new Date(date.getTime()); 38 | assertThat(other, DateMatchers.sameHour(date)); 39 | } 40 | 41 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 42 | public void isDateNotSameHour() { 43 | Date date = new Date(), other = addDateField(date, Calendar.HOUR, 1); 44 | assertThat(other, DateMatchers.sameHour(date)); 45 | } 46 | 47 | @Test 48 | public void isDateSameMonthDifferentDay() { 49 | Date date = new Date(), other = addDateField(date, Calendar.DAY_OF_WEEK, 1); 50 | assertThat(other, DateMatchers.sameHour(date)); 51 | } 52 | 53 | @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) 54 | public void isSqlDateSameHour() { 55 | Date date = new Date(), other = new java.sql.Date(date.getTime()); 56 | assertThat(other, DateMatchers.sameHour(date)); 57 | } 58 | 59 | @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) 60 | public void isDateSameHourSqlDate() { 61 | Date date = new Date(); 62 | java.sql.Date other = new java.sql.Date(date.getTime()); 63 | assertThat(date, DateMatchers.sameHour(other)); 64 | } 65 | 66 | @Test 67 | public void isDateSameHourOfDay() { 68 | Date date = new Date(), other = new Date(date.getTime()); 69 | assertThat(other, DateMatchers.sameHourOfDay(date)); 70 | } 71 | 72 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 73 | public void isDateNotSameHourOfDay() { 74 | Date date = new Date(), other = addDateField(date, Calendar.HOUR, 1); 75 | assertThat(other, DateMatchers.sameHourOfDay(date)); 76 | } 77 | 78 | @Test 79 | public void isDateSameHourOfDayDifferentDay() { 80 | Date date = new Date(), other = addDateField(date, Calendar.DAY_OF_WEEK, 1); 81 | assertThat(other, DateMatchers.sameHourOfDay(date)); 82 | } 83 | 84 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 85 | public void isDateSameHourOfDayMidnightLocallyDifferentTimeZoneLowerOffsetPerspective() { 86 | assertThat(JAN_01_2012_MIDNIGHT_GMT_AS_DATE, DateMatchers.sameHourOfDay(JAN_01_2012_MIDNIGHT_CET_AS_DATE).atZone(ZoneIds.GMT)); 87 | } 88 | 89 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 90 | public void isDateSameHourOfDayMidnightLocallyDifferentTimeZoneHigherOffsetPerspective() { 91 | assertThat(JAN_01_2012_MIDNIGHT_GMT_AS_DATE, DateMatchers.sameHourOfDay(JAN_01_2012_MIDNIGHT_CET_AS_DATE).atZone(ZoneIds.CET)); 92 | } 93 | 94 | @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) 95 | public void isDateSameHourOfDaySqlDate() { 96 | Date date = new Date(); 97 | java.sql.Date other = new java.sql.Date(date.getTime()); 98 | assertThat(date, DateMatchers.sameHourOfDay(other)); 99 | } 100 | 101 | // LocalDateTime Matchers 102 | @Test 103 | public void isLocalDateTimeSameHourOfDay() { 104 | LocalDateTime date = LocalDateTime.now(), other = date; 105 | assertThat(other, LocalDateTimeMatchers.sameHourOfDay(date)); 106 | } 107 | 108 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 109 | public void isLocalDateTimeNotSameHourOfDay() { 110 | LocalDateTime date = LocalDateTime.now(), other = date.plusHours(1); 111 | assertThat(other, LocalDateTimeMatchers.sameHourOfDay(date)); 112 | } 113 | 114 | @Test 115 | public void isLocalDateTimeSameHourOfDayDifferentDay() { 116 | LocalDateTime date = LocalDateTime.now(), other = date.plusDays(1); 117 | assertThat(other, LocalDateTimeMatchers.sameHourOfDay(date)); 118 | } 119 | 120 | // LocalTime Matchers 121 | @Test 122 | public void isLocalTimeSameHourOfDay() { 123 | LocalTime date = LocalTime.now(), other = date; 124 | assertThat(other, LocalTimeMatchers.sameHourOfDay(date)); 125 | } 126 | 127 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 128 | public void isLocalTimeNotSameHourOfDay() { 129 | LocalTime date = LocalTime.now(), other = date.plusHours(1); 130 | assertThat(other, LocalTimeMatchers.sameHourOfDay(date)); 131 | } 132 | 133 | // OffsetDateTime Matchers 134 | @Test 135 | public void isOffsetDateTimeSameHourOfDay() { 136 | OffsetDateTime date = OffsetDateTime.now(), other = date; 137 | assertThat(other, OffsetDateTimeMatchers.sameHourOfDay(date)); 138 | } 139 | 140 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 141 | public void isOffsetDateTimeNotSameHourOfDay() { 142 | OffsetDateTime date = OffsetDateTime.now(), other = date.plusHours(1); 143 | assertThat(other, OffsetDateTimeMatchers.sameHourOfDay(date)); 144 | } 145 | 146 | @Test 147 | public void isOffsetDateTimeSameHourOfDayDifferentDay() { 148 | OffsetDateTime date = OffsetDateTime.now(), other = date.plusDays(1); 149 | assertThat(other, OffsetDateTimeMatchers.sameHourOfDay(date)); 150 | } 151 | 152 | // Instant Matchers 153 | @Test 154 | public void isInstantSameHourOfDay() { 155 | Instant date = Instant.now(), other = date; 156 | assertThat(other, InstantMatchers.sameHourOfDay(date)); 157 | } 158 | 159 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 160 | public void isInstantNotSameHourOfDay() { 161 | Instant date = Instant.now(), other = date.plus(1, ChronoUnit.HOURS); 162 | assertThat(other, InstantMatchers.sameHourOfDay(date)); 163 | } 164 | 165 | @Test 166 | public void isInstantSameHourOfDayDifferentDay() { 167 | Instant date = Instant.now(), other = date.plus(1, ChronoUnit.DAYS); 168 | assertThat(other, InstantMatchers.sameHourOfDay(date)); 169 | } 170 | } 171 | -------------------------------------------------------------------------------- /src/test/java/org/exparity/hamcrest/date/core/IsSameMillisecondOfSecondTest.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import static org.exparity.hamcrest.date.testutils.DateMatcherTestUtils.addDateField; 4 | import static org.hamcrest.MatcherAssert.assertThat; 5 | 6 | import java.util.Calendar; 7 | import java.util.Date; 8 | 9 | import org.exparity.hamcrest.date.DateMatchers; 10 | import org.testng.annotations.Test; 11 | 12 | /** 13 | * Unit Tests for the {@link DateMatchers} class 14 | * 15 | * @author Stewart Bissett 16 | */ 17 | @SuppressWarnings("deprecation") 18 | public class IsSameMillisecondOfSecondTest { 19 | 20 | private static final String ASSERTION_PATTERN = "\\sExpected: the date has the millisecond [0-9]+?\\s but: the date has the millisecond [0-9]+"; 21 | 22 | // Date Matchers 23 | @Test 24 | public void isDateSameMillisecond() { 25 | Date date = new Date(), other = new Date(date.getTime()); 26 | assertThat(other, DateMatchers.sameMillisecond(date)); 27 | } 28 | 29 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 30 | public void isDateNotSameMillisecond() { 31 | Date date = new Date(), other = addDateField(date, Calendar.MILLISECOND, 1); 32 | assertThat(other, DateMatchers.sameMillisecond(date)); 33 | } 34 | 35 | @Test 36 | public void isDateSameMillisecondDifferentSecond() { 37 | Date date = new Date(), other = addDateField(date, Calendar.SECOND, 1); 38 | assertThat(other, DateMatchers.sameMillisecond(date)); 39 | } 40 | 41 | @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) 42 | public void isSqlDateSameMillisecond() { 43 | Date date = new Date(), other = new java.sql.Date(date.getTime()); 44 | assertThat(other, DateMatchers.sameMillisecond(date)); 45 | } 46 | 47 | @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) 48 | public void isDateSameMillisecondSqlDate() { 49 | Date date = new Date(); 50 | java.sql.Date other = new java.sql.Date(date.getTime()); 51 | assertThat(date, DateMatchers.sameMillisecond(other)); 52 | } 53 | 54 | @Test 55 | public void isDateSameMillisecondOfSecond() { 56 | Date date = new Date(), other = new Date(date.getTime()); 57 | assertThat(other, DateMatchers.sameMillisecondOfSecond(date)); 58 | } 59 | 60 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 61 | public void isDateNotSameMillisecondOfSecond() { 62 | Date date = new Date(), other = addDateField(date, Calendar.MILLISECOND, 1); 63 | assertThat(other, DateMatchers.sameMillisecondOfSecond(date)); 64 | } 65 | 66 | @Test 67 | public void isDateSameMillisecondOfSecondDifferentSecond() { 68 | Date date = new Date(), other = addDateField(date, Calendar.SECOND, 1); 69 | assertThat(other, DateMatchers.sameMillisecondOfSecond(date)); 70 | } 71 | 72 | @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) 73 | public void isSqlDateSameMillisecondOfSecond() { 74 | Date date = new Date(), other = new java.sql.Date(date.getTime()); 75 | assertThat(other, DateMatchers.sameMillisecondOfSecond(date)); 76 | } 77 | 78 | @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) 79 | public void isDateSameMillisecondOfSecondSqlDate() { 80 | Date date = new Date(); 81 | java.sql.Date other = new java.sql.Date(date.getTime()); 82 | assertThat(date, DateMatchers.sameMillisecondOfSecond(other)); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/test/java/org/exparity/hamcrest/date/core/IsSameMinuteOfHourTest.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import static org.exparity.hamcrest.date.testutils.DateMatcherTestUtils.addDateField; 4 | import static org.hamcrest.MatcherAssert.assertThat; 5 | 6 | import java.time.Instant; 7 | import java.time.LocalDateTime; 8 | import java.time.LocalTime; 9 | import java.time.OffsetDateTime; 10 | import java.time.ZonedDateTime; 11 | import java.time.temporal.ChronoUnit; 12 | import java.util.Calendar; 13 | import java.util.Date; 14 | 15 | import org.exparity.hamcrest.date.DateMatchers; 16 | import org.exparity.hamcrest.date.InstantMatchers; 17 | import org.exparity.hamcrest.date.LocalDateTimeMatchers; 18 | import org.exparity.hamcrest.date.LocalTimeMatchers; 19 | import org.exparity.hamcrest.date.OffsetDateTimeMatchers; 20 | import org.exparity.hamcrest.date.ZonedDateTimeMatchers; 21 | import org.testng.annotations.Test; 22 | 23 | /** 24 | * Unit Tests for the {@link DateMatchers} class 25 | * 26 | * @author Stewart Bissett 27 | */ 28 | @SuppressWarnings("deprecation") 29 | public class IsSameMinuteOfHourTest { 30 | 31 | private static final String ASSERTION_PATTERN = "\\sExpected: the date has the minute [0-9]+?\\s but: the date has the minute [0-9]+"; 32 | 33 | // Date Matchers 34 | @Test 35 | public void isDateSameMinute() { 36 | Date date = new Date(), other = new Date(date.getTime()); 37 | assertThat(other, DateMatchers.sameMinute(date)); 38 | } 39 | 40 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 41 | public void isDateNotSameMinute() { 42 | Date date = new Date(), other = addDateField(date, Calendar.MINUTE, 1); 43 | assertThat(other, DateMatchers.sameMinute(date)); 44 | } 45 | 46 | @Test 47 | public void isDateSameMonthDifferentHour() { 48 | Date date = new Date(), other = addDateField(date, Calendar.HOUR, 1); 49 | assertThat(other, DateMatchers.sameMinute(date)); 50 | } 51 | 52 | @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) 53 | public void isSqlDateSameMinute() { 54 | Date date = new Date(), other = new java.sql.Date(date.getTime()); 55 | assertThat(other, DateMatchers.sameMinute(date)); 56 | } 57 | 58 | @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) 59 | public void isDateSameMinuteSqlDate() { 60 | Date date = new Date(); 61 | java.sql.Date other = new java.sql.Date(date.getTime()); 62 | assertThat(other, DateMatchers.sameMinute(date)); 63 | } 64 | 65 | @Test 66 | public void isDateSameMinuteOfHour() { 67 | Date date = new Date(), other = new Date(date.getTime()); 68 | assertThat(other, DateMatchers.sameMinuteOfHour(date)); 69 | } 70 | 71 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 72 | public void isDateNotSameMinuteOfHour() { 73 | Date date = new Date(), other = addDateField(date, Calendar.MINUTE, 1); 74 | assertThat(other, DateMatchers.sameMinuteOfHour(date)); 75 | } 76 | 77 | @Test 78 | public void isDateSameMinuteOfHourDifferentHour() { 79 | Date date = new Date(), other = addDateField(date, Calendar.HOUR, 1); 80 | assertThat(other, DateMatchers.sameMinuteOfHour(date)); 81 | } 82 | 83 | @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) 84 | public void isSqlDateSameMinuteOfHour() { 85 | Date date = new Date(), other = new java.sql.Date(date.getTime()); 86 | assertThat(other, DateMatchers.sameMinuteOfHour(date)); 87 | } 88 | 89 | @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) 90 | public void isSameMinuteOfHourSqlDate() { 91 | Date date = new Date(); 92 | java.sql.Date other = new java.sql.Date(date.getTime()); 93 | assertThat(other, DateMatchers.sameMinuteOfHour(date)); 94 | } 95 | 96 | // LocalDateTime Matchers 97 | @Test 98 | public void isLocalDateTimeSameMinuteOfHour() { 99 | LocalDateTime date = LocalDateTime.now(), other = date; 100 | assertThat(other, LocalDateTimeMatchers.sameMinuteOfHour(date)); 101 | } 102 | 103 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 104 | public void isLocalDateTimeNotSameMinuteOfHour() { 105 | LocalDateTime date = LocalDateTime.now(), other = date.plusMinutes(1); 106 | assertThat(other, LocalDateTimeMatchers.sameMinuteOfHour(date)); 107 | } 108 | 109 | @Test 110 | public void isLocalDateTimeSameMinuteOfHourDifferentHour() { 111 | LocalDateTime date = LocalDateTime.now(), other = date.plusHours(1); 112 | assertThat(other, LocalDateTimeMatchers.sameMinuteOfHour(date)); 113 | } 114 | 115 | // LocalTime Matchers 116 | @Test 117 | public void isLocalTimeSameMinuteOfHour() { 118 | LocalTime date = LocalTime.now(), other = date; 119 | assertThat(other, LocalTimeMatchers.sameMinuteOfHour(date)); 120 | } 121 | 122 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 123 | public void isLocalTimeNotSameMinuteOfHour() { 124 | LocalTime date = LocalTime.now(), other = date.plusMinutes(1); 125 | assertThat(other, LocalTimeMatchers.sameMinuteOfHour(date)); 126 | } 127 | 128 | @Test 129 | public void isLocalTimeSameMinuteOfHourDifferentHour() { 130 | LocalTime date = LocalTime.now(), other = date.plusHours(1); 131 | assertThat(other, LocalTimeMatchers.sameMinuteOfHour(date)); 132 | } 133 | 134 | // ZonedDateTime Matchers 135 | @Test 136 | public void isZonedDateTimeSameMinuteOfHour() { 137 | ZonedDateTime date = ZonedDateTime.now(), other = date; 138 | assertThat(other, ZonedDateTimeMatchers.sameMinuteOfHour(date)); 139 | } 140 | 141 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 142 | public void isZonedDateTimeNotSameMinuteOfHour() { 143 | ZonedDateTime date = ZonedDateTime.now(), other = date.plusMinutes(1); 144 | assertThat(other, ZonedDateTimeMatchers.sameMinuteOfHour(date)); 145 | } 146 | 147 | @Test 148 | public void isZonedDateTimeSameMinuteOfHourDifferentHour() { 149 | ZonedDateTime date = ZonedDateTime.now(), other = date.plusHours(1); 150 | assertThat(other, ZonedDateTimeMatchers.sameMinuteOfHour(date)); 151 | } 152 | 153 | // OffsetDateTime Matchers 154 | @Test 155 | public void isOffsetDateTimeSameMinuteOfHour() { 156 | OffsetDateTime date = OffsetDateTime.now(), other = date; 157 | assertThat(other, OffsetDateTimeMatchers.sameMinuteOfHour(date)); 158 | } 159 | 160 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 161 | public void isOffsetDateTimeNotSameMinuteOfHour() { 162 | OffsetDateTime date = OffsetDateTime.now(), other = date.plusMinutes(1); 163 | assertThat(other, OffsetDateTimeMatchers.sameMinuteOfHour(date)); 164 | } 165 | 166 | @Test 167 | public void isOffsetDateTimeSameMinuteOfHourDifferentHour() { 168 | OffsetDateTime date = OffsetDateTime.now(), other = date.plusHours(1); 169 | assertThat(other, OffsetDateTimeMatchers.sameMinuteOfHour(date)); 170 | } 171 | 172 | // Instant Matchers 173 | @Test 174 | public void isInstantSameMinuteOfHour() { 175 | Instant date = Instant.now(), other = date; 176 | assertThat(other, InstantMatchers.sameMinuteOfHour(date)); 177 | } 178 | 179 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 180 | public void isInstantNotSameMinuteOfHour() { 181 | Instant date = Instant.now(), other = date.plus(1, ChronoUnit.MINUTES); 182 | assertThat(other, InstantMatchers.sameMinuteOfHour(date)); 183 | } 184 | 185 | @Test 186 | public void isInstantSameMinuteOfHourDifferentHour() { 187 | Instant date = Instant.now(), other = date.plus(1, ChronoUnit.HOURS); 188 | assertThat(other, InstantMatchers.sameMinuteOfHour(date)); 189 | } 190 | 191 | } 192 | -------------------------------------------------------------------------------- /src/test/java/org/exparity/hamcrest/date/core/IsSameSecondOfMinuteTest.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import static org.exparity.hamcrest.date.testutils.DateMatcherTestUtils.addDateField; 4 | import static org.hamcrest.MatcherAssert.assertThat; 5 | 6 | import java.time.Instant; 7 | import java.time.LocalDateTime; 8 | import java.time.LocalTime; 9 | import java.time.OffsetDateTime; 10 | import java.time.ZonedDateTime; 11 | import java.time.temporal.ChronoUnit; 12 | import java.util.Calendar; 13 | import java.util.Date; 14 | 15 | import org.exparity.hamcrest.date.DateMatchers; 16 | import org.exparity.hamcrest.date.InstantMatchers; 17 | import org.exparity.hamcrest.date.LocalDateTimeMatchers; 18 | import org.exparity.hamcrest.date.LocalTimeMatchers; 19 | import org.exparity.hamcrest.date.OffsetDateTimeMatchers; 20 | import org.exparity.hamcrest.date.ZonedDateTimeMatchers; 21 | import org.testng.annotations.Test; 22 | 23 | /** 24 | * Unit Tests for the {@link DateMatchers} class 25 | * 26 | * @author Stewart Bissett 27 | */ 28 | @SuppressWarnings("deprecation") 29 | public class IsSameSecondOfMinuteTest { 30 | 31 | private static final String ASSERTION_PATTERN = "\\sExpected: the date has the second [0-9]+?\\s but: the date has the second [0-9]+"; 32 | 33 | // Date Matchers 34 | @Test 35 | public void isDateSameSecond() { 36 | Date date = new Date(), other = new Date(date.getTime()); 37 | assertThat(other, DateMatchers.sameSecond(date)); 38 | } 39 | 40 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 41 | public void isDateNotSameMinute() { 42 | Date date = new Date(), other = addDateField(date, Calendar.SECOND, 1); 43 | assertThat(other, DateMatchers.sameSecond(date)); 44 | } 45 | 46 | @Test 47 | public void isDateSameSecondDifferentMinute() { 48 | Date date = new Date(), other = addDateField(date, Calendar.MINUTE, 1); 49 | assertThat(other, DateMatchers.sameSecond(date)); 50 | } 51 | 52 | @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) 53 | public void isSqlDateSameSecond() { 54 | Date date = new Date(), other = new java.sql.Date(date.getTime()); 55 | assertThat(other, DateMatchers.sameSecond(date)); 56 | } 57 | 58 | @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) 59 | public void isDateSameSecondSqlDate() { 60 | Date date = new Date(); 61 | java.sql.Date other = new java.sql.Date(date.getTime()); 62 | assertThat(date, DateMatchers.sameSecond(other)); 63 | } 64 | 65 | @Test 66 | public void isDateSameSecondOfMinute() { 67 | Date date = new Date(), other = new Date(date.getTime()); 68 | assertThat(other, DateMatchers.sameSecondOfMinute(date)); 69 | } 70 | 71 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 72 | public void isDateNotSameSecondOfMinute() { 73 | Date date = new Date(), other = addDateField(date, Calendar.SECOND, 1); 74 | assertThat(other, DateMatchers.sameSecondOfMinute(date)); 75 | } 76 | 77 | @Test 78 | public void isDateSameSecondOfMinuteDifferentMinute() { 79 | Date date = new Date(), other = addDateField(date, Calendar.MINUTE, 1); 80 | assertThat(other, DateMatchers.sameSecondOfMinute(date)); 81 | } 82 | 83 | @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) 84 | public void isSqlDateSameSecondOfMinute() { 85 | Date date = new Date(), other = new java.sql.Date(date.getTime()); 86 | assertThat(other, DateMatchers.sameSecondOfMinute(date)); 87 | } 88 | 89 | @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) 90 | public void isDateSameSecondOfMinuteSqlDate() { 91 | Date date = new Date(); 92 | java.sql.Date other = new java.sql.Date(date.getTime()); 93 | assertThat(date, DateMatchers.sameSecondOfMinute(other)); 94 | } 95 | 96 | // LocalDateTime Matchers 97 | @Test 98 | public void isLocalDateTimeSameSecondOfMinute() { 99 | LocalDateTime date = LocalDateTime.now(), other = date; 100 | assertThat(other, LocalDateTimeMatchers.sameSecondOfMinute(date)); 101 | } 102 | 103 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 104 | public void isLocalDateTimeNotSameSecondOfMinute() { 105 | LocalDateTime date = LocalDateTime.now(), other = date.plusSeconds(1); 106 | assertThat(other, LocalDateTimeMatchers.sameSecondOfMinute(date)); 107 | } 108 | 109 | @Test 110 | public void isLocalDateTimeSameSecondOfMinuteDifferentMinute() { 111 | LocalDateTime date = LocalDateTime.now(), other = date.plusMinutes(1); 112 | assertThat(other, LocalDateTimeMatchers.sameSecondOfMinute(date)); 113 | } 114 | 115 | // LocalTime Matchers 116 | @Test 117 | public void isLocalTimeSameSecondOfMinute() { 118 | LocalTime date = LocalTime.now(), other = date; 119 | assertThat(other, LocalTimeMatchers.sameSecondOfMinute(date)); 120 | } 121 | 122 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 123 | public void isLocalTimeNotSameSecondOfMinute() { 124 | LocalTime date = LocalTime.now(), other = date.plusSeconds(1); 125 | assertThat(other, LocalTimeMatchers.sameSecondOfMinute(date)); 126 | } 127 | 128 | @Test 129 | public void isLocalTimeSameSecondOfMinuteDifferentMinute() { 130 | LocalTime date = LocalTime.now(), other = date.plusMinutes(1); 131 | assertThat(other, LocalTimeMatchers.sameSecondOfMinute(date)); 132 | } 133 | 134 | // ZonedDateTime Matchers 135 | @Test 136 | public void isZonedDateTimeSameSecondOfMinute() { 137 | ZonedDateTime date = ZonedDateTime.now(), other = date; 138 | assertThat(other, ZonedDateTimeMatchers.sameSecondOfMinute(date)); 139 | } 140 | 141 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 142 | public void isZonedDateTimeNotSameSecondOfMinute() { 143 | ZonedDateTime date = ZonedDateTime.now(), other = date.plusSeconds(1); 144 | assertThat(other, ZonedDateTimeMatchers.sameSecondOfMinute(date)); 145 | } 146 | 147 | @Test 148 | public void isZonedDateTimeSameSecondOfMinuteDifferentMinute() { 149 | ZonedDateTime date = ZonedDateTime.now(), other = date.plusMinutes(1); 150 | assertThat(other, ZonedDateTimeMatchers.sameSecondOfMinute(date)); 151 | } 152 | 153 | // OffsetDateTime Matchers 154 | @Test 155 | public void isOffsetDateTimeSameSecondOfMinute() { 156 | OffsetDateTime date = OffsetDateTime.now(), other = date; 157 | assertThat(other, OffsetDateTimeMatchers.sameSecondOfMinute(date)); 158 | } 159 | 160 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 161 | public void isOffsetDateTimeNotSameSecondOfMinute() { 162 | OffsetDateTime date = OffsetDateTime.now(), other = date.plusSeconds(1); 163 | assertThat(other, OffsetDateTimeMatchers.sameSecondOfMinute(date)); 164 | } 165 | 166 | @Test 167 | public void isOffsetDateTimeSameSecondOfMinuteDifferentMinute() { 168 | OffsetDateTime date = OffsetDateTime.now(), other = date.plusMinutes(1); 169 | assertThat(other, OffsetDateTimeMatchers.sameSecondOfMinute(date)); 170 | } 171 | 172 | // Instant Matchers 173 | @Test 174 | public void isInstantSameSecondOfMinute() { 175 | Instant date = Instant.now(), other = date; 176 | assertThat(other, InstantMatchers.sameSecondOfMinute(date)); 177 | } 178 | 179 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 180 | public void isInstantNotSameSecondOfMinute() { 181 | Instant date = Instant.now(), other = date.plus(1, ChronoUnit.SECONDS); 182 | assertThat(other, InstantMatchers.sameSecondOfMinute(date)); 183 | } 184 | 185 | @Test 186 | public void isInstantSameSecondOfMinuteDifferentMinute() { 187 | Instant date = Instant.now(), other = date.plus(1, ChronoUnit.MINUTES); 188 | assertThat(other, InstantMatchers.sameSecondOfMinute(date)); 189 | } 190 | 191 | } 192 | -------------------------------------------------------------------------------- /src/test/java/org/exparity/hamcrest/date/core/IsSameYearTest.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import static org.exparity.hamcrest.date.testutils.Dates.*; 4 | import static org.hamcrest.MatcherAssert.assertThat; 5 | 6 | import java.time.Instant; 7 | import java.time.LocalDate; 8 | import java.time.LocalDateTime; 9 | import java.time.OffsetDateTime; 10 | import java.time.ZoneId; 11 | import java.time.ZonedDateTime; 12 | import java.time.temporal.ChronoUnit; 13 | 14 | import org.exparity.hamcrest.date.DateMatchers; 15 | import org.exparity.hamcrest.date.InstantMatchers; 16 | import org.exparity.hamcrest.date.LocalDateMatchers; 17 | import org.exparity.hamcrest.date.LocalDateTimeMatchers; 18 | import org.exparity.hamcrest.date.OffsetDateTimeMatchers; 19 | import org.exparity.hamcrest.date.SqlDateMatchers; 20 | import org.exparity.hamcrest.date.ZonedDateTimeMatchers; 21 | import org.testng.annotations.Test; 22 | 23 | /** 24 | * @author Stewart Bissett 25 | */ 26 | public class IsSameYearTest { 27 | 28 | private static final String ASSERTION_PATTERN = "\\sExpected: the date has the year [0-9]+?\\s but: the date has the year [0-9]+"; 29 | 30 | // Date Matchers 31 | @Test 32 | public void isDateSameYear() { 33 | assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.sameYear(SEP_04_2015_NOON_UTC_AS_DATE)); 34 | } 35 | 36 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 37 | public void isDateNotSameYear() { 38 | assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.sameYear(AUG_04_2016_NOON_UTC_AS_DATE)); 39 | } 40 | 41 | @Test 42 | public void isDateSameYearAsSqlDate() { 43 | assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.sameYear(SEP_04_2015_AS_SQL)); 44 | } 45 | 46 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 47 | public void isDateNotSameYearAsSqlDate() { 48 | assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.sameYear(AUG_04_2016_AS_SQL)); 49 | } 50 | 51 | // java.sql.Date Matchers 52 | @Test 53 | public void isSqlDateSameYear() { 54 | assertThat(AUG_04_2015_AS_SQL, SqlDateMatchers.sameYear(AUG_04_2015_AS_SQL)); 55 | } 56 | 57 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 58 | public void isSqlDateNotSameYear() { 59 | assertThat(AUG_04_2015_AS_SQL, SqlDateMatchers.sameYear(AUG_04_2016_AS_SQL)); 60 | } 61 | 62 | @Test 63 | public void isSqlDateSameYearAsDate() { 64 | assertThat(AUG_04_2015_AS_SQL, SqlDateMatchers.sameYear(AUG_04_2015_NOON_UTC_AS_DATE)); 65 | } 66 | 67 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 68 | public void isSqlDateNotSameYearAsDate() { 69 | assertThat(AUG_04_2015_AS_SQL, SqlDateMatchers.sameYear(AUG_04_2016_NOON_UTC_AS_DATE)); 70 | } 71 | 72 | @Test 73 | public void isSqlDateSameYearUsingDateMatchers() { 74 | assertThat(AUG_04_2015_AS_SQL, DateMatchers.sameYear(AUG_04_2015_AS_SQL)); 75 | } 76 | 77 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 78 | public void isSqlDateNotSameYearUsingDateMatchers() { 79 | assertThat(AUG_04_2015_AS_SQL, DateMatchers.sameYear(AUG_04_2016_AS_SQL)); 80 | } 81 | 82 | @Test 83 | public void isSqlDateSameYearAsDateUsingDateMatchers() { 84 | assertThat(AUG_04_2015_AS_SQL, DateMatchers.sameYear(AUG_04_2015_NOON_UTC_AS_DATE)); 85 | } 86 | 87 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 88 | public void isSqlDateNotSameYearAsDateUsingDateMatchers() { 89 | assertThat(AUG_04_2015_AS_SQL, DateMatchers.sameYear(AUG_04_2016_NOON_UTC_AS_DATE)); 90 | } 91 | 92 | // LocalDate Matchers 93 | @Test 94 | public void isLocalDateSameYear() { 95 | LocalDate date = LocalDate.now(), other = date; 96 | assertThat(other, LocalDateMatchers.sameYear(date)); 97 | } 98 | 99 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 100 | public void isLocalDateNotSameYear() { 101 | LocalDate date = LocalDate.now(), other = date.plusYears(1); 102 | assertThat(other, LocalDateMatchers.sameYear(date)); 103 | } 104 | 105 | // LocalDateTime Matchers 106 | @Test 107 | public void isLocalDateTimeSameYear() { 108 | LocalDateTime date = LocalDateTime.now(), other = date; 109 | assertThat(other, LocalDateTimeMatchers.sameYear(date)); 110 | } 111 | 112 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 113 | public void isLocalDateTimeNotSameYear() { 114 | LocalDateTime date = LocalDateTime.now(), other = date.plusYears(1); 115 | assertThat(other, LocalDateTimeMatchers.sameYear(date)); 116 | } 117 | 118 | // ZonedDateTime Matchers 119 | @Test 120 | public void isZonedDateTimeSameYear() { 121 | ZonedDateTime date = ZonedDateTime.now(), other = date; 122 | assertThat(other, ZonedDateTimeMatchers.sameYear(date)); 123 | } 124 | 125 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 126 | public void isZonedDateTimeNotSameYear() { 127 | ZonedDateTime date = ZonedDateTime.now(), other = date.plusYears(1); 128 | assertThat(other, ZonedDateTimeMatchers.sameYear(date)); 129 | } 130 | 131 | // OffsetDateTime Matchers 132 | @Test 133 | public void isOffsetDateTimeSameYear() { 134 | OffsetDateTime date = OffsetDateTime.now(), other = date; 135 | assertThat(other, OffsetDateTimeMatchers.sameYear(date)); 136 | } 137 | 138 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 139 | public void isOffsetDateTimeNotSameYear() { 140 | OffsetDateTime date = OffsetDateTime.now(), other = date.plusYears(1); 141 | assertThat(other, OffsetDateTimeMatchers.sameYear(date)); 142 | } 143 | 144 | // Instant Matchers 145 | @Test 146 | public void isInstantSameYear() { 147 | Instant date = Instant.now(), other = date; 148 | assertThat(other, InstantMatchers.sameYear(date)); 149 | } 150 | 151 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 152 | public void isInstantNotSameYear() { 153 | Instant date = Instant.now(), other = ZonedDateTime.ofInstant(date, ZoneId.systemDefault()).plusYears(1).toInstant(); 154 | assertThat(other, InstantMatchers.sameYear(date)); 155 | } 156 | } 157 | -------------------------------------------------------------------------------- /src/test/java/org/exparity/hamcrest/date/core/IsSecondTest.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.core; 2 | 3 | import static org.exparity.hamcrest.date.testutils.Dates.*; 4 | import static org.hamcrest.MatcherAssert.assertThat; 5 | 6 | import java.time.LocalTime; 7 | 8 | import org.exparity.hamcrest.date.DateMatchers; 9 | import org.exparity.hamcrest.date.InstantMatchers; 10 | import org.exparity.hamcrest.date.LocalDateTimeMatchers; 11 | import org.exparity.hamcrest.date.LocalTimeMatchers; 12 | import org.exparity.hamcrest.date.OffsetDateTimeMatchers; 13 | import org.exparity.hamcrest.date.ZonedDateTimeMatchers; 14 | import org.testng.annotations.Test; 15 | 16 | /** 17 | * Unit Tests for the {@link IsSecond} class 18 | * 19 | * @author Stewart Bissett 20 | */ 21 | @SuppressWarnings("deprecation") 22 | public class IsSecondTest { 23 | 24 | private static final String ASSERTION_PATTERN = "\\sExpected: the date has the second [0-9]+?\\s but: the date has the second [0-9]+"; 25 | 26 | // Date Matchers 27 | @Test 28 | public void isDateSecond() { 29 | assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.isSecond(0)); 30 | } 31 | 32 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 33 | public void isDateNotSecond() { 34 | assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.isSecond(1)); 35 | } 36 | 37 | @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) 38 | public void isSqlDateSecond() { 39 | assertThat(AUG_04_2015_AS_SQL, DateMatchers.isSecond(0)); 40 | } 41 | 42 | @Test 43 | public void isDateSameSecond() { 44 | assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.sameSecond(0)); 45 | } 46 | 47 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 48 | public void isDateNotSameSecond() { 49 | assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.sameSecond(1)); 50 | } 51 | 52 | @Test(expectedExceptions = TemporalConversionException.class, expectedExceptionsMessageRegExp = TemporalConverters.UNSUPPORTED_SQL_DATE_UNIT) 53 | public void isSqlDateSameSecond() { 54 | assertThat(AUG_04_2015_AS_SQL, DateMatchers.sameSecond(0)); 55 | } 56 | 57 | // LocalDateTime Matchers 58 | @Test 59 | public void isLocalDateTimeSecond() { 60 | assertThat(AUG_04_2015_NOON, LocalDateTimeMatchers.isSecond(0)); 61 | } 62 | 63 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 64 | public void isLocalDateTimeNotSecond() { 65 | assertThat(AUG_04_2015_NOON, LocalDateTimeMatchers.isSecond(1)); 66 | } 67 | 68 | // LocalTime Matchers 69 | @Test 70 | public void isLocalTimeSecond() { 71 | assertThat(LocalTime.NOON, LocalTimeMatchers.isSecond(0)); 72 | } 73 | 74 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 75 | public void isLocalimeNotSecond() { 76 | assertThat(LocalTime.NOON, LocalTimeMatchers.isSecond(1)); 77 | } 78 | 79 | // ZonedDateTime Matchers 80 | @Test 81 | public void isZonedDateTimeSecond() { 82 | assertThat(AUG_04_2015_NOON_UTC, ZonedDateTimeMatchers.isSecond(0)); 83 | } 84 | 85 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 86 | public void isZonedDateTimeNotSecond() { 87 | assertThat(AUG_04_2015_NOON_UTC, ZonedDateTimeMatchers.isSecond(1)); 88 | } 89 | 90 | // OffsetDateTime Matchers 91 | @Test 92 | public void isOffsetDateTimeSecond() { 93 | assertThat(AUG_04_2015_NOON_OFFSET_UTC, OffsetDateTimeMatchers.isSecond(0)); 94 | } 95 | 96 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 97 | public void isOffsetDateTimeNotSecond() { 98 | assertThat(AUG_04_2015_NOON_OFFSET_UTC, OffsetDateTimeMatchers.isSecond(1)); 99 | } 100 | 101 | // Instant Matchers 102 | @Test 103 | public void isInstantSecond() { 104 | assertThat(AUG_04_2015_NOON_INSTANT_UTC, InstantMatchers.isSecond(0)); 105 | } 106 | 107 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 108 | public void isInstantNotSecond() { 109 | assertThat(AUG_04_2015_NOON_INSTANT_UTC, InstantMatchers.isSecond(1)); 110 | } 111 | 112 | } 113 | -------------------------------------------------------------------------------- /src/test/java/org/exparity/hamcrest/date/core/IsYearTest.java: -------------------------------------------------------------------------------- 1 | 2 | package org.exparity.hamcrest.date.core; 3 | 4 | import static org.exparity.hamcrest.date.testutils.Dates.*; 5 | import static org.junit.Assert.assertThat; 6 | 7 | import org.exparity.hamcrest.date.DateMatchers; 8 | import org.exparity.hamcrest.date.InstantMatchers; 9 | import org.exparity.hamcrest.date.LocalDateMatchers; 10 | import org.exparity.hamcrest.date.LocalDateTimeMatchers; 11 | import org.exparity.hamcrest.date.OffsetDateTimeMatchers; 12 | import org.exparity.hamcrest.date.SqlDateMatchers; 13 | import org.exparity.hamcrest.date.ZonedDateTimeMatchers; 14 | import org.testng.annotations.Test; 15 | 16 | /** 17 | * Unit test for {@link IsYear} 18 | * 19 | * @author Stewart Bissett 20 | */ 21 | public class IsYearTest { 22 | 23 | private static final String ASSERTION_PATTERN = "\\sExpected: the date has the year [0-9]+?\\s but: the date has the year [0-9]+"; 24 | 25 | // Date Matchers 26 | @Test 27 | public void isDateYear() { 28 | assertThat(AUG_04_2016_MIDNIGHT_UTC_AS_DATE, DateMatchers.isYear(2016)); 29 | } 30 | 31 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 32 | public void isDateNotYear() { 33 | assertThat(AUG_04_2015_NOON_UTC_AS_DATE, DateMatchers.isYear(2016)); 34 | } 35 | 36 | // java.sql.Date Matchers 37 | @Test 38 | public void isSqlDateYear() { 39 | assertThat(AUG_04_2016_AS_SQL, SqlDateMatchers.isYear(2016)); 40 | } 41 | 42 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 43 | public void isSqlDateNotYear() { 44 | assertThat(AUG_04_2015_AS_SQL, SqlDateMatchers.isYear(2016)); 45 | } 46 | 47 | @Test 48 | public void isSqlDateYearUsingDateMatchers() { 49 | assertThat(AUG_04_2016_AS_SQL, DateMatchers.isYear(2016)); 50 | } 51 | 52 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 53 | public void isSqlDateNotYearUsingDateMatchers() { 54 | assertThat(AUG_04_2015_AS_SQL, DateMatchers.isYear(2016)); 55 | } 56 | 57 | // LocalDate Matchers 58 | @Test 59 | public void isLocalDateYear() { 60 | assertThat(AUG_04_2016, LocalDateMatchers.isYear(2016)); 61 | } 62 | 63 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 64 | public void isLocalDateNotYear() { 65 | assertThat(AUG_04_2015, LocalDateMatchers.isYear(2016)); 66 | } 67 | 68 | // LocalDateTime Matchers 69 | @Test 70 | public void isLocalDateTimeYear() { 71 | assertThat(AUG_04_2016_NOON, LocalDateTimeMatchers.isYear(2016)); 72 | } 73 | 74 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 75 | public void isLocalDateTimeNotYear() { 76 | assertThat(AUG_04_2015_NOON, LocalDateTimeMatchers.isYear(2016)); 77 | } 78 | 79 | // ZonedDateTime Matchers 80 | @Test 81 | public void isZonedDateTimeYear() { 82 | assertThat(AUG_04_2016_NOON_UTC, ZonedDateTimeMatchers.isYear(2016)); 83 | } 84 | 85 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 86 | public void isZonedDateTimeNotYear() { 87 | assertThat(AUG_04_2015_NOON_UTC, ZonedDateTimeMatchers.isYear(2016)); 88 | } 89 | 90 | // OffsetDateTime Matchers 91 | @Test 92 | public void isOffsetDateTimeYear() { 93 | assertThat(AUG_04_2016_NOON_OFFSET_UTC, OffsetDateTimeMatchers.isYear(2016)); 94 | } 95 | 96 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 97 | public void isOffsetDateTimeNotYear() { 98 | assertThat(AUG_04_2015_NOON_OFFSET_UTC, OffsetDateTimeMatchers.isYear(2016)); 99 | } 100 | 101 | // Instant Matchers 102 | @Test 103 | public void isInstantYear() { 104 | assertThat(AUG_04_2016_NOON_INSTANT_UTC, InstantMatchers.isYear(2016)); 105 | } 106 | 107 | @Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = ASSERTION_PATTERN) 108 | public void isInstantNotYear() { 109 | assertThat(AUG_04_2015_NOON_INSTANT_UTC, InstantMatchers.isYear(2016)); 110 | } 111 | 112 | } 113 | -------------------------------------------------------------------------------- /src/test/java/org/exparity/hamcrest/date/testutils/DateMatcherTestUtils.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.testutils; 2 | 3 | import java.util.Calendar; 4 | import java.util.Date; 5 | 6 | /** 7 | * Utility class for manipulating dates 8 | * 9 | * @author Stewart Bissett 10 | */ 11 | public abstract class DateMatcherTestUtils { 12 | 13 | public static Date addDateField(final Date date, final int field, final int duration) { 14 | Calendar cal = Calendar.getInstance(); 15 | cal.setTime(date); 16 | cal.add(field, duration); 17 | return cal.getTime(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/org/exparity/hamcrest/date/testutils/TimeZones.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.testutils; 2 | 3 | import java.util.TimeZone; 4 | 5 | /** 6 | * Static repository of timezone used for testing 7 | * 8 | * @author Stewart Bissett 9 | */ 10 | public abstract class TimeZones { 11 | 12 | public static final TimeZone PST_AS_TZ = TimeZone.getTimeZone("PST"); 13 | public static final TimeZone EST_AS_TZ = TimeZone.getTimeZone("EST"); 14 | public static final TimeZone CET_AS_TZ = TimeZone.getTimeZone("CET"); 15 | public static final TimeZone GMT_AS_TZ = TimeZone.getTimeZone("GMT"); 16 | public static final TimeZone UTC_AS_TZ = TimeZone.getTimeZone("UTC"); 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/test/java/org/exparity/hamcrest/date/testutils/ZoneIds.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.testutils; 2 | 3 | import java.time.ZoneId; 4 | 5 | /** 6 | * Static repository of {@link ZoneId} 7 | * 8 | * @author Stewart Bissett 9 | */ 10 | public abstract class ZoneIds { 11 | 12 | public static final ZoneId GMT = ZoneId.of("GMT"); // 00:00 13 | public static final ZoneId UTC = ZoneId.of("UTC"); // 00:00 14 | public static final ZoneId CET = ZoneId.of("UTC+1"); // +01:00 15 | public static final ZoneId EST = ZoneId.of("UTC-5"); // -05:00 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/org/exparity/hamcrest/date/testutils/ZoneOffsets.java: -------------------------------------------------------------------------------- 1 | package org.exparity.hamcrest.date.testutils; 2 | 3 | import java.time.ZoneId; 4 | import java.time.ZoneOffset; 5 | 6 | /** 7 | * Static repository of {@link ZoneId} 8 | * 9 | * @author Stewart Bissett 10 | */ 11 | public abstract class ZoneOffsets { 12 | 13 | public static final ZoneOffset UTC = ZoneOffset.UTC; // 00:00 14 | public static final ZoneOffset CET = ZoneOffset.ofHours(1); // +01:00 15 | public static final ZoneOffset EST = ZoneOffset.ofHours(-5); // -05:00 16 | 17 | } 18 | --------------------------------------------------------------------------------