|
| 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