File tree Expand file tree Collapse file tree
src/test/java/tools/jackson/dataformat/xml/ser Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments