From e9c156b01661b53e3a33f63fb45c0beda5bfa731 Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Thu, 8 Oct 2009 19:32:15 +0000 Subject: [PATCH] Added inheritance tests using toJson() method as well. --- .../gson/functional/InheritanceTest.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/gson/src/test/java/com/google/gson/functional/InheritanceTest.java b/gson/src/test/java/com/google/gson/functional/InheritanceTest.java index 22bb897d..ce65fd1f 100644 --- a/gson/src/test/java/com/google/gson/functional/InheritanceTest.java +++ b/gson/src/test/java/com/google/gson/functional/InheritanceTest.java @@ -90,6 +90,12 @@ public class InheritanceTest extends TestCase { assertEquals(Sub.SUB_NAME, json.get(Sub.SUB_FIELD_KEY).getAsString()); } + public void testBaseSerializedAsSubForToJsonMethod() { + Base base = new Sub(); + String json = gson.toJson(base); + assertTrue(json.contains(Sub.SUB_NAME)); + } + public void testBaseSerializedAsBaseWhenSpecifiedWithExplicitType() { Base base = new Sub(); JsonObject json = gson.toJsonTree(base, Base.class).getAsJsonObject(); @@ -97,12 +103,25 @@ public class InheritanceTest extends TestCase { assertNull(json.get(Sub.SUB_FIELD_KEY)); } + public void testBaseSerializedAsBaseWhenSpecifiedWithExplicitTypeForToJsonMethod() { + Base base = new Sub(); + String json = gson.toJson(base, Base.class); + assertTrue(json.contains(Base.BASE_NAME)); + assertFalse(json.contains(Sub.SUB_FIELD_KEY)); + } + public void testBaseSerializedAsSubWhenSpecifiedWithExplicitType() { Base base = new Sub(); JsonObject json = gson.toJsonTree(base, Sub.class).getAsJsonObject(); assertEquals(Sub.SUB_NAME, json.get(Sub.SUB_FIELD_KEY).getAsString()); } + public void testBaseSerializedAsSubWhenSpecifiedWithExplicitTypeForToJsonMethod() { + Base base = new Sub(); + String json = gson.toJson(base, Sub.class); + assertTrue(json.contains(Sub.SUB_NAME)); + } + private static class SubTypeOfNested extends Nested { private final long value = 5;