Add tests to demonstrate the issue

Run the FieldNamingTest with the JVM options: `-Duser.language=tr
-Duser.region=TR`
This commit is contained in:
Sebastian Chlan 2015-06-09 15:12:38 +01:00
parent 2cac11b449
commit 299ee89852

View File

@ -21,6 +21,8 @@ import static com.google.gson.FieldNamingPolicy.LOWER_CASE_WITH_DASHES;
import static com.google.gson.FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES; import static com.google.gson.FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES;
import static com.google.gson.FieldNamingPolicy.UPPER_CAMEL_CASE; import static com.google.gson.FieldNamingPolicy.UPPER_CAMEL_CASE;
import static com.google.gson.FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES; import static com.google.gson.FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
@ -28,45 +30,51 @@ import junit.framework.TestCase;
public final class FieldNamingTest extends TestCase { public final class FieldNamingTest extends TestCase {
public void testIdentity() { public void testIdentity() {
Gson gson = new GsonBuilder().setFieldNamingPolicy(IDENTITY).create(); Gson gson = getGsonWithNamingPolicy(IDENTITY);
assertEquals("{'lowerCamel':1,'UpperCamel':2,'_lowerCamelLeadingUnderscore':3," + assertEquals("{'lowerCamel':1,'UpperCamel':2,'_lowerCamelLeadingUnderscore':3," +
"'_UpperCamelLeadingUnderscore':4,'lower_words':5,'UPPER_WORDS':6," + "'_UpperCamelLeadingUnderscore':4,'lower_words':5,'UPPER_WORDS':6," +
"'annotatedName':7}", "'annotatedName':7,'lowerId':8}",
gson.toJson(new TestNames()).replace('\"', '\'')); gson.toJson(new TestNames()).replace('\"', '\''));
} }
public void testUpperCamelCase() { public void testUpperCamelCase() {
Gson gson = new GsonBuilder().setFieldNamingPolicy(UPPER_CAMEL_CASE).create(); Gson gson = getGsonWithNamingPolicy(UPPER_CAMEL_CASE);
assertEquals("{'LowerCamel':1,'UpperCamel':2,'_LowerCamelLeadingUnderscore':3," + assertEquals("{'LowerCamel':1,'UpperCamel':2,'_LowerCamelLeadingUnderscore':3," +
"'_UpperCamelLeadingUnderscore':4,'Lower_words':5,'UPPER_WORDS':6," + "'_UpperCamelLeadingUnderscore':4,'Lower_words':5,'UPPER_WORDS':6," +
"'annotatedName':7}", "'annotatedName':7,'LowerId':8}",
gson.toJson(new TestNames()).replace('\"', '\'')); gson.toJson(new TestNames()).replace('\"', '\''));
} }
public void testUpperCamelCaseWithSpaces() { public void testUpperCamelCaseWithSpaces() {
Gson gson = new GsonBuilder().setFieldNamingPolicy(UPPER_CAMEL_CASE_WITH_SPACES).create(); Gson gson = getGsonWithNamingPolicy(UPPER_CAMEL_CASE_WITH_SPACES);
assertEquals("{'Lower Camel':1,'Upper Camel':2,'_Lower Camel Leading Underscore':3," + assertEquals("{'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," + "'_ Upper Camel Leading Underscore':4,'Lower_words':5,'U P P E R_ W O R D S':6," +
"'annotatedName':7}", "'annotatedName':7,'Lower Id':8}",
gson.toJson(new TestNames()).replace('\"', '\'')); gson.toJson(new TestNames()).replace('\"', '\''));
} }
public void testLowerCaseWithUnderscores() { public void testLowerCaseWithUnderscores() {
Gson gson = new GsonBuilder().setFieldNamingPolicy(LOWER_CASE_WITH_UNDERSCORES).create(); Gson gson = getGsonWithNamingPolicy(LOWER_CASE_WITH_UNDERSCORES);
assertEquals("{'lower_camel':1,'upper_camel':2,'_lower_camel_leading_underscore':3," + assertEquals("{'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," + "'__upper_camel_leading_underscore':4,'lower_words':5,'u_p_p_e_r__w_o_r_d_s':6," +
"'annotatedName':7}", "'annotatedName':7,'lower_id':8}",
gson.toJson(new TestNames()).replace('\"', '\'')); gson.toJson(new TestNames()).replace('\"', '\''));
} }
public void testLowerCaseWithDashes() { public void testLowerCaseWithDashes() {
Gson gson = new GsonBuilder().setFieldNamingPolicy(LOWER_CASE_WITH_DASHES).create(); Gson gson = getGsonWithNamingPolicy(LOWER_CASE_WITH_DASHES);
assertEquals("{'lower-camel':1,'upper-camel':2,'_lower-camel-leading-underscore':3," + assertEquals("{'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," + "'_-upper-camel-leading-underscore':4,'lower_words':5,'u-p-p-e-r_-w-o-r-d-s':6," +
"'annotatedName':7}", "'annotatedName':7,'lower-id':8}",
gson.toJson(new TestNames()).replace('\"', '\'')); gson.toJson(new TestNames()).replace('\"', '\''));
} }
private Gson getGsonWithNamingPolicy(FieldNamingPolicy fieldNamingPolicy){
return new GsonBuilder()
.setFieldNamingPolicy(fieldNamingPolicy)
.create();
}
@SuppressWarnings("unused") // fields are used reflectively @SuppressWarnings("unused") // fields are used reflectively
private static class TestNames { private static class TestNames {
int lowerCamel = 1; int lowerCamel = 1;
@ -76,5 +84,6 @@ public final class FieldNamingTest extends TestCase {
int lower_words = 5; int lower_words = 5;
int UPPER_WORDS = 6; int UPPER_WORDS = 6;
@SerializedName("annotatedName") int annotated = 7; @SerializedName("annotatedName") int annotated = 7;
int lowerId = 8;
} }
} }