From a526da7cdd2529eb3d13cfbd35664ee82a02d565 Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Wed, 19 Jan 2011 22:24:10 +0000 Subject: [PATCH] Tests that demonstrate that type variables work. Fixes issue 168. --- .../gson/functional/TypeVariableTest.java | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/gson/src/test/java/com/google/gson/functional/TypeVariableTest.java b/gson/src/test/java/com/google/gson/functional/TypeVariableTest.java index 75007776..2d7503eb 100644 --- a/gson/src/test/java/com/google/gson/functional/TypeVariableTest.java +++ b/gson/src/test/java/com/google/gson/functional/TypeVariableTest.java @@ -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 arrayList = new ArrayList(); @@ -47,6 +50,17 @@ public class TypeVariableTest extends TestCase { assertEquals(bar1, bar2); } + public void testTypeVariablesViaTypeParameter() throws Exception { + Gson gson = new Gson(); + Foo original = new Foo("e", 5, false); + original.map.put("f", Arrays.asList(6, 7)); + Type type = new TypeToken>() {}.getType(); + String json = gson.toJson(original, type); + assertEquals("{\"someSField\":\"e\",\"someTField\":5,\"map\":{\"f\":[6,7]},\"redField\":false}", + json); + assertEquals(original, gson.>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 { - 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 extends Red { - private final S someSField; - private final T someTField; + private S someSField; + private T someTField; public final Map> map = new HashMap>(); + public Foo() {} + public Foo(S sValue, T tValue, Boolean redField) { super(redField); this.someSField = sValue;