|
1 | 1 | package tools.jackson.dataformat.xml.dos; |
2 | 2 |
|
3 | | -import java.util.ArrayList; |
4 | | -import java.util.List; |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
5 | 5 |
|
6 | 6 | import org.junit.jupiter.api.Test; |
7 | 7 |
|
|
17 | 17 |
|
18 | 18 | /** |
19 | 19 | * Simple unit tests to verify that we fail gracefully if you attempt to serialize |
20 | | - * data that is cyclic (eg a list that contains itself). |
| 20 | + * data that is cyclic (eg a map that contains itself). |
21 | 21 | */ |
22 | 22 | public class CyclicXMLDataSerTest extends XmlTestUtil |
23 | 23 | { |
24 | 24 | private final XmlMapper MAPPER = newMapper(); |
25 | 25 |
|
26 | 26 | @Test |
27 | | - public void testListWithSelfReference() throws Exception { |
28 | | - // Avoid direct loop as serializer might be able to catch |
29 | | - List<Object> list1 = new ArrayList<>(); |
30 | | - List<Object> list2 = new ArrayList<>(); |
31 | | - list1.add(list2); |
32 | | - list2.add(list1); |
| 27 | + public void testMapWithSelfReference() throws Exception { |
| 28 | + // Use Maps (rather than Lists) so the cycle exercises object-nesting |
| 29 | + // depth without tripping XmlWriteFeature.FAIL_ON_NESTED_ARRAYS. |
| 30 | + // Two distinct Maps avoid trivial self-loop detection. |
| 31 | + Map<String, Object> map1 = new HashMap<>(); |
| 32 | + Map<String, Object> map2 = new HashMap<>(); |
| 33 | + map1.put("ref", map2); |
| 34 | + map2.put("ref", map1); |
33 | 35 | try { |
34 | | - MAPPER.writeValueAsString(list1); |
| 36 | + MAPPER.writeValueAsString(map1); |
35 | 37 | fail("expected DatabindException for infinite recursion"); |
36 | 38 | } catch (DatabindException e) { |
37 | 39 | String exceptionPrefix = String.format("Document nesting depth (%d) exceeds the maximum allowed", |
38 | 40 | StreamWriteConstraints.DEFAULT_MAX_DEPTH + 1); |
39 | | - assertTrue(e.getMessage().startsWith(exceptionPrefix)); |
| 41 | + assertTrue(e.getMessage().startsWith(exceptionPrefix), |
| 42 | + "Unexpected message: " + e.getMessage()); |
40 | 43 | } |
41 | 44 | } |
42 | 45 | } |
0 commit comments