Skip to content

Commit

Permalink
Add class and test to explore issue #20.
Browse files Browse the repository at this point in the history
  • Loading branch information
gkopff committed Jun 8, 2017
1 parent 273d706 commit 8e5b022
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/test/java/com/fatboyindustrial/gsonjavatime/Issue20.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
32 changes: 32 additions & 0 deletions src/test/java/com/fatboyindustrial/gsonjavatime/Issue20Test.java
Original file line number Diff line number Diff line change
@@ -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));
}
}

0 comments on commit 8e5b022

Please sign in to comment.