1
1
/*
2
- * Copyright 2002-2017 the original author or authors.
2
+ * Copyright 2002-2018 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
26
26
27
27
import com .google .gson .reflect .TypeToken ;
28
28
import org .junit .Test ;
29
+ import org .skyscreamer .jsonassert .JSONAssert ;
29
30
30
31
import org .springframework .core .ParameterizedTypeReference ;
31
32
import org .springframework .http .MediaType ;
39
40
* Gson 2.x converter tests.
40
41
*
41
42
* @author Roy Clarkson
43
+ * @author Juergen Hoeller
42
44
*/
43
45
public class GsonHttpMessageConverterTests {
44
46
45
47
private static final Charset UTF8 = Charset .forName ("UTF-8" );
46
48
47
- private GsonHttpMessageConverter converter = new GsonHttpMessageConverter ();
49
+ private final GsonHttpMessageConverter converter = new GsonHttpMessageConverter ();
48
50
49
51
50
52
@ Test
@@ -76,9 +78,9 @@ public void readTyped() throws IOException {
76
78
assertEquals ("Foo" , result .getString ());
77
79
assertEquals (42 , result .getNumber ());
78
80
assertEquals (42F , result .getFraction (), 0F );
79
- assertArrayEquals (new String []{"Foo" , "Bar" }, result .getArray ());
81
+ assertArrayEquals (new String [] {"Foo" , "Bar" }, result .getArray ());
80
82
assertTrue (result .isBool ());
81
- assertArrayEquals (new byte []{0x1 , 0x2 }, result .getBytes ());
83
+ assertArrayEquals (new byte [] {0x1 , 0x2 }, result .getBytes ());
82
84
}
83
85
84
86
@ Test
@@ -104,7 +106,7 @@ public void readUntyped() throws IOException {
104
106
for (int i = 0 ; i < 2 ; i ++) {
105
107
bytes [i ] = resultBytes .get (i ).byteValue ();
106
108
}
107
- assertArrayEquals (new byte []{0x1 , 0x2 }, bytes );
109
+ assertArrayEquals (new byte [] {0x1 , 0x2 }, bytes );
108
110
}
109
111
110
112
@ Test
@@ -114,9 +116,9 @@ public void write() throws IOException {
114
116
body .setString ("Foo" );
115
117
body .setNumber (42 );
116
118
body .setFraction (42F );
117
- body .setArray (new String []{"Foo" , "Bar" });
119
+ body .setArray (new String [] {"Foo" , "Bar" });
118
120
body .setBool (true );
119
- body .setBytes (new byte []{0x1 , 0x2 });
121
+ body .setBytes (new byte [] {0x1 , 0x2 });
120
122
this .converter .write (body , null , outputMessage );
121
123
String result = outputMessage .getBodyAsString (UTF8 );
122
124
assertTrue (result .contains ("\" string\" :\" Foo\" " ));
@@ -129,6 +131,28 @@ public void write() throws IOException {
129
131
outputMessage .getHeaders ().getContentType ());
130
132
}
131
133
134
+ @ Test
135
+ public void writeWithBaseType () throws IOException {
136
+ MockHttpOutputMessage outputMessage = new MockHttpOutputMessage ();
137
+ MyBean body = new MyBean ();
138
+ body .setString ("Foo" );
139
+ body .setNumber (42 );
140
+ body .setFraction (42F );
141
+ body .setArray (new String [] {"Foo" , "Bar" });
142
+ body .setBool (true );
143
+ body .setBytes (new byte [] {0x1 , 0x2 });
144
+ this .converter .write (body , MyBase .class , null , outputMessage );
145
+ String result = outputMessage .getBodyAsString (UTF8 );
146
+ assertTrue (result .contains ("\" string\" :\" Foo\" " ));
147
+ assertTrue (result .contains ("\" number\" :42" ));
148
+ assertTrue (result .contains ("fraction\" :42.0" ));
149
+ assertTrue (result .contains ("\" array\" :[\" Foo\" ,\" Bar\" ]" ));
150
+ assertTrue (result .contains ("\" bool\" :true" ));
151
+ assertTrue (result .contains ("\" bytes\" :[1,2]" ));
152
+ assertEquals ("Invalid content-type" , new MediaType ("application" , "json" , UTF8 ),
153
+ outputMessage .getHeaders ().getContentType ());
154
+ }
155
+
132
156
@ Test
133
157
public void writeUTF16 () throws IOException {
134
158
Charset utf16 = Charset .forName ("UTF-16BE" );
@@ -174,14 +198,14 @@ protected TypeToken<?> getTypeToken(Type type) {
174
198
assertEquals ("Foo" , result .getString ());
175
199
assertEquals (42 , result .getNumber ());
176
200
assertEquals (42F , result .getFraction (), 0F );
177
- assertArrayEquals (new String [] { "Foo" , "Bar" }, result .getArray ());
201
+ assertArrayEquals (new String [] {"Foo" , "Bar" }, result .getArray ());
178
202
assertTrue (result .isBool ());
179
- assertArrayEquals (new byte [] { 0x1 , 0x2 }, result .getBytes ());
203
+ assertArrayEquals (new byte [] {0x1 , 0x2 }, result .getBytes ());
180
204
}
181
205
182
206
@ Test
183
207
@ SuppressWarnings ("unchecked" )
184
- public void readParameterizedType () throws IOException {
208
+ public void readAndWriteParameterizedType () throws Exception {
185
209
ParameterizedTypeReference <List <MyBean >> beansList = new ParameterizedTypeReference <List <MyBean >>() {
186
210
};
187
211
@@ -190,39 +214,80 @@ public void readParameterizedType() throws IOException {
190
214
MockHttpInputMessage inputMessage = new MockHttpInputMessage (body .getBytes (UTF8 ));
191
215
inputMessage .getHeaders ().setContentType (new MediaType ("application" , "json" ));
192
216
193
- GsonHttpMessageConverter converter = new GsonHttpMessageConverter ();
194
217
List <MyBean > results = (List <MyBean >) converter .read (beansList .getType (), null , inputMessage );
195
218
assertEquals (1 , results .size ());
196
219
MyBean result = results .get (0 );
197
220
assertEquals ("Foo" , result .getString ());
198
221
assertEquals (42 , result .getNumber ());
199
222
assertEquals (42F , result .getFraction (), 0F );
200
- assertArrayEquals (new String [] { "Foo" , "Bar" }, result .getArray ());
223
+ assertArrayEquals (new String [] {"Foo" , "Bar" }, result .getArray ());
201
224
assertTrue (result .isBool ());
202
- assertArrayEquals (new byte [] { 0x1 , 0x2 }, result .getBytes ());
225
+ assertArrayEquals (new byte [] {0x1 , 0x2 }, result .getBytes ());
226
+
227
+ MockHttpOutputMessage outputMessage = new MockHttpOutputMessage ();
228
+ converter .write (results , beansList .getType (), new MediaType ("application" , "json" ), outputMessage );
229
+ JSONAssert .assertEquals (body , outputMessage .getBodyAsString (UTF8 ), true );
203
230
}
204
231
205
232
@ Test
206
- public void prefixJson () throws Exception {
233
+ @ SuppressWarnings ("unchecked" )
234
+ public void writeParameterizedBaseType () throws Exception {
235
+ ParameterizedTypeReference <List <MyBean >> beansList = new ParameterizedTypeReference <List <MyBean >>() {};
236
+ ParameterizedTypeReference <List <MyBase >> baseList = new ParameterizedTypeReference <List <MyBase >>() {};
237
+
238
+ String body = "[{\" bytes\" :[1,2],\" array\" :[\" Foo\" ,\" Bar\" ]," +
239
+ "\" number\" :42,\" string\" :\" Foo\" ,\" bool\" :true,\" fraction\" :42.0}]" ;
240
+ MockHttpInputMessage inputMessage = new MockHttpInputMessage (body .getBytes (UTF8 ));
241
+ inputMessage .getHeaders ().setContentType (new MediaType ("application" , "json" ));
242
+
243
+ List <MyBean > results = (List <MyBean >) converter .read (beansList .getType (), null , inputMessage );
244
+ assertEquals (1 , results .size ());
245
+ MyBean result = results .get (0 );
246
+ assertEquals ("Foo" , result .getString ());
247
+ assertEquals (42 , result .getNumber ());
248
+ assertEquals (42F , result .getFraction (), 0F );
249
+ assertArrayEquals (new String [] {"Foo" , "Bar" }, result .getArray ());
250
+ assertTrue (result .isBool ());
251
+ assertArrayEquals (new byte [] {0x1 , 0x2 }, result .getBytes ());
252
+
253
+ MockHttpOutputMessage outputMessage = new MockHttpOutputMessage ();
254
+ converter .write (results , baseList .getType (), new MediaType ("application" , "json" ), outputMessage );
255
+ JSONAssert .assertEquals (body , outputMessage .getBodyAsString (UTF8 ), true );
256
+ }
257
+
258
+ @ Test
259
+ public void prefixJson () throws IOException {
207
260
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage ();
208
261
this .converter .setPrefixJson (true );
209
262
this .converter .writeInternal ("foo" , null , outputMessage );
210
263
assertEquals (")]}', \" foo\" " , outputMessage .getBodyAsString (UTF8 ));
211
264
}
212
265
213
266
@ Test
214
- public void prefixJsonCustom () throws Exception {
267
+ public void prefixJsonCustom () throws IOException {
215
268
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage ();
216
269
this .converter .setJsonPrefix (")))" );
217
270
this .converter .writeInternal ("foo" , null , outputMessage );
218
271
assertEquals (")))\" foo\" " , outputMessage .getBodyAsString (UTF8 ));
219
272
}
220
273
221
274
222
- public static class MyBean {
275
+ public static class MyBase {
223
276
224
277
private String string ;
225
278
279
+ public String getString () {
280
+ return string ;
281
+ }
282
+
283
+ public void setString (String string ) {
284
+ this .string = string ;
285
+ }
286
+ }
287
+
288
+
289
+ public static class MyBean extends MyBase {
290
+
226
291
private int number ;
227
292
228
293
private float fraction ;
@@ -233,30 +298,6 @@ public static class MyBean {
233
298
234
299
private byte [] bytes ;
235
300
236
- public byte [] getBytes () {
237
- return bytes ;
238
- }
239
-
240
- public void setBytes (byte [] bytes ) {
241
- this .bytes = bytes ;
242
- }
243
-
244
- public boolean isBool () {
245
- return bool ;
246
- }
247
-
248
- public void setBool (boolean bool ) {
249
- this .bool = bool ;
250
- }
251
-
252
- public String getString () {
253
- return string ;
254
- }
255
-
256
- public void setString (String string ) {
257
- this .string = string ;
258
- }
259
-
260
301
public int getNumber () {
261
302
return number ;
262
303
}
@@ -280,6 +321,32 @@ public String[] getArray() {
280
321
public void setArray (String [] array ) {
281
322
this .array = array ;
282
323
}
324
+
325
+ public boolean isBool () {
326
+ return bool ;
327
+ }
328
+
329
+ public void setBool (boolean bool ) {
330
+ this .bool = bool ;
331
+ }
332
+
333
+ public byte [] getBytes () {
334
+ return bytes ;
335
+ }
336
+
337
+ public void setBytes (byte [] bytes ) {
338
+ this .bytes = bytes ;
339
+ }
340
+ }
341
+
342
+
343
+ public static class ListHolder <E > {
344
+
345
+ public List <E > listField ;
346
+ }
347
+
348
+
349
+ public static class MyBeanListHolder extends ListHolder <MyBean > {
283
350
}
284
351
285
352
}
0 commit comments