From 102f8b3a71fa33cdac09d0e6ecdf8881702c10a7 Mon Sep 17 00:00:00 2001 From: Joel Leitch Date: Fri, 9 Oct 2009 21:23:51 +0000 Subject: [PATCH] Adding a few more assert to the InstanceCreatorTest to ensure that the default values are set for sub class fields. --- .../gson/functional/InstanceCreatorTest.java | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/gson/src/test/java/com/google/gson/functional/InstanceCreatorTest.java b/gson/src/test/java/com/google/gson/functional/InstanceCreatorTest.java index 4a179862..e673cf86 100644 --- a/gson/src/test/java/com/google/gson/functional/InstanceCreatorTest.java +++ b/gson/src/test/java/com/google/gson/functional/InstanceCreatorTest.java @@ -16,10 +16,6 @@ package com.google.gson.functional; -import java.lang.reflect.Type; - -import junit.framework.TestCase; - import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.InstanceCreator; @@ -27,14 +23,18 @@ import com.google.gson.common.TestTypes.Base; import com.google.gson.common.TestTypes.ClassWithBaseField; import com.google.gson.common.TestTypes.Sub; +import junit.framework.TestCase; + +import java.lang.reflect.Type; + /** - * Functional Test exercising custom serialization only. When test applies to both + * Functional Test exercising custom serialization only. When test applies to both * serialization and deserialization then add it to CustomTypeAdapterTest. * * @author Inderjeet Singh */ public class InstanceCreatorTest extends TestCase { - + public void testInstanceCreatorReturnsBaseType() { Gson gson = new GsonBuilder() .registerTypeAdapter(Base.class, new InstanceCreator() { @@ -47,7 +47,7 @@ public class InstanceCreatorTest extends TestCase { Base base = gson.fromJson(json, Base.class); assertEquals("BaseRevised", base.baseName); } - + public void testInstanceCreatorReturnsSubTypeForTopLevelObject() { Gson gson = new GsonBuilder() .registerTypeAdapter(Base.class, new InstanceCreator() { @@ -56,11 +56,16 @@ public class InstanceCreatorTest extends TestCase { } }) .create(); + String json = "{baseName:'Base',subName:'SubRevised'}"; Base base = gson.fromJson(json, Base.class); - assertFalse("SubRevised".equals(((Sub)base).subName)); + assertTrue(base instanceof Sub); + + Sub sub = (Sub) base; + assertFalse("SubRevised".equals(sub.subName)); + assertEquals(Sub.SUB_NAME, sub.subName); } - + public void testInstanceCreatorReturnsSubTypeForField() { Gson gson = new GsonBuilder() .registerTypeAdapter(Base.class, new InstanceCreator() {