-
-
Notifications
You must be signed in to change notification settings - Fork 683
/
AbstractOffsetDateTimeAssert.java
719 lines (682 loc) · 32.7 KB
/
AbstractOffsetDateTimeAssert.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
/**
* 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-2015 the original author or authors.
*/
package org.assertj.core.api;
import static org.assertj.core.error.ShouldBeAfter.shouldBeAfter;
import static org.assertj.core.error.ShouldBeAfterOrEqualsTo.shouldBeAfterOrEqualsTo;
import static org.assertj.core.error.ShouldBeBefore.shouldBeBefore;
import static org.assertj.core.error.ShouldBeBeforeOrEqualsTo.shouldBeBeforeOrEqualsTo;
import static org.assertj.core.error.ShouldBeEqualIgnoringHours.shouldBeEqualIgnoringHours;
import static org.assertj.core.error.ShouldBeEqualIgnoringMinutes.shouldBeEqualIgnoringMinutes;
import static org.assertj.core.error.ShouldBeEqualIgnoringNanos.shouldBeEqualIgnoringNanos;
import static org.assertj.core.error.ShouldBeEqualIgnoringSeconds.shouldBeEqualIgnoringSeconds;
import static org.assertj.core.error.ShouldBeEqualIgnoringTimezone.shouldBeEqualIgnoringTimezone;
import java.time.OffsetDateTime;
import org.assertj.core.internal.Failures;
import org.assertj.core.internal.Objects;
/**
* Assertions for {@link java.time.OffsetDateTime} type from new Date & Time API introduced in Java 8.
*
* @author Paweł Stawicki
* @author Joel Costigliola
* @author Marcin Zajączkowski
*/
public abstract class AbstractOffsetDateTimeAssert<S extends AbstractOffsetDateTimeAssert<S>> extends
AbstractAssert<S, OffsetDateTime> {
public static final String NULL_OFFSET_DATE_TIME_PARAMETER_MESSAGE = "The OffsetDateTime to compare actual with should not be null";
/**
* Creates a new <code>{@link org.assertj.core.api.AbstractOffsetDateTimeAssert}</code>.
*
* @param selfType the "self type"
* @param actual the actual value to verify
*/
protected AbstractOffsetDateTimeAssert(OffsetDateTime actual, Class<?> selfType) {
super(actual, selfType);
}
// visible for test
protected OffsetDateTime getActual() {
return actual;
}
/**
* Verifies that the actual {@code OffsetDateTime} is <b>strictly</b> before the given one.
* <p>
* Example :
* <p>
*
* <pre><code class='java'>
* assertThat(parse("2000-01-01T23:59:59Z")).isBefore(parse("2000-01-02T00:00:00Z"));
* </code></pre>
*
* @param other the given {@link java.time.OffsetDateTime}.
* @return this assertion object.
* @throws AssertionError if the actual {@code OffsetDateTime} is {@code null}.
* @throws IllegalArgumentException if other {@code OffsetDateTime} is {@code null}.
* @throws AssertionError if the actual {@code OffsetDateTime} is not strictly before the given one.
*/
public S isBefore(OffsetDateTime other) {
Objects.instance().assertNotNull(info, actual);
assertOffsetDateTimeParameterIsNotNull(other);
if (!actual.isBefore(other)) {
throw Failures.instance().failure(info, shouldBeBefore(actual, other));
}
return myself;
}
/**
* Same assertion as {@link #isBefore(java.time.OffsetDateTime)} but the {@link java.time.OffsetDateTime} is built
* from given String, which
* must follow <a href=
* "http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_OFFSET_DATE_TIME"
* >ISO OffsetDateTime format</a> to allow calling {@link java.time.OffsetDateTime#parse(CharSequence)} method.
* <p>
* Example :
* <p>
*
* <pre><code class='java'>
* // use directly String in comparison to avoid writing the code to perform the conversion
* assertThat(parse("2000-01-01T23:59:59Z")).isBefore("2000-01-02T00:00:00Z");
* </code></pre>
*
* @param offsetDateTimeAsString String representing a {@link java.time.OffsetDateTime}.
* @return this assertion object.
* @throws AssertionError if the actual {@code OffsetDateTime} is {@code null}.
* @throws IllegalArgumentException if given String is null or can't be converted to a
* {@link java.time.OffsetDateTime}.
* @throws AssertionError if the actual {@code OffsetDateTime} is not strictly before the
* {@link java.time.OffsetDateTime} built
* from given String.
*/
public S isBefore(String offsetDateTimeAsString) {
assertOffsetDateTimeAsStringParameterIsNotNull(offsetDateTimeAsString);
return isBefore(OffsetDateTime.parse(offsetDateTimeAsString));
}
/**
* Verifies that the actual {@code OffsetDateTime} is before or equals to the given one.
* <p>
* Example :
* <p>
*
* <pre><code class='java'>
* assertThat(parse("2000-01-01T23:59:59Z")).isBeforeOrEqualTo(parse("2000-01-01T23:59:59Z"))
* .isBeforeOrEqualTo(parse("2000-01-02T00:00:00Z"));
* </code></pre>
*
* @param other the given {@link java.time.OffsetDateTime}.
* @return this assertion object.
* @throws AssertionError if the actual {@code OffsetDateTime} is {@code null}.
* @throws IllegalArgumentException if other {@code OffsetDateTime} is {@code null}.
* @throws AssertionError if the actual {@code OffsetDateTime} is not before or equals to the given one.
*/
public S isBeforeOrEqualTo(OffsetDateTime other) {
Objects.instance().assertNotNull(info, actual);
assertOffsetDateTimeParameterIsNotNull(other);
if (actual.isAfter(other)) {
throw Failures.instance().failure(info, shouldBeBeforeOrEqualsTo(actual, other));
}
return myself;
}
/**
* Same assertion as {@link #isBeforeOrEqualTo(java.time.OffsetDateTime)} but the {@link java.time.OffsetDateTime} is
* built from given
* String, which must follow <a href=
* "http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_OFFSET_DATE_TIME"
* >ISO OffsetDateTime format</a> to allow calling {@link java.time.OffsetDateTime#parse(CharSequence)} method.
* <p>
* Example :
* <p>
*
* <pre><code class='java'>
* // use String in comparison to avoid conversion
* assertThat(parse("2000-01-01T23:59:59Z")).isBeforeOrEqualTo("2000-01-01T23:59:59Z")
* .isBeforeOrEqualTo("2000-01-02T00:00:00Z");
* </code></pre>
*
* @param offsetDateTimeAsString String representing a {@link java.time.OffsetDateTime}.
* @return this assertion object.
* @throws AssertionError if the actual {@code OffsetDateTime} is {@code null}.
* @throws IllegalArgumentException if given String is null or can't be converted to a
* {@link java.time.OffsetDateTime}.
* @throws AssertionError if the actual {@code OffsetDateTime} is not before or equals to the
* {@link java.time.OffsetDateTime} built from given String.
*/
public S isBeforeOrEqualTo(String offsetDateTimeAsString) {
assertOffsetDateTimeAsStringParameterIsNotNull(offsetDateTimeAsString);
return isBeforeOrEqualTo(OffsetDateTime.parse(offsetDateTimeAsString));
}
/**
* Verifies that the actual {@code OffsetDateTime} is after or equals to the given one.
* <p>
* Example :
* <p>
*
* <pre><code class='java'>
* assertThat(parse("2000-01-01T00:00:00Z")).isAfterOrEqualTo(parse("2000-01-01T00:00:00Z"))
* .isAfterOrEqualTo(parse("1999-12-31T23:59:59Z"));
* </code></pre>
*
* @param other the given {@link java.time.OffsetDateTime}.
* @return this assertion object.
* @throws AssertionError if the actual {@code OffsetDateTime} is {@code null}.
* @throws IllegalArgumentException if other {@code OffsetDateTime} is {@code null}.
* @throws AssertionError if the actual {@code OffsetDateTime} is not after or equals to the given one.
*/
public S isAfterOrEqualTo(OffsetDateTime other) {
Objects.instance().assertNotNull(info, actual);
assertOffsetDateTimeParameterIsNotNull(other);
if (actual.isBefore(other)) {
throw Failures.instance().failure(info, shouldBeAfterOrEqualsTo(actual, other));
}
return myself;
}
/**
* Same assertion as {@link #isAfterOrEqualTo(java.time.OffsetDateTime)} but the {@link java.time.OffsetDateTime} is
* built from given
* String, which must follow <a href=
* "http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_OFFSET_DATE_TIME"
* >ISO OffsetDateTime format</a> to allow calling {@link java.time.OffsetDateTime#parse(CharSequence)} method.
* <p>
* Example :
* <p>
*
* <pre><code class='java'>
* // use String in comparison to avoid conversion
* assertThat(parse("2000-01-01T00:00:00Z")).isAfterOrEqualTo("2000-01-01T00:00:00Z")
* .isAfterOrEqualTo("1999-12-31T23:59:59Z");
* </code></pre>
*
* @param offsetDateTimeAsString String representing a {@link java.time.OffsetDateTime}.
* @return this assertion object.
* @throws AssertionError if the actual {@code OffsetDateTime} is {@code null}.
* @throws IllegalArgumentException if given String is null or can't be converted to a
* {@link java.time.OffsetDateTime}.
* @throws AssertionError if the actual {@code OffsetDateTime} is not after or equals to the
* {@link java.time.OffsetDateTime} built from given String.
*/
public S isAfterOrEqualTo(String offsetDateTimeAsString) {
assertOffsetDateTimeAsStringParameterIsNotNull(offsetDateTimeAsString);
return isAfterOrEqualTo(OffsetDateTime.parse(offsetDateTimeAsString));
}
/**
* Verifies that the actual {@code OffsetDateTime} is <b>strictly</b> after the given one.
* <p>
* Example :
* <p>
*
* <pre><code class='java'>
* assertThat(parse("2000-01-01T00:00:00Z")).isAfter(parse("1999-12-31T23:59:59Z"));
* </code></pre>
*
* @param other the given {@link java.time.OffsetDateTime}.
* @return this assertion object.
* @throws AssertionError if the actual {@code OffsetDateTime} is {@code null}.
* @throws IllegalArgumentException if other {@code OffsetDateTime} is {@code null}.
* @throws AssertionError if the actual {@code OffsetDateTime} is not strictly after the given one.
*/
public S isAfter(OffsetDateTime other) {
Objects.instance().assertNotNull(info, actual);
assertOffsetDateTimeParameterIsNotNull(other);
if (!actual.isAfter(other)) {
throw Failures.instance().failure(info, shouldBeAfter(actual, other));
}
return myself;
}
/**
* Same assertion as {@link #isAfter(java.time.OffsetDateTime)} but the {@link java.time.OffsetDateTime} is built from
* given a String that
* must follow <a href=
* "http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_OFFSET_DATE_TIME"
* >ISO OffsetDateTime format</a> to allow calling {@link java.time.OffsetDateTime#parse(CharSequence)} method.
* <p>
* Example :
* <p>
*
* <pre><code class='java'>
* // use String in comparison to avoid conversion
* assertThat(parse("2000-01-01T00:00:00Z")).isAfter("1999-12-31T23:59:59Z");
* </code></pre>
*
* @param offsetDateTimeAsString String representing a {@link java.time.OffsetDateTime}.
* @return this assertion object.
* @throws AssertionError if the actual {@code OffsetDateTime} is {@code null}.
* @throws IllegalArgumentException if given String is null or can't be converted to a
* {@link java.time.OffsetDateTime}.
* @throws AssertionError if the actual {@code OffsetDateTime} is not strictly after the
* {@link java.time.OffsetDateTime} built from given String.
*/
public S isAfter(String offsetDateTimeAsString) {
assertOffsetDateTimeAsStringParameterIsNotNull(offsetDateTimeAsString);
return isAfter(OffsetDateTime.parse(offsetDateTimeAsString));
}
/**
* Same assertion as {@link #isEqualTo(Object)} (where Object is expected to be {@link java.time.OffsetDateTime}) but
* here you
* pass {@link java.time.OffsetDateTime} String representation that must follow <a href=
* "http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_OFFSET_DATE_TIME"
* >ISO OffsetDateTime format</a> to allow calling {@link java.time.OffsetDateTime#parse(CharSequence)} method.
* <p>
* Example :
* <p>
*
* <pre><code class='java'>
* // use directly String in comparison to avoid writing the code to perform the conversion
* assertThat(parse("2000-01-01T00:00:00Z")).isEqualTo("2000-01-01T00:00:00Z");
* </code></pre>
*
* @param dateTimeAsString String representing a {@link java.time.OffsetDateTime}.
* @return this assertion object.
* @throws AssertionError if the actual {@code OffsetDateTime} is {@code null}.
* @throws IllegalArgumentException if given String is null or can't be converted to a
* {@link java.time.OffsetDateTime}.
* @throws AssertionError if the actual {@code OffsetDateTime} is not equal to the {@link java.time.OffsetDateTime}
* built from given String.
*/
public S isEqualTo(String dateTimeAsString) {
assertOffsetDateTimeAsStringParameterIsNotNull(dateTimeAsString);
return isEqualTo(OffsetDateTime.parse(dateTimeAsString));
}
/**
* Same assertion as {@link #isNotEqualTo(Object)} (where Object is expected to be {@link java.time.OffsetDateTime})
* but here you
* pass {@link java.time.OffsetDateTime} String representation that must follow <a href=
* "http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_OFFSET_DATE_TIME"
* >ISO OffsetDateTime format</a> to allow calling {@link java.time.OffsetDateTime#parse(CharSequence)} method.
* <p>
* Example :
* <p>
*
* <pre><code class='java'>
* // use directly String in comparison to avoid writing the code to perform the conversion
* assertThat(parse("2000-01-01T00:00:00Z")).isNotEqualTo("2000-01-15T00:00:00Z");
* </code></pre>
*
* @param dateTimeAsString String representing a {@link java.time.OffsetDateTime}.
* @return this assertion object.
* @throws AssertionError if the actual {@code OffsetDateTime} is {@code null}.
* @throws IllegalArgumentException if given String is null or can't be converted to a
* {@link java.time.OffsetDateTime}.
* @throws AssertionError if the actual {@code OffsetDateTime} is equal to the {@link java.time.OffsetDateTime} built
* from given String.
*/
public S isNotEqualTo(String dateTimeAsString) {
assertOffsetDateTimeAsStringParameterIsNotNull(dateTimeAsString);
return isNotEqualTo(OffsetDateTime.parse(dateTimeAsString));
}
/**
* Same assertion as {@link #isIn(Object...)} (where Objects are expected to be {@link java.time.OffsetDateTime}) but
* here you
* pass {@link java.time.OffsetDateTime} String representations that must follow <a href=
* "http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_OFFSET_DATE_TIME"
* >ISO OffsetDateTime format</a> to allow calling {@link java.time.OffsetDateTime#parse(CharSequence)} method.
* <p>
* Example :
* <p>
*
* <pre><code class='java'>
* // use String based representation of OffsetDateTime
* assertThat(parse("2000-01-01T00:00:00Z")).isIn("1999-12-31T00:00:00Z", "2000-01-01T00:00:00Z");
* </code></pre>
*
* @param dateTimesAsString String array representing {@link java.time.OffsetDateTime}s.
* @return this assertion object.
* @throws AssertionError if the actual {@code OffsetDateTime} is {@code null}.
* @throws IllegalArgumentException if given String is null or can't be converted to a
* {@link java.time.OffsetDateTime}.
* @throws AssertionError if the actual {@code OffsetDateTime} is not in the {@link java.time.OffsetDateTime}s built
* from given Strings.
*/
public S isIn(String... dateTimesAsString) {
checkIsNotNullAndNotEmpty(dateTimesAsString);
return isIn(convertToOffsetDateTimeArray(dateTimesAsString));
}
/**
* Same assertion as {@link #isNotIn(Object...)} (where Objects are expected to be {@link java.time.OffsetDateTime})
* but here you
* pass {@link java.time.OffsetDateTime} String representations that must follow <a href=
* "http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_OFFSET_DATE_TIME"
* >ISO OffsetDateTime format</a> to allow calling {@link java.time.OffsetDateTime#parse(CharSequence)} method.
* <p>
* Example :
* <p>
*
* <pre><code class='java'>
* // use String based representation of OffsetDateTime
* assertThat(parse("2000-01-01T00:00:00Z")).isNotIn("1999-12-31T00:00:00Z", "2000-01-02T00:00:00Z");
* </code></pre>
*
* @param dateTimesAsString Array of String representing a {@link java.time.OffsetDateTime}.
* @return this assertion object.
* @throws AssertionError if the actual {@code OffsetDateTime} is {@code null}.
* @throws IllegalArgumentException if given String is null or can't be converted to a
* {@link java.time.OffsetDateTime}.
* @throws AssertionError if the actual {@code OffsetDateTime} is in the {@link java.time.OffsetDateTime}s built from
* given Strings.
*/
public S isNotIn(String... dateTimesAsString) {
checkIsNotNullAndNotEmpty(dateTimesAsString);
return isNotIn(convertToOffsetDateTimeArray(dateTimesAsString));
}
/**
* Verifies that actual and given {@code OffsetDateTime} have same year, month, day, hour, minute and second fields,
* (nanosecond fields are ignored in comparison).
* <p>
* Assertion can fail with OffsetDateTimes in same chronological nanosecond time window, e.g :
* <p>
* 2000-01-01T00:00:<b>01.000000000</b>+01:00 and 2000-01-01T00:00:<b>00.999999999</b>+01:00.
* <p>
* Assertion fails as second fields differ even if time difference is only 1ns.
* <p>
* Code example :
* <p>
*
* <pre><code class='java'>
* // successful assertions
* OffsetDateTime OffsetDateTime1 = OffsetDateTime.of(2000, 1, 1, 0, 0, 1, 0, ZoneOffset.UTC);
* OffsetDateTime OffsetDateTime2 = OffsetDateTime.of(2000, 1, 1, 0, 0, 1, 456, ZoneOffset.UTC);
* assertThat(OffsetDateTime1).isEqualToIgnoringNanos(OffsetDateTime2);
* <p>
* // failing assertions (even if time difference is only 1ns)
* OffsetDateTime OffsetDateTimeA = OffsetDateTime.of(2000, 1, 1, 0, 0, 1, 0, ZoneOffset.UTC);
* OffsetDateTime OffsetDateTimeB = OffsetDateTime.of(2000, 1, 1, 0, 0, 0, 999999999, ZoneOffset.UTC);
* assertThat(OffsetDateTimeA).isEqualToIgnoringNanos(OffsetDateTimeB);
* </code></pre>
*
* @param other the given {@link java.time.OffsetDateTime}.
* @return this assertion object.
* @throws AssertionError if the actual {@code OffsetDateTime} is {@code null}.
* @throws IllegalArgumentException if other {@code OffsetDateTime} is {@code null}.
* @throws AssertionError if the actual {@code OffsetDateTime} is are not equal with nanoseconds ignored.
*/
public S isEqualToIgnoringNanos(OffsetDateTime other) {
Objects.instance().assertNotNull(info, actual);
assertOffsetDateTimeParameterIsNotNull(other);
if (!areEqualIgnoringNanos(actual, other)) {
throw Failures.instance().failure(info, shouldBeEqualIgnoringNanos(actual, other));
}
return myself;
}
/**
* Verifies that actual and given {@code OffsetDateTime} have same year, month, day, hour, minute, second and
* nanosecond fields,
* (timezone fields are ignored in comparison).
* <p>
* Code example :
* <p>
*
* <pre><code class='java'>
* // successful assertions
* OffsetDateTime OffsetDateTime1 = OffsetDateTime.of(2000, 1, 1, 0, 0, 1, 0, ZoneOffset.UTC);
* OffsetDateTime OffsetDateTime2 = OffsetDateTime.of(2000, 1, 1, 0, 0, 1, 0, ZoneOffset.MAX);
* assertThat(OffsetDateTime1).isEqualToIgnoringTimezone(OffsetDateTime2);
* <p>
* // failing assertions
* OffsetDateTime OffsetDateTimeA = OffsetDateTime.of(2000, 1, 1, 0, 0, 1, 0, ZoneOffset.UTC);
* OffsetDateTime OffsetDateTimeB = OffsetDateTime.of(2000, 1, 1, 0, 0, 0, 999999999, ZoneOffset.UTC);
* assertThat(OffsetDateTimeA).isEqualToIgnoringTimezone(OffsetDateTimeB);
* </code></pre>
*
* @param other the given {@link java.time.OffsetDateTime}.
* @return this assertion object.
* @throws AssertionError if the actual {@code OffsetDateTime} is {@code null}.
* @throws IllegalArgumentException if other {@code OffsetDateTime} is {@code null}.
* @throws AssertionError if the actual {@code OffsetDateTime} is are not equal with timezone ignored.
*/
public S isEqualToIgnoringTimezone(OffsetDateTime other) {
Objects.instance().assertNotNull(info, actual);
assertOffsetDateTimeParameterIsNotNull(other);
if (!areEqualIgnoringTimezone(actual, other)) {
throw Failures.instance().failure(info, shouldBeEqualIgnoringTimezone(actual, other));
}
return myself;
}
/**
* Verifies that actual and given {@link java.time.OffsetDateTime} have same year, month, day, hour and minute fields
* (second and
* nanosecond fields are ignored in comparison).
* <p>
* Assertion can fail with OffsetDateTimes in same chronological second time window, e.g :
* <p>
* 2000-01-01T00:<b>01:00</b>.000+01:00 and 2000-01-01T00:<b>00:59</b>.000+01:00.
* <p>
* Assertion fails as minute fields differ even if time difference is only 1s.
* <p>
* Code example :
* <p>
*
* <pre><code class='java'>
* // successful assertions
* OffsetDateTime OffsetDateTime1 = OffsetDateTime.of(2000, 1, 1, 23, 50, 0, 0, ZoneOffset.UTC);
* OffsetDateTime OffsetDateTime2 = OffsetDateTime.of(2000, 1, 1, 23, 50, 10, 456, ZoneOffset.UTC);
* assertThat(OffsetDateTime1).isEqualToIgnoringSeconds(OffsetDateTime2);
* <p>
* // failing assertions (even if time difference is only 1ms)
* OffsetDateTime OffsetDateTimeA = OffsetDateTime.of(2000, 1, 1, 23, 50, 00, 000, ZoneOffset.UTC);
* OffsetDateTime OffsetDateTimeB = OffsetDateTime.of(2000, 1, 1, 23, 49, 59, 999, ZoneOffset.UTC);
* assertThat(OffsetDateTimeA).isEqualToIgnoringSeconds(OffsetDateTimeB);
* </code></pre>
*
* @param other the given {@link java.time.OffsetDateTime}.
* @return this assertion object.
* @throws AssertionError if the actual {@code OffsetDateTime} is {@code null}.
* @throws IllegalArgumentException if other {@code OffsetDateTime} is {@code null}.
* @throws AssertionError if the actual {@code OffsetDateTime} is are not equal with second and nanosecond fields
* ignored.
*/
public S isEqualToIgnoringSeconds(OffsetDateTime other) {
Objects.instance().assertNotNull(info, actual);
assertOffsetDateTimeParameterIsNotNull(other);
if (!areEqualIgnoringSeconds(actual, other)) {
throw Failures.instance().failure(info, shouldBeEqualIgnoringSeconds(actual, other));
}
return myself;
}
/**
* Verifies that actual and given {@code OffsetDateTime} have same year, month, day and hour fields (minute, second
* and
* nanosecond fields are ignored in comparison).
* <p>
* Assertion can fail with OffsetDateTimes in same chronological second time window, e.g :
* <p>
* 2000-01-01T<b>01:00</b>:00.000+01:00 and 2000-01-01T<b>00:59:59</b>.000+01:00.
* <p>
* Time difference is only 1s but hour fields differ.
* <p>
* Code example :
* <p>
*
* <pre><code class='java'>
* // successful assertions
* OffsetDateTime OffsetDateTime1 = OffsetDateTime.of(2000, 1, 1, 23, 50, 0, 0, ZoneOffset.UTC);
* OffsetDateTime OffsetDateTime2 = OffsetDateTime.of(2000, 1, 1, 23, 00, 2, 7, ZoneOffset.UTC);
* assertThat(OffsetDateTime1).isEqualToIgnoringMinutes(OffsetDateTime2);
* <p>
* // failing assertions (even if time difference is only 1ms)
* OffsetDateTime OffsetDateTimeA = OffsetDateTime.of(2000, 1, 1, 01, 00, 00, 000, ZoneOffset.UTC);
* OffsetDateTime OffsetDateTimeB = OffsetDateTime.of(2000, 1, 1, 00, 59, 59, 999, ZoneOffset.UTC);
* assertThat(OffsetDateTimeA).isEqualToIgnoringMinutes(OffsetDateTimeB);
* </code></pre>
*
* @param other the given {@link java.time.OffsetDateTime}.
* @return this assertion object.
* @throws AssertionError if the actual {@code OffsetDateTime} is {@code null}.
* @throws IllegalArgumentException if other {@code OffsetDateTime} is {@code null}.
* @throws AssertionError if the actual {@code OffsetDateTime} is are not equal ignoring minute, second and nanosecond
* fields.
*/
public S isEqualToIgnoringMinutes(OffsetDateTime other) {
Objects.instance().assertNotNull(info, actual);
assertOffsetDateTimeParameterIsNotNull(other);
if (!areEqualIgnoringMinutes(actual, other)) {
throw Failures.instance().failure(info, shouldBeEqualIgnoringMinutes(actual, other));
}
return myself;
}
/**
* Verifies that actual and given {@code OffsetDateTime} have same year, month and day fields (hour, minute, second
* and
* nanosecond fields are ignored in comparison).
* <p>
* Assertion can fail with OffsetDateTimes in same chronological minute time window, e.g :
* <p>
* 2000-01-<b>01T23:59</b>:00.000+01:00 and 2000-01-02T<b>00:00</b>:00.000+01:00.
* <p>
* Time difference is only 1min but day fields differ.
* <p>
* Code example :
* <p>
*
* <pre><code class='java'>
* // successful assertions
* OffsetDateTime OffsetDateTime1 = OffsetDateTime.of(2000, 1, 1, 23, 59, 59, 999, ZoneOffset.UTC);
* OffsetDateTime OffsetDateTime2 = OffsetDateTime.of(2000, 1, 1, 00, 00, 00, 000, ZoneOffset.UTC);
* assertThat(OffsetDateTime1).isEqualToIgnoringHours(OffsetDateTime2);
* <p>
* // failing assertions (even if time difference is only 1ms)
* OffsetDateTime OffsetDateTimeA = OffsetDateTime.of(2000, 1, 2, 00, 00, 00, 000, ZoneOffset.UTC);
* OffsetDateTime OffsetDateTimeB = OffsetDateTime.of(2000, 1, 1, 23, 59, 59, 999, ZoneOffset.UTC);
* assertThat(OffsetDateTimeA).isEqualToIgnoringHours(OffsetDateTimeB);
* </code></pre>
*
* @param other the given {@link java.time.OffsetDateTime}.
* @return this assertion object.
* @throws AssertionError if the actual {@code OffsetDateTime} is {@code null}.
* @throws IllegalArgumentException if other {@code OffsetDateTime} is {@code null}.
* @throws AssertionError if the actual {@code OffsetDateTime} is are not equal with second and nanosecond fields
* ignored.
*/
public S isEqualToIgnoringHours(OffsetDateTime other) {
Objects.instance().assertNotNull(info, actual);
assertOffsetDateTimeParameterIsNotNull(other);
if (!haveSameYearMonthAndDayOfMonth(actual, other)) {
throw Failures.instance().failure(info, shouldBeEqualIgnoringHours(actual, other));
}
return myself;
}
/**
* Returns true if both OffsetDateTime are in the same year, month and day of month, hour, minute and second, false
* otherwise.
*
* @param actual the actual OffsetDateTime. expected not be null
* @param other the other OffsetDateTime. expected not be null
* @return true if both OffsetDateTime are in the same year, month and day of month, hour, minute and second, false
* otherwise.
*/
private static boolean areEqualIgnoringNanos(OffsetDateTime actual, OffsetDateTime other) {
return areEqualIgnoringSeconds(actual, other) && actual.getSecond() == other.getSecond();
}
/**
* Returns true if both OffsetDateTime are in the same year, month, day of month, hour and minute, false otherwise.
*
* @param actual the actual OffsetDateTime. expected not be null
* @param other the other OffsetDateTime. expected not be null
* @return true if both OffsetDateTime are in the same year, month, day of month, hour and minute, false otherwise.
*/
private static boolean areEqualIgnoringSeconds(OffsetDateTime actual, OffsetDateTime other) {
return areEqualIgnoringMinutes(actual, other) && actual.getMinute() == other.getMinute();
}
/**
* Returns true if both OffsetDateTime are in the same year, month, day of month and hour, false otherwise.
*
* @param actual the actual OffsetDateTime. expected not be null
* @param other the other OffsetDateTime. expected not be null
* @return true if both OffsetDateTime are in the same year, month, day of month and hour, false otherwise.
*/
private static boolean areEqualIgnoringMinutes(OffsetDateTime actual, OffsetDateTime other) {
return haveSameYearMonthAndDayOfMonth(actual, other) && actual.getHour() == other.getHour();
}
/**
* Returns true if both OffsetDateTime are in the same year, month and day of month, false otherwise.
*
* @param actual the actual OffsetDateTime. expected not be null
* @param other the other OffsetDateTime. expected not be null
* @return true if both OffsetDateTime are in the same year, month and day of month, false otherwise
*/
private static boolean haveSameYearMonthAndDayOfMonth(OffsetDateTime actual, OffsetDateTime other) {
return haveSameYearAndMonth(actual, other) && actual.getDayOfMonth() == other.getDayOfMonth();
}
/**
* Returns true if both OffsetDateTime are in the same year and month, false otherwise.
*
* @param actual the actual OffsetDateTime. expected not be null
* @param other the other OffsetDateTime. expected not be null
* @return true if both OffsetDateTime are in the same year and month, false otherwise
*/
private static boolean haveSameYearAndMonth(OffsetDateTime actual, OffsetDateTime other) {
return haveSameYear(actual, other) && actual.getMonth() == other.getMonth();
}
/**
* Returns true if both OffsetDateTime are in the same year, false otherwise.
*
* @param actual the actual OffsetDateTime. expected not be null
* @param other the other OffsetDateTime. expected not be null
* @return true if both OffsetDateTime are in the same year, false otherwise
*/
private static boolean haveSameYear(OffsetDateTime actual, OffsetDateTime other) {
return actual.getYear() == other.getYear();
}
/**
* Returns true if both OffsetDateTime are in the same hour, minute, second and nanosecond false otherwise.
*
* @param actual the actual OffsetDateTime. expected not be null
* @param other the other OffsetDateTime. expected not be null
* @return true if both OffsetDateTime are in the same hour, minute, second and nanosecond false otherwise.
*/
private static boolean areEqualIgnoringTimezone(OffsetDateTime actual, OffsetDateTime other) {
return areEqualIgnoringNanos(actual, other) && haveSameNano(actual, other);
}
/**
* Returns true if both OffsetDateTime are in the same nanosecond, false otherwise.
*
* @param actual the actual OffsetDateTime. expected not be null
* @param other the other OffsetDateTime. expected not be null
* @return true if both OffsetDateTime are in the same year, false otherwise
*/
private static boolean haveSameNano(OffsetDateTime actual, OffsetDateTime other) {
return actual.getNano() == other.getNano();
}
private static Object[] convertToOffsetDateTimeArray(String... dateTimesAsString) {
OffsetDateTime[] dates = new OffsetDateTime[dateTimesAsString.length];
for (int i = 0; i < dateTimesAsString.length; i++) {
dates[i] = OffsetDateTime.parse(dateTimesAsString[i]);
}
return dates;
}
private void checkIsNotNullAndNotEmpty(Object[] values) {
if (values == null) throw new IllegalArgumentException("The given OffsetDateTime array should not be null");
if (values.length == 0) throw new IllegalArgumentException("The given OffsetDateTime array should not be empty");
}
/**
* Check that the {@link java.time.OffsetDateTime} string representation to compare actual
* {@link java.time.OffsetDateTime} to is not null,
* otherwise throws a {@link IllegalArgumentException} with an explicit message
*
* @param offsetDateTimeAsString String representing the {@link java.time.OffsetDateTime} to compare actual with
* @throws IllegalArgumentException with an explicit message if the given {@link String} is null
*/
private static void assertOffsetDateTimeAsStringParameterIsNotNull(String offsetDateTimeAsString) {
if (offsetDateTimeAsString == null) {
// @format:off
throw new IllegalArgumentException("The String representing the OffsetDateTime to compare actual with should not be null");
// @format:on
}
}
/**
* Check that the {@link java.time.OffsetDateTime} to compare actual {@link java.time.OffsetDateTime} to is not null,
* in that case throws a {@link IllegalArgumentException} with an explicit message
*
* @param other the {@link java.time.OffsetDateTime} to check
* @throws IllegalArgumentException with an explicit message if the given {@link java.time.OffsetDateTime} is null
*/
private static void assertOffsetDateTimeParameterIsNotNull(OffsetDateTime other) {
if (other == null) {
throw new IllegalArgumentException("The OffsetDateTime to compare actual with should not be null");
}
}
}