diff --git a/src/test/java/com/fatboyindustrial/gsonjavatime/Issue20.java b/src/test/java/com/fatboyindustrial/gsonjavatime/Issue20.java new file mode 100644 index 0000000..3b29166 --- /dev/null +++ b/src/test/java/com/fatboyindustrial/gsonjavatime/Issue20.java @@ -0,0 +1,37 @@ +package com.fatboyindustrial.gsonjavatime; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZoneOffset; + +/** + * Test case from {@code https://github.com/gkopff/gson-javatime-serialisers/issues/20}. + */ +@SuppressWarnings({ "FieldCanBeLocal", "unused" }) // used reflectively +public class Issue20 +{ + private static final Gson gson = Converters.registerAll(new GsonBuilder()).create(); + + private final Instant date; + private final LocalDateTime date2; + private final LocalDate date3; + private final Instant date4; + + public Issue20(final Instant date) + { + this.date = date; + this.date2 = LocalDateTime.ofInstant(date, ZoneId.of("UTC")); + this.date3 = this.date2.toLocalDate(); + this.date4 = this.date3.atStartOfDay().toInstant(ZoneOffset.UTC); + } + + public String toJSON() + { + return gson.toJson(this); + } +} diff --git a/src/test/java/com/fatboyindustrial/gsonjavatime/Issue20Test.java b/src/test/java/com/fatboyindustrial/gsonjavatime/Issue20Test.java new file mode 100644 index 0000000..9dd30c4 --- /dev/null +++ b/src/test/java/com/fatboyindustrial/gsonjavatime/Issue20Test.java @@ -0,0 +1,32 @@ +package com.fatboyindustrial.gsonjavatime; + +import org.junit.Test; + +import java.time.Instant; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +/** + * Test for {@code https://github.com/gkopff/gson-javatime-serialisers/issues/20}. + */ +public class Issue20Test +{ + /** + * Tests that the serialisation produces ISO format strings. + */ + @Test + public void test() + { + final Instant instant = Instant.parse("2017-06-08T22:11:28.566Z"); + + final Issue20 issue = new Issue20(instant); + final String expected = + "{\"date\":\"2017-06-08T22:11:28.566Z\"," + + "\"date2\":\"2017-06-08T22:11:28.566\"," + + "\"date3\":\"2017-06-08\"," + + "\"date4\":\"2017-06-08T00:00:00Z\"}"; + + assertThat(issue.toJSON(), is(expected)); + } +}