Skip to content

Commit

Permalink
Test that GraphAdapterBuilder works with collections.
Browse files Browse the repository at this point in the history
  • Loading branch information
swankjesse committed Jan 1, 2012
1 parent bb8dca7 commit efde667
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ void write(JsonWriter out) throws IOException {

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@

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 {
Expand All @@ -38,7 +43,7 @@ public void testSerialization() {
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() {
Expand Down Expand Up @@ -72,7 +77,7 @@ public void testSerializationDirectSelfReference() {
Gson gson = gsonBuilder.create();

assertEquals("{'0x1':{'name':'SUICIDE','beats':'0x1'}}",
gson.toJson(suicide).replace('\"', '\''));
gson.toJson(suicide).replace('"', '\''));
}

public void testDeserializationDirectSelfReference() {
Expand All @@ -89,6 +94,42 @@ public void testDeserializationDirectSelfReference() {
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;
Expand Down

0 comments on commit efde667

Please sign in to comment.