From d3983fe7e9334033a80ebd7d7f298f6c8f1bd9fb Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Wed, 23 Oct 2019 12:25:59 -0700 Subject: Move #2501 test to better place --- .../databind/deser/CyclicTypesDeserTest.java | 31 ---------------------- .../jackson/databind/ser/CyclicTypeSerTest.java | 31 +++++++++++++++++----- 2 files changed, 25 insertions(+), 37 deletions(-) (limited to 'src/test/java') diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/CyclicTypesDeserTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/CyclicTypesDeserTest.java index 5c26c353d..1fa3862c7 100644 --- a/src/test/java/com/fasterxml/jackson/databind/deser/CyclicTypesDeserTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/deser/CyclicTypesDeserTest.java @@ -110,35 +110,4 @@ public class CyclicTypesDeserTest assertNotNull(json); assertEquals(aposToQuotes("{'id':1,'parent':{'id':1}}"), json); } - - // [Issue#2501]: Should be possible to replace null cyclic ref - public void testReplacedCycle() throws Exception - { - Selfie405 self1 = new Selfie405(1); - self1.parent = self1; - - // First: ignore WRITE_SELF_REFERENCES_AS_NULL property if FAIL_ON_SELF_REFERENCES is enabled - ObjectWriter w = MAPPER.writer() - .with(SerializationFeature.FAIL_ON_SELF_REFERENCES) - .with(SerializationFeature.WRITE_SELF_REFERENCES_AS_NULL); - assertTrue(w.isEnabled(SerializationFeature.FAIL_ON_SELF_REFERENCES)); - assertTrue(w.isEnabled(SerializationFeature.WRITE_SELF_REFERENCES_AS_NULL)); - try { - w.writeValueAsString(self1); - fail("Should fail with direct self-ref"); - } catch (JsonMappingException e) { - verifyException(e, "Direct self-reference"); - } - - // Second: write as null - w = MAPPER.writer() - .without(SerializationFeature.FAIL_ON_SELF_REFERENCES) - .with(SerializationFeature.WRITE_SELF_REFERENCES_AS_NULL); - assertFalse(w.isEnabled(SerializationFeature.FAIL_ON_SELF_REFERENCES)); - assertTrue(w.isEnabled(SerializationFeature.WRITE_SELF_REFERENCES_AS_NULL)); - String json = w.writeValueAsString(self1); - assertNotNull(json); - assertEquals(aposToQuotes("{'id':1,'parent':null}"), json); - } - } diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/CyclicTypeSerTest.java b/src/test/java/com/fasterxml/jackson/databind/ser/CyclicTypeSerTest.java index d6cdf2756..644779644 100644 --- a/src/test/java/com/fasterxml/jackson/databind/ser/CyclicTypeSerTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/ser/CyclicTypeSerTest.java @@ -2,7 +2,6 @@ package com.fasterxml.jackson.databind.ser; import java.util.*; - import com.fasterxml.jackson.databind.*; /** @@ -30,6 +29,14 @@ public class CyclicTypeSerTest public void assignNext(Bean n) { _next = n; } } + static class Selfie2501 { + public int id; + + public Selfie2501 parent; + + public Selfie2501(int id) { this.id = id; } + } + /* /********************************************************** /* Types @@ -38,7 +45,7 @@ public class CyclicTypeSerTest private final ObjectMapper MAPPER = newJsonMapper(); - public void testLinked() throws Exception + public void testLinkedButNotCyclic() throws Exception { Bean last = new Bean(null, "last"); Bean first = new Bean(last, "first"); @@ -55,10 +62,7 @@ public class CyclicTypeSerTest assertNull(map2.get("next")); } - /** - * Test for verifying that [JACKSON-158] works as expected - */ - public void testSelfReference() throws Exception + public void testSimpleDirectSelfReference() throws Exception { Bean selfRef = new Bean(null, "self-refs"); Bean first = new Bean(selfRef, "first"); @@ -70,4 +74,19 @@ public class CyclicTypeSerTest verifyException(e, "Direct self-reference leading to cycle"); } } + + // [databind#2501]: Should be possible to replace null cyclic ref + public void testReplacedCycle() throws Exception + { + Selfie2501 self1 = new Selfie2501(1); + self1.parent = self1; + + ObjectWriter w = MAPPER.writer() + .without(SerializationFeature.FAIL_ON_SELF_REFERENCES) + .with(SerializationFeature.WRITE_SELF_REFERENCES_AS_NULL) + ; + final String json = w.writeValueAsString(self1); + assertNotNull(json); + assertEquals(aposToQuotes("{'id':1,'parent':null}"), json); + } } -- cgit v1.2.3