|
| 1 | +package com.fasterxml.jackson.datatype.jsr310; |
| 2 | + |
| 3 | +import static org.hamcrest.core.IsEqual.equalTo; |
| 4 | +import static org.junit.Assert.assertThat; |
| 5 | +import static org.junit.internal.matchers.StringContains.containsString; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 8 | +import com.fasterxml.jackson.databind.module.SimpleModule; |
| 9 | +import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; |
| 10 | +import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; |
| 11 | +import org.junit.Test; |
| 12 | +import org.junit.runner.RunWith; |
| 13 | +import org.junit.runners.Parameterized; |
| 14 | +import org.junit.runners.Parameterized.Parameters; |
| 15 | + |
| 16 | +import java.text.SimpleDateFormat; |
| 17 | +import java.time.LocalDate; |
| 18 | +import java.time.LocalDateTime; |
| 19 | +import java.time.ZoneId; |
| 20 | +import java.time.format.DateTimeFormatter; |
| 21 | +import java.time.format.DateTimeFormatterBuilder; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.Collection; |
| 24 | + |
| 25 | +@RunWith(Parameterized.class) |
| 26 | +public class TestLocalDateTimeSerializationWithCustomFormatter { |
| 27 | + private final DateTimeFormatter formatter; |
| 28 | + |
| 29 | + public TestLocalDateTimeSerializationWithCustomFormatter(DateTimeFormatter formatter) { |
| 30 | + this.formatter = formatter; |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testSerialization() throws Exception { |
| 35 | + LocalDateTime dateTime = LocalDateTime.now(); |
| 36 | + assertThat("Failed to serialize with " + formatter, serializeWith(dateTime, formatter), containsString(dateTime.format(formatter))); |
| 37 | + } |
| 38 | + |
| 39 | + private String serializeWith(LocalDateTime dateTime, DateTimeFormatter formatter) throws Exception { |
| 40 | + ObjectMapper mapper = new ObjectMapper().registerModule(new SimpleModule().addSerializer(new LocalDateTimeSerializer(formatter))); |
| 41 | + return mapper.writeValueAsString(dateTime); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testDeserialization() throws Exception { |
| 46 | + LocalDateTime dateTime = LocalDateTime.now(); |
| 47 | + assertThat(deserializeWith(dateTime.format(formatter), formatter), equalTo(dateTime)); |
| 48 | + } |
| 49 | + |
| 50 | + private LocalDateTime deserializeWith(String json, DateTimeFormatter formatter) throws Exception { |
| 51 | + ObjectMapper mapper = new ObjectMapper().registerModule(new SimpleModule().addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(formatter))); |
| 52 | + return mapper.readValue("\"" + json + "\"", LocalDateTime.class); |
| 53 | + } |
| 54 | + |
| 55 | + @Parameters |
| 56 | + public static Collection<Object[]> customFormatters() { |
| 57 | + Collection<Object[]> formatters = new ArrayList<>(); |
| 58 | + formatters.add(new Object[]{DateTimeFormatter.ISO_DATE_TIME}); |
| 59 | + formatters.add(new Object[]{DateTimeFormatter.ISO_LOCAL_DATE_TIME}); |
| 60 | + return formatters; |
| 61 | + } |
| 62 | +} |
0 commit comments