├── .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 [](https://travis-ci.org/eXparity/hamcrest-date) [](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