/
JodaTimeAssertionsExamples.java
256 lines (231 loc) · 12.8 KB
/
JodaTimeAssertionsExamples.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2016 the original author or authors.
*/
package org.assertj.examples;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.jodatime.api.Assertions.assertThat;
import static org.joda.time.DateTimeZone.UTC;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.junit.jupiter.api.Test;
/**
* Joda Time assertions examples.
*
* @author Joel Costigliola
*/
public class JodaTimeAssertionsExamples extends AbstractAssertionsExamples {
@Test
public void dateTime_assertions_examples() {
assertThat(new DateTime("2000-01-01")).isEqualTo(new DateTime("2000-01-01")).isNotEqualTo(
new DateTime("2000-01-15"));
// same assertions but parameters is String based representation of DateTime
assertThat(new DateTime("2000-01-01")).isEqualTo("2000-01-01").isNotEqualTo("2000-01-15");
assertThat(new DateTime("2000-01-01")).isBeforeOrEqualTo(new DateTime("2000-01-01"));
assertThat(new DateTime("2000-01-01")).isAfterOrEqualTo(new DateTime("2000-01-01"));
assertThat(new DateTime("2000-01-01")).isBefore(new DateTime("2000-01-02")).isAfter(new DateTime("1999-12-31"));
// same assertions but parameters is String based representation of DateTime
assertThat(new DateTime("2000-01-01")).isBefore("2000-01-02").isAfter("1999-12-31");
assertThat(new DateTime("2000-01-01")).isIn(new DateTime("1999-12-31"), new DateTime("2000-01-01"));
assertThat(new DateTime("2000-01-01")).isNotIn(new DateTime("1999-12-31"), new DateTime("2000-01-02"));
// same assertions but parameters is String based representation of DateTime
assertThat(new DateTime("2000-01-01")).isIn("1999-12-31", "2000-01-01").isNotIn("1999-12-31", "2000-01-02");
DateTime dateTime = new DateTime(1999, 12, 31, 23, 59, 59, 999, DateTimeZone.UTC);
assertThat(dateTime).hasYear(1999)
.hasMonthOfYear(12)
.hasDayOfMonth(31)
.hasHourOfDay(23)
.hasMinuteOfHour(59)
.hasSecondOfMinute(59)
.hasMillisOfSecond(999);
}
@Test
public void localDateTime_assertions_examples() {
assertThat(new LocalDateTime("2000-01-01")).isEqualTo(new LocalDateTime("2000-01-01")).isNotEqualTo(
new LocalDateTime("2000-01-15"));
// same assertions but parameters is String based representation of LocalDateTime
assertThat(new LocalDateTime("2000-01-01")).isEqualTo("2000-01-01").isNotEqualTo("2000-01-15");
assertThat(new LocalDateTime("2000-01-01")).isBefore(new LocalDateTime("2000-01-02")).isAfter(
new LocalDateTime("1999-12-31"));
// same assertions but parameters is String based representation of LocalDateTime
assertThat(new LocalDateTime("2000-01-01")).isBefore("2000-01-02").isAfter("1999-12-31");
assertThat(new LocalDateTime("2000-01-01")).isIn(new LocalDateTime("1999-12-31"), new LocalDateTime("2000-01-01"));
assertThat(new LocalDateTime("2000-01-01")).isNotIn(new LocalDateTime("1999-12-31"),
new LocalDateTime("2000-01-02"));
// same assertions but parameters is String based representation of LocalDateTime
assertThat(new LocalDateTime("2000-01-01")).isIn("1999-12-31", "2000-01-01").isNotIn("1999-12-31", "2000-01-02");
LocalDateTime localDateTime = new LocalDateTime(1999, 12, 31, 23, 59, 59, 999);
assertThat(localDateTime).hasYear(1999)
.hasMonthOfYear(12)
.hasDayOfMonth(31)
.hasHourOfDay(23)
.hasMinuteOfHour(59)
.hasSecondOfMinute(59)
.hasMillisOfSecond(999);
}
@Test
public void mixing_core_and_joda_time_assertions_examples() {
// assertThat comes from org.assertj.jodatime.api.JODA_TIME.assertThat static import
assertThat(new DateTime("2000-01-01")).isEqualTo(new DateTime("2000-01-01"));
// assertThat comes from org.assertj.core.api.Assertions.assertThat static import
assertThat("hello world").startsWith("hello");
// let's see if ShouldBeAfter error
try {
assertThat(new DateTime(10)).isAfter(new DateTime(1000));
} catch (AssertionError e) {
logAssertionErrorMessage("isAfter", e);
}
}
@Test
public void date_time_comparison_with_precision_level_examples() {
// successful assertions ignoring ...
// ... milliseconds
DateTime dateTime1 = new DateTime(2000, 1, 1, 0, 0, 1, 0, UTC);
DateTime dateTime2 = new DateTime(2000, 1, 1, 0, 0, 1, 456, UTC);
assertThat(dateTime1).isEqualToIgnoringMillis(dateTime2);
// ... seconds
dateTime1 = new DateTime(2000, 1, 1, 23, 50, 0, 0, UTC);
dateTime2 = new DateTime(2000, 1, 1, 23, 50, 10, 456, UTC);
assertThat(dateTime1).isEqualToIgnoringSeconds(dateTime2);
// ... minutes
dateTime1 = new DateTime(2000, 1, 1, 23, 50, 0, 0, UTC);
dateTime2 = new DateTime(2000, 1, 1, 23, 00, 2, 7, UTC);
assertThat(dateTime1).isEqualToIgnoringMinutes(dateTime2);
// ... hours
dateTime1 = new DateTime(2000, 1, 1, 23, 59, 59, 999, UTC);
dateTime2 = new DateTime(2000, 1, 1, 00, 00, 00, 000, UTC);
assertThat(dateTime1).isEqualToIgnoringHours(dateTime2);
// failing assertions even if time difference is 1ms (compared fields differ)
try {
DateTime dateTimeA = new DateTime(2000, 1, 1, 0, 0, 1, 0);
DateTime dateTimeB = new DateTime(2000, 1, 1, 0, 0, 0, 999);
assertThat(dateTimeA).isEqualToIgnoringMillis(dateTimeB);
} catch (AssertionError e) {
logAssertionErrorMessage("DateTimeAssert.isEqualToIgnoringMillis", e);
}
try {
DateTime dateTimeA = new DateTime(2000, 1, 1, 23, 50, 00, 000);
DateTime dateTimeB = new DateTime(2000, 1, 1, 23, 49, 59, 999);
assertThat(dateTimeA).isEqualToIgnoringSeconds(dateTimeB);
} catch (AssertionError e) {
logAssertionErrorMessage("DateTimeAssert.isEqualToIgnoringSeconds", e);
}
try {
DateTime dateTimeA = new DateTime(2000, 1, 1, 01, 00, 00, 000);
DateTime dateTimeB = new DateTime(2000, 1, 1, 00, 59, 59, 999);
assertThat(dateTimeA).isEqualToIgnoringMinutes(dateTimeB);
} catch (AssertionError e) {
logAssertionErrorMessage("DateTimeAssert.isEqualToIgnoringMinutes", e);
}
try {
DateTime dateTimeA = new DateTime(2000, 1, 2, 00, 00, 00, 000);
DateTime dateTimeB = new DateTime(2000, 1, 1, 23, 59, 59, 999);
assertThat(dateTimeA).isEqualToIgnoringHours(dateTimeB);
} catch (AssertionError e) {
logAssertionErrorMessage("DateTimeAssert.isEqualToIgnoringHours", e);
}
}
@Test
public void local_date_time_comparison_with_precision_level_examples() {
// successful assertions ignoring ...
// ... milliseconds
LocalDateTime localDateTime1 = new LocalDateTime(2000, 1, 1, 0, 0, 1, 0);
LocalDateTime localDateTime2 = new LocalDateTime(2000, 1, 1, 0, 0, 1, 456);
assertThat(localDateTime1).isEqualToIgnoringMillis(localDateTime2);
// ... seconds
localDateTime1 = new LocalDateTime(2000, 1, 1, 23, 50, 0, 0);
localDateTime2 = new LocalDateTime(2000, 1, 1, 23, 50, 10, 456);
assertThat(localDateTime1).isEqualToIgnoringSeconds(localDateTime2);
// ... minutes
localDateTime1 = new LocalDateTime(2000, 1, 1, 23, 50, 0, 0);
localDateTime2 = new LocalDateTime(2000, 1, 1, 23, 00, 2, 7);
assertThat(localDateTime1).isEqualToIgnoringMinutes(localDateTime2);
// ... hours
localDateTime1 = new LocalDateTime(2000, 1, 1, 23, 59, 59, 999);
localDateTime2 = new LocalDateTime(2000, 1, 1, 00, 00, 00, 000);
assertThat(localDateTime1).isEqualToIgnoringHours(localDateTime2);
// failing assertions even if time difference is 1ms (compared fields differ)
try {
LocalDateTime localDateTimeA = new LocalDateTime(2000, 1, 1, 0, 0, 1, 0);
LocalDateTime localDateTimeB = new LocalDateTime(2000, 1, 1, 0, 0, 0, 999);
assertThat(localDateTimeA).isEqualToIgnoringMillis(localDateTimeB);
} catch (AssertionError e) {
logAssertionErrorMessage("LocalDateTimeAssert.isEqualToIgnoringMillis", e);
}
try {
LocalDateTime localDateTimeA = new LocalDateTime(2000, 1, 1, 23, 50, 00, 000);
LocalDateTime localDateTimeB = new LocalDateTime(2000, 1, 1, 23, 49, 59, 999);
assertThat(localDateTimeA).isEqualToIgnoringSeconds(localDateTimeB);
} catch (AssertionError e) {
logAssertionErrorMessage("LocalDateTimeAssert.isEqualToIgnoringSeconds", e);
}
try {
LocalDateTime localDateTimeA = new LocalDateTime(2000, 1, 1, 01, 00, 00, 000);
LocalDateTime localDateTimeB = new LocalDateTime(2000, 1, 1, 00, 59, 59, 999);
assertThat(localDateTimeA).isEqualToIgnoringMinutes(localDateTimeB);
} catch (AssertionError e) {
logAssertionErrorMessage("LocalDateTimeAssert.isEqualToIgnoringMinutes", e);
}
try {
LocalDateTime localDateTimeA = new LocalDateTime(2000, 1, 2, 00, 00, 00, 000);
LocalDateTime localDateTimeB = new LocalDateTime(2000, 1, 1, 23, 59, 59, 999);
assertThat(localDateTimeA).isEqualToIgnoringHours(localDateTimeB);
} catch (AssertionError e) {
logAssertionErrorMessage("LocalDateTimeAssert.isEqualToIgnoringHours", e);
}
}
@Test
public void localDate_assertions_examples() {
assertThat(new LocalDate("2000-01-01")).isEqualTo(new LocalDate("2000-01-01"))
.isNotEqualTo(new LocalDate("2000-01-15"));
// same assertions but parameters is String based representation of LocalDate
assertThat(new LocalDate("2000-01-01")).isEqualTo("2000-01-01")
.isNotEqualTo("2000-01-15");
assertThat(new LocalDate("2000-01-01")).isBefore(new LocalDate("2000-01-02"))
.isAfter(new LocalDate("1999-12-31"));
// same assertions but parameters is String based representation of LocalDate
assertThat(new LocalDate("2000-01-01")).isBefore("2000-01-02")
.isAfter("1999-12-31");
assertThat(new LocalDate("2000-01-01")).isIn(new LocalDate("1999-12-31"),
new LocalDate("2000-01-01"));
assertThat(new LocalDate("2000-01-01")).isNotIn(new LocalDate("1999-12-31"),
new LocalDate("2000-01-02"));
// same assertions but parameters is String based representation of LocalDate
assertThat(new LocalDate("2000-01-01")).isIn("1999-12-31", "2000-01-01")
.isNotIn("1999-12-31", "2000-01-02");
LocalDate localDate = new LocalDate(2000, 1, 1);
assertThat(localDate).hasYear(2000)
.hasMonthOfYear(1)
.hasDayOfMonth(1)
.isBefore(new LocalDate(2000, 1, 2))
.isBefore("2000-01-02")
.isBeforeOrEqualTo(new LocalDate(2000, 1, 1))
.isBeforeOrEqualTo("2000-01-01")
.isBeforeOrEqualTo(new LocalDate(2000, 1, 2))
.isBeforeOrEqualTo("2000-01-02")
.isEqualTo(new LocalDate(2000, 1, 1))
.isEqualTo("2000-01-01")
.isAfterOrEqualTo(new LocalDate(2000, 1, 1))
.isAfterOrEqualTo("2000-01-01")
.isAfterOrEqualTo(new LocalDate(1999, 12, 31))
.isAfterOrEqualTo("1999-12-31")
.isAfter(new LocalDate(1999, 12, 31))
.isAfter("1999-12-31")
.isNotEqualTo("2000-01-15")
.isNotEqualTo(new LocalDate(2000, 1, 15))
.isIn(new LocalDate(1999, 12, 31), new LocalDate(2000, 1, 1))
.isIn("1999-12-31", "2000-01-01")
.isNotIn(new LocalDate(1999, 12, 31), new LocalDate(2000, 1, 2))
.isNotIn("1999-12-31", "2000-01-02");
}
}