Skip to content

Commit

Permalink
Make date-formatting tests less fragile with regular expressions. (#2450
Browse files Browse the repository at this point in the history
)

* 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.
  • Loading branch information
eamonnmcmanus committed Jul 26, 2023
1 parent 5a87d80 commit 5055b62
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -384,11 +384,7 @@ public void testBitSetDeserialization() {
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
Expand Down Expand Up @@ -420,11 +416,7 @@ public void testDefaultDateSerializationUsingBuilder() {
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
Expand Down
7 changes: 2 additions & 5 deletions gson/src/test/java/com/google/gson/functional/ObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -570,11 +570,8 @@ static final class Product {
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,22 @@ private void assertFormattingAlwaysEmitsUsLocale(Locale locale) {
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);
Expand Down Expand Up @@ -150,9 +151,7 @@ public void testFormatUsesDefaultTimezone() throws Exception {
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);
Expand Down Expand Up @@ -222,9 +221,10 @@ private static TypeAdapter<Date> dateAdapter(TypeAdapterFactory adapterFactory)
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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,10 @@ public void testDefaultSqlTimeDeserialization() {
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
Expand Down

0 comments on commit 5055b62

Please sign in to comment.