From 5055b6246314e04f54f7d7e2c46b7d22786a9089 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89amonn=20McManus?= Date: Wed, 26 Jul 2023 12:24:04 -0700 Subject: [PATCH] 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. --- .../functional/DefaultTypeAdaptersTest.java | 12 ++------ .../google/gson/functional/ObjectTest.java | 7 ++--- .../bind/DefaultDateTypeAdapterTest.java | 28 +++++++++---------- .../gson/internal/sql/SqlTypesGsonTest.java | 9 +++--- 4 files changed, 22 insertions(+), 34 deletions(-) diff --git a/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java b/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java index e70c175b3c..e858062dd1 100644 --- a/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java +++ b/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java @@ -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 @@ -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 diff --git a/gson/src/test/java/com/google/gson/functional/ObjectTest.java b/gson/src/test/java/com/google/gson/functional/ObjectTest.java index b0de9da48d..e0efd94dee 100644 --- a/gson/src/test/java/com/google/gson/functional/ObjectTest.java +++ b/gson/src/test/java/com/google/gson/functional/ObjectTest.java @@ -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 { diff --git a/gson/src/test/java/com/google/gson/internal/bind/DefaultDateTypeAdapterTest.java b/gson/src/test/java/com/google/gson/internal/bind/DefaultDateTypeAdapterTest.java index 2034e38fc6..835bc9dd49 100644 --- a/gson/src/test/java/com/google/gson/internal/bind/DefaultDateTypeAdapterTest.java +++ b/gson/src/test/java/com/google/gson/internal/bind/DefaultDateTypeAdapterTest.java @@ -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); @@ -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); @@ -222,9 +221,10 @@ private static TypeAdapter dateAdapter(TypeAdapterFactory adapterFactory) return adapter; } - private static void assertFormatted(String formatted, TypeAdapterFactory adapterFactory) { + private static void assertFormatted(String formattedPattern, TypeAdapterFactory adapterFactory) { TypeAdapter 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") diff --git a/gson/src/test/java/com/google/gson/internal/sql/SqlTypesGsonTest.java b/gson/src/test/java/com/google/gson/internal/sql/SqlTypesGsonTest.java index b04c56ab34..8233275c37 100644 --- a/gson/src/test/java/com/google/gson/internal/sql/SqlTypesGsonTest.java +++ b/gson/src/test/java/com/google/gson/internal/sql/SqlTypesGsonTest.java @@ -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