Skip to content

Commit 1f038d7

Browse files
authored
[JSTEP-5] Tests to verify current behavior and discuss further action DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS (#5142)
1 parent 377ef58 commit 1f038d7

File tree

3 files changed

+223
-1
lines changed

3 files changed

+223
-1
lines changed

pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@
169169
<version>${version.mockito}</version>
170170
<scope>test</scope>
171171
</dependency>
172-
173172
</dependencies>
174173

175174
<!-- Alas, need to include snapshot reference since otherwise can not find
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package tools.jackson.databind.ext.javatime.ser;
2+
3+
import java.time.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
import tools.jackson.databind.ObjectMapper;
7+
import tools.jackson.databind.cfg.DateTimeFeature;
8+
import tools.jackson.databind.ext.javatime.DateTimeTestBase;
9+
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
12+
// [databind#5142] JSTEP-5 Tests to verify current behavior and discuss further action DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS
13+
public class WriteDatesAsTimestamps5142JavaTimeTests
14+
extends DateTimeTestBase
15+
{
16+
private static final ObjectMapper WITH_TIMESTAMP_MAPPER = mapperBuilder()
17+
.enable(DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS)
18+
.build();
19+
20+
private static final ObjectMapper WITHOUT_TIMESTAMP_MAPPER = mapperBuilder()
21+
.disable(DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS)
22+
.build();
23+
24+
@Test
25+
public void testWriteDatesAsTimeStamps() throws Exception {
26+
// java.time.OffsetDateTime
27+
_testTimestamp(
28+
LocalDateTime.of(2025, 5, 4, 18, 1, 0),
29+
LocalDateTime.class,
30+
// Expected "[2025,5,4,18,1,0]",
31+
"[2025,5,4,18,1]", // Acutal
32+
"\"2025-05-04T18:01:00\""
33+
);
34+
// java.time.ZonedDateTime
35+
_testTimestamp(
36+
LocalDateTime.of(2025, 5, 4, 18, 1, 2),
37+
LocalDateTime.class,
38+
"[2025,5,4,18,1,2]",
39+
"\"2025-05-04T18:01:02\""
40+
);
41+
// java.time.LocalDate
42+
_testTimestamp(
43+
LocalDate.of(2025, 5, 4),
44+
LocalDate.class,
45+
"[2025,5,4]",
46+
"\"2025-05-04\""
47+
);
48+
// java.time.LocalTime
49+
_testTimestamp(
50+
LocalTime.of(18, 1, 2),
51+
LocalTime.class,
52+
"[18,1,2]",
53+
"\"18:01:02\""
54+
);
55+
// java.time.Instant
56+
_testTimestamp(
57+
Instant.ofEpochMilli(1234567890123L),
58+
Instant.class,
59+
// Expected "1234567890123",
60+
"1234567890.123000000",
61+
"\"2009-02-13T23:31:30.123Z\""
62+
);
63+
// java.time.ZoneId
64+
_testTimestamp(
65+
ZoneId.of("UTC"),
66+
ZoneId.class,
67+
"\"UTC\"",
68+
"\"UTC\""
69+
);
70+
// java.time.ZoneOffset
71+
_testTimestamp(
72+
ZoneOffset.ofHours(2),
73+
ZoneOffset.class,
74+
"\"+02:00\"",
75+
"\"+02:00\""
76+
);
77+
// java.time.Duration
78+
_testTimestamp(
79+
Duration.ofHours(2),
80+
Duration.class,
81+
// Expected "7200000",
82+
"\"PT2H\"", // Actual
83+
"\"PT2H\""
84+
);
85+
// java.time.Period
86+
_testTimestamp(
87+
Period.of(2025, 5, 4),
88+
Period.class,
89+
// Expected "[2025,5,4]",
90+
"\"P2025Y5M4D\"", // Actual
91+
"\"P2025Y5M4D\""
92+
);
93+
// java.time.Year
94+
_testTimestamp(
95+
Year.of(2025),
96+
Year.class,
97+
"2025", // Actual
98+
"2025"
99+
);
100+
// java.time.YearMonth
101+
_testTimestamp(
102+
YearMonth.of(2025, 5),
103+
YearMonth.class,
104+
"[2025,5]",
105+
"\"2025-05\""
106+
);
107+
// java.time.MonthDay
108+
_testTimestamp(
109+
MonthDay.of(5, 4),
110+
MonthDay.class,
111+
"\"--05-04\"",
112+
"\"--05-04\""
113+
);
114+
// java.time.OffsetTime
115+
_testTimestamp(
116+
OffsetTime.of(18, 1, 2, 0, ZoneOffset.UTC),
117+
OffsetTime.class,
118+
"[18,1,2,\"Z\"]",
119+
"\"18:01:02Z\""
120+
);
121+
// java.time.OffsetDateTime
122+
_testTimestamp(
123+
OffsetDateTime.of(2025, 5, 4, 18, 1, 2, 0, ZoneOffset.UTC),
124+
OffsetDateTime.class,
125+
// Expected... "[2025,5,4,18,1,2,0]",
126+
"1746381662.000000000",
127+
"\"2025-05-04T18:01:02Z\""
128+
);
129+
// java.time.ZonedDateTime
130+
_testTimestamp(
131+
ZonedDateTime.of(2025, 5, 4, 18, 1, 2, 0, ZoneOffset.UTC),
132+
ZonedDateTime.class,
133+
// Expected... "[2025,5,4,18,1,2,0]",
134+
"1746381662.000000000",
135+
"\"2025-05-04T18:01:02Z\""
136+
);
137+
// java.time.LocalDateTime
138+
_testTimestamp(
139+
LocalDateTime.of(2025, 5, 4, 18, 1, 2),
140+
LocalDateTime.class,
141+
"[2025,5,4,18,1,2]",
142+
"\"2025-05-04T18:01:02\""
143+
);
144+
145+
}
146+
147+
private static <T> void _testTimestamp(T value, Class<?> clazz, String withString, String withoutString) {
148+
assertEquals(
149+
withString,
150+
WITH_TIMESTAMP_MAPPER.writerFor(clazz).writeValueAsString(value),
151+
String.format("withTimestampMapper : Expected %s, got %s", withString, value)
152+
);
153+
assertEquals(
154+
withoutString,
155+
WITHOUT_TIMESTAMP_MAPPER.writerFor(clazz).writeValueAsString(value),
156+
String.format("withoutTimestampMapper : Expected %s, got %s", withoutString, value)
157+
);
158+
}
159+
160+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package tools.jackson.databind.ser.jdk;
2+
3+
import java.util.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
import tools.jackson.databind.ObjectMapper;
7+
import tools.jackson.databind.cfg.DateTimeFeature;
8+
import tools.jackson.databind.ext.javatime.DateTimeTestBase;
9+
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
12+
// [databind#5142] JSTEP-5 Tests to verify current behavior and discuss further action DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS
13+
public class WriteDatesAsTimestamps5142JdkTests
14+
extends DateTimeTestBase
15+
{
16+
private static final ObjectMapper WITH_TIMESTAMP_MAPPER = mapperBuilder()
17+
.enable(DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS)
18+
.build();
19+
20+
private static final ObjectMapper WITHOUT_TIMESTAMP_MAPPER = mapperBuilder()
21+
.disable(DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS)
22+
.build();
23+
24+
@Test
25+
public void testWriteDatesAsTimeStamps()
26+
throws Exception
27+
{
28+
// java.util.Date
29+
_testTimestamp(
30+
new Date(1234567890123L),
31+
Date.class,
32+
// Expected... [2009,1,13,23,31,30,123],
33+
"1234567890123",
34+
"\"2009-02-13T23:31:30.123Z\""
35+
);
36+
37+
// java.util.Calendar
38+
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
39+
cal.setTimeInMillis(1234567890123L);
40+
_testTimestamp(
41+
cal,
42+
Calendar.class,
43+
// Expected... [2009,1,13,23,31,30,123],
44+
"1234567890123",
45+
"\"2009-02-13T23:31:30.123Z\""
46+
);
47+
48+
}
49+
50+
private static <T> void _testTimestamp(T value, Class<?> clazz, String withString, String withoutString) {
51+
assertEquals(
52+
withString,
53+
WITH_TIMESTAMP_MAPPER.writerFor(clazz).writeValueAsString(value),
54+
String.format("withTimestampMapper : Expected %s, got %s", withString, value)
55+
);
56+
assertEquals(
57+
withoutString,
58+
WITHOUT_TIMESTAMP_MAPPER.writerFor(clazz).writeValueAsString(value),
59+
String.format("withoutTimestampMapper : Expected %s, got %s", withoutString, value)
60+
);
61+
}
62+
63+
}

0 commit comments

Comments
 (0)