|
| 1 | +package com.fasterxml.jackson.dataformat.xml; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.databind.*; |
| 6 | +import com.fasterxml.jackson.databind.ser.DefaultSerializerProvider; |
| 7 | +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; |
| 8 | +import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator; |
| 9 | +import com.fasterxml.jackson.dataformat.xml.ser.XmlSerializerProvider; |
| 10 | +import com.fasterxml.jackson.dataformat.xml.util.XmlRootNameLookup; |
| 11 | + |
| 12 | +public class MapperCopyTest extends XmlTestBase |
| 13 | +{ |
| 14 | + @JacksonXmlRootElement(localName = "AnnotatedName") |
| 15 | + static class Pojo282 |
| 16 | + { |
| 17 | + public int a = 3; |
| 18 | + } |
| 19 | + |
| 20 | + public void testMapperCopy() |
| 21 | + { |
| 22 | + XmlMapper mapper1 = new XmlMapper(); |
| 23 | + mapper1.setXMLTextElementName("foo"); |
| 24 | + mapper1.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true); |
| 25 | + mapper1.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); |
| 26 | + |
| 27 | + XmlMapper mapper2 = mapper1.copy(); |
| 28 | + assertNotSame(mapper1, mapper2); |
| 29 | + XmlFactory xf1 = mapper1.getFactory(); |
| 30 | + XmlFactory xf2 = mapper2.getFactory(); |
| 31 | + assertNotSame(xf1, xf2); |
| 32 | + assertEquals(XmlFactory.class, xf2.getClass()); |
| 33 | + |
| 34 | + // and incomplete copy as well |
| 35 | + assertEquals(xf1.getXMLTextElementName(), xf2.getXMLTextElementName()); |
| 36 | + assertEquals(xf1._xmlGeneratorFeatures, xf2._xmlGeneratorFeatures); |
| 37 | + assertEquals(xf1._xmlParserFeatures, xf2._xmlParserFeatures); |
| 38 | + |
| 39 | + SerializationConfig sc1 = mapper1.getSerializationConfig(); |
| 40 | + SerializationConfig sc2 = mapper2.getSerializationConfig(); |
| 41 | + assertNotSame(sc1, sc2); |
| 42 | + assertEquals( |
| 43 | + "serialization features did not get copied", |
| 44 | + sc1.getSerializationFeatures(), |
| 45 | + sc2.getSerializationFeatures() |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + public void testSerializerProviderCopy() { |
| 50 | + DefaultSerializerProvider provider = new XmlSerializerProvider(new XmlRootNameLookup()); |
| 51 | + DefaultSerializerProvider copy = provider.copy(); |
| 52 | + assertNotSame(provider, copy); |
| 53 | + } |
| 54 | + |
| 55 | + public void testMapperSerialization() throws Exception |
| 56 | + { |
| 57 | + XmlMapper mapper1 = newMapper(); |
| 58 | + mapper1.setXMLTextElementName("foo"); |
| 59 | + assertEquals("foo", mapper1.getFactory().getXMLTextElementName()); |
| 60 | + |
| 61 | + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); |
| 62 | + ObjectOutputStream objectStream = new ObjectOutputStream(bytes); |
| 63 | + objectStream.writeObject(mapper1); |
| 64 | + objectStream.close(); |
| 65 | + |
| 66 | + ObjectInputStream input = new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray())); |
| 67 | + XmlMapper mapper2 = (XmlMapper) input.readObject(); |
| 68 | + input.close(); |
| 69 | + |
| 70 | + assertEquals("foo", mapper2.getFactory().getXMLTextElementName()); |
| 71 | + } |
| 72 | + |
| 73 | + /* |
| 74 | + // [dataformat-xml#282] |
| 75 | + public void testCopyWith() throws Exception |
| 76 | + { |
| 77 | + XmlMapper xmlMapper = newMapper(); |
| 78 | + final ObjectMapper xmlMapperNoAnno = xmlMapper.copy() |
| 79 | + .disable(MapperFeature.USE_ANNOTATIONS) |
| 80 | + .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); |
| 81 | +
|
| 82 | + String xml1 = xmlMapper.writeValueAsString(new Pojo282()); |
| 83 | + String xml2 = xmlMapperNoAnno.writeValueAsString(new Pojo282()); |
| 84 | +
|
| 85 | + if (!xml1.contains("AnnotatedName")) { |
| 86 | + fail("Should use name 'AnnotatedName', xml = "+xml1); |
| 87 | + } |
| 88 | + if (!xml2.contains("Pojo282") |
| 89 | + || xml2.contains("AnnotatedName")) { |
| 90 | + fail("Should NOT use name 'AnnotatedName' but 'Pojo282', xml = "+xml1); |
| 91 | + } |
| 92 | + } |
| 93 | + */ |
| 94 | +} |
0 commit comments