17
17
package org .springframework .test .web .servlet .samples .standalone ;
18
18
19
19
import java .io .StringWriter ;
20
- import java .nio .charset .StandardCharsets ;
20
+ import java .nio .charset .Charset ;
21
21
import java .util .Collection ;
22
22
import java .util .concurrent .Callable ;
23
23
import java .util .concurrent .CompletableFuture ;
@@ -78,7 +78,7 @@ public void callable() throws Exception {
78
78
public void streaming () throws Exception {
79
79
this .mockMvc .perform (get ("/1" ).param ("streaming" , "true" ))
80
80
.andExpect (request ().asyncStarted ())
81
- .andDo (r -> r . getAsyncResult () ) // fetch async result similar to "asyncDispatch" builder
81
+ .andDo (MvcResult :: getAsyncResult ) // fetch async result similar to "asyncDispatch" builder
82
82
.andExpect (status ().isOk ())
83
83
.andExpect (content ().string ("name=Joe" ));
84
84
}
@@ -87,7 +87,7 @@ public void streaming() throws Exception {
87
87
public void streamingSlow () throws Exception {
88
88
this .mockMvc .perform (get ("/1" ).param ("streamingSlow" , "true" ))
89
89
.andExpect (request ().asyncStarted ())
90
- .andDo (r -> r . getAsyncResult () )
90
+ .andDo (MvcResult :: getAsyncResult )
91
91
.andExpect (status ().isOk ())
92
92
.andExpect (content ().string ("name=Joe&someBoolean=true" ));
93
93
}
@@ -96,7 +96,7 @@ public void streamingSlow() throws Exception {
96
96
public void streamingJson () throws Exception {
97
97
this .mockMvc .perform (get ("/1" ).param ("streamingJson" , "true" ))
98
98
.andExpect (request ().asyncStarted ())
99
- .andDo (r -> r . getAsyncResult () )
99
+ .andDo (MvcResult :: getAsyncResult )
100
100
.andExpect (status ().isOk ())
101
101
.andExpect (content ().contentType (MediaType .APPLICATION_JSON_UTF8_VALUE ))
102
102
.andExpect (content ().string ("{\" name\" :\" Joe\" ,\" someDouble\" :0.5}" ));
@@ -129,10 +129,7 @@ public void deferredResultWithImmediateValue() throws Exception {
129
129
.andExpect (content ().string ("{\" name\" :\" Joe\" ,\" someDouble\" :0.0,\" someBoolean\" :false}" ));
130
130
}
131
131
132
- /**
133
- * SPR-13079
134
- */
135
- @ Test
132
+ @ Test // SPR-13079
136
133
public void deferredResultWithDelayedError () throws Exception {
137
134
MvcResult mvcResult = this .mockMvc .perform (get ("/1" ).param ("deferredResultWithDelayedError" , "true" ))
138
135
.andExpect (request ().asyncStarted ())
@@ -157,10 +154,7 @@ public void listenableFuture() throws Exception {
157
154
.andExpect (content ().string ("{\" name\" :\" Joe\" ,\" someDouble\" :0.0,\" someBoolean\" :false}" ));
158
155
}
159
156
160
- /**
161
- * SPR-12597
162
- */
163
- @ Test
157
+ @ Test // SPR-12597
164
158
public void completableFutureWithImmediateValue () throws Exception {
165
159
MvcResult mvcResult = this .mockMvc .perform (get ("/1" ).param ("completableFutureWithImmediateValue" , "true" ))
166
160
.andExpect (request ().asyncStarted ())
@@ -172,10 +166,7 @@ public void completableFutureWithImmediateValue() throws Exception {
172
166
.andExpect (content ().string ("{\" name\" :\" Joe\" ,\" someDouble\" :0.0,\" someBoolean\" :false}" ));
173
167
}
174
168
175
- /**
176
- * SPR-12735
177
- */
178
- @ Test
169
+ @ Test // SPR-12735
179
170
public void printAsyncResult () throws Exception {
180
171
StringWriter writer = new StringWriter ();
181
172
@@ -203,12 +194,9 @@ public void printAsyncResult() throws Exception {
203
194
@ RequestMapping (path = "/{id}" , produces = "application/json" )
204
195
private static class AsyncController {
205
196
206
- private final Collection <DeferredResult <Person >> deferredResults =
207
- new CopyOnWriteArrayList <DeferredResult <Person >>();
208
-
209
- private final Collection <ListenableFutureTask <Person >> futureTasks =
210
- new CopyOnWriteArrayList <ListenableFutureTask <Person >>();
197
+ private final Collection <DeferredResult <Person >> deferredResults = new CopyOnWriteArrayList <>();
211
198
199
+ private final Collection <ListenableFutureTask <Person >> futureTasks = new CopyOnWriteArrayList <>();
212
200
213
201
@ RequestMapping (params = "callable" )
214
202
public Callable <Person > getCallable () {
@@ -217,7 +205,7 @@ public Callable<Person> getCallable() {
217
205
218
206
@ RequestMapping (params = "streaming" )
219
207
public StreamingResponseBody getStreaming () {
220
- return os -> os .write ("name=Joe" .getBytes ());
208
+ return os -> os .write ("name=Joe" .getBytes (Charset . forName ( "UTF-8" ) ));
221
209
}
222
210
223
211
@ RequestMapping (params = "streamingSlow" )
@@ -226,7 +214,7 @@ public StreamingResponseBody getStreamingSlow() {
226
214
os .write ("name=Joe" .getBytes ());
227
215
try {
228
216
Thread .sleep (200 );
229
- os .write ("&someBoolean=true" .getBytes ());
217
+ os .write ("&someBoolean=true" .getBytes (Charset . forName ( "UTF-8" ) ));
230
218
}
231
219
catch (InterruptedException e ) {
232
220
/* no-op */
@@ -237,26 +225,26 @@ public StreamingResponseBody getStreamingSlow() {
237
225
@ RequestMapping (params = "streamingJson" )
238
226
public ResponseEntity <StreamingResponseBody > getStreamingJson () {
239
227
return ResponseEntity .ok ().contentType (MediaType .APPLICATION_JSON_UTF8 )
240
- .body (os -> os .write ("{\" name\" :\" Joe\" ,\" someDouble\" :0.5}" .getBytes (StandardCharsets . UTF_8 )));
228
+ .body (os -> os .write ("{\" name\" :\" Joe\" ,\" someDouble\" :0.5}" .getBytes (Charset . forName ( "UTF-8" ) )));
241
229
}
242
230
243
231
@ RequestMapping (params = "deferredResult" )
244
232
public DeferredResult <Person > getDeferredResult () {
245
- DeferredResult <Person > deferredResult = new DeferredResult <Person >();
233
+ DeferredResult <Person > deferredResult = new DeferredResult <>();
246
234
this .deferredResults .add (deferredResult );
247
235
return deferredResult ;
248
236
}
249
237
250
238
@ RequestMapping (params = "deferredResultWithImmediateValue" )
251
239
public DeferredResult <Person > getDeferredResultWithImmediateValue () {
252
- DeferredResult <Person > deferredResult = new DeferredResult <Person >();
240
+ DeferredResult <Person > deferredResult = new DeferredResult <>();
253
241
deferredResult .setResult (new Person ("Joe" ));
254
242
return deferredResult ;
255
243
}
256
244
257
245
@ RequestMapping (params = "deferredResultWithDelayedError" )
258
246
public DeferredResult <Person > getDeferredResultWithDelayedError () {
259
- final DeferredResult <Person > deferredResult = new DeferredResult <Person >();
247
+ final DeferredResult <Person > deferredResult = new DeferredResult <>();
260
248
new Thread () {
261
249
public void run () {
262
250
try {
@@ -273,14 +261,14 @@ public void run() {
273
261
274
262
@ RequestMapping (params = "listenableFuture" )
275
263
public ListenableFuture <Person > getListenableFuture () {
276
- ListenableFutureTask <Person > futureTask = new ListenableFutureTask <Person >(() -> new Person ("Joe" ));
264
+ ListenableFutureTask <Person > futureTask = new ListenableFutureTask <>(() -> new Person ("Joe" ));
277
265
this .futureTasks .add (futureTask );
278
266
return futureTask ;
279
267
}
280
268
281
269
@ RequestMapping (params = "completableFutureWithImmediateValue" )
282
270
public CompletableFuture <Person > getCompletableFutureWithImmediateValue () {
283
- CompletableFuture <Person > future = new CompletableFuture <Person >();
271
+ CompletableFuture <Person > future = new CompletableFuture <>();
284
272
future .complete (new Person ("Joe" ));
285
273
return future ;
286
274
}
@@ -291,7 +279,7 @@ public String errorHandler(Exception e) {
291
279
return e .getMessage ();
292
280
}
293
281
294
- public void onMessage (String name ) {
282
+ void onMessage (String name ) {
295
283
for (DeferredResult <Person > deferredResult : this .deferredResults ) {
296
284
deferredResult .setResult (new Person (name ));
297
285
this .deferredResults .remove (deferredResult );
0 commit comments