Tests that demonstrate that type variables work.

Fixes issue 168.
This commit is contained in:
Jesse Wilson 2011-01-19 22:24:10 +00:00
parent edc71130c4
commit a526da7cdd

View File

@ -17,6 +17,9 @@ package com.google.gson.functional;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.Arrays;
import junit.framework.TestCase;
import java.util.ArrayList;
@ -32,7 +35,7 @@ import java.util.Map;
*/
public class TypeVariableTest extends TestCase {
public void disabled_testAdvancedTypeVariables() throws Exception {
public void testAdvancedTypeVariables() throws Exception {
Gson gson = new Gson();
Bar bar1 = new Bar("someString", 1, true);
ArrayList<Integer> arrayList = new ArrayList<Integer>();
@ -47,6 +50,17 @@ public class TypeVariableTest extends TestCase {
assertEquals(bar1, bar2);
}
public void testTypeVariablesViaTypeParameter() throws Exception {
Gson gson = new Gson();
Foo<String, Integer> original = new Foo<String, Integer>("e", 5, false);
original.map.put("f", Arrays.asList(6, 7));
Type type = new TypeToken<Foo<String, Integer>>() {}.getType();
String json = gson.toJson(original, type);
assertEquals("{\"someSField\":\"e\",\"someTField\":5,\"map\":{\"f\":[6,7]},\"redField\":false}",
json);
assertEquals(original, gson.<Foo<String, Integer>>fromJson(json, type));
}
public void testBasicTypeVariables() throws Exception {
Gson gson = new Gson();
Blue blue1 = new Blue(true);
@ -77,7 +91,9 @@ public class TypeVariableTest extends TestCase {
}
public static class Red<S> {
protected final S redField;
protected S redField;
public Red() {}
public Red(S redField) {
this.redField = redField;
@ -85,10 +101,12 @@ public class TypeVariableTest extends TestCase {
}
public static class Foo<S, T> extends Red<Boolean> {
private final S someSField;
private final T someTField;
private S someSField;
private T someTField;
public final Map<S, List<T>> map = new HashMap<S, List<T>>();
public Foo() {}
public Foo(S sValue, T tValue, Boolean redField) {
super(redField);
this.someSField = sValue;