Skip to content

Commit ce61138

Browse files
committed
Add a reproduction of #282
1 parent f5b9381 commit ce61138

File tree

3 files changed

+130
-65
lines changed

3 files changed

+130
-65
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
}

src/test/java/com/fasterxml/jackson/dataformat/xml/VersionInfoTest.java

-65
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
package com.fasterxml.jackson.dataformat.xml;
22

3-
import java.io.*;
4-
53
import com.fasterxml.jackson.core.Version;
64
import com.fasterxml.jackson.core.Versioned;
7-
import com.fasterxml.jackson.databind.SerializationConfig;
8-
import com.fasterxml.jackson.databind.SerializationFeature;
9-
import com.fasterxml.jackson.databind.ser.DefaultSerializerProvider;
10-
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
11-
import com.fasterxml.jackson.dataformat.xml.ser.XmlSerializerProvider;
12-
import com.fasterxml.jackson.dataformat.xml.util.XmlRootNameLookup;
135

146
public class VersionInfoTest extends XmlTestBase
157
{
@@ -18,63 +10,6 @@ public void testMapperVersions()
1810
assertVersion(new XmlMapper());
1911
assertVersion(new XmlFactory());
2012
}
21-
22-
// @since 2.1
23-
// [Issue#48]: ObjectMapper.copy()
24-
public void testMapperCopy()
25-
{
26-
XmlMapper mapper1 = new XmlMapper();
27-
mapper1.setXMLTextElementName("foo");
28-
mapper1.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
29-
mapper1.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
30-
31-
XmlMapper mapper2 = mapper1.copy();
32-
assertNotSame(mapper1, mapper2);
33-
XmlFactory xf1 = mapper1.getFactory();
34-
XmlFactory xf2 = mapper2.getFactory();
35-
assertNotSame(xf1, xf2);
36-
assertEquals(XmlFactory.class, xf2.getClass());
37-
38-
// and [Issue#48] as well, incomplete copy...
39-
assertEquals(xf1.getXMLTextElementName(), xf2.getXMLTextElementName());
40-
assertEquals(xf1._xmlGeneratorFeatures, xf2._xmlGeneratorFeatures);
41-
assertEquals(xf1._xmlParserFeatures, xf2._xmlParserFeatures);
42-
43-
// and [Issue#233]
44-
SerializationConfig sc1 = mapper1.getSerializationConfig();
45-
SerializationConfig sc2 = mapper2.getSerializationConfig();
46-
assertNotSame(sc1, sc2);
47-
assertEquals(
48-
"serialization features did not get copied",
49-
sc1.getSerializationFeatures(),
50-
sc2.getSerializationFeatures()
51-
);
52-
}
53-
54-
public void testSerializerProviderCopy() {
55-
DefaultSerializerProvider provider = new XmlSerializerProvider(new XmlRootNameLookup());
56-
DefaultSerializerProvider copy = provider.copy();
57-
assertNotSame(provider, copy);
58-
}
59-
60-
// Another test for [Issue#48]
61-
public void testMapperSerialization() throws Exception
62-
{
63-
XmlMapper mapper1 = new XmlMapper();
64-
mapper1.setXMLTextElementName("foo");
65-
assertEquals("foo", mapper1.getFactory().getXMLTextElementName());
66-
67-
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
68-
ObjectOutputStream objectStream = new ObjectOutputStream(bytes);
69-
objectStream.writeObject(mapper1);
70-
objectStream.close();
71-
72-
ObjectInputStream input = new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()));
73-
XmlMapper mapper2 = (XmlMapper) input.readObject();
74-
input.close();
75-
76-
assertEquals("foo", mapper2.getFactory().getXMLTextElementName());
77-
}
7813

7914
/*
8015
/**********************************************************
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fasterxml.jackson.dataformat.xml.failing;
2+
3+
import com.fasterxml.jackson.databind.*;
4+
5+
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
6+
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
7+
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
8+
9+
public class MapperCopyTest extends XmlTestBase
10+
{
11+
@JacksonXmlRootElement(localName = "AnnotatedName")
12+
static class Pojo282
13+
{
14+
public int a = 3;
15+
}
16+
17+
// [dataformat-xml#282]
18+
public void testCopyWith() throws Exception
19+
{
20+
XmlMapper xmlMapper = newMapper();
21+
final ObjectMapper xmlMapperNoAnno = xmlMapper.copy()
22+
.disable(MapperFeature.USE_ANNOTATIONS)
23+
.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
24+
25+
String xml1 = xmlMapper.writeValueAsString(new Pojo282());
26+
String xml2 = xmlMapperNoAnno.writeValueAsString(new Pojo282());
27+
28+
if (!xml1.contains("AnnotatedName")) {
29+
fail("Should use name 'AnnotatedName', xml = "+xml1);
30+
}
31+
if (!xml2.contains("Pojo282")
32+
|| xml2.contains("AnnotatedName")) {
33+
fail("Should NOT use name 'AnnotatedName' but 'Pojo282', xml = "+xml1);
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)