Skip to content

Commit 9eeb352

Browse files
Refactor tests covering date string conversion
1 parent 9707d51 commit 9eeb352

1 file changed

Lines changed: 69 additions & 53 deletions

File tree

assertj-core/src/test/java/org/assertj/core/api/date/DateAssert_with_string_based_date_representation_Test.java

Lines changed: 69 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,22 @@
1515
import static java.lang.String.format;
1616
import static org.assertj.core.api.Assertions.assertThat;
1717
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
18-
import static org.assertj.core.api.Assertions.catchThrowable;
1918
import static org.assertj.core.api.Assertions.registerCustomDateFormat;
2019
import static org.assertj.core.api.Assertions.useDefaultDateFormatsOnly;
20+
import static org.assertj.core.api.BDDAssertions.then;
21+
import static org.assertj.core.util.AssertionsUtil.expectAssertionError;
2122
import static org.assertj.core.util.DateUtil.parseDatetime;
2223
import static org.assertj.core.util.DateUtil.parseDatetimeWithMs;
2324

2425
import java.sql.Timestamp;
25-
import java.text.ParseException;
2626
import java.text.SimpleDateFormat;
27+
import java.time.LocalDate;
28+
import java.time.LocalDateTime;
29+
import java.time.OffsetDateTime;
30+
import java.time.ZoneId;
31+
import java.time.ZoneOffset;
2732
import java.util.Date;
28-
import java.util.TimeZone;
33+
import java.util.concurrent.TimeUnit;
2934

3035
import org.assertj.core.api.DateAssertBaseTest;
3136
import org.assertj.core.util.DateUtil;
@@ -47,7 +52,6 @@ public void tearDown() {
4752

4853
@Test
4954
void date_assertion_using_default_date_string_representation() {
50-
5155
// datetime with ms is supported
5256
final Date date1timeWithMS = parseDatetimeWithMs("2003-04-26T03:01:02.999");
5357
assertThat(date1timeWithMS).isEqualTo("2003-04-26T03:01:02.999");
@@ -56,52 +60,68 @@ void date_assertion_using_default_date_string_representation() {
5660
assertThat(datetime).isEqualTo("2003-04-26T03:01:02.000");
5761
assertThat(datetime).isEqualTo("2003-04-26T03:01:02");
5862
// date is supported
59-
final Date date = DateUtil.parse("2003-04-26");
63+
Date date = DateUtil.parse("2003-04-26");
6064
assertThat(date).isEqualTo("2003-04-26");
6165
assertThat(date).isEqualTo("2003-04-26T00:00:00");
6266
assertThat(date).isEqualTo("2003-04-26T00:00:00.000");
67+
68+
// TODO add test with nanos
6369
}
6470

6571
@Test
66-
void date_assertion_should_support_timestamp_string_representation() throws ParseException {
67-
Date date = DateUtil.newTimestampDateFormat().parse("2015-05-08 11:30:00.560");
68-
String timestampAsString = DateUtil.newTimestampDateFormat().format(new Timestamp(date.getTime()));
69-
70-
assertThat(date).isEqualTo(timestampAsString);
72+
void date_assertion_should_support_local_date_string_representation() {
73+
// GIVEN
74+
LocalDate localDate = LocalDate.of(2003, 4, 26);
75+
ZoneId systemDefault = ZoneId.systemDefault();
76+
Date date = Date.from(localDate.atStartOfDay(systemDefault).toInstant());
77+
// WHEN/THEN
78+
then(date).isEqualTo("2003-04-26");
7179
}
7280

7381
@Test
74-
void date_assertion_should_support_date_with_utc_time_zone_string_representation() throws ParseException {
75-
SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
76-
isoFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
77-
Date date = isoFormat.parse("2003-04-26T00:00:00");
78-
79-
assertThat(date).isEqualTo("2003-04-26T00:00:00+00:00");
82+
void date_assertion_should_support_timestamp_string_representation() {
83+
// GIVEN
84+
Date date = new Date(Timestamp.valueOf("2003-04-26 13:01:02.999").getTime());
85+
// WHEN/THEN
86+
then(date).isEqualTo("2003-04-26 13:01:02.999");
8087
}
8188

8289
@Test
83-
void date_assertion_should_support_date_with_ms_and_utc_time_zone_string_representation() throws ParseException {
90+
void date_assertion_should_support_date_with_iso_offset_datetime_string_representation() {
8491
// GIVEN
85-
SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
86-
isoFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
87-
// WHEN
88-
Date date = isoFormat.parse("2003-04-26T00:00:00.123");
89-
// THEN
90-
assertThat(date).isEqualTo("2003-04-26T00:00:00.123+00:00");
92+
OffsetDateTime offsetDateTime = OffsetDateTime.of(2003, 4, 26, 13, 1, 2, 0, ZoneOffset.of("+01:00"));
93+
Date date = Date.from(offsetDateTime.toInstant());
94+
// WHEN/THEN
95+
then(date).isEqualTo("2003-04-26T13:01:02+01:00");
9196
}
9297

9398
@Test
94-
void date_assertion_should_support_date_with_utc_time_zone_in_different_time_zone_string_representation() throws ParseException {
95-
SimpleDateFormat isoDateFormatUtc = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
96-
isoDateFormatUtc.setTimeZone(TimeZone.getTimeZone("UTC"));
97-
98-
SimpleDateFormat isoDateFormatNewYork = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
99-
isoDateFormatNewYork.setTimeZone(TimeZone.getTimeZone("America/New_York"));
99+
void date_assertion_should_support_date_with_iso_offset_datetime_string_representation_with_millis() {
100+
// GIVEN
101+
int nanos = (int) TimeUnit.MILLISECONDS.toNanos(999);
102+
OffsetDateTime isoOffsetDateTime = OffsetDateTime.of(2003, 4, 26, 13, 1, 2, nanos, ZoneOffset.of("+02:00"));
103+
Date date = Date.from(isoOffsetDateTime.toInstant());
104+
// WHEN/THEN
105+
then(date).isEqualTo("2003-04-26T13:01:02.999+02:00");
106+
}
100107

101-
Date date = isoDateFormatUtc.parse("2003-04-26T00:00:00");
102-
String newYorkDate = isoDateFormatNewYork.format(date);
108+
@Test
109+
void date_assertion_should_support_date_with_iso_local_datetime_string_representation_with_millis() {
110+
// GIVEN
111+
int nanos = (int) TimeUnit.MILLISECONDS.toNanos(999);
112+
LocalDateTime localDateTimeWithMillis = LocalDateTime.of(2003, 4, 26, 13, 1, 2, nanos);
113+
Date date = Date.from(localDateTimeWithMillis.atZone(ZoneId.systemDefault()).toInstant());
114+
// WHEN/THEN
115+
then(date).isEqualTo("2003-04-26T13:01:02.999");
116+
}
103117

104-
assertThat(date).isEqualTo(newYorkDate);
118+
@Test
119+
void date_assertion_should_support_date_with_iso_local_datetime_string_representation() {
120+
// GIVEN
121+
LocalDateTime localDateTimeWithMillis = LocalDateTime.of(2003, 4, 26, 13, 1, 2, 0);
122+
Date date = Date.from(localDateTimeWithMillis.atZone(ZoneId.systemDefault()).toInstant());
123+
// WHEN/THEN
124+
then(date).isEqualTo("2003-04-26T13:01:02");
105125
}
106126

107127
@Test
@@ -131,18 +151,17 @@ void should_fail_if_given_date_string_representation_cant_be_parsed_with_any_cus
131151
registerCustomDateFormat("yyyy/MM/dd'T'HH:mm:ss");
132152
// registering again has no effect
133153
registerCustomDateFormat("yyyy/MM/dd'T'HH:mm:ss");
134-
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThat(date).withDateFormat("yyyy/MM/dd")
135-
.isEqualTo("2003 04 26"))
136-
.withMessage(format("Failed to parse 2003 04 26 with any of these date formats:%n"
137-
+
138-
" [yyyy/MM/dd'T'HH:mm:ss,%n" +
139-
" yyyy/MM/dd,%n" +
140-
" yyyy-MM-dd'T'HH:mm:ss.SSSX,%n" +
141-
" yyyy-MM-dd'T'HH:mm:ss.SSS,%n" +
142-
" yyyy-MM-dd HH:mm:ss.SSS,%n" +
143-
" yyyy-MM-dd'T'HH:mm:ssX,%n" +
144-
" yyyy-MM-dd'T'HH:mm:ss,%n" +
145-
" yyyy-MM-dd]"));
154+
AssertionError error = expectAssertionError(() -> assertThat(date).withDateFormat("yyyy/MM/dd").isEqualTo("2003 04 26"));
155+
assertThat(error).hasMessage(format("Failed to parse 2003 04 26 with any of these date formats:%n"
156+
+
157+
" [yyyy/MM/dd'T'HH:mm:ss,%n" +
158+
" yyyy/MM/dd,%n" +
159+
" yyyy-MM-dd'T'HH:mm:ss.SSSX,%n" +
160+
" yyyy-MM-dd'T'HH:mm:ss.SSS,%n" +
161+
" yyyy-MM-dd HH:mm:ss.SSS,%n" +
162+
" yyyy-MM-dd'T'HH:mm:ssX,%n" +
163+
" yyyy-MM-dd'T'HH:mm:ss,%n" +
164+
" yyyy-MM-dd]"));
146165
}
147166

148167
@Test
@@ -165,11 +184,10 @@ void use_custom_date_formats_set_from_Assertions_entry_point() {
165184

166185
// WHEN
167186
// fail : the registered format does not match the given date
168-
Throwable error = catchThrowable(() -> assertThat(date).isEqualTo("2003/04/26"));
187+
AssertionError error = expectAssertionError(() -> assertThat(date).isEqualTo("2003/04/26"));
169188

170189
// THEN
171-
assertThat(error).isInstanceOf(AssertionError.class)
172-
.hasMessage(format("Failed to parse 2003/04/26 with any of these date formats:%n" +
190+
assertThat(error).hasMessage(format("Failed to parse 2003/04/26 with any of these date formats:%n" +
173191
" [yyyy/MM/dd'T'HH:mm:ss,%n" +
174192
" yyyy-MM-dd'T'HH:mm:ss.SSSX,%n" +
175193
" yyyy-MM-dd'T'HH:mm:ss.SSS,%n" +
@@ -201,11 +219,10 @@ void use_custom_date_formats_first_then_defaults_to_parse_a_date() {
201219

202220
// WHEN
203221
// date with a custom format : failure since the default formats don't match.
204-
Throwable error = catchThrowable(() -> assertThat(date).isEqualTo("2003/04/26"));
222+
AssertionError error = expectAssertionError(() -> assertThat(date).isEqualTo("2003/04/26"));
205223

206224
// THEN
207-
assertThat(error).isInstanceOf(AssertionError.class)
208-
.hasMessage(format("Failed to parse 2003/04/26 with any of these date formats:%n" +
225+
assertThat(error).hasMessage(format("Failed to parse 2003/04/26 with any of these date formats:%n" +
209226
" [yyyy-MM-dd'T'HH:mm:ss.SSSX,%n" +
210227
" yyyy-MM-dd'T'HH:mm:ss.SSS,%n" +
211228
" yyyy-MM-dd HH:mm:ss.SSS,%n" +
@@ -222,11 +239,10 @@ void use_custom_date_formats_first_then_defaults_to_parse_a_date() {
222239

223240
// WHEN
224241
// but if not format at all matches, it fails.
225-
error = catchThrowable(() -> assertThat(date).isEqualTo("2003 04 26"));
242+
error = expectAssertionError(() -> assertThat(date).isEqualTo("2003 04 26"));
226243

227244
// THEN
228-
assertThat(error).isInstanceOf(AssertionError.class)
229-
.hasMessage(format("Failed to parse 2003 04 26 with any of these date formats:%n" +
245+
assertThat(error).hasMessage(format("Failed to parse 2003 04 26 with any of these date formats:%n" +
230246
" [yyyy/MM/dd,%n" +
231247
" yyyy-MM-dd'T'HH:mm:ss.SSSX,%n" +
232248
" yyyy-MM-dd'T'HH:mm:ss.SSS,%n" +

0 commit comments

Comments
 (0)