4
4
import javax .xml .namespace .QName ;
5
5
6
6
import com .fasterxml .jackson .databind .*;
7
+ import com .fasterxml .jackson .databind .testutil .NoCheckSubTypeValidator ;
7
8
import com .fasterxml .jackson .databind .type .TypeFactory ;
8
9
9
10
/**
@@ -17,24 +18,29 @@ public class MiscJavaXMLTypesReadWriteTest
17
18
{
18
19
private final ObjectMapper MAPPER = newJsonMapper ();
19
20
21
+ private final ObjectMapper POLY_MAPPER = jsonMapperBuilder ()
22
+ .activateDefaultTyping (NoCheckSubTypeValidator .instance ,
23
+ ObjectMapper .DefaultTyping .NON_FINAL )
24
+ .build ();
25
+
20
26
/*
21
- /**********************************************************
27
+ /**********************************************************************
22
28
/* Serializer tests
23
- /**********************************************************
29
+ /**********************************************************************
24
30
*/
25
31
26
32
public void testQNameSer () throws Exception
27
33
{
28
34
QName qn = new QName ("http://abc" , "tag" , "prefix" );
29
- assertEquals (q (qn .toString ()), serializeAsString (qn ));
35
+ assertEquals (q (qn .toString ()), MAPPER . writeValueAsString (qn ));
30
36
}
31
37
32
38
public void testDurationSer () throws Exception
33
39
{
34
40
DatatypeFactory dtf = DatatypeFactory .newInstance ();
35
41
// arbitrary value
36
42
Duration dur = dtf .newDurationDayTime (false , 15 , 19 , 58 , 1 );
37
- assertEquals (q (dur .toString ()), serializeAsString (dur ));
43
+ assertEquals (q (dur .toString ()), MAPPER . writeValueAsString (dur ));
38
44
}
39
45
40
46
public void testXMLGregorianCalendarSerAndDeser () throws Exception
@@ -76,13 +82,13 @@ private String removeZ(String dateStr) {
76
82
}
77
83
return dateStr ;
78
84
}
79
-
85
+
80
86
/*
81
- /**********************************************************
87
+ /**********************************************************************
82
88
/* Deserializer tests
83
- /**********************************************************
89
+ /**********************************************************************
84
90
*/
85
-
91
+
86
92
// First things first: must be able to load the deserializers...
87
93
public void testDeserializerLoading ()
88
94
{
@@ -101,7 +107,7 @@ public void testQNameDeser() throws Exception
101
107
qn , MAPPER .readValue (q (qstr ), QName .class ));
102
108
}
103
109
104
- public void testCalendarDeser () throws Exception
110
+ public void testXMLGregorianCalendarDeser () throws Exception
105
111
{
106
112
DatatypeFactory dtf = DatatypeFactory .newInstance ();
107
113
XMLGregorianCalendar cal = dtf .newXMLGregorianCalendar
@@ -120,4 +126,23 @@ public void testDurationDeser() throws Exception
120
126
assertEquals ("Should deserialize to equal Duration ('" +exp +"')" , dur ,
121
127
MAPPER .readValue (q (exp ), Duration .class ));
122
128
}
129
+
130
+ /*
131
+ /**********************************************************************
132
+ /* Polymorphic handling tests
133
+ /**********************************************************************
134
+ */
135
+
136
+
137
+ public void testPolymorphicXMLGregorianCalendar () throws Exception
138
+ {
139
+ XMLGregorianCalendar cal = DatatypeFactory .newInstance ().newXMLGregorianCalendar
140
+ (1974 , 10 , 10 , 18 , 15 , 17 , 123 , 0 );
141
+ String json = POLY_MAPPER .writeValueAsString (cal );
142
+ Object result = POLY_MAPPER .readValue (json , Object .class );
143
+ if (!(result instanceof XMLGregorianCalendar )) {
144
+ fail ("Expected a `XMLGregorianCalendar`, got: " +result .getClass ());
145
+ }
146
+ assertEquals (cal , result );
147
+ }
123
148
}
0 commit comments