Adding tests to ensure the adapter from the old to new naming strategy is working properly.
This commit is contained in:
parent
9816426bba
commit
bf2a0e4e0b
@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2010 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;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unit test for the {@link FieldNamingStrategy2Adapter} class.
|
||||||
|
*
|
||||||
|
* @author Inderjeet Singh
|
||||||
|
* @author Joel Leitch
|
||||||
|
*/
|
||||||
|
public class FieldNamingStrategy2AdapterTest extends TestCase {
|
||||||
|
|
||||||
|
public void testSimpleAdapter() throws Exception {
|
||||||
|
Field field = String.class.getFields()[0];
|
||||||
|
String expectedFieldName = field.getName().toUpperCase();
|
||||||
|
FieldNamingStrategy2 adapter =
|
||||||
|
new FieldNamingStrategy2Adapter(new UpperCaseNamingStrategy());
|
||||||
|
assertEquals(expectedFieldName, adapter.translateName(
|
||||||
|
new FieldAttributes(String.class, field)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class UpperCaseNamingStrategy implements FieldNamingStrategy {
|
||||||
|
public String translateName(Field f) {
|
||||||
|
return f.getName().toUpperCase();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -15,7 +15,10 @@
|
|||||||
*/
|
*/
|
||||||
package com.google.gson.functional;
|
package com.google.gson.functional;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
|
||||||
import com.google.gson.FieldNamingPolicy;
|
import com.google.gson.FieldNamingPolicy;
|
||||||
|
import com.google.gson.FieldNamingStrategy;
|
||||||
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;
|
||||||
@ -110,6 +113,19 @@ public class NamingPolicyTest extends TestCase {
|
|||||||
assertEquals("{\"a\":3.0}", actual);
|
assertEquals("{\"a\":3.0}", actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testDeprecatedNamingStrategy() throws Exception {
|
||||||
|
Gson gson = builder.setFieldNamingStrategy(new UpperCaseNamingStrategy()).create();
|
||||||
|
ClassWithDuplicateFields target = new ClassWithDuplicateFields(10);
|
||||||
|
String actual = gson.toJson(target);
|
||||||
|
assertEquals("{\"A\":10}", actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class UpperCaseNamingStrategy implements FieldNamingStrategy {
|
||||||
|
public String translateName(Field f) {
|
||||||
|
return f.getName().toUpperCase();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
private static class ClassWithDuplicateFields {
|
private static class ClassWithDuplicateFields {
|
||||||
public Integer a;
|
public Integer a;
|
||||||
|
Loading…
Reference in New Issue
Block a user