Skip to content

Commit ab652db

Browse files
committed
...
1 parent ab10c5c commit ab652db

2 files changed

Lines changed: 17 additions & 14 deletions

File tree

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package tools.jackson.dataformat.xml.dos;
22

3-
import java.util.ArrayList;
4-
import java.util.List;
3+
import java.util.HashMap;
4+
import java.util.Map;
55

66
import org.junit.jupiter.api.Test;
77

@@ -17,26 +17,29 @@
1717

1818
/**
1919
* 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).
2121
*/
2222
public class CyclicXMLDataSerTest extends XmlTestUtil
2323
{
2424
private final XmlMapper MAPPER = newMapper();
2525

2626
@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);
3335
try {
34-
MAPPER.writeValueAsString(list1);
36+
MAPPER.writeValueAsString(map1);
3537
fail("expected DatabindException for infinite recursion");
3638
} catch (DatabindException e) {
3739
String exceptionPrefix = String.format("Document nesting depth (%d) exceeds the maximum allowed",
3840
StreamWriteConstraints.DEFAULT_MAX_DEPTH + 1);
39-
assertTrue(e.getMessage().startsWith(exceptionPrefix));
41+
assertTrue(e.getMessage().startsWith(exceptionPrefix),
42+
"Unexpected message: " + e.getMessage());
4043
}
4144
}
4245
}

src/test/java/tools/jackson/dataformat/xml/tofix/MultidimArray556Test.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import org.junit.jupiter.api.Test;
77

8-
import tools.jackson.core.exc.StreamWriteException;
8+
import tools.jackson.databind.DatabindException;
99
import tools.jackson.dataformat.xml.XmlMapper;
1010
import tools.jackson.dataformat.xml.XmlTestUtil;
1111
import tools.jackson.dataformat.xml.XmlWriteFeature;
@@ -30,7 +30,7 @@ public void test2DPrimitiveArrayFailsFast() throws Exception
3030
try {
3131
MAPPER.writeValueAsString(new boolean[][] { { true }, { false } });
3232
fail("Should not pass: nested arrays must be rejected");
33-
} catch (StreamWriteException e) {
33+
} catch (DatabindException e) {
3434
verifyException(e, "does not support nested arrays");
3535
}
3636
}
@@ -44,7 +44,7 @@ public void testNestedListFailsFast() throws Exception
4444
try {
4545
MAPPER.writeValueAsString(nested);
4646
fail("Should not pass: nested Collections must be rejected");
47-
} catch (StreamWriteException e) {
47+
} catch (DatabindException e) {
4848
verifyException(e, "does not support nested arrays");
4949
}
5050
}

0 commit comments

Comments
 (0)