From 3cbe355cb6b1ab5d84ddb75837430c3d9584119b Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Wed, 23 Nov 2011 05:13:17 +0000 Subject: [PATCH] Test precedence of type hierarchy adapters. --- .../functional/TypeAdapterPrecedenceTest.java | 100 +++++++++++------- 1 file changed, 61 insertions(+), 39 deletions(-) diff --git a/gson/src/test/java/com/google/gson/functional/TypeAdapterPrecedenceTest.java b/gson/src/test/java/com/google/gson/functional/TypeAdapterPrecedenceTest.java index 0dc20aa1..616251e4 100644 --- a/gson/src/test/java/com/google/gson/functional/TypeAdapterPrecedenceTest.java +++ b/gson/src/test/java/com/google/gson/functional/TypeAdapterPrecedenceTest.java @@ -21,7 +21,6 @@ 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; @@ -33,64 +32,87 @@ import java.lang.reflect.Type; import junit.framework.TestCase; public final class TypeAdapterPrecedenceTest extends TestCase { - private static final JsonSerializer FOO_SERIALIZER = new JsonSerializer() { - public JsonElement serialize(Foo src, Type typeOfSrc, JsonSerializationContext context) { - return new JsonPrimitive(src.name + " (via FOO_SERIALIZER)"); - } - }; - - private static final JsonDeserializer FOO_DESERIALIZER = new JsonDeserializer() { - public Foo deserialize(JsonElement json, Type typeOfT, - JsonDeserializationContext context) throws JsonParseException { - return new Foo(json.getAsString() + " (via FOO_DESERIALIZER)"); - } - }; - - private static final TypeAdapter FOO_TYPE_ADAPTER = new TypeAdapter() { - @Override public Foo read(JsonReader reader) throws IOException { - return new Foo(reader.nextString() + " (via FOO_TYPE_ADAPTER)"); - } - @Override public void write(JsonWriter writer, Foo value) throws IOException { - writer.value(value.name + " (via FOO_TYPE_ADAPTER)"); - } - }; - public void testSerializeNonstreamingTypeAdapterFollowedByStreamingTypeAdapter() { Gson gson = new GsonBuilder() - .registerTypeAdapter(Foo.class, FOO_SERIALIZER) - .registerTypeAdapter(Foo.class, FOO_TYPE_ADAPTER) + .registerTypeAdapter(Foo.class, newSerializer("serializer")) + .registerTypeAdapter(Foo.class, newDeserializer("deserializer")) + .registerTypeAdapter(Foo.class, newTypeAdapter("type adapter")) .create(); - assertEquals("\"foo (via FOO_SERIALIZER)\"", gson.toJson(new Foo("foo"))); + assertEquals("\"foo via serializer\"", gson.toJson(new Foo("foo"))); + assertEquals("foo via deserializer", gson.fromJson("foo", Foo.class).name); } - public void testSerializeStreamingTypeAdapterFollowedByNonstreamingTypeAdapter() { + public void testStreamingFollowedByNonstreaming() { Gson gson = new GsonBuilder() - .registerTypeAdapter(Foo.class, FOO_TYPE_ADAPTER) - .registerTypeAdapter(Foo.class, FOO_SERIALIZER) + .registerTypeAdapter(Foo.class, newTypeAdapter("type adapter")) + .registerTypeAdapter(Foo.class, newSerializer("serializer")) + .registerTypeAdapter(Foo.class, newDeserializer("deserializer")) .create(); - assertEquals("\"foo (via FOO_TYPE_ADAPTER)\"", gson.toJson(new Foo("foo"))); + assertEquals("\"foo via type adapter\"", gson.toJson(new Foo("foo"))); + assertEquals("foo via type adapter", gson.fromJson("foo", Foo.class).name); } - public void testDeserializeNonstreamingTypeAdapterFollowedByStreamingTypeAdapter() { + public void testStreamingHierarchicalFollowedByNonstreaming() { Gson gson = new GsonBuilder() - .registerTypeAdapter(Foo.class, FOO_DESERIALIZER) - .registerTypeAdapter(Foo.class, FOO_TYPE_ADAPTER) + .registerTypeHierarchyAdapter(Foo.class, newTypeAdapter("type adapter")) + .registerTypeAdapter(Foo.class, newSerializer("serializer")) + .registerTypeAdapter(Foo.class, newDeserializer("deserializer")) .create(); - assertEquals("foo (via FOO_DESERIALIZER)", gson.fromJson("foo", Foo.class).name); + assertEquals("\"foo via type adapter\"", gson.toJson(new Foo("foo"))); + assertEquals("foo via type adapter", gson.fromJson("foo", Foo.class).name); } - public void testDeserializeStreamingTypeAdapterFollowedByNonstreamingTypeAdapter() { + public void testStreamingFollowedByNonstreamingHierarchical() { Gson gson = new GsonBuilder() - .registerTypeAdapter(Foo.class, FOO_TYPE_ADAPTER) - .registerTypeAdapter(Foo.class, FOO_DESERIALIZER) + .registerTypeAdapter(Foo.class, newTypeAdapter("type adapter")) + .registerTypeHierarchyAdapter(Foo.class, newSerializer("serializer")) + .registerTypeHierarchyAdapter(Foo.class, newDeserializer("deserializer")) .create(); - assertEquals("foo (via FOO_TYPE_ADAPTER)", gson.fromJson("foo", Foo.class).name); + assertEquals("\"foo via type adapter\"", gson.toJson(new Foo("foo"))); + assertEquals("foo via type adapter", gson.fromJson("foo", Foo.class).name); + } + + public void testStreamingHierarchicalFollowedByNonstreamingHierarchical() { + Gson gson = new GsonBuilder() + .registerTypeHierarchyAdapter(Foo.class, newSerializer("serializer")) + .registerTypeHierarchyAdapter(Foo.class, newDeserializer("deserializer")) + .registerTypeHierarchyAdapter(Foo.class, newTypeAdapter("type adapter")) + .create(); + assertEquals("\"foo via type adapter\"", gson.toJson(new Foo("foo"))); + assertEquals("foo via type adapter", gson.fromJson("foo", Foo.class).name); } private static class Foo { - private final String name; + final String name; private Foo(String name) { this.name = name; } } + + private JsonSerializer newSerializer(final String name) { + return new JsonSerializer() { + public JsonElement serialize(Foo src, Type typeOfSrc, JsonSerializationContext context) { + return new JsonPrimitive(src.name + " via " + name); + } + }; + } + + private JsonDeserializer newDeserializer(final String name) { + return new JsonDeserializer() { + public Foo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) { + return new Foo(json.getAsString() + " via " + name); + } + }; + } + + private TypeAdapter newTypeAdapter(final String name) { + return new TypeAdapter() { + @Override public Foo read(JsonReader reader) throws IOException { + return new Foo(reader.nextString() + " via " + name); + } + @Override public void write(JsonWriter writer, Foo value) throws IOException { + writer.value(value.name + " via " + name); + } + }; + } }