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

View File

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

View File

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

View File

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