Make date-formatting tests less fragile with regular expressions. (#2450)

* Make date-formatting tests less fragile with regular expressions.

This is not great. We should really ensure that formatted dates are the same
regardless of JDK version. There is code that attempts to do that but it is not
really effective. So for now we fudge around the differences by using regular
expressions to paper over the differences.

* Temporarily add test-debugging code.

* Another attempt at debugging a test failure.

* Fix pattern in assertion.
This commit is contained in:
Éamonn McManus 2023-07-26 12:24:04 -07:00 committed by GitHub
parent 5a87d80655
commit 5055b62463
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 34 deletions

View File

@ -384,11 +384,7 @@ public class DefaultTypeAdaptersTest {
public void testDefaultDateSerialization() { public void testDefaultDateSerialization() {
Date now = new Date(1315806903103L); Date now = new Date(1315806903103L);
String json = gson.toJson(now); String json = gson.toJson(now);
if (JavaVersion.isJava9OrLater()) { assertThat(json).matches("\"Sep 11, 2011,? 10:55:03\\hPM\"");
assertThat(json).isEqualTo("\"Sep 11, 2011, 10:55:03 PM\"");
} else {
assertThat(json).isEqualTo("\"Sep 11, 2011 10:55:03 PM\"");
}
} }
@Test @Test
@ -420,11 +416,7 @@ public class DefaultTypeAdaptersTest {
Gson gson = new GsonBuilder().create(); Gson gson = new GsonBuilder().create();
Date now = new Date(1315806903103L); Date now = new Date(1315806903103L);
String json = gson.toJson(now); String json = gson.toJson(now);
if (JavaVersion.isJava9OrLater()) { assertThat(json).matches("\"Sep 11, 2011,? 10:55:03\\hPM\"");
assertThat(json).isEqualTo("\"Sep 11, 2011, 10:55:03 PM\"");
} else {
assertThat(json).isEqualTo("\"Sep 11, 2011 10:55:03 PM\"");
}
} }
@Test @Test

View File

@ -570,11 +570,8 @@ public class ObjectTest {
public void testDateAsMapObjectField() { public void testDateAsMapObjectField() {
HasObjectMap a = new HasObjectMap(); HasObjectMap a = new HasObjectMap();
a.map.put("date", new Date(0)); a.map.put("date", new Date(0));
if (JavaVersion.isJava9OrLater()) { assertThat(gson.toJson(a))
assertThat(gson.toJson(a)).isEqualTo("{\"map\":{\"date\":\"Dec 31, 1969, 4:00:00 PM\"}}"); .matches("\\{\"map\":\\{\"date\":\"Dec 31, 1969,? 4:00:00\\hPM\"\\}\\}");
} else {
assertThat(gson.toJson(a)).isEqualTo("{\"map\":{\"date\":\"Dec 31, 1969 4:00:00 PM\"}}");
}
} }
static class HasObjectMap { static class HasObjectMap {

View File

@ -58,21 +58,22 @@ public class DefaultDateTypeAdapterTest {
Locale defaultLocale = Locale.getDefault(); Locale defaultLocale = Locale.getDefault();
Locale.setDefault(locale); Locale.setDefault(locale);
try { try {
String afterYearSep = JavaVersion.isJava9OrLater() ? ", " : " "; // The patterns here attempt to accommodate minor date-time formatting differences between JDK
String afterYearLongSep = JavaVersion.isJava9OrLater() ? " at " : " "; // versions. Ideally Gson would serialize in a way that is independent of the JDK version.
String utcFull = JavaVersion.isJava9OrLater() ? "Coordinated Universal Time" : "UTC"; // Note: \h means "horizontal space", because some JDK versions use Narrow No Break Space
assertFormatted(String.format("Jan 1, 1970%s12:00:00 AM", afterYearSep), // (U+202F) before the AM or PM indication.
DateType.DATE.createDefaultsAdapterFactory()); String utcFull = "(Coordinated Universal Time|UTC)";
assertFormatted("Jan 1, 1970,? 12:00:00\\hAM", DateType.DATE.createDefaultsAdapterFactory());
assertFormatted("1/1/70", DateType.DATE.createAdapterFactory(DateFormat.SHORT)); assertFormatted("1/1/70", DateType.DATE.createAdapterFactory(DateFormat.SHORT));
assertFormatted("Jan 1, 1970", DateType.DATE.createAdapterFactory(DateFormat.MEDIUM)); assertFormatted("Jan 1, 1970", DateType.DATE.createAdapterFactory(DateFormat.MEDIUM));
assertFormatted("January 1, 1970", DateType.DATE.createAdapterFactory(DateFormat.LONG)); assertFormatted("January 1, 1970", DateType.DATE.createAdapterFactory(DateFormat.LONG));
assertFormatted(String.format("1/1/70%s12:00 AM", afterYearSep), assertFormatted("1/1/70,? 12:00\\hAM",
DateType.DATE.createAdapterFactory(DateFormat.SHORT, DateFormat.SHORT)); DateType.DATE.createAdapterFactory(DateFormat.SHORT, DateFormat.SHORT));
assertFormatted(String.format("Jan 1, 1970%s12:00:00 AM", afterYearSep), assertFormatted("Jan 1, 1970,? 12:00:00\\hAM",
DateType.DATE.createAdapterFactory(DateFormat.MEDIUM, DateFormat.MEDIUM)); DateType.DATE.createAdapterFactory(DateFormat.MEDIUM, DateFormat.MEDIUM));
assertFormatted(String.format("January 1, 1970%s12:00:00 AM UTC", afterYearLongSep), assertFormatted("January 1, 1970(,| at)? 12:00:00\\hAM UTC",
DateType.DATE.createAdapterFactory(DateFormat.LONG, DateFormat.LONG)); DateType.DATE.createAdapterFactory(DateFormat.LONG, DateFormat.LONG));
assertFormatted(String.format("Thursday, January 1, 1970%s12:00:00 AM %s", afterYearLongSep, utcFull), assertFormatted("Thursday, January 1, 1970(,| at)? 12:00:00\\hAM " + utcFull,
DateType.DATE.createAdapterFactory(DateFormat.FULL, DateFormat.FULL)); DateType.DATE.createAdapterFactory(DateFormat.FULL, DateFormat.FULL));
} finally { } finally {
TimeZone.setDefault(defaultTimeZone); TimeZone.setDefault(defaultTimeZone);
@ -150,9 +151,7 @@ public class DefaultDateTypeAdapterTest {
Locale defaultLocale = Locale.getDefault(); Locale defaultLocale = Locale.getDefault();
Locale.setDefault(Locale.US); Locale.setDefault(Locale.US);
try { try {
String afterYearSep = JavaVersion.isJava9OrLater() ? ", " : " "; assertFormatted("Dec 31, 1969,? 4:00:00\\hPM", DateType.DATE.createDefaultsAdapterFactory());
assertFormatted(String.format("Dec 31, 1969%s4:00:00 PM", afterYearSep),
DateType.DATE.createDefaultsAdapterFactory());
assertParsed("Dec 31, 1969 4:00:00 PM", DateType.DATE.createDefaultsAdapterFactory()); assertParsed("Dec 31, 1969 4:00:00 PM", DateType.DATE.createDefaultsAdapterFactory());
} finally { } finally {
TimeZone.setDefault(defaultTimeZone); TimeZone.setDefault(defaultTimeZone);
@ -222,9 +221,10 @@ public class DefaultDateTypeAdapterTest {
return adapter; return adapter;
} }
private static void assertFormatted(String formatted, TypeAdapterFactory adapterFactory) { private static void assertFormatted(String formattedPattern, TypeAdapterFactory adapterFactory) {
TypeAdapter<Date> adapter = dateAdapter(adapterFactory); TypeAdapter<Date> adapter = dateAdapter(adapterFactory);
assertThat(adapter.toJson(new Date(0))).isEqualTo(toLiteral(formatted)); String json = adapter.toJson(new Date(0));
assertThat(json).matches(toLiteral(formattedPattern));
} }
@SuppressWarnings("UndefinedEquals") @SuppressWarnings("UndefinedEquals")

View File

@ -113,11 +113,10 @@ public class SqlTypesGsonTest {
public void testDefaultSqlTimestampSerialization() { public void testDefaultSqlTimestampSerialization() {
Timestamp now = new java.sql.Timestamp(1259875082000L); Timestamp now = new java.sql.Timestamp(1259875082000L);
String json = gson.toJson(now); String json = gson.toJson(now);
if (JavaVersion.isJava9OrLater()) { // The exact format of the serialized date-time string depends on the JDK version. The pattern
assertThat(json).isEqualTo("\"Dec 3, 2009, 1:18:02 PM\""); // here allows for an optional comma after the date, and what might be U+202F (Narrow No-Break
} else { // Space) before "PM".
assertThat(json).isEqualTo("\"Dec 3, 2009 1:18:02 PM\""); assertThat(json).matches("\"Dec 3, 2009,? 1:18:02\\hPM\"");
}
} }
@Test @Test