2
2
3
3
import com .fasterxml .jackson .annotation .JsonSubTypes ;
4
4
import com .fasterxml .jackson .annotation .JsonTypeInfo ;
5
+
5
6
import com .fasterxml .jackson .databind .BaseMapTest ;
6
7
import com .fasterxml .jackson .databind .ObjectMapper ;
7
8
import com .fasterxml .jackson .databind .exc .UnrecognizedPropertyException ;
8
- import org .junit .Assert ;
9
9
10
10
import java .util .List ;
11
11
12
-
13
- /**
14
- * Tests for sub-types id user uses,
15
- * 1. only names in all sub-types
16
- * 2. 'names' for multi-keys while 'name' for singular keys
17
- *
18
- * [ annotations#171 ]
19
- * As per this feature, keys holding similar java object types can be incorporated
20
- * within the same annotation.
21
- * See this <a href="https://github.com/FasterXML/jackson-annotations/issues/171">issue/improvement</a>
22
- */
23
- public class TestMultipleTypeNames extends BaseMapTest {
24
-
25
- // serializer-deserializer
26
- private final ObjectMapper MAPPER = objectMapper ();
27
-
12
+ // Tests for [databind#2761] (and [annotations#171]
13
+ public class TestMultipleTypeNames extends BaseMapTest
14
+ {
15
+ private final ObjectMapper MAPPER = newJsonMapper ();
28
16
29
17
// common classes
30
18
static class MultiTypeName { }
@@ -39,10 +27,6 @@ static class B extends MultiTypeName {
39
27
public float getY () { return y ; }
40
28
}
41
29
42
-
43
-
44
-
45
-
46
30
// data for test 1
47
31
static class WrapperForNamesTest {
48
32
private List <BaseForNamesTest > base ;
@@ -53,49 +37,19 @@ static class BaseForNamesTest {
53
37
private String type ;
54
38
public String getType () { return type ; }
55
39
56
- @ JsonTypeInfo (
40
+ @ JsonTypeInfo (
57
41
use = JsonTypeInfo .Id .NAME ,
58
42
include = JsonTypeInfo .As .EXTERNAL_PROPERTY ,
59
43
property = "type"
60
44
)
61
- @ JsonSubTypes (value = {
45
+ @ JsonSubTypes (value = {
62
46
@ JsonSubTypes .Type (value = A .class , names = "a" ),
63
47
@ JsonSubTypes .Type (value = B .class , names = {"b" ,"c" }),
64
48
})
65
49
private MultiTypeName data ;
66
50
public MultiTypeName getData () { return data ; }
67
51
}
68
52
69
- public void testOnlyNames () throws Exception {
70
- String json ;
71
- WrapperForNamesTest w ;
72
-
73
- // TC 1 : all KV serialisation
74
- json = "{\" base\" : [{\" type\" :\" a\" , \" data\" : {\" x\" : 5}}, {\" type\" :\" b\" , \" data\" : {\" y\" : 3.1}}, {\" type\" :\" c\" , \" data\" : {\" y\" : 33.8}}]}" ;
75
- w = MAPPER .readValue (json , WrapperForNamesTest .class );
76
- Assert .assertNotNull (w );
77
- Assert .assertEquals (3 , w .base .size ());
78
- Assert .assertTrue (w .base .get (0 ).data instanceof A );
79
- Assert .assertEquals (5l , ((A ) w .base .get (0 ).data ).x );
80
- Assert .assertTrue (w .base .get (1 ).data instanceof B );
81
- Assert .assertEquals (3.1f , ((B ) w .base .get (1 ).data ).y , 0 );
82
- Assert .assertTrue (w .base .get (2 ).data instanceof B );
83
- Assert .assertEquals (33.8f , ((B ) w .base .get (2 ).data ).y , 0 );
84
-
85
-
86
- // TC 2 : incorrect serialisation
87
- json = "{\" data\" : [{\" type\" :\" a\" , \" data\" : {\" x\" : 2.2}}, {\" type\" :\" b\" , \" data\" : {\" y\" : 5.3}}, {\" type\" :\" c\" , \" data\" : {\" y\" : 9.8}}]}" ;
88
- try {
89
- MAPPER .readValue (json , WrapperForNamesTest .class );
90
- Assert .fail ("This serialisation should fail 'coz of x being float" );
91
- } catch (UnrecognizedPropertyException e ) {}
92
- }
93
-
94
-
95
-
96
-
97
-
98
- // data for test 2
99
53
static class WrapperForNameAndNamesTest {
100
54
private List <BaseForNameAndNamesTest > base ;
101
55
public List <BaseForNameAndNamesTest > getBase () { return base ; }
@@ -105,42 +59,78 @@ static class BaseForNameAndNamesTest {
105
59
private String type ;
106
60
public String getType () { return type ; }
107
61
108
- @ JsonTypeInfo (
62
+ @ JsonTypeInfo (
109
63
use = JsonTypeInfo .Id .NAME ,
110
64
include = JsonTypeInfo .As .EXTERNAL_PROPERTY ,
111
65
property = "type"
112
66
)
113
- @ JsonSubTypes (value = {
67
+ @ JsonSubTypes (value = {
114
68
@ JsonSubTypes .Type (value = A .class , name = "a" ),
115
69
@ JsonSubTypes .Type (value = B .class , names = {"b" ,"c" }),
116
70
})
117
71
private MultiTypeName data ;
118
72
public MultiTypeName getData () { return data ; }
119
73
}
120
74
121
- public void testNameAndNames () throws Exception {
75
+ /*
76
+ /**********************************************************
77
+ /* Test methods
78
+ /**********************************************************
79
+ */
80
+
81
+ public void testOnlyNames () throws Exception
82
+ {
83
+ String json ;
84
+ WrapperForNamesTest w ;
85
+
86
+ // TC 1 : all KV serialisation
87
+ json = "{\" base\" : [{\" type\" :\" a\" , \" data\" : {\" x\" : 5}}, {\" type\" :\" b\" , \" data\" : {\" y\" : 3.1}}, {\" type\" :\" c\" , \" data\" : {\" y\" : 33.8}}]}" ;
88
+ w = MAPPER .readValue (json , WrapperForNamesTest .class );
89
+ assertNotNull (w );
90
+ assertEquals (3 , w .base .size ());
91
+ assertTrue (w .base .get (0 ).data instanceof A );
92
+ assertEquals (5l , ((A ) w .base .get (0 ).data ).x );
93
+ assertTrue (w .base .get (1 ).data instanceof B );
94
+ assertEquals (3.1f , ((B ) w .base .get (1 ).data ).y , 0 );
95
+ assertTrue (w .base .get (2 ).data instanceof B );
96
+ assertEquals (33.8f , ((B ) w .base .get (2 ).data ).y , 0 );
97
+
98
+
99
+ // TC 2 : incorrect serialisation
100
+ json = "{\" data\" : [{\" type\" :\" a\" , \" data\" : {\" x\" : 2.2}}, {\" type\" :\" b\" , \" data\" : {\" y\" : 5.3}}, {\" type\" :\" c\" , \" data\" : {\" y\" : 9.8}}]}" ;
101
+ try {
102
+ MAPPER .readValue (json , WrapperForNamesTest .class );
103
+ fail ("This serialisation should fail 'coz of x being float" );
104
+ } catch (UnrecognizedPropertyException e ) {
105
+ verifyException (e , "Unrecognized field \" data\" " );
106
+ }
107
+ }
108
+
109
+ public void testNameAndNames () throws Exception
110
+ {
122
111
String json ;
123
112
WrapperForNameAndNamesTest w ;
124
113
125
114
// TC 1 : all KV serialisation
126
115
json = "{\" base\" : [{\" type\" :\" a\" , \" data\" : {\" x\" : 5}}, {\" type\" :\" b\" , \" data\" : {\" y\" : 3.1}}, {\" type\" :\" c\" , \" data\" : {\" y\" : 33.8}}]}" ;
127
116
w = MAPPER .readValue (json , WrapperForNameAndNamesTest .class );
128
- Assert . assertNotNull (w );
129
- Assert . assertEquals (3 , w .base .size ());
130
- Assert . assertTrue (w .base .get (0 ).data instanceof A );
131
- Assert . assertEquals (5l , ((A ) w .base .get (0 ).data ).x );
132
- Assert . assertTrue (w .base .get (1 ).data instanceof B );
133
- Assert . assertEquals (3.1f , ((B ) w .base .get (1 ).data ).y , 0 );
134
- Assert . assertTrue (w .base .get (2 ).data instanceof B );
135
- Assert . assertEquals (33.8f , ((B ) w .base .get (2 ).data ).y , 0 );
117
+ assertNotNull (w );
118
+ assertEquals (3 , w .base .size ());
119
+ assertTrue (w .base .get (0 ).data instanceof A );
120
+ assertEquals (5l , ((A ) w .base .get (0 ).data ).x );
121
+ assertTrue (w .base .get (1 ).data instanceof B );
122
+ assertEquals (3.1f , ((B ) w .base .get (1 ).data ).y , 0 );
123
+ assertTrue (w .base .get (2 ).data instanceof B );
124
+ assertEquals (33.8f , ((B ) w .base .get (2 ).data ).y , 0 );
136
125
137
126
138
127
// TC 2 : incorrect serialisation
139
128
json = "{\" data\" : [{\" type\" :\" a\" , \" data\" : {\" x\" : 2.2}}, {\" type\" :\" b\" , \" data\" : {\" y\" : 5.3}}, {\" type\" :\" c\" , \" data\" : {\" y\" : 9.8}}]}" ;
140
129
try {
141
130
MAPPER .readValue (json , WrapperForNameAndNamesTest .class );
142
- Assert .fail ("This serialisation should fail 'coz of x being float" );
143
- } catch (UnrecognizedPropertyException e ) {}
131
+ fail ("This serialisation should fail 'coz of x being float" );
132
+ } catch (UnrecognizedPropertyException e ) {
133
+ verifyException (e , "Unrecognized field \" data\" " );
134
+ }
144
135
}
145
-
146
136
}
0 commit comments