Skip to content

Commit 1320f00

Browse files
committed
Minor cleanup for test
1 parent a9c90e3 commit 1320f00

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

src/main/java/com/fasterxml/jackson/dataformat/xml/ser/ToXmlGenerator.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ private Feature(boolean defaultState) {
141141

142142
protected final IOContext _ioContext;
143143

144+
/**
145+
* @since 2.16
146+
*/
147+
protected final StreamWriteConstraints _streamWriteConstraints;
148+
144149
/**
145150
* Bit flag composed of bits that indicate which
146151
* {@link ToXmlGenerator.Feature}s
@@ -225,6 +230,7 @@ public ToXmlGenerator(IOContext ctxt, int stdFeatures, int xmlFeatures,
225230
super(stdFeatures, codec);
226231
_formatFeatures = xmlFeatures;
227232
_ioContext = ctxt;
233+
_streamWriteConstraints = ctxt.streamWriteConstraints();
228234
_originalXmlWriter = sw;
229235
_xmlWriter = Stax2WriterAdapter.wrapIfNecessary(sw);
230236
_stax2Emulation = (_xmlWriter != sw);
@@ -323,7 +329,7 @@ public JsonGenerator overrideFormatFeatures(int values, int mask)
323329

324330
@Override
325331
public StreamWriteConstraints streamWriteConstraints() {
326-
return _ioContext.streamWriteConstraints();
332+
return _streamWriteConstraints;
327333
}
328334

329335
public ToXmlGenerator enable(Feature f) {
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,37 @@
11
package com.fasterxml.jackson.dataformat.xml.ser.dos;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
36
import com.fasterxml.jackson.core.StreamWriteConstraints;
4-
import com.fasterxml.jackson.databind.JsonMappingException;
7+
8+
import com.fasterxml.jackson.databind.DatabindException;
9+
510
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
611
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
712

8-
import java.util.ArrayList;
9-
import java.util.List;
10-
1113
/**
1214
* Simple unit tests to verify that we fail gracefully if you attempt to serialize
1315
* data that is cyclic (eg a list that contains itself).
1416
*/
15-
public class CyclicDataSerTest extends XmlTestBase
17+
public class CyclicXMLDataSerTest extends XmlTestBase
1618
{
1719
private final XmlMapper MAPPER = newMapper();
1820

1921
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);
2227
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) {
2631
String exceptionPrefix = String.format("Document nesting depth (%d) exceeds the maximum allowed",
2732
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));
3035
}
3136
}
3237
}

0 commit comments

Comments
 (0)