Add serialization tests in different timezone

This commit is contained in:
Antonin Delpeuch 2018-12-06 15:46:20 +09:00
parent cf58092f2d
commit be137fc53c
3 changed files with 50 additions and 9 deletions

View File

@ -35,6 +35,7 @@ package com.google.refine.tests.expr.functions.strings;
import java.time.OffsetDateTime;
import java.util.Properties;
import java.util.TimeZone;
import org.slf4j.LoggerFactory;
import org.testng.Assert;

View File

@ -2,24 +2,46 @@ package com.google.refine.tests.io;
import java.io.IOException;
import java.io.InputStream;
import java.util.TimeZone;
import org.apache.commons.io.IOUtils;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import com.google.refine.tests.util.TestUtils;
import com.google.refine.util.ParsingUtilities;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.google.refine.ProjectMetadata;
public class ProjectMetadataTests {
private String jsonSaveMode = null;
private String jsonNonSaveMode = null;
@BeforeSuite
public void setUpJson() throws IOException {
InputStream f = ProjectMetadataTests.class.getClassLoader().getResourceAsStream("example_project_metadata.json");
jsonNonSaveMode = IOUtils.toString(f);
f = ProjectMetadataTests.class.getClassLoader().getResourceAsStream("example_project_metadata_save_mode.json");
jsonSaveMode = IOUtils.toString(f);
}
@Test
public void serializeProjectMetadata() throws IOException {
InputStream f = ProjectMetadataTests.class.getClassLoader().getResourceAsStream("example_project_metadata.json");
String json = IOUtils.toString(f);
f = ProjectMetadataTests.class.getClassLoader().getResourceAsStream("example_project_metadata_save_mode.json");
String fullJson = IOUtils.toString(f);
f = ProjectMetadataTests.class.getClassLoader().getResourceAsStream("example_project_metadata_save_mode.json");
ProjectMetadata metadata = ParsingUtilities.mapper.readValue(f, ProjectMetadata.class);
TestUtils.isSerializedTo(metadata, json);
TestUtils.isSerializedTo(metadata, fullJson, true);
ProjectMetadata metadata = ParsingUtilities.mapper.readValue(jsonSaveMode, ProjectMetadata.class);
TestUtils.isSerializedTo(metadata, jsonNonSaveMode);
TestUtils.isSerializedTo(metadata, jsonSaveMode, true);
}
@Test
public void serializeProjectMetadataInDifferentTimezone() throws JsonParseException, JsonMappingException, IOException {
TimeZone.setDefault(TimeZone.getTimeZone("JST"));
try {
ProjectMetadata metadata = ParsingUtilities.mapper.readValue(jsonSaveMode, ProjectMetadata.class);
TestUtils.isSerializedTo(metadata, jsonNonSaveMode);
TestUtils.isSerializedTo(metadata, jsonSaveMode, true);
} finally {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
}
}
}

View File

@ -37,6 +37,7 @@ import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
import org.apache.commons.lang3.text.StrSubstitutor;
import org.slf4j.LoggerFactory;
@ -89,6 +90,23 @@ public class ParsingUtilitiesTests extends RefineTest {
Assert.assertEquals(2017, ParsingUtilities.stringToLocalDate("2017-04-03T08:09:43+00:00").getYear());
}
/**
* Converting between string and local time must be reversible, no matter the timezone.
*/
@Test
public void stringToLocalDateNonUTC() {
TimeZone.setDefault(TimeZone.getTimeZone("JST"));
try {
Assert.assertEquals(ParsingUtilities.stringToLocalDate("2001-08-12T00:00:00Z").getHour(), 9);
Assert.assertEquals(ParsingUtilities.localDateToString(
ParsingUtilities.stringToLocalDate("2001-08-12T00:00:00Z")),
"2001-08-12T00:00:00Z");
} finally {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
}
}
@Test
public void parseProjectModifiedBeforeJDK8() {
String modified = "2014-01-15T21:46:25Z";