16
16
17
17
package org .springframework .http .converter .json ;
18
18
19
- import java .io .ByteArrayInputStream ;
20
19
import java .io .IOException ;
21
- import java .io .InputStream ;
22
20
import java .lang .reflect .Field ;
23
21
import java .lang .reflect .Type ;
24
22
import java .nio .charset .Charset ;
40
38
import static org .assertj .core .api .Assertions .assertThat ;
41
39
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
42
40
import static org .assertj .core .api .Assertions .within ;
43
- import static org .mockito .Mockito .never ;
44
- import static org .mockito .Mockito .spy ;
45
- import static org .mockito .Mockito .verify ;
46
41
47
42
/**
48
43
* Gson 2.x converter tests.
49
44
*
50
45
* @author Roy Clarkson
51
46
* @author Juergen Hoeller
52
47
*/
53
- public class GsonHttpMessageConverterTests {
48
+ class GsonHttpMessageConverterTests {
54
49
55
50
private final GsonHttpMessageConverter converter = new GsonHttpMessageConverter ();
56
51
57
52
58
53
@ Test
59
- public void canRead () {
54
+ void canRead () {
60
55
assertThat (this .converter .canRead (MyBean .class , new MediaType ("application" , "json" ))).isTrue ();
61
56
assertThat (this .converter .canRead (Map .class , new MediaType ("application" , "json" ))).isTrue ();
62
57
}
63
58
64
59
@ Test
65
- public void canWrite () {
60
+ void canWrite () {
66
61
assertThat (this .converter .canWrite (MyBean .class , new MediaType ("application" , "json" ))).isTrue ();
67
62
assertThat (this .converter .canWrite (Map .class , new MediaType ("application" , "json" ))).isTrue ();
68
63
}
69
64
70
65
@ Test
71
- public void canReadAndWriteMicroformats () {
66
+ void canReadAndWriteMicroformats () {
72
67
assertThat (this .converter .canRead (MyBean .class , new MediaType ("application" , "vnd.test-micro-type+json" ))).isTrue ();
73
68
assertThat (this .converter .canWrite (MyBean .class , new MediaType ("application" , "vnd.test-micro-type+json" ))).isTrue ();
74
69
}
75
70
76
71
@ Test
77
- public void readTyped () throws IOException {
72
+ void readTyped () throws IOException {
78
73
String body = "{\" bytes\" :[1,2],\" array\" :[\" Foo\" ,\" Bar\" ]," +
79
74
"\" number\" :42,\" string\" :\" Foo\" ,\" bool\" :true,\" fraction\" :42.0}" ;
80
- InputStream inputStream = spy (new ByteArrayInputStream (body .getBytes (StandardCharsets .UTF_8 )));
81
- MockHttpInputMessage inputMessage = new MockHttpInputMessage (inputStream );
75
+ MockHttpInputMessage inputMessage = new MockHttpInputMessage (body .getBytes (StandardCharsets .UTF_8 ));
82
76
inputMessage .getHeaders ().setContentType (new MediaType ("application" , "json" ));
83
77
MyBean result = (MyBean ) this .converter .read (MyBean .class , inputMessage );
84
78
@@ -89,12 +83,11 @@ public void readTyped() throws IOException {
89
83
assertThat (result .getArray ()).isEqualTo (new String [] {"Foo" , "Bar" });
90
84
assertThat (result .isBool ()).isTrue ();
91
85
assertThat (result .getBytes ()).isEqualTo (new byte [] {0x1 , 0x2 });
92
- verify (inputStream , never ()).close ();
93
86
}
94
87
95
88
@ Test
96
89
@ SuppressWarnings ("unchecked" )
97
- public void readUntyped () throws IOException {
90
+ void readUntyped () throws IOException {
98
91
String body = "{\" bytes\" :[1,2],\" array\" :[\" Foo\" ,\" Bar\" ]," +
99
92
"\" number\" :42,\" string\" :\" Foo\" ,\" bool\" :true,\" fraction\" :42.0}" ;
100
93
MockHttpInputMessage inputMessage = new MockHttpInputMessage (body .getBytes ("UTF-8" ));
@@ -120,7 +113,7 @@ public void readUntyped() throws IOException {
120
113
}
121
114
122
115
@ Test
123
- public void write () throws IOException {
116
+ void write () throws IOException {
124
117
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage ();
125
118
MyBean body = new MyBean ();
126
119
body .setString ("Foo" );
@@ -140,11 +133,10 @@ public void write() throws IOException {
140
133
assertThat (result .contains ("\" bytes\" :[1,2]" )).isTrue ();
141
134
assertThat (outputMessage .getHeaders ().getContentType ())
142
135
.as ("Invalid content-type" ).isEqualTo (new MediaType ("application" , "json" , utf8 ));
143
- verify (outputMessage .getBody (), never ()).close ();
144
136
}
145
137
146
138
@ Test
147
- public void writeWithBaseType () throws IOException {
139
+ void writeWithBaseType () throws IOException {
148
140
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage ();
149
141
MyBean body = new MyBean ();
150
142
body .setString ("Foo" );
@@ -167,7 +159,7 @@ public void writeWithBaseType() throws IOException {
167
159
}
168
160
169
161
@ Test
170
- public void writeUTF16 () throws IOException {
162
+ void writeUTF16 () throws IOException {
171
163
MediaType contentType = new MediaType ("application" , "json" , StandardCharsets .UTF_16BE );
172
164
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage ();
173
165
String body = "H\u00e9 llo W\u00f6 rld" ;
@@ -177,7 +169,7 @@ public void writeUTF16() throws IOException {
177
169
}
178
170
179
171
@ Test
180
- public void readInvalidJson () throws IOException {
172
+ void readInvalidJson () throws IOException {
181
173
String body = "FooBar" ;
182
174
MockHttpInputMessage inputMessage = new MockHttpInputMessage (body .getBytes (StandardCharsets .UTF_8 ));
183
175
inputMessage .getHeaders ().setContentType (new MediaType ("application" , "json" ));
@@ -187,7 +179,7 @@ public void readInvalidJson() throws IOException {
187
179
188
180
@ Test
189
181
@ SuppressWarnings ("unchecked" )
190
- public void readAndWriteGenerics () throws Exception {
182
+ void readAndWriteGenerics () throws Exception {
191
183
Field beansList = ListHolder .class .getField ("listField" );
192
184
193
185
String body = "[{\" bytes\" :[1,2],\" array\" :[\" Foo\" ,\" Bar\" ]," +
@@ -214,7 +206,7 @@ public void readAndWriteGenerics() throws Exception {
214
206
215
207
@ Test
216
208
@ SuppressWarnings ("unchecked" )
217
- public void readAndWriteParameterizedType () throws Exception {
209
+ void readAndWriteParameterizedType () throws Exception {
218
210
ParameterizedTypeReference <List <MyBean >> beansList = new ParameterizedTypeReference <List <MyBean >>() {
219
211
};
220
212
@@ -241,7 +233,7 @@ public void readAndWriteParameterizedType() throws Exception {
241
233
242
234
@ Test
243
235
@ SuppressWarnings ("unchecked" )
244
- public void writeParameterizedBaseType () throws Exception {
236
+ void writeParameterizedBaseType () throws Exception {
245
237
ParameterizedTypeReference <List <MyBean >> beansList = new ParameterizedTypeReference <List <MyBean >>() {};
246
238
ParameterizedTypeReference <List <MyBase >> baseList = new ParameterizedTypeReference <List <MyBase >>() {};
247
239
@@ -267,15 +259,15 @@ public void writeParameterizedBaseType() throws Exception {
267
259
}
268
260
269
261
@ Test
270
- public void prefixJson () throws IOException {
262
+ void prefixJson () throws IOException {
271
263
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage ();
272
264
this .converter .setPrefixJson (true );
273
265
this .converter .writeInternal ("foo" , null , outputMessage );
274
266
assertThat (outputMessage .getBodyAsString (StandardCharsets .UTF_8 )).isEqualTo (")]}', \" foo\" " );
275
267
}
276
268
277
269
@ Test
278
- public void prefixJsonCustom () throws IOException {
270
+ void prefixJsonCustom () throws IOException {
279
271
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage ();
280
272
this .converter .setJsonPrefix (")))" );
281
273
this .converter .writeInternal ("foo" , null , outputMessage );
0 commit comments