|
| 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.LocalDateDeserializer; |
| 10 | +import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer; |
| 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.time.LocalDate; |
| 17 | +import java.time.format.DateTimeFormatter; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Collection; |
| 20 | + |
| 21 | +@RunWith(Parameterized.class) |
| 22 | +public class TestLocalDateSerializationWithCustomFormatter { |
| 23 | + private final DateTimeFormatter formatter; |
| 24 | + |
| 25 | + public TestLocalDateSerializationWithCustomFormatter(DateTimeFormatter formatter) { |
| 26 | + this.formatter = formatter; |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void testSerialization() throws Exception { |
| 31 | + LocalDate date = LocalDate.now(); |
| 32 | + assertThat("Failed to serialize with " + formatter, serializeWith(date, formatter), containsString(date.format(formatter))); |
| 33 | + } |
| 34 | + |
| 35 | + private String serializeWith(LocalDate date, DateTimeFormatter formatter) throws Exception { |
| 36 | + ObjectMapper mapper = new ObjectMapper().registerModule(new SimpleModule().addSerializer(new LocalDateSerializer(formatter))); |
| 37 | + return mapper.writeValueAsString(date); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testDeserialization() throws Exception { |
| 42 | + LocalDate date = LocalDate.now(); |
| 43 | + assertThat(deserializeWith(date.format(formatter), formatter), equalTo(date)); |
| 44 | + } |
| 45 | + |
| 46 | + private LocalDate deserializeWith(String json, DateTimeFormatter formatter) throws Exception { |
| 47 | + ObjectMapper mapper = new ObjectMapper().registerModule(new SimpleModule().addDeserializer(LocalDate.class, new LocalDateDeserializer(formatter))); |
| 48 | + return mapper.readValue("\"" + json + "\"", LocalDate.class); |
| 49 | + } |
| 50 | + |
| 51 | + @Parameters |
| 52 | + public static Collection<Object[]> customFormatters() { |
| 53 | + Collection<Object[]> formatters = new ArrayList<>(); |
| 54 | + formatters.add(new Object[]{DateTimeFormatter.BASIC_ISO_DATE}); |
| 55 | + formatters.add(new Object[]{DateTimeFormatter.ISO_DATE}); |
| 56 | + formatters.add(new Object[]{DateTimeFormatter.ISO_LOCAL_DATE}); |
| 57 | + formatters.add(new Object[]{DateTimeFormatter.ISO_ORDINAL_DATE}); |
| 58 | + formatters.add(new Object[]{DateTimeFormatter.ISO_WEEK_DATE}); |
| 59 | + formatters.add(new Object[]{DateTimeFormatter.ofPattern("MM/dd/yyyy")}); |
| 60 | + return formatters; |
| 61 | + } |
| 62 | +} |
0 commit comments