/*
 * Copyright (C) 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.gson.functional;

import static com.google.gson.FieldNamingPolicy.IDENTITY;
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.UPPER_CAMEL_CASE;
import static com.google.gson.FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;
import junit.framework.TestCase;

public final class FieldNamingTest extends TestCase {
  public void testIdentity() {
    Gson gson = new GsonBuilder().setFieldNamingPolicy(IDENTITY).create();
    assertEquals("{'lowerCamel':1,'UpperCamel':2,'_lowerCamelLeadingUnderscore':3," +
        "'_UpperCamelLeadingUnderscore':4,'lower_words':5,'UPPER_WORDS':6," +
        "'annotatedName':7}",
        gson.toJson(new TestNames()).replace('\"', '\''));
  }

  public void testUpperCamelCase() {
    Gson gson = new GsonBuilder().setFieldNamingPolicy(UPPER_CAMEL_CASE).create();
    assertEquals("{'LowerCamel':1,'UpperCamel':2,'_LowerCamelLeadingUnderscore':3," +
        "'_UpperCamelLeadingUnderscore':4,'Lower_words':5,'UPPER_WORDS':6," +
        "'annotatedName':7}",
        gson.toJson(new TestNames()).replace('\"', '\''));
  }

  public void testUpperCamelCaseWithSpaces() {
    Gson gson = new GsonBuilder().setFieldNamingPolicy(UPPER_CAMEL_CASE_WITH_SPACES).create();
    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," +
        "'annotatedName':7}",
        gson.toJson(new TestNames()).replace('\"', '\''));
  }

  public void testLowerCaseWithUnderscores() {
    Gson gson = new GsonBuilder().setFieldNamingPolicy(LOWER_CASE_WITH_UNDERSCORES).create();
    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," +
        "'annotatedName':7}",
        gson.toJson(new TestNames()).replace('\"', '\''));
  }

  public void testLowerCaseWithDashes() {
    Gson gson = new GsonBuilder().setFieldNamingPolicy(LOWER_CASE_WITH_DASHES).create();
    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," +
        "'annotatedName':7}",
        gson.toJson(new TestNames()).replace('\"', '\''));
  }

  @SuppressWarnings("unused") // fields are used reflectively
  private static class TestNames {
    int lowerCamel = 1;
    int UpperCamel = 2;
    int _lowerCamelLeadingUnderscore = 3;
    int _UpperCamelLeadingUnderscore = 4;
    int lower_words = 5;
    int UPPER_WORDS = 6;
    @SerializedName("annotatedName") int annotated = 7;
  }
}

@@ -981,16 +981,59 @@
 _SPACES;
+%0A%0Aimport com.google.gson.FieldNamingPolicy;
 %0Aimport 
@@ -1280,34 +1280,19 @@
 n = 
-new GsonBuilder().setField
+getGsonWith
 Nami
@@ -1309,25 +1309,16 @@
 DENTITY)
-.create()
 ;%0A    as
@@ -1493,32 +1493,44 @@
 annotatedName':7
+,'lowerId':8
 %7D%22,%0A        gson
@@ -1638,34 +1638,19 @@
 n = 
-new GsonBuilder().setField
+getGsonWith
 Nami
@@ -1675,25 +1675,16 @@
 EL_CASE)
-.create()
 ;%0A    as
@@ -1859,32 +1859,44 @@
 annotatedName':7
+,'LowerId':8
 %7D%22,%0A        gson
@@ -2014,34 +2014,19 @@
 n = 
-new GsonBuilder().setField
+getGsonWith
 Nami
@@ -2063,25 +2063,16 @@
 _SPACES)
-.create()
 ;%0A    as
@@ -2265,32 +2265,45 @@
 annotatedName':7
+,'Lower Id':8
 %7D%22,%0A        gson
@@ -2421,34 +2421,19 @@
 n = 
-new GsonBuilder().setField
+getGsonWith
 Nami
@@ -2469,25 +2469,16 @@
 RSCORES)
-.create()
 ;%0A    as
@@ -2671,32 +2671,45 @@
 annotatedName':7
+,'lower_id':8
 %7D%22,%0A        gson
@@ -2822,34 +2822,19 @@
 n = 
-new GsonBuilder().setField
+getGsonWith
 Nami
@@ -2869,17 +2869,8 @@
 HES)
-.create()
 ;%0A  
@@ -3075,16 +3075,29 @@
 dName':7
+,'lower-id':8
 %7D%22,%0A    
@@ -3156,16 +3156,193 @@
 );%0A  %7D%0A%0A
+  private Gson getGsonWithNamingPolicy(FieldNamingPolicy fieldNamingPolicy)%7B%0A    return new GsonBuilder()%0A      .setFieldNamingPolicy(fieldNamingPolicy)%0A        .create();%0A  %7D%0A%0A
   @Suppr
@@ -3612,16 +3612,16 @@
 DS = 6;%0A
-
     @Ser
@@ -3668,14 +3668,35 @@
 ed = 7;%0A
+    int lowerId = 8;%0A
   %7D%0A%7D%0A

Add tests to demonstrate the issue