-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add cyclic data serialization tests (#4016)
- Loading branch information
Showing
2 changed files
with
48 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/test/java/com/fasterxml/jackson/databind/ser/dos/CyclicDataSerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.fasterxml.jackson.databind.ser.dos; | ||
|
||
import com.fasterxml.jackson.core.StreamWriteConstraints; | ||
import com.fasterxml.jackson.databind.BaseMapTest; | ||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException; | ||
import com.fasterxml.jackson.databind.ser.CyclicTypeSerTest; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Simple unit tests to verify that we fail gracefully if you attempt to serialize | ||
* data that is cyclic (eg a list that contains itself). | ||
*/ | ||
public class CyclicDataSerTest | ||
extends BaseMapTest | ||
{ | ||
private final ObjectMapper MAPPER = newJsonMapper(); | ||
|
||
public void testLinkedAndCyclic() throws Exception { | ||
CyclicTypeSerTest.Bean bean = new CyclicTypeSerTest.Bean(null, "last"); | ||
bean.assignNext(bean); | ||
try { | ||
writeAndMap(MAPPER, bean); | ||
fail("expected InvalidDefinitionException"); | ||
} catch (InvalidDefinitionException idex) { | ||
assertTrue("InvalidDefinitionException message is as expected?", | ||
idex.getMessage().startsWith("Direct self-reference leading to cycle")); | ||
} | ||
} | ||
|
||
public void testListWithSelfReference() throws Exception { | ||
List<Object> list = new ArrayList<>(); | ||
list.add(list); | ||
try { | ||
writeAndMap(MAPPER, list); | ||
fail("expected JsonMappingException"); | ||
} catch (JsonMappingException jmex) { | ||
String exceptionPrefix = String.format("Document nesting depth (%d) exceeds the maximum allowed", | ||
StreamWriteConstraints.DEFAULT_MAX_DEPTH + 1); | ||
assertTrue("JsonMappingException message is as expected?", | ||
jmex.getMessage().startsWith(exceptionPrefix)); | ||
} | ||
} | ||
} |