From 8aaad56b4f0b7ae5d4dce633af32fbf2d4635637 Mon Sep 17 00:00:00 2001 From: sandhu5 Date: Thu, 2 Nov 2023 20:00:07 -0500 Subject: [PATCH] Fix flaky tests in FieldNamingTest --- .../gson/functional/FieldNamingTest.java | 53 ++++++++++--------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/gson/src/test/java/com/google/gson/functional/FieldNamingTest.java b/gson/src/test/java/com/google/gson/functional/FieldNamingTest.java index b2ea8a9537..9d7ad8d513 100644 --- a/gson/src/test/java/com/google/gson/functional/FieldNamingTest.java +++ b/gson/src/test/java/com/google/gson/functional/FieldNamingTest.java @@ -29,60 +29,61 @@ import com.google.gson.GsonBuilder; import com.google.gson.annotations.SerializedName; import org.junit.Test; +import java.util.Arrays; public final class FieldNamingTest { @Test public void testIdentity() { Gson gson = getGsonWithNamingPolicy(IDENTITY); - assertThat(gson.toJson(new TestNames()).replace('\"', '\'')) - .isEqualTo("{'lowerCamel':1,'UpperCamel':2,'_lowerCamelLeadingUnderscore':3," + - "'_UpperCamelLeadingUnderscore':4,'lower_words':5,'UPPER_WORDS':6," + - "'annotatedName':7,'lowerId':8,'_9':9}"); + String[] matches = {"'lowerCamel':1","'UpperCamel':2","'_lowerCamelLeadingUnderscore':3", + "'_UpperCamelLeadingUnderscore':4","'lower_words':5","'UPPER_WORDS':6", + "'annotatedName':7","'lowerId':8","'_9':9"}; + assertThat(Arrays.stream(matches).allMatch(gson.toJson(new TestNames()).replace('\"', '\'')::contains)).isTrue(); } @Test public void testUpperCamelCase() { Gson gson = getGsonWithNamingPolicy(UPPER_CAMEL_CASE); - assertThat(gson.toJson(new TestNames()).replace('\"', '\'')) - .isEqualTo("{'LowerCamel':1,'UpperCamel':2,'_LowerCamelLeadingUnderscore':3," + - "'_UpperCamelLeadingUnderscore':4,'Lower_words':5,'UPPER_WORDS':6," + - "'annotatedName':7,'LowerId':8,'_9':9}"); + String[] matches = {"'LowerCamel':1","'UpperCamel':2","'_LowerCamelLeadingUnderscore':3", + "'_UpperCamelLeadingUnderscore':4","'Lower_words':5","'UPPER_WORDS':6", + "'annotatedName':7","'LowerId':8","'_9':9"}; + assertThat(Arrays.stream(matches).allMatch(gson.toJson(new TestNames()).replace('\"', '\'')::contains)).isTrue(); } @Test public void testUpperCamelCaseWithSpaces() { Gson gson = getGsonWithNamingPolicy(UPPER_CAMEL_CASE_WITH_SPACES); - assertThat(gson.toJson(new TestNames()).replace('\"', '\'')) - .isEqualTo("{'Lower Camel':1,'Upper Camel':2,'_Lower Camel Leading Underscore':3," + - "'_ Upper Camel Leading Underscore':4,'Lower_words':5,'U P P E R_ W O R D S':6," + - "'annotatedName':7,'Lower Id':8,'_9':9}"); + String[] matches = {"'Lower Camel':1","'Upper Camel':2","'_Lower Camel Leading Underscore':3", + "'_ Upper Camel Leading Underscore':4","'Lower_words':5","'U P P E R_ W O R D S':6", + "'annotatedName':7","'Lower Id':8","'_9':9"}; + assertThat(Arrays.stream(matches).allMatch(gson.toJson(new TestNames()).replace('\"', '\'')::contains)).isTrue(); } @Test public void testUpperCaseWithUnderscores() { Gson gson = getGsonWithNamingPolicy(UPPER_CASE_WITH_UNDERSCORES); - assertThat(gson.toJson(new TestNames()).replace('\"', '\'')) - .isEqualTo("{'LOWER_CAMEL':1,'UPPER_CAMEL':2,'_LOWER_CAMEL_LEADING_UNDERSCORE':3," + - "'__UPPER_CAMEL_LEADING_UNDERSCORE':4,'LOWER_WORDS':5,'U_P_P_E_R__W_O_R_D_S':6," + - "'annotatedName':7,'LOWER_ID':8,'_9':9}"); - } + String[] matches = {"'LOWER_CAMEL':1","'UPPER_CAMEL':2","'_LOWER_CAMEL_LEADING_UNDERSCORE':3", + "'__UPPER_CAMEL_LEADING_UNDERSCORE':4","'LOWER_WORDS':5","'U_P_P_E_R__W_O_R_D_S':6", + "'annotatedName':7","'LOWER_ID':8","'_9':9"}; + assertThat(Arrays.stream(matches).allMatch(gson.toJson(new TestNames()).replace('\"', '\'')::contains)).isTrue(); + } @Test public void testLowerCaseWithUnderscores() { Gson gson = getGsonWithNamingPolicy(LOWER_CASE_WITH_UNDERSCORES); - assertThat(gson.toJson(new TestNames()).replace('\"', '\'')) - .isEqualTo("{'lower_camel':1,'upper_camel':2,'_lower_camel_leading_underscore':3," + - "'__upper_camel_leading_underscore':4,'lower_words':5,'u_p_p_e_r__w_o_r_d_s':6," + - "'annotatedName':7,'lower_id':8,'_9':9}"); - } + String[] matches = {"'lower_camel':1","'upper_camel':2","'_lower_camel_leading_underscore':3", + "'__upper_camel_leading_underscore':4","'lower_words':5","'u_p_p_e_r__w_o_r_d_s':6", + "'annotatedName':7","'lower_id':8","'_9':9"}; + assertThat(Arrays.stream(matches).allMatch(gson.toJson(new TestNames()).replace('\"', '\'')::contains)).isTrue(); + } @Test public void testLowerCaseWithDashes() { Gson gson = getGsonWithNamingPolicy(LOWER_CASE_WITH_DASHES); - assertThat(gson.toJson(new TestNames()).replace('\"', '\'')) - .isEqualTo("{'lower-camel':1,'upper-camel':2,'_lower-camel-leading-underscore':3," + - "'_-upper-camel-leading-underscore':4,'lower_words':5,'u-p-p-e-r_-w-o-r-d-s':6," + - "'annotatedName':7,'lower-id':8,'_9':9}"); + String[] matches = {"'lower-camel':1","'upper-camel':2","'_lower-camel-leading-underscore':3", + "'_-upper-camel-leading-underscore':4","'lower_words':5","'u-p-p-e-r_-w-o-r-d-s':6", + "'annotatedName':7","'lower-id':8","'_9':9"}; + assertThat(Arrays.stream(matches).allMatch(gson.toJson(new TestNames()).replace('\"', '\'')::contains)).isTrue(); } private Gson getGsonWithNamingPolicy(FieldNamingPolicy fieldNamingPolicy){