1
1
/*
2
- * Copyright 2002-2015 the original author or authors.
2
+ * Copyright 2002-2016 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.
56
56
*
57
57
* @author Rossen Stoyanchev
58
58
* @author Sam Brannen
59
+ * @author Juergen Hoeller
59
60
*/
60
61
public class ServletInvocableHandlerMethodTests {
61
62
@@ -117,19 +118,17 @@ public void invokeAndHandle_VoidRequestNotModified() throws Exception {
117
118
this .mavContainer .isRequestHandled ());
118
119
}
119
120
120
- // SPR-9159
121
-
122
- @ Test
121
+ @ Test // SPR-9159
123
122
public void invokeAndHandle_NotVoidWithResponseStatusAndReason () throws Exception {
124
123
ServletInvocableHandlerMethod handlerMethod = getHandlerMethod (new Handler (), "responseStatusWithReason" );
125
124
handlerMethod .invokeAndHandle (this .webRequest , this .mavContainer );
126
125
127
- assertTrue ("When a status reason w/ used, the the request is handled" , this .mavContainer .isRequestHandled ());
126
+ assertTrue ("When a status reason w/ used, the request is handled" , this .mavContainer .isRequestHandled ());
128
127
assertEquals (HttpStatus .BAD_REQUEST .value (), this .response .getStatus ());
129
128
assertEquals ("400 Bad Request" , this .response .getErrorMessage ());
130
129
}
131
130
132
- @ Test (expected = HttpMessageNotWritableException .class )
131
+ @ Test (expected = HttpMessageNotWritableException .class )
133
132
public void invokeAndHandle_Exception () throws Exception {
134
133
this .returnValueHandlers .addHandler (new ExceptionRaisingReturnValueHandler ());
135
134
@@ -160,28 +159,52 @@ public void invokeAndHandle_DynamicReturnValue() throws Exception {
160
159
161
160
@ Test
162
161
public void wrapConcurrentResult_MethodLevelResponseBody () throws Exception {
163
- wrapConcurrentResult_ResponseBody (new MethodLevelResponseBodyHandler ());
162
+ wrapConcurrentResult_ResponseBody (new MethodLevelResponseBodyHandler (), "bar" , String .class );
163
+ }
164
+
165
+ @ Test
166
+ public void wrapConcurrentResult_MethodLevelResponseBodyEmpty () throws Exception {
167
+ wrapConcurrentResult_ResponseBody (new MethodLevelResponseBodyHandler (), null , String .class );
164
168
}
165
169
166
170
@ Test
167
171
public void wrapConcurrentResult_TypeLevelResponseBody () throws Exception {
168
- wrapConcurrentResult_ResponseBody (new TypeLevelResponseBodyHandler ());
172
+ wrapConcurrentResult_ResponseBody (new TypeLevelResponseBodyHandler (), "bar" , String .class );
173
+ }
174
+
175
+ @ Test
176
+ public void wrapConcurrentResult_TypeLevelResponseBodyEmpty () throws Exception {
177
+ wrapConcurrentResult_ResponseBody (new TypeLevelResponseBodyHandler (), null , String .class );
178
+ }
179
+
180
+ @ Test
181
+ public void wrapConcurrentResult_DeferredResultSubclass () throws Exception {
182
+ wrapConcurrentResult_ResponseBody (new DeferredResultSubclassHandler (), "bar" , String .class );
169
183
}
170
184
171
- private void wrapConcurrentResult_ResponseBody (Object handler ) throws Exception {
172
- List <HttpMessageConverter <?>> converters = new ArrayList <HttpMessageConverter <?>>();
185
+ @ Test
186
+ public void wrapConcurrentResult_DeferredResultSubclassEmpty () throws Exception {
187
+ wrapConcurrentResult_ResponseBody (new DeferredResultSubclassHandler (), null , CustomDeferredResult .class );
188
+ }
189
+
190
+ private void wrapConcurrentResult_ResponseBody (Object handler , Object result , Class <?> expectedReturnType )
191
+ throws Exception {
192
+
193
+ List <HttpMessageConverter <?>> converters = new ArrayList <>();
173
194
converters .add (new StringHttpMessageConverter ());
195
+ this .returnValueHandlers .addHandler (new ModelAndViewMethodReturnValueHandler ());
174
196
this .returnValueHandlers .addHandler (new RequestResponseBodyMethodProcessor (converters ));
175
197
ServletInvocableHandlerMethod handlerMethod = getHandlerMethod (handler , "handle" );
176
- handlerMethod = handlerMethod .wrapConcurrentResult ("bar" );
177
- handlerMethod .invokeAndHandle (this .webRequest , this .mavContainer );
178
198
179
- assertEquals ("bar" , this .response .getContentAsString ());
199
+ handlerMethod = handlerMethod .wrapConcurrentResult (result );
200
+ handlerMethod .invokeAndHandle (this .webRequest , this .mavContainer );
201
+ assertEquals ((result != null ? result .toString () : "" ), this .response .getContentAsString ());
202
+ assertEquals (expectedReturnType , handlerMethod .getReturnValueType (result ).getParameterType ());
180
203
}
181
204
182
205
@ Test
183
206
public void wrapConcurrentResult_ResponseEntity () throws Exception {
184
- List <HttpMessageConverter <?>> converters = new ArrayList <HttpMessageConverter <?> >();
207
+ List <HttpMessageConverter <?>> converters = new ArrayList <>();
185
208
converters .add (new StringHttpMessageConverter ());
186
209
this .returnValueHandlers .addHandler (new HttpEntityMethodProcessor (converters ));
187
210
ServletInvocableHandlerMethod handlerMethod = getHandlerMethod (new ResponseEntityHandler (), "handleDeferred" );
@@ -191,11 +214,9 @@ public void wrapConcurrentResult_ResponseEntity() throws Exception {
191
214
assertEquals ("bar" , this .response .getContentAsString ());
192
215
}
193
216
194
- // SPR-12287
195
-
196
- @ Test
217
+ @ Test // SPR-12287
197
218
public void wrapConcurrentResult_ResponseEntityNullBody () throws Exception {
198
- List <HttpMessageConverter <?>> converters = new ArrayList <HttpMessageConverter <?> >();
219
+ List <HttpMessageConverter <?>> converters = new ArrayList <>();
199
220
converters .add (new StringHttpMessageConverter ());
200
221
List <Object > advice = Collections .singletonList (mock (ResponseBodyAdvice .class ));
201
222
HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor (converters , null , advice );
@@ -210,7 +231,7 @@ public void wrapConcurrentResult_ResponseEntityNullBody() throws Exception {
210
231
211
232
@ Test
212
233
public void wrapConcurrentResult_ResponseEntityNullReturnValue () throws Exception {
213
- List <HttpMessageConverter <?>> converters = new ArrayList <HttpMessageConverter <?> >();
234
+ List <HttpMessageConverter <?>> converters = new ArrayList <>();
214
235
converters .add (new StringHttpMessageConverter ());
215
236
List <Object > advice = Collections .singletonList (mock (ResponseBodyAdvice .class ));
216
237
HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor (converters , null , advice );
@@ -225,7 +246,7 @@ public void wrapConcurrentResult_ResponseEntityNullReturnValue() throws Exceptio
225
246
226
247
@ Test
227
248
public void wrapConcurrentResult_ResponseBodyEmitter () throws Exception {
228
- List <HttpMessageConverter <?>> converters = new ArrayList <HttpMessageConverter <?> >();
249
+ List <HttpMessageConverter <?>> converters = new ArrayList <>();
229
250
converters .add (new StringHttpMessageConverter ());
230
251
this .returnValueHandlers .addHandler (new ResponseBodyEmitterReturnValueHandler (converters ));
231
252
ServletInvocableHandlerMethod handlerMethod = getHandlerMethod (new AsyncHandler (), "handleWithEmitter" );
@@ -247,9 +268,7 @@ public void wrapConcurrentResult_StreamingResponseBody() throws Exception {
247
268
assertEquals ("" , this .response .getContentAsString ());
248
269
}
249
270
250
- // SPR-12287 (16/Oct/14 comments)
251
-
252
- @ Test
271
+ @ Test // SPR-12287 (16/Oct/14 comments)
253
272
public void responseEntityRawTypeWithNullBody () throws Exception {
254
273
List <HttpMessageConverter <?>> converters = Collections .singletonList (new StringHttpMessageConverter ());
255
274
List <Object > advice = Collections .singletonList (mock (ResponseBodyAdvice .class ));
@@ -272,6 +291,8 @@ private ServletInvocableHandlerMethod getHandlerMethod(Object controller,
272
291
return handlerMethod ;
273
292
}
274
293
294
+
295
+ @ SuppressWarnings ("unused" )
275
296
@ ResponseStatus
276
297
@ Retention (RetentionPolicy .RUNTIME )
277
298
@interface ComposedResponseStatus {
@@ -280,6 +301,7 @@ private ServletInvocableHandlerMethod getHandlerMethod(Object controller,
280
301
HttpStatus responseStatus () default HttpStatus .INTERNAL_SERVER_ERROR ;
281
302
}
282
303
304
+
283
305
@ SuppressWarnings ("unused" )
284
306
private static class Handler {
285
307
@@ -311,6 +333,16 @@ public Object dynamicReturnValue(@RequestParam(required=false) String param) {
311
333
}
312
334
}
313
335
336
+
337
+ @ SuppressWarnings ("unused" )
338
+ @ ResponseStatus (HttpStatus .BAD_REQUEST )
339
+ private static class ResponseStatusHandler {
340
+
341
+ public void handle () {
342
+ }
343
+ }
344
+
345
+
314
346
private static class MethodLevelResponseBodyHandler {
315
347
316
348
@ ResponseBody
@@ -319,15 +351,30 @@ public DeferredResult<String> handle() {
319
351
}
320
352
}
321
353
354
+
322
355
@ SuppressWarnings ("unused" )
323
356
@ ResponseBody
324
357
private static class TypeLevelResponseBodyHandler {
325
358
326
359
public DeferredResult <String > handle () {
327
- return new DeferredResult <String >();
360
+ return new DeferredResult <>();
328
361
}
329
362
}
330
363
364
+
365
+ private static class DeferredResultSubclassHandler {
366
+
367
+ @ ResponseBody
368
+ public CustomDeferredResult handle () {
369
+ return new CustomDeferredResult ();
370
+ }
371
+ }
372
+
373
+
374
+ private static class CustomDeferredResult extends DeferredResult <String > {
375
+ }
376
+
377
+
331
378
@ SuppressWarnings ("unused" )
332
379
private static class ResponseEntityHandler {
333
380
@@ -340,6 +387,7 @@ public ResponseEntity<Void> handleRawType() {
340
387
}
341
388
}
342
389
390
+
343
391
private static class ExceptionRaisingReturnValueHandler implements HandlerMethodReturnValueHandler {
344
392
345
393
@ Override
@@ -354,6 +402,7 @@ public void handleReturnValue(Object returnValue, MethodParameter returnType,
354
402
}
355
403
}
356
404
405
+
357
406
@ SuppressWarnings ("unused" )
358
407
private static class AsyncHandler {
359
408
@@ -366,4 +415,4 @@ public StreamingResponseBody handleWithStreaming() {
366
415
}
367
416
}
368
417
369
- }
418
+ }
0 commit comments