• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import static org.junit.Assert.assertEquals;
26 
27 import com.google.gson.FieldNamingPolicy;
28 import com.google.gson.Gson;
29 import com.google.gson.GsonBuilder;
30 import com.google.gson.annotations.SerializedName;
31 import org.junit.Test;
32 
33 public final class FieldNamingTest {
34   @Test
testIdentity()35   public void testIdentity() {
36     Gson gson = getGsonWithNamingPolicy(IDENTITY);
37     assertEquals("{'lowerCamel':1,'UpperCamel':2,'_lowerCamelLeadingUnderscore':3," +
38         "'_UpperCamelLeadingUnderscore':4,'lower_words':5,'UPPER_WORDS':6," +
39         "'annotatedName':7,'lowerId':8,'_9':9}",
40         gson.toJson(new TestNames()).replace('\"', '\''));
41   }
42 
43   @Test
testUpperCamelCase()44   public void testUpperCamelCase() {
45     Gson gson = getGsonWithNamingPolicy(UPPER_CAMEL_CASE);
46     assertEquals("{'LowerCamel':1,'UpperCamel':2,'_LowerCamelLeadingUnderscore':3," +
47         "'_UpperCamelLeadingUnderscore':4,'Lower_words':5,'UPPER_WORDS':6," +
48         "'annotatedName':7,'LowerId':8,'_9':9}",
49         gson.toJson(new TestNames()).replace('\"', '\''));
50   }
51 
52   @Test
testUpperCamelCaseWithSpaces()53   public void testUpperCamelCaseWithSpaces() {
54     Gson gson = getGsonWithNamingPolicy(UPPER_CAMEL_CASE_WITH_SPACES);
55     assertEquals("{'Lower Camel':1,'Upper Camel':2,'_Lower Camel Leading Underscore':3," +
56         "'_ Upper Camel Leading Underscore':4,'Lower_words':5,'U P P E R_ W O R D S':6," +
57         "'annotatedName':7,'Lower Id':8,'_9':9}",
58         gson.toJson(new TestNames()).replace('\"', '\''));
59   }
60 
61   @Test
testUpperCaseWithUnderscores()62   public void testUpperCaseWithUnderscores() {
63     Gson gson = getGsonWithNamingPolicy(UPPER_CASE_WITH_UNDERSCORES);
64     assertEquals("{'LOWER_CAMEL':1,'UPPER_CAMEL':2,'_LOWER_CAMEL_LEADING_UNDERSCORE':3," +
65         "'__UPPER_CAMEL_LEADING_UNDERSCORE':4,'LOWER_WORDS':5,'U_P_P_E_R__W_O_R_D_S':6," +
66         "'annotatedName':7,'LOWER_ID':8,'_9':9}",
67         gson.toJson(new TestNames()).replace('\"', '\''));
68   }
69 
70   @Test
testLowerCaseWithUnderscores()71   public void testLowerCaseWithUnderscores() {
72     Gson gson = getGsonWithNamingPolicy(LOWER_CASE_WITH_UNDERSCORES);
73     assertEquals("{'lower_camel':1,'upper_camel':2,'_lower_camel_leading_underscore':3," +
74         "'__upper_camel_leading_underscore':4,'lower_words':5,'u_p_p_e_r__w_o_r_d_s':6," +
75         "'annotatedName':7,'lower_id':8,'_9':9}",
76         gson.toJson(new TestNames()).replace('\"', '\''));
77   }
78 
79   @Test
testLowerCaseWithDashes()80   public void testLowerCaseWithDashes() {
81     Gson gson = getGsonWithNamingPolicy(LOWER_CASE_WITH_DASHES);
82     assertEquals("{'lower-camel':1,'upper-camel':2,'_lower-camel-leading-underscore':3," +
83         "'_-upper-camel-leading-underscore':4,'lower_words':5,'u-p-p-e-r_-w-o-r-d-s':6," +
84         "'annotatedName':7,'lower-id':8,'_9':9}",
85         gson.toJson(new TestNames()).replace('\"', '\''));
86   }
87 
getGsonWithNamingPolicy(FieldNamingPolicy fieldNamingPolicy)88   private Gson getGsonWithNamingPolicy(FieldNamingPolicy fieldNamingPolicy){
89     return new GsonBuilder()
90       .setFieldNamingPolicy(fieldNamingPolicy)
91         .create();
92   }
93 
94   @SuppressWarnings("unused") // fields are used reflectively
95   private static class TestNames {
96     int lowerCamel = 1;
97     int UpperCamel = 2;
98     int _lowerCamelLeadingUnderscore = 3;
99     int _UpperCamelLeadingUnderscore = 4;
100     int lower_words = 5;
101     int UPPER_WORDS = 6;
102     @SerializedName("annotatedName") int annotated = 7;
103     int lowerId = 8;
104     int _9 = 9;
105   }
106 }
107