|
1 | 1 | package com.fasterxml.jackson.dataformat.xml.ser.dos; |
2 | 2 |
|
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
3 | 6 | import com.fasterxml.jackson.core.StreamWriteConstraints; |
4 | | -import com.fasterxml.jackson.databind.JsonMappingException; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.databind.DatabindException; |
| 9 | + |
5 | 10 | import com.fasterxml.jackson.dataformat.xml.XmlMapper; |
6 | 11 | import com.fasterxml.jackson.dataformat.xml.XmlTestBase; |
7 | 12 |
|
8 | | -import java.util.ArrayList; |
9 | | -import java.util.List; |
10 | | - |
11 | 13 | /** |
12 | 14 | * Simple unit tests to verify that we fail gracefully if you attempt to serialize |
13 | 15 | * data that is cyclic (eg a list that contains itself). |
14 | 16 | */ |
15 | | -public class CyclicDataSerTest extends XmlTestBase |
| 17 | +public class CyclicXMLDataSerTest extends XmlTestBase |
16 | 18 | { |
17 | 19 | private final XmlMapper MAPPER = newMapper(); |
18 | 20 |
|
19 | 21 | public void testListWithSelfReference() throws Exception { |
20 | | - List<Object> list = new ArrayList<>(); |
21 | | - list.add(list); |
| 22 | + // Avoid direct loop as serializer might be able to catch |
| 23 | + List<Object> list1 = new ArrayList<>(); |
| 24 | + List<Object> list2 = new ArrayList<>(); |
| 25 | + list1.add(list2); |
| 26 | + list2.add(list1); |
22 | 27 | try { |
23 | | - MAPPER.writeValueAsString(list); |
24 | | - fail("expected JsonMappingException"); |
25 | | - } catch (JsonMappingException jmex) { |
| 28 | + MAPPER.writeValueAsString(list1); |
| 29 | + fail("expected DatabindException for infinite recursion"); |
| 30 | + } catch (DatabindException e) { |
26 | 31 | String exceptionPrefix = String.format("Document nesting depth (%d) exceeds the maximum allowed", |
27 | 32 | StreamWriteConstraints.DEFAULT_MAX_DEPTH + 1); |
28 | | - assertTrue("JsonMappingException message is as expected?", |
29 | | - jmex.getMessage().startsWith(exceptionPrefix)); |
| 33 | + assertTrue("Exception message is as expected?", |
| 34 | + e.getMessage().startsWith(exceptionPrefix)); |
30 | 35 | } |
31 | 36 | } |
32 | 37 | } |
0 commit comments