/* * Copyright (C) 2008 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.gson.functional; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; import com.google.gson.common.TestTypes.ClassOverridingEquals; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Collection; import org.junit.Before; import org.junit.Test; /** * Functional tests related to circular reference detection and error reporting. * * @author Inderjeet Singh * @author Joel Leitch */ public class CircularReferenceTest { private Gson gson; @Before public void setUp() throws Exception { gson = new Gson(); } @Test public void testCircularSerialization() throws Exception { ContainsReferenceToSelfType a = new ContainsReferenceToSelfType(); ContainsReferenceToSelfType b = new ContainsReferenceToSelfType(); a.children.add(b); b.children.add(a); try { gson.toJson(a); fail("Circular types should not get printed!"); } catch (StackOverflowError expected) { } } @Test public void testSelfReferenceIgnoredInSerialization() throws Exception { ClassOverridingEquals objA = new ClassOverridingEquals(); objA.ref = objA; String json = gson.toJson(objA); assertFalse(json.contains("ref")); // self-reference is ignored } @Test public void testSelfReferenceArrayFieldSerialization() throws Exception { ClassWithSelfReferenceArray objA = new ClassWithSelfReferenceArray(); objA.children = new ClassWithSelfReferenceArray[]{objA}; try { gson.toJson(objA); fail("Circular reference to self can not be serialized!"); } catch (StackOverflowError expected) { } } @Test public void testSelfReferenceCustomHandlerSerialization() throws Exception { ClassWithSelfReference obj = new ClassWithSelfReference(); obj.child = obj; Gson gson = new GsonBuilder().registerTypeAdapter(ClassWithSelfReference.class, new JsonSerializer() { @Override public JsonElement serialize(ClassWithSelfReference src, Type typeOfSrc, JsonSerializationContext context) { JsonObject obj = new JsonObject(); obj.addProperty("property", "value"); obj.add("child", context.serialize(src.child)); return obj; } }).create(); try { gson.toJson(obj); fail("Circular reference to self can not be serialized!"); } catch (StackOverflowError expected) { } } @Test public void testDirectedAcyclicGraphSerialization() throws Exception { ContainsReferenceToSelfType a = new ContainsReferenceToSelfType(); ContainsReferenceToSelfType b = new ContainsReferenceToSelfType(); ContainsReferenceToSelfType c = new ContainsReferenceToSelfType(); a.children.add(b); a.children.add(c); b.children.add(c); assertNotNull(gson.toJson(a)); } @Test public void testDirectedAcyclicGraphDeserialization() throws Exception { String json = "{\"children\":[{\"children\":[{\"children\":[]}]},{\"children\":[]}]}"; ContainsReferenceToSelfType target = gson.fromJson(json, ContainsReferenceToSelfType.class); assertNotNull(target); assertEquals(2, target.children.size()); } private static class ContainsReferenceToSelfType { Collection children = new ArrayList<>(); } private static class ClassWithSelfReference { ClassWithSelfReference child; } private static class ClassWithSelfReferenceArray { @SuppressWarnings("unused") ClassWithSelfReferenceArray[] children; } }