|
| 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 | +} |
0 commit comments