Skip to content

Commit ae7e650

Browse files
committed
Create GeneratorInitializer315Test.java
1 parent 4dfd6cd commit ae7e650

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package tools.jackson.dataformat.xml.ser;
2+
3+
import javax.xml.stream.XMLStreamException;
4+
import javax.xml.stream.XMLStreamWriter;
5+
6+
import tools.jackson.databind.cfg.GeneratorInitializer;
7+
import tools.jackson.dataformat.xml.XmlMapper;
8+
import tools.jackson.dataformat.xml.XmlTestUtil;
9+
10+
import org.junit.jupiter.api.Test;
11+
12+
import static org.junit.jupiter.api.Assertions.*;
13+
14+
// [dataformat-xml#315]: allow custom encoding in XML declaration via GeneratorInitializer
15+
public class GeneratorInitializer315Test extends XmlTestUtil
16+
{
17+
@Test
18+
public void testCustomEncodingViaGeneratorInitializer() throws Exception {
19+
GeneratorInitializer initializer = (config, gen) -> {
20+
ToXmlGenerator xmlGen = (ToXmlGenerator) gen;
21+
XMLStreamWriter sw = xmlGen.getStaxWriter();
22+
try {
23+
sw.writeStartDocument("ISO-8859-1", "1.0");
24+
} catch (XMLStreamException e) {
25+
throw new RuntimeException("Failed to write StartDocument event", e);
26+
}
27+
};
28+
XmlMapper mapper = XmlMapper.builder()
29+
.generatorInitializer(initializer)
30+
.build();
31+
String xml = mapper.writeValueAsString(new StringBean("test"));
32+
assertTrue(xml.contains("encoding='ISO-8859-1'") || xml.contains("encoding=\"ISO-8859-1\""),
33+
"Expected ISO-8859-1 encoding in XML declaration, got: " + xml);
34+
}
35+
}

0 commit comments

Comments
 (0)