Test that GraphAdapterBuilder works with collections.

This commit is contained in:
Jesse Wilson 2012-01-01 15:48:01 +00:00
parent bb8dca71c4
commit efde6674e1
2 changed files with 44 additions and 3 deletions

View File

@ -291,7 +291,7 @@ public final class GraphAdapterBuilder {
void read(Graph graph) throws IOException {
if (graph.nextCreate != null) {
throw new IllegalStateException("Unexpected recursive call to read()");
throw new IllegalStateException("Unexpected recursive call to read() for " + id);
}
graph.nextCreate = this;
value = typeAdapter.fromJsonTree(element);

View File

@ -18,6 +18,11 @@ package com.google.gson.graph;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import junit.framework.TestCase;
public final class GraphAdapterBuilderTest extends TestCase {
@ -38,7 +43,7 @@ public final class GraphAdapterBuilderTest extends TestCase {
assertEquals("{'0x1':{'name':'ROCK','beats':'0x2'}," +
"'0x2':{'name':'SCISSORS','beats':'0x3'}," +
"'0x3':{'name':'PAPER','beats':'0x1'}}",
gson.toJson(rock).replace('\"', '\''));
gson.toJson(rock).replace('"', '\''));
}
public void testDeserialization() {
@ -72,7 +77,7 @@ public final class GraphAdapterBuilderTest extends TestCase {
Gson gson = gsonBuilder.create();
assertEquals("{'0x1':{'name':'SUICIDE','beats':'0x1'}}",
gson.toJson(suicide).replace('\"', '\''));
gson.toJson(suicide).replace('"', '\''));
}
public void testDeserializationDirectSelfReference() {
@ -89,6 +94,42 @@ public final class GraphAdapterBuilderTest extends TestCase {
assertSame(suicide, suicide.beats);
}
public void testSerializeListOfLists() {
Type listOfListsType = new TypeToken<List<List<?>>>() {}.getType();
Type listOfAnyType = new TypeToken<List<?>>() {}.getType();
List<List<?>> listOfLists = new ArrayList<List<?>>();
listOfLists.add(listOfLists);
listOfLists.add(new ArrayList<Object>());
GsonBuilder gsonBuilder = new GsonBuilder();
new GraphAdapterBuilder()
.addType(listOfListsType)
.addType(listOfAnyType)
.registerOn(gsonBuilder);
Gson gson = gsonBuilder.create();
String json = gson.toJson(listOfLists, listOfListsType);
assertEquals("{'0x1':['0x1','0x2'],'0x2':[]}", json.replace('"', '\''));
}
public void testDeserializeListOfLists() {
Type listOfAnyType = new TypeToken<List<?>>() {}.getType();
Type listOfListsType = new TypeToken<List<List<?>>>() {}.getType();
GsonBuilder gsonBuilder = new GsonBuilder();
new GraphAdapterBuilder()
.addType(listOfListsType)
.addType(listOfAnyType)
.registerOn(gsonBuilder);
Gson gson = gsonBuilder.create();
List<List<?>> listOfLists = gson.fromJson("{'0x1':['0x1','0x2'],'0x2':[]}", listOfListsType);
assertEquals(2, listOfLists.size());
assertSame(listOfLists, listOfLists.get(0));
assertEquals(Collections.emptyList(), listOfLists.get(1));
}
static class Roshambo {
String name;
Roshambo beats;