1 /* 2 * Copyright (C) 2011 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.gson.graph; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertSame; 21 22 import java.lang.reflect.Type; 23 import java.util.ArrayList; 24 import java.util.Collections; 25 import java.util.List; 26 27 import org.junit.Test; 28 29 import com.google.gson.Gson; 30 import com.google.gson.GsonBuilder; 31 import com.google.gson.reflect.TypeToken; 32 33 public final class GraphAdapterBuilderTest { 34 @Test testSerialization()35 public void testSerialization() { 36 Roshambo rock = new Roshambo("ROCK"); 37 Roshambo scissors = new Roshambo("SCISSORS"); 38 Roshambo paper = new Roshambo("PAPER"); 39 rock.beats = scissors; 40 scissors.beats = paper; 41 paper.beats = rock; 42 43 GsonBuilder gsonBuilder = new GsonBuilder(); 44 new GraphAdapterBuilder() 45 .addType(Roshambo.class) 46 .registerOn(gsonBuilder); 47 Gson gson = gsonBuilder.create(); 48 49 assertEquals("{'0x1':{'name':'ROCK','beats':'0x2'}," + 50 "'0x2':{'name':'SCISSORS','beats':'0x3'}," + 51 "'0x3':{'name':'PAPER','beats':'0x1'}}", 52 gson.toJson(rock).replace('"', '\'')); 53 } 54 55 @Test testDeserialization()56 public void testDeserialization() { 57 String json = "{'0x1':{'name':'ROCK','beats':'0x2'}," + 58 "'0x2':{'name':'SCISSORS','beats':'0x3'}," + 59 "'0x3':{'name':'PAPER','beats':'0x1'}}"; 60 61 GsonBuilder gsonBuilder = new GsonBuilder(); 62 new GraphAdapterBuilder() 63 .addType(Roshambo.class) 64 .registerOn(gsonBuilder); 65 Gson gson = gsonBuilder.create(); 66 67 Roshambo rock = gson.fromJson(json, Roshambo.class); 68 assertEquals("ROCK", rock.name); 69 Roshambo scissors = rock.beats; 70 assertEquals("SCISSORS", scissors.name); 71 Roshambo paper = scissors.beats; 72 assertEquals("PAPER", paper.name); 73 assertSame(rock, paper.beats); 74 } 75 76 @Test testDeserializationDirectSelfReference()77 public void testDeserializationDirectSelfReference() { 78 String json = "{'0x1':{'name':'SUICIDE','beats':'0x1'}}"; 79 80 GsonBuilder gsonBuilder = new GsonBuilder(); 81 new GraphAdapterBuilder() 82 .addType(Roshambo.class) 83 .registerOn(gsonBuilder); 84 Gson gson = gsonBuilder.create(); 85 86 Roshambo suicide = gson.fromJson(json, Roshambo.class); 87 assertEquals("SUICIDE", suicide.name); 88 assertSame(suicide, suicide.beats); 89 } 90 91 @Test testSerializeListOfLists()92 public void testSerializeListOfLists() { 93 Type listOfListsType = new TypeToken<List<List<?>>>() {}.getType(); 94 Type listOfAnyType = new TypeToken<List<?>>() {}.getType(); 95 96 List<List<?>> listOfLists = new ArrayList<>(); 97 listOfLists.add(listOfLists); 98 listOfLists.add(new ArrayList<>()); 99 100 GsonBuilder gsonBuilder = new GsonBuilder(); 101 new GraphAdapterBuilder() 102 .addType(listOfListsType) 103 .addType(listOfAnyType) 104 .registerOn(gsonBuilder); 105 Gson gson = gsonBuilder.create(); 106 107 String json = gson.toJson(listOfLists, listOfListsType); 108 assertEquals("{'0x1':['0x1','0x2'],'0x2':[]}", json.replace('"', '\'')); 109 } 110 111 @Test testDeserializeListOfLists()112 public void testDeserializeListOfLists() { 113 Type listOfAnyType = new TypeToken<List<?>>() {}.getType(); 114 Type listOfListsType = new TypeToken<List<List<?>>>() {}.getType(); 115 116 GsonBuilder gsonBuilder = new GsonBuilder(); 117 new GraphAdapterBuilder() 118 .addType(listOfListsType) 119 .addType(listOfAnyType) 120 .registerOn(gsonBuilder); 121 Gson gson = gsonBuilder.create(); 122 123 List<List<?>> listOfLists = gson.fromJson("{'0x1':['0x1','0x2'],'0x2':[]}", listOfListsType); 124 assertEquals(2, listOfLists.size()); 125 assertSame(listOfLists, listOfLists.get(0)); 126 assertEquals(Collections.emptyList(), listOfLists.get(1)); 127 } 128 129 @Test testSerializationWithMultipleTypes()130 public void testSerializationWithMultipleTypes() { 131 Company google = new Company("Google"); 132 new Employee("Jesse", google); 133 new Employee("Joel", google); 134 135 GsonBuilder gsonBuilder = new GsonBuilder(); 136 new GraphAdapterBuilder() 137 .addType(Company.class) 138 .addType(Employee.class) 139 .registerOn(gsonBuilder); 140 Gson gson = gsonBuilder.create(); 141 142 assertEquals("{'0x1':{'name':'Google','employees':['0x2','0x3']}," 143 + "'0x2':{'name':'Jesse','company':'0x1'}," 144 + "'0x3':{'name':'Joel','company':'0x1'}}", 145 gson.toJson(google).replace('"', '\'')); 146 } 147 148 @Test testDeserializationWithMultipleTypes()149 public void testDeserializationWithMultipleTypes() { 150 GsonBuilder gsonBuilder = new GsonBuilder(); 151 new GraphAdapterBuilder() 152 .addType(Company.class) 153 .addType(Employee.class) 154 .registerOn(gsonBuilder); 155 Gson gson = gsonBuilder.create(); 156 157 String json = "{'0x1':{'name':'Google','employees':['0x2','0x3']}," 158 + "'0x2':{'name':'Jesse','company':'0x1'}," 159 + "'0x3':{'name':'Joel','company':'0x1'}}"; 160 Company company = gson.fromJson(json, Company.class); 161 assertEquals("Google", company.name); 162 Employee jesse = company.employees.get(0); 163 assertEquals("Jesse", jesse.name); 164 assertEquals(company, jesse.company); 165 Employee joel = company.employees.get(1); 166 assertEquals("Joel", joel.name); 167 assertEquals(company, joel.company); 168 } 169 170 static class Roshambo { 171 String name; 172 Roshambo beats; Roshambo(String name)173 Roshambo(String name) { 174 this.name = name; 175 } 176 } 177 178 static class Employee { 179 final String name; 180 final Company company; Employee(String name, Company company)181 Employee(String name, Company company) { 182 this.name = name; 183 this.company = company; 184 this.company.employees.add(this); 185 } 186 } 187 188 static class Company { 189 final String name; 190 final List<Employee> employees = new ArrayList<>(); Company(String name)191 Company(String name) { 192 this.name = name; 193 } 194 } 195 } 196