-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add class and test to explore issue #20.
- Loading branch information
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/test/java/com/fatboyindustrial/gsonjavatime/Issue20.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
src/test/java/com/fatboyindustrial/gsonjavatime/Issue20Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |