1 /* 2 * Copyright (C) 2011 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.gson.functional; 18 19 import static com.google.gson.FieldNamingPolicy.IDENTITY; 20 import static com.google.gson.FieldNamingPolicy.LOWER_CASE_WITH_DASHES; 21 import static com.google.gson.FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES; 22 import static com.google.gson.FieldNamingPolicy.UPPER_CAMEL_CASE; 23 import static com.google.gson.FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES; 24 import static com.google.gson.FieldNamingPolicy.UPPER_CASE_WITH_UNDERSCORES; 25 26 import com.google.gson.FieldNamingPolicy; 27 import com.google.gson.Gson; 28 import com.google.gson.GsonBuilder; 29 import com.google.gson.annotations.SerializedName; 30 import junit.framework.TestCase; 31 32 public final class FieldNamingTest extends TestCase { testIdentity()33 public void testIdentity() { 34 Gson gson = getGsonWithNamingPolicy(IDENTITY); 35 assertEquals("{'lowerCamel':1,'UpperCamel':2,'_lowerCamelLeadingUnderscore':3," + 36 "'_UpperCamelLeadingUnderscore':4,'lower_words':5,'UPPER_WORDS':6," + 37 "'annotatedName':7,'lowerId':8,'_9':9}", 38 gson.toJson(new TestNames()).replace('\"', '\'')); 39 } 40 testUpperCamelCase()41 public void testUpperCamelCase() { 42 Gson gson = getGsonWithNamingPolicy(UPPER_CAMEL_CASE); 43 assertEquals("{'LowerCamel':1,'UpperCamel':2,'_LowerCamelLeadingUnderscore':3," + 44 "'_UpperCamelLeadingUnderscore':4,'Lower_words':5,'UPPER_WORDS':6," + 45 "'annotatedName':7,'LowerId':8,'_9':9}", 46 gson.toJson(new TestNames()).replace('\"', '\'')); 47 } 48 testUpperCamelCaseWithSpaces()49 public void testUpperCamelCaseWithSpaces() { 50 Gson gson = getGsonWithNamingPolicy(UPPER_CAMEL_CASE_WITH_SPACES); 51 assertEquals("{'Lower Camel':1,'Upper Camel':2,'_Lower Camel Leading Underscore':3," + 52 "'_ Upper Camel Leading Underscore':4,'Lower_words':5,'U P P E R_ W O R D S':6," + 53 "'annotatedName':7,'Lower Id':8,'_9':9}", 54 gson.toJson(new TestNames()).replace('\"', '\'')); 55 } 56 testUpperCaseWithUnderscores()57 public void testUpperCaseWithUnderscores() { 58 Gson gson = getGsonWithNamingPolicy(UPPER_CASE_WITH_UNDERSCORES); 59 assertEquals("{'LOWER_CAMEL':1,'UPPER_CAMEL':2,'_LOWER_CAMEL_LEADING_UNDERSCORE':3," + 60 "'__UPPER_CAMEL_LEADING_UNDERSCORE':4,'LOWER_WORDS':5,'U_P_P_E_R__W_O_R_D_S':6," + 61 "'annotatedName':7,'LOWER_ID':8,'_9':9}", 62 gson.toJson(new TestNames()).replace('\"', '\'')); 63 } 64 testLowerCaseWithUnderscores()65 public void testLowerCaseWithUnderscores() { 66 Gson gson = getGsonWithNamingPolicy(LOWER_CASE_WITH_UNDERSCORES); 67 assertEquals("{'lower_camel':1,'upper_camel':2,'_lower_camel_leading_underscore':3," + 68 "'__upper_camel_leading_underscore':4,'lower_words':5,'u_p_p_e_r__w_o_r_d_s':6," + 69 "'annotatedName':7,'lower_id':8,'_9':9}", 70 gson.toJson(new TestNames()).replace('\"', '\'')); 71 } 72 testLowerCaseWithDashes()73 public void testLowerCaseWithDashes() { 74 Gson gson = getGsonWithNamingPolicy(LOWER_CASE_WITH_DASHES); 75 assertEquals("{'lower-camel':1,'upper-camel':2,'_lower-camel-leading-underscore':3," + 76 "'_-upper-camel-leading-underscore':4,'lower_words':5,'u-p-p-e-r_-w-o-r-d-s':6," + 77 "'annotatedName':7,'lower-id':8,'_9':9}", 78 gson.toJson(new TestNames()).replace('\"', '\'')); 79 } 80 getGsonWithNamingPolicy(FieldNamingPolicy fieldNamingPolicy)81 private Gson getGsonWithNamingPolicy(FieldNamingPolicy fieldNamingPolicy){ 82 return new GsonBuilder() 83 .setFieldNamingPolicy(fieldNamingPolicy) 84 .create(); 85 } 86 87 @SuppressWarnings("unused") // fields are used reflectively 88 private static class TestNames { 89 int lowerCamel = 1; 90 int UpperCamel = 2; 91 int _lowerCamelLeadingUnderscore = 3; 92 int _UpperCamelLeadingUnderscore = 4; 93 int lower_words = 5; 94 int UPPER_WORDS = 6; 95 @SerializedName("annotatedName") int annotated = 7; 96 int lowerId = 8; 97 int _9 = 9; 98 } 99 } 100