1
1
package com .fasterxml .jackson .dataformat .xml .ser ;
2
2
3
- import java .io .*;
4
3
import java .util .*;
5
4
6
5
import com .fasterxml .jackson .annotation .JsonProperty ;
6
+
7
7
import com .fasterxml .jackson .dataformat .xml .XmlMapper ;
8
8
import com .fasterxml .jackson .dataformat .xml .XmlTestBase ;
9
9
import com .fasterxml .jackson .dataformat .xml .annotation .JacksonXmlCData ;
@@ -31,6 +31,22 @@ static class AttrAndElem
31
31
public int attr = 42 ;
32
32
}
33
33
34
+ static class Floats
35
+ {
36
+ public float elem ;
37
+
38
+ @ JacksonXmlProperty (isAttribute =true , localName ="attr" )
39
+ public float attr ;
40
+ }
41
+
42
+ static class Doubles
43
+ {
44
+ public double elem ;
45
+
46
+ @ JacksonXmlProperty (isAttribute =true , localName ="attr" )
47
+ public double attr ;
48
+ }
49
+
34
50
static class WrapperBean <T >
35
51
{
36
52
public T value ;
@@ -81,37 +97,37 @@ static class CustomMap extends LinkedHashMap<String, Integer> { }
81
97
82
98
private final XmlMapper _xmlMapper = new XmlMapper ();
83
99
84
- public void testSimpleAttribute () throws IOException
100
+ public void testSimpleAttribute () throws Exception
85
101
{
86
102
String xml = _xmlMapper .writeValueAsString (new AttributeBean ());
87
103
xml = removeSjsxpNamespace (xml );
88
104
assertEquals ("<AttributeBean attr=\" something\" />" , xml );
89
105
}
90
106
91
- public void testSimpleNsElem () throws IOException
107
+ public void testSimpleNsElem () throws Exception
92
108
{
93
109
String xml = _xmlMapper .writeValueAsString (new NsElemBean ());
94
110
xml = removeSjsxpNamespace (xml );
95
111
// here we assume woodstox automatic prefixes, not very robust but:
96
112
assertEquals ("<NsElemBean><wstxns1:text xmlns:wstxns1=\" http://foo\" >blah</wstxns1:text></NsElemBean>" , xml );
97
113
}
98
114
99
- public void testSimpleNsElemWithJsonProp () throws IOException
115
+ public void testSimpleNsElemWithJsonProp () throws Exception
100
116
{
101
117
String xml = _xmlMapper .writeValueAsString (new NsElemBean2 ());
102
118
xml = removeSjsxpNamespace (xml );
103
119
// here we assume woodstox automatic prefixes, not very robust but:
104
120
assertEquals ("<NsElemBean2><wstxns1:text xmlns:wstxns1=\" http://foo\" >blah</wstxns1:text></NsElemBean2>" , xml );
105
121
}
106
122
107
- public void testSimpleAttrAndElem () throws IOException
123
+ public void testSimpleAttrAndElem () throws Exception
108
124
{
109
125
String xml = _xmlMapper .writeValueAsString (new AttrAndElem ());
110
126
xml = removeSjsxpNamespace (xml );
111
127
assertEquals ("<AttrAndElem id=\" 42\" ><elem>whatever</elem></AttrAndElem>" , xml );
112
128
}
113
129
114
- public void testMap () throws IOException
130
+ public void testMap () throws Exception
115
131
{
116
132
// First, map in a general wrapper
117
133
LinkedHashMap <String ,Integer > map = new LinkedHashMap <String ,Integer >();
@@ -136,7 +152,7 @@ public void testMap() throws IOException
136
152
xml );
137
153
}
138
154
139
- public void testNakedMap () throws IOException
155
+ public void testNakedMap () throws Exception
140
156
{
141
157
CustomMap input = new CustomMap ();
142
158
input .put ("a" , 123 );
@@ -152,14 +168,14 @@ public void testNakedMap() throws IOException
152
168
assertEquals (Integer .valueOf (456 ), result .get ("b" ));
153
169
}
154
170
155
- public void testCDataString () throws IOException
171
+ public void testCDataString () throws Exception
156
172
{
157
173
String xml = _xmlMapper .writeValueAsString (new CDataStringBean ());
158
174
xml = removeSjsxpNamespace (xml );
159
175
assertEquals ("<CDataStringBean><value><![CDATA[<some<data\" ]]></value></CDataStringBean>" , xml );
160
176
}
161
177
162
- public void testCDataStringArray () throws IOException
178
+ public void testCDataStringArray () throws Exception
163
179
{
164
180
String xml = _xmlMapper .writeValueAsString (new CDataStringArrayBean ());
165
181
xml = removeSjsxpNamespace (xml );
@@ -175,4 +191,62 @@ public void testJAXB() throws Exception
175
191
System.out.println("JAXB -> "+sw);
176
192
}
177
193
*/
194
+
195
+ public void testFloatInfinity () throws Exception
196
+ {
197
+ Floats infinite = new Floats ();
198
+ infinite .attr = Float .POSITIVE_INFINITY ;
199
+ infinite .elem = Float .NEGATIVE_INFINITY ;
200
+
201
+ Floats finite = new Floats ();
202
+ finite .attr = 42.5f ;
203
+ finite .elem = 1337.875f ;
204
+
205
+ checkFloatInfinity (infinite , false , "<Floats attr=\" Infinity\" ><elem>-Infinity</elem></Floats>" );
206
+ checkFloatInfinity (finite , false , "<Floats attr=\" 42.5\" ><elem>1337.875</elem></Floats>" );
207
+ checkFloatInfinity (infinite , true , "<Floats attr=\" INF\" ><elem>-INF</elem></Floats>" );
208
+ checkFloatInfinity (finite , true , "<Floats attr=\" 42.5\" ><elem>1337.875</elem></Floats>" );
209
+ }
210
+
211
+ private void checkFloatInfinity (Floats original , boolean xmlSchemaConforming , String expectedXml ) throws Exception
212
+ {
213
+ _xmlMapper .configure (ToXmlGenerator .Feature .WRITE_XML_SCHEMA_CONFORMING_FLOATS , xmlSchemaConforming );
214
+
215
+ String xml = _xmlMapper .writeValueAsString (original );
216
+ xml = removeSjsxpNamespace (xml );
217
+ assertEquals (expectedXml , xml );
218
+
219
+ Floats deserialized = _xmlMapper .readValue (xml , Floats .class );
220
+ assertEquals (original .attr , deserialized .attr );
221
+ assertEquals (original .elem , deserialized .elem );
222
+ }
223
+
224
+ public void testDoubleInfinity () throws Exception
225
+ {
226
+ Doubles infinite = new Doubles ();
227
+ infinite .attr = Double .POSITIVE_INFINITY ;
228
+ infinite .elem = Double .NEGATIVE_INFINITY ;
229
+
230
+ Doubles finite = new Doubles ();
231
+ finite .attr = 42.5d ;
232
+ finite .elem = 1337.875d ;
233
+
234
+ checkDoubleInfinity (infinite , false , "<Doubles attr=\" Infinity\" ><elem>-Infinity</elem></Doubles>" );
235
+ checkDoubleInfinity (finite , false , "<Doubles attr=\" 42.5\" ><elem>1337.875</elem></Doubles>" );
236
+ checkDoubleInfinity (infinite , true , "<Doubles attr=\" INF\" ><elem>-INF</elem></Doubles>" );
237
+ checkDoubleInfinity (finite , true , "<Doubles attr=\" 42.5\" ><elem>1337.875</elem></Doubles>" );
238
+ }
239
+
240
+ private void checkDoubleInfinity (Doubles original , boolean xmlSchemaConforming , String expectedXml ) throws Exception
241
+ {
242
+ _xmlMapper .configure (ToXmlGenerator .Feature .WRITE_XML_SCHEMA_CONFORMING_FLOATS , xmlSchemaConforming );
243
+
244
+ String xml = _xmlMapper .writeValueAsString (original );
245
+ xml = removeSjsxpNamespace (xml );
246
+ assertEquals (expectedXml , xml );
247
+
248
+ Doubles deserialized = _xmlMapper .readValue (xml , Doubles .class );
249
+ assertEquals (original .attr , deserialized .attr );
250
+ assertEquals (original .elem , deserialized .elem );
251
+ }
178
252
}
0 commit comments