|
| 1 | +package tools.jackson.dataformat.xml.ser; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import tools.jackson.databind.ObjectWriter; |
| 6 | +import tools.jackson.dataformat.xml.XmlMapper; |
| 7 | +import tools.jackson.dataformat.xml.XmlTestUtil; |
| 8 | +import tools.jackson.dataformat.xml.XmlWriteFeature; |
| 9 | + |
| 10 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 11 | +import static org.junit.jupiter.api.Assertions.fail; |
| 12 | + |
| 13 | +public class XmlGeneratorInitializerTest extends XmlTestUtil |
| 14 | +{ |
| 15 | + private final XmlMapper MAPPER = newMapper(); |
| 16 | + |
| 17 | + // [dataformat-xml#150]: DTD writing -- ok cases |
| 18 | + @Test |
| 19 | + public void testDTDWithOnlyRootElement() throws Exception |
| 20 | + { |
| 21 | + ObjectWriter w = MAPPER.writer().with( |
| 22 | + new XmlGeneratorInitializer() |
| 23 | + .setDTD("StringBean", null, null, null)); |
| 24 | + assertEquals(a2q("<!DOCTYPE StringBean>" |
| 25 | + +"<StringBean><text>test</text></StringBean>"), |
| 26 | + w.writeValueAsString(new StringBean("test"))); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void testDTDWithPublicId() throws Exception |
| 31 | + { |
| 32 | + ObjectWriter w = MAPPER.writer().with( |
| 33 | + new XmlGeneratorInitializer() |
| 34 | + .setDTD("StringBean", "system", "http://foo.bar", "")); |
| 35 | + assertEquals(a2q("<!DOCTYPE StringBean PUBLIC 'http://foo.bar' 'system'>" |
| 36 | + +"<StringBean><text>test</text></StringBean>"), |
| 37 | + w.writeValueAsString(new StringBean("test"))); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testDTDWithSystemIdOnly() throws Exception |
| 42 | + { |
| 43 | + ObjectWriter w = MAPPER.writer().with( |
| 44 | + new XmlGeneratorInitializer() |
| 45 | + .setDTD("StringBean", "system", "", null)); |
| 46 | + assertEquals(a2q("<!DOCTYPE StringBean SYSTEM 'system'>" |
| 47 | + +"<StringBean><text>test</text></StringBean>"), |
| 48 | + w.writeValueAsString(new StringBean("test"))); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testDTDWithInternalSubset() throws Exception |
| 53 | + { |
| 54 | + ObjectWriter w = MAPPER.writer().with( |
| 55 | + new XmlGeneratorInitializer() |
| 56 | + .setDTD("StringBean", "system", "http://foo.bar", "<!ELEMENT root (#PCDATA)>")); |
| 57 | + assertEquals(a2q("<!DOCTYPE StringBean PUBLIC 'http://foo.bar' 'system' " |
| 58 | + +"[<!ELEMENT root (#PCDATA)>]>" |
| 59 | + +"<StringBean><text>test</text></StringBean>"), |
| 60 | + w.writeValueAsString(new StringBean("test"))); |
| 61 | + } |
| 62 | + |
| 63 | + // Verify prolog ordering: XML declaration must come before DOCTYPE |
| 64 | + @Test |
| 65 | + public void testDTDWithXmlDeclaration() throws Exception |
| 66 | + { |
| 67 | + XmlMapper mapper = XmlMapper.builder() |
| 68 | + .configure(XmlWriteFeature.WRITE_XML_DECLARATION, true) |
| 69 | + .build(); |
| 70 | + ObjectWriter w = mapper.writer().with( |
| 71 | + new XmlGeneratorInitializer() |
| 72 | + .setDTD("StringBean", "system", "http://foo.bar", null)); |
| 73 | + // XML declaration is emitted with single quotes, DOCTYPE with double quotes, |
| 74 | + // so cannot use a2q() on the whole string here. |
| 75 | + assertEquals("<?xml version='1.0' encoding='UTF-8'?>" |
| 76 | + +"<!DOCTYPE StringBean PUBLIC \"http://foo.bar\" \"system\">" |
| 77 | + +"<StringBean><text>test</text></StringBean>", |
| 78 | + w.writeValueAsString(new StringBean("test"))); |
| 79 | + } |
| 80 | + |
| 81 | + // [dataformat-xml#150]: DTD writing -- failing cases |
| 82 | + @Test |
| 83 | + public void testDTDInvalidNoRoot() throws Exception |
| 84 | + { |
| 85 | + try { |
| 86 | + /*ObjectWriter w =*/ MAPPER.writer().with( |
| 87 | + new XmlGeneratorInitializer() |
| 88 | + .setDTD("", null, null, null)); |
| 89 | + fail("Should not pass"); |
| 90 | + } catch (IllegalArgumentException e) { |
| 91 | + verifyException(e, "Illegal argument for 'rootName': must be"); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // Other tests |
| 96 | +} |
0 commit comments