33import java .util .ArrayList ;
44import java .util .List ;
55
6+ import com .fasterxml .jackson .annotation .JsonCreator ;
67import com .fasterxml .jackson .annotation .JsonIdentityInfo ;
8+ import com .fasterxml .jackson .annotation .JsonProperty ;
9+ import com .fasterxml .jackson .annotation .JsonSubTypes ;
710import com .fasterxml .jackson .annotation .JsonTypeInfo ;
811import com .fasterxml .jackson .annotation .ObjectIdGenerators ;
12+
913import com .fasterxml .jackson .dataformat .xml .XmlMapper ;
1014import com .fasterxml .jackson .dataformat .xml .XmlTestBase ;
1115
@@ -47,30 +51,35 @@ public Wrapper(){}
4751 public Wrapper (List <TypeWithClassPropertyAndObjectId > data ) { this .data = data ; }
4852 }
4953
50- /*
51- /**********************************************************
52- /* Set up
53- /**********************************************************
54- */
54+ // [dataformat-xml#451]
55+ @ JsonTypeInfo (use = JsonTypeInfo .Id .DEDUCTION )
56+ @ JsonSubTypes (@ JsonSubTypes .Type (Child451 .class ))
57+ public interface Value451 {}
5558
56- protected XmlMapper _xmlMapper ;
59+ public static class Child451 implements Value451 {
60+ private final String property1 ;
5761
58- // let's actually reuse XmlMapper to make things bit faster
59- @ Override
60- public void setUp () throws Exception {
61- super .setUp ();
62- _xmlMapper = new XmlMapper ();
63- }
62+ @ JsonCreator (mode = JsonCreator .Mode .PROPERTIES )
63+ public Child451 (@ JsonProperty ("property1" ) String property1 ) {
64+ this .property1 = property1 ;
65+ }
66+
67+ public String getProperty1 () {
68+ return property1 ;
69+ }
70+ }
6471
6572 /*
66- /**********************************************************
67- /* Unit tests
68- /**********************************************************
73+ /**********************************************************************
74+ /* Test methods
75+ /**********************************************************************
6976 */
7077
78+ private final XmlMapper MAPPER = newMapper ();
79+
7180 public void testAsClassProperty () throws Exception
7281 {
73- String xml = _xmlMapper .writeValueAsString (new SubTypeWithClassProperty ("Foobar" ));
82+ String xml = MAPPER .writeValueAsString (new SubTypeWithClassProperty ("Foobar" ));
7483
7584 // Type info should be written as an attribute, so:
7685 final String exp =
@@ -80,36 +89,46 @@ public void testAsClassProperty() throws Exception
8089 ;
8190 assertEquals (exp , xml );
8291
83- Object result = _xmlMapper .readValue (xml , BaseTypeWithClassProperty .class );
92+ Object result = MAPPER .readValue (xml , BaseTypeWithClassProperty .class );
8493 assertNotNull (result );
8594 assertEquals (SubTypeWithClassProperty .class , result .getClass ());
8695 assertEquals ("Foobar" , ((SubTypeWithClassProperty ) result ).name );
8796 }
8897
8998 public void testAsClassObject () throws Exception
9099 {
91- String xml = _xmlMapper .writeValueAsString (new SubTypeWithClassObject ("Foobar" ));
92- Object result = _xmlMapper .readValue (xml , BaseTypeWithClassObject .class );
100+ String xml = MAPPER .writeValueAsString (new SubTypeWithClassObject ("Foobar" ));
101+ Object result = MAPPER .readValue (xml , BaseTypeWithClassObject .class );
93102 assertNotNull (result );
94103 assertEquals (SubTypeWithClassObject .class , result .getClass ());
95104 assertEquals ("Foobar" , ((SubTypeWithClassObject ) result ).name );
96105 }
97106
98- /**
99- * Test for [dataformat-xml#81]
100- */
107+ // Test for [dataformat-xml#81]
101108 public void testAsPropertyWithObjectId () throws Exception
102109 {
103110 List <TypeWithClassPropertyAndObjectId > data = new ArrayList <PolymorphicTypesTest .TypeWithClassPropertyAndObjectId >();
104111 TypeWithClassPropertyAndObjectId object = new TypeWithClassPropertyAndObjectId ("Foobar" );
105112 data .add (object );
106113 // This will be written as an id reference instead of object; as such, no type info will be written.
107114 data .add (object );
108- String xml = _xmlMapper .writeValueAsString (new Wrapper (data ));
109- Wrapper result = _xmlMapper .readValue (xml , Wrapper .class );
115+ String xml = MAPPER .writeValueAsString (new Wrapper (data ));
116+ Wrapper result = MAPPER .readValue (xml , Wrapper .class );
110117 assertNotNull (result );
111118 assertSame (result .data .get (0 ), result .data .get (1 ));
112119 assertEquals ("Foobar" , result .data .get (0 ).id );
113120 }
121+
122+ // Test for [dataformat-xml#451]
123+ public void testDeduction () throws Exception
124+ {
125+ String xml = MAPPER .writeValueAsString (new Child451 ("value1" ));
126+ assertTrue (xml .contains ("<property1>value1</property1>" ));
127+
128+ // and try reading back for funsies
129+ Value451 result = MAPPER .readValue (xml , Value451 .class );
130+ assertNotNull (result );
131+ assertEquals (Child451 .class , result .getClass ());
132+ }
114133}
115134
0 commit comments