From 6a951b427e2f0b37597da343401029312ff8288c Mon Sep 17 00:00:00 2001 From: Joel Leitch Date: Fri, 5 Nov 2010 22:57:44 +0000 Subject: [PATCH] Adding new tests (two of which are disabled, but prepping for 1.7). --- .../com/google/gson/functional/EnumTest.java | 57 +++++++++++---- .../gson/functional/TypeVariableTest.java | 73 +++++++++++++++---- 2 files changed, 104 insertions(+), 26 deletions(-) diff --git a/gson/src/test/java/com/google/gson/functional/EnumTest.java b/gson/src/test/java/com/google/gson/functional/EnumTest.java index bdd7b4da..438ada02 100644 --- a/gson/src/test/java/com/google/gson/functional/EnumTest.java +++ b/gson/src/test/java/com/google/gson/functional/EnumTest.java @@ -16,18 +16,26 @@ package com.google.gson.functional; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonParseException; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; +import com.google.gson.common.MoreAsserts; +import com.google.gson.reflect.TypeToken; + +import junit.framework.TestCase; + import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Collection; import java.util.EnumSet; import java.util.Set; -import junit.framework.TestCase; - -import com.google.gson.Gson; -import com.google.gson.common.MoreAsserts; -import com.google.gson.reflect.TypeToken; - /** * Functional tests for Java 5.0 enums. * @@ -35,25 +43,25 @@ import com.google.gson.reflect.TypeToken; * @author Joel Leitch */ public class EnumTest extends TestCase { - + private Gson gson; - + @Override protected void setUp() throws Exception { super.setUp(); gson = new Gson(); } - + public void testTopLevelEnumSerialization() throws Exception { String result = gson.toJson(MyEnum.VALUE1); assertEquals('"' + MyEnum.VALUE1.toString() + '"', result); } - + public void testTopLevelEnumDeserialization() throws Exception { MyEnum result = gson.fromJson('"' + MyEnum.VALUE1.toString() + '"', MyEnum.class); assertEquals(MyEnum.VALUE1, result); } - + public void testTopLevelEnumInASingleElementArrayDeserialization() { String json = "[" + MyEnum.VALUE1.getExpectedJson() + "]"; MyEnum target = gson.fromJson(json, MyEnum.class); @@ -91,7 +99,7 @@ public class EnumTest extends TestCase { assertEquals(MyEnum.VALUE1,target.value1); assertEquals(MyEnum.VALUE2,target.value2); } - + private static enum MyEnum { VALUE1, VALUE2; @@ -112,6 +120,17 @@ public class EnumTest extends TestCase { * Test for issue 226. */ public void testEnumSubclass() { + assertRoshambo(); + } + + public void disabled_testEnumSubclassWithRegisteredTypeAdapter() { + gson = new GsonBuilder() + .registerTypeHierarchyAdapter(Roshambo.class, new MyEnumTypeAdapter()) + .create(); + assertRoshambo(); + } + + private void assertRoshambo() { assertFalse(Roshambo.class == Roshambo.ROCK.getClass()); assertEquals("\"ROCK\"", gson.toJson(Roshambo.ROCK)); assertEquals("[\"ROCK\",\"PAPER\",\"SCISSORS\"]", gson.toJson(EnumSet.allOf(Roshambo.class))); @@ -120,7 +139,7 @@ public class EnumTest extends TestCase { gson.fromJson("[\"ROCK\",\"PAPER\",\"SCISSORS\"]", new TypeToken>() {}.getType())); } - private enum Roshambo { + public enum Roshambo { ROCK { @Override Roshambo defeats() { return SCISSORS; @@ -139,4 +158,16 @@ public class EnumTest extends TestCase { abstract Roshambo defeats(); } + + private static class MyEnumTypeAdapter + implements JsonSerializer, JsonDeserializer { + public JsonElement serialize(Roshambo src, Type typeOfSrc, JsonSerializationContext context) { + return new JsonPrimitive("123" + src.name()); + } + + public Roshambo deserialize(JsonElement json, Type classOfT, JsonDeserializationContext context) + throws JsonParseException { + return Roshambo.valueOf(json.getAsString().substring(3)); + } + } } 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 c463ef2b..f9965a26 100644 --- a/gson/src/test/java/com/google/gson/functional/TypeVariableTest.java +++ b/gson/src/test/java/com/google/gson/functional/TypeVariableTest.java @@ -26,16 +26,19 @@ import java.util.Map; /** * Functional test for Gson serialization and deserialization of + * classes with type variables. + * * @author Joel Leitch */ public class TypeVariableTest extends TestCase { - public void testSingle() throws Exception { + public void disabled_testAdvancedTypeVariables() throws Exception { Gson gson = new Gson(); - Bar bar1 = new Bar("someString", 1); + Bar bar1 = new Bar("someString", 1, true); ArrayList arrayList = new ArrayList(); arrayList.add(1); arrayList.add(2); + arrayList.add(3); bar1.map.put("key1", arrayList); bar1.map.put("key2", new ArrayList()); String json = gson.toJson(bar1); @@ -44,36 +47,80 @@ public class TypeVariableTest extends TestCase { assertEquals(bar1, bar2); } - public static class Foo { + public void testBasicTypeVariables() throws Exception { + Gson gson = new Gson(); + Blue blue1 = new Blue(true); + String json = gson.toJson(blue1); + + Blue blue2 = gson.fromJson(json, Blue.class); + assertEquals(blue1, blue2); + } + + public static class Blue extends Red { + public Blue() { + super(false); + } + + public Blue(boolean value) { + super(value); + } + + // Technically, we should implement hashcode too + @Override + public boolean equals(Object o) { + if (!(o instanceof Blue)) { + return false; + } else { + Blue blue = (Blue) o; + return redField.equals(blue.redField); + + } + } + } + + public static class Red { + protected final S redField; + + public Red(S redField) { + this.redField = redField; + } + } + + public static class Foo extends Red { private final S someSField; private final T someTField; public final Map> map = new HashMap>(); - public Foo(S sValue, T tValue) { + public Foo(S sValue, T tValue, Boolean redField) { + super(redField); this.someSField = sValue; this.someTField = tValue; } - @SuppressWarnings("unchecked") + // Technically, we should implement hashcode too @Override + @SuppressWarnings("unchecked") public boolean equals(Object o) { - if (!(o instanceof Foo)) { + if (!(o instanceof Foo)) { return false; + } else { + Foo realFoo = (Foo) o; + return redField.equals(realFoo.redField) + && someTField.equals(realFoo.someTField) + && someSField.equals(realFoo.someSField) + && map.equals(realFoo.map); + } - Foo realFoo = (Foo) o; - return someTField.equals(realFoo.someTField) - && someSField.equals(realFoo.someSField) - && map.equals(realFoo.map); } } public static class Bar extends Foo { public Bar() { - this("", 0); + this("", 0, false); } - public Bar(String s, Integer i) { - super(s, i); + public Bar(String s, Integer i, boolean b) { + super(s, i, b); } } }