From ad1c1a731d4a2f4fb11fa057521179a3b24bb757 Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Fri, 14 Nov 2008 22:11:12 +0000 Subject: [PATCH] Added more tests in response to Issue 70. --- .../functional/CustomTypeAdaptersTest.java | 36 ++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/gson/src/test/java/com/google/gson/functional/CustomTypeAdaptersTest.java b/gson/src/test/java/com/google/gson/functional/CustomTypeAdaptersTest.java index ac1fbaff..ab8d381a 100644 --- a/gson/src/test/java/com/google/gson/functional/CustomTypeAdaptersTest.java +++ b/gson/src/test/java/com/google/gson/functional/CustomTypeAdaptersTest.java @@ -33,7 +33,9 @@ import com.google.gson.reflect.TypeToken; import junit.framework.TestCase; import java.lang.reflect.Type; +import java.util.HashMap; import java.util.HashSet; +import java.util.Map; import java.util.Set; /** @@ -300,7 +302,7 @@ public class CustomTypeAdaptersTest extends TestCase { StringHolder holder = new StringHolder("Jacob", "Tomaw"); Set setOfHolders = new HashSet(); setOfHolders.add(holder); - String json = gson.toJson(holder); + String json = gson.toJson(setOfHolders); assertTrue(json.contains("Jacob:Tomaw")); } @@ -310,9 +312,35 @@ public class CustomTypeAdaptersTest extends TestCase { .registerTypeAdapter(StringHolder.class, new StringHolderTypeAdapter()) .create(); Type setType = new TypeToken>() {}.getType(); - Set setOfFoo = gson.fromJson("['Jacob:Tomaw']", setType); - assertEquals(1, setOfFoo.size()); - StringHolder foo = setOfFoo.iterator().next(); + Set setOfHolders = gson.fromJson("['Jacob:Tomaw']", setType); + assertEquals(1, setOfHolders.size()); + StringHolder foo = setOfHolders.iterator().next(); + assertEquals("Jacob", foo.part1); + assertEquals("Tomaw", foo.part2); + } + + // Test created from Issue 70 + public void testCustomAdapterInvokedForMapElementSerialization() { + Gson gson = new GsonBuilder() + .registerTypeAdapter(StringHolder.class, new StringHolderTypeAdapter()) + .create(); + Type mapType = new TypeToken>() {}.getType(); + StringHolder holder = new StringHolder("Jacob", "Tomaw"); + Map mapOfHolders = new HashMap(); + mapOfHolders.put("foo", holder); + String json = gson.toJson(mapOfHolders); + assertTrue(json.contains("\"foo\":\"Jacob:Tomaw\"")); + } + + // Test created from Issue 70 + public void testCustomAdapterInvokedForMapElementDeserialization() { + Gson gson = new GsonBuilder() + .registerTypeAdapter(StringHolder.class, new StringHolderTypeAdapter()) + .create(); + Type mapType = new TypeToken>() {}.getType(); + Map mapOfFoo = gson.fromJson("{'foo':'Jacob:Tomaw'}", mapType); + assertEquals(1, mapOfFoo.size()); + StringHolder foo = mapOfFoo.get("foo"); assertEquals("Jacob", foo.part1); assertEquals("Tomaw", foo.part2); }