From 13bbbb5b79c9617116b1923b1faf2633559987c6 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Tue, 14 Jun 2016 20:43:28 -0700 Subject: [PATCH] Improve test for #1261 --- .../failing/ObjectWithCreator1261Test.java | 109 +++++++++++++++++- 1 file changed, 104 insertions(+), 5 deletions(-) diff --git a/src/test/java/com/fasterxml/jackson/failing/ObjectWithCreator1261Test.java b/src/test/java/com/fasterxml/jackson/failing/ObjectWithCreator1261Test.java index 315d99b848..2fc17a01d3 100644 --- a/src/test/java/com/fasterxml/jackson/failing/ObjectWithCreator1261Test.java +++ b/src/test/java/com/fasterxml/jackson/failing/ObjectWithCreator1261Test.java @@ -1,16 +1,115 @@ package com.fasterxml.jackson.failing; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; +import java.io.StringReader; +import java.util.*; import com.fasterxml.jackson.annotation.*; - import com.fasterxml.jackson.databind.*; public class ObjectWithCreator1261Test extends BaseMapTest { + static class Answer + { + public SortedMap parents; + + public Answer() { + parents = new TreeMap(); + } + } + + @JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id") + static class Parent + { + public Map children; + + @JsonIdentityReference(alwaysAsId = true) + public Child favoriteChild; + + public String name; + + protected Parent() { } + + public Parent(String name, boolean ignored) { + children = new TreeMap(); + this.name = name; + } + } + + @JsonInclude(JsonInclude.Include.NON_NULL) + @JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id") + static class Child + { + public String name; + + @JsonIdentityReference(alwaysAsId = true) + public Parent parent; + + @JsonIdentityReference(alwaysAsId = true) + public List parentAsList; + + public String someNullProperty; + + protected Child() { } + + @JsonCreator + public Child(@JsonProperty("name") String name, + @JsonProperty("someNullProperty") String someNullProperty) { + this.name = name; + this.someNullProperty = someNullProperty; + } + + public Child(String n) { + name = n; + } + } + + public void testObjectIds1261() throws Exception + { + ObjectMapper mapper = new ObjectMapper(); + mapper.enable(SerializationFeature.INDENT_OUTPUT); + mapper.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY); + + Answer initialAnswer = createInitialAnswer(); + String initialAnswerString = mapper.writeValueAsString(initialAnswer); +// System.out.println("Initial answer:\n"+initialAnswerString); + JsonNode tree = mapper.readTree(initialAnswerString); + Answer deserializedAnswer = mapper.readValue(initialAnswerString, + Answer.class); + String reserializedAnswerString = mapper + .writeValueAsString(deserializedAnswer); + JsonNode newTree = mapper.readTree(reserializedAnswerString); + if (!tree.equals(newTree)) { + fail("Original and recovered Json are different. Recovered = \n" + + reserializedAnswerString + "\n"); + } + } + + private Answer createInitialAnswer() { + Answer answer = new Answer(); + String child1Name = "child1"; + String child2Name = "child2"; + String parent1Name = "parent1"; + String parent2Name = "parent2"; + Parent parent1 = new Parent(parent1Name, false); + answer.parents.put(parent1Name, parent1); + Child child1 = new Child(child1Name); + child1.parent = parent1; + child1.parentAsList = Collections.singletonList(parent1); + Child child2 = new Child(child2Name); + Parent parent2 = new Parent(parent2Name, false); + child2.parent = parent2; + child2.parentAsList = Collections.singletonList(parent2); + parent1.children.put(child1Name, child1); + parent1.children.put(child2Name, child2); + answer.parents.put(parent2Name, parent2); + return answer; + } + // 14-Jun-2016, tatu: Original test, has a cycle which may render it impossible + // to support (although better error message would be nice). + // Left here in case we'll fix the other problem above. + + /* static class Answer { private List _parents; @@ -111,5 +210,5 @@ private static Answer createInitialAnswer() { parent1.setFavoriteChild(child5); return answer; } - +*/ }