Skip to content

Commit 0d41028

Browse files
authored
Expand test wrt [databind#5860], improve XmlMapper support for it (#843)
1 parent be16e4e commit 0d41028

File tree

3 files changed

+78
-37
lines changed

3 files changed

+78
-37
lines changed

src/main/java/tools/jackson/dataformat/xml/XmlMapper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,6 @@ public boolean isEnabled(XmlWriteFeature f) {
475475
return _serializationConfig.hasFormatFeature(f);
476476
}
477477

478-
479478
/*
480479
/**********************************************************************
481480
/* XML-specific access
@@ -497,7 +496,8 @@ public FromXmlParser createParser(XMLStreamReader r) throws IOException {
497496
*/
498497
public ToXmlGenerator createGenerator(XMLStreamWriter w) throws IOException {
499498
SerializationContextExt prov = _serializationContext(serializationConfig());
500-
return tokenStreamFactory().createGenerator(prov, w);
499+
return (ToXmlGenerator) _initializeGenerator(
500+
tokenStreamFactory().createGenerator(prov, w));
501501
}
502502

503503
/**

src/test/java/tools/jackson/dataformat/xml/ser/GeneratorInitializer315Test.java

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package tools.jackson.dataformat.xml.ser;
2+
3+
import java.io.StringWriter;
4+
5+
import javax.xml.stream.XMLOutputFactory;
6+
import javax.xml.stream.XMLStreamException;
7+
import javax.xml.stream.XMLStreamWriter;
8+
9+
import tools.jackson.databind.cfg.GeneratorInitializer;
10+
import tools.jackson.dataformat.xml.XmlMapper;
11+
import tools.jackson.dataformat.xml.XmlTestUtil;
12+
13+
import org.junit.jupiter.api.Test;
14+
15+
import static org.junit.jupiter.api.Assertions.*;
16+
17+
// Test to ensure that XML module supports
18+
// [https://github.com/FasterXML/jackson-databind/issues/5860] (GeneratorInitializer
19+
// abstraction)
20+
public class GeneratorInitializerTest extends XmlTestUtil
21+
{
22+
private final GeneratorInitializer ISO_8859_INITIALIZER = (config, gen) -> {
23+
ToXmlGenerator xmlGen = (ToXmlGenerator) gen;
24+
XMLStreamWriter sw = xmlGen.getStaxWriter();
25+
try {
26+
sw.writeStartDocument("ISO-8859-1", "1.0");
27+
} catch (XMLStreamException e) {
28+
throw new RuntimeException("Failed to write StartDocument event", e);
29+
}
30+
};
31+
32+
// Test via writeValueAsString (goes through ObjectWriter)
33+
@Test
34+
public void testCustomEncodingViaGeneratorInitializer() throws Exception {
35+
XmlMapper mapper = XmlMapper.builder()
36+
.generatorInitializer(ISO_8859_INITIALIZER)
37+
.build();
38+
String xml = mapper.writeValueAsString(new StringBean("test"));
39+
assertTrue(xml.contains("encoding='ISO-8859-1'") || xml.contains("encoding=\"ISO-8859-1\""),
40+
"Expected ISO-8859-1 encoding in XML declaration, got: " + xml);
41+
}
42+
43+
// Test via writeValue(XMLStreamWriter, Object) — XML-specific path
44+
@Test
45+
public void testCustomEncodingViaWriteValueXMLStreamWriter() throws Exception {
46+
XmlMapper mapper = XmlMapper.builder()
47+
.generatorInitializer(ISO_8859_INITIALIZER)
48+
.build();
49+
StringWriter sw = new StringWriter();
50+
XMLStreamWriter xmlSW = XMLOutputFactory.newFactory().createXMLStreamWriter(sw);
51+
mapper.writeValue(xmlSW, new StringBean("test"));
52+
xmlSW.close();
53+
String xml = sw.toString();
54+
assertTrue(xml.contains("encoding='ISO-8859-1'") || xml.contains("encoding=\"ISO-8859-1\""),
55+
"Expected ISO-8859-1 encoding in XML declaration, got: " + xml);
56+
}
57+
58+
// Test via createGenerator(XMLStreamWriter) — direct generator creation
59+
@Test
60+
public void testCustomEncodingViaCreateGeneratorXMLStreamWriter() throws Exception {
61+
XmlMapper mapper = XmlMapper.builder()
62+
.generatorInitializer(ISO_8859_INITIALIZER)
63+
.build();
64+
StringWriter sw = new StringWriter();
65+
XMLStreamWriter xmlSW = XMLOutputFactory.newFactory().createXMLStreamWriter(sw);
66+
ToXmlGenerator gen = mapper.createGenerator(xmlSW);
67+
gen.setNextName(new javax.xml.namespace.QName("root"));
68+
gen.writeStartObject();
69+
gen.writeEndObject();
70+
gen.close();
71+
xmlSW.close();
72+
String xml = sw.toString();
73+
assertTrue(xml.contains("encoding='ISO-8859-1'") || xml.contains("encoding=\"ISO-8859-1\""),
74+
"Expected ISO-8859-1 encoding in XML declaration, got: " + xml);
75+
}
76+
}

0 commit comments

Comments
 (0)