1
1
/*
2
- * Copyright 2002-2016 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.
25
25
import org .junit .BeforeClass ;
26
26
import org .junit .Test ;
27
27
28
+ import org .springframework .aop .framework .ProxyFactory ;
28
29
import org .springframework .beans .FatalBeanException ;
29
30
import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
30
31
import org .springframework .context .annotation .Bean ;
38
39
import org .springframework .web .bind .annotation .ExceptionHandler ;
39
40
import org .springframework .web .bind .annotation .ResponseBody ;
40
41
import org .springframework .web .bind .annotation .RestControllerAdvice ;
42
+ import org .springframework .web .context .support .WebApplicationObjectSupport ;
41
43
import org .springframework .web .method .HandlerMethod ;
42
44
import org .springframework .web .method .annotation .ModelMethodProcessor ;
43
45
import org .springframework .web .method .support .HandlerMethodArgumentResolver ;
@@ -213,8 +215,8 @@ public void resolveRedirectAttributesAtArgument() throws Exception {
213
215
214
216
@ Test
215
217
public void resolveExceptionGlobalHandler () throws Exception {
216
- AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext (MyConfig .class );
217
- this .resolver .setApplicationContext (cxt );
218
+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (MyConfig .class );
219
+ this .resolver .setApplicationContext (ctx );
218
220
this .resolver .afterPropertiesSet ();
219
221
220
222
IllegalAccessException ex = new IllegalAccessException ();
@@ -228,8 +230,8 @@ public void resolveExceptionGlobalHandler() throws Exception {
228
230
229
231
@ Test
230
232
public void resolveExceptionGlobalHandlerOrdered () throws Exception {
231
- AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext (MyConfig .class );
232
- this .resolver .setApplicationContext (cxt );
233
+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (MyConfig .class );
234
+ this .resolver .setApplicationContext (ctx );
233
235
this .resolver .afterPropertiesSet ();
234
236
235
237
IllegalStateException ex = new IllegalStateException ();
@@ -243,8 +245,8 @@ public void resolveExceptionGlobalHandlerOrdered() throws Exception {
243
245
244
246
@ Test // SPR-12605
245
247
public void resolveExceptionWithHandlerMethodArg () throws Exception {
246
- AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext (MyConfig .class );
247
- this .resolver .setApplicationContext (cxt );
248
+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (MyConfig .class );
249
+ this .resolver .setApplicationContext (ctx );
248
250
this .resolver .afterPropertiesSet ();
249
251
250
252
ArrayIndexOutOfBoundsException ex = new ArrayIndexOutOfBoundsException ();
@@ -258,8 +260,8 @@ public void resolveExceptionWithHandlerMethodArg() throws Exception {
258
260
259
261
@ Test
260
262
public void resolveExceptionWithAssertionError () throws Exception {
261
- AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext (MyConfig .class );
262
- this .resolver .setApplicationContext (cxt );
263
+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (MyConfig .class );
264
+ this .resolver .setApplicationContext (ctx );
263
265
this .resolver .afterPropertiesSet ();
264
266
265
267
AssertionError err = new AssertionError ("argh" );
@@ -274,8 +276,8 @@ public void resolveExceptionWithAssertionError() throws Exception {
274
276
275
277
@ Test
276
278
public void resolveExceptionWithAssertionErrorAsRootCause () throws Exception {
277
- AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext (MyConfig .class );
278
- this .resolver .setApplicationContext (cxt );
279
+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (MyConfig .class );
280
+ this .resolver .setApplicationContext (ctx );
279
281
this .resolver .afterPropertiesSet ();
280
282
281
283
AssertionError err = new AssertionError ("argh" );
@@ -290,8 +292,8 @@ public void resolveExceptionWithAssertionErrorAsRootCause() throws Exception {
290
292
291
293
@ Test
292
294
public void resolveExceptionControllerAdviceHandler () throws Exception {
293
- AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext (MyControllerAdviceConfig .class );
294
- this .resolver .setApplicationContext (cxt );
295
+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (MyControllerAdviceConfig .class );
296
+ this .resolver .setApplicationContext (ctx );
295
297
this .resolver .afterPropertiesSet ();
296
298
297
299
IllegalStateException ex = new IllegalStateException ();
@@ -305,8 +307,8 @@ public void resolveExceptionControllerAdviceHandler() throws Exception {
305
307
306
308
@ Test
307
309
public void resolveExceptionControllerAdviceNoHandler () throws Exception {
308
- AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext (MyControllerAdviceConfig .class );
309
- this .resolver .setApplicationContext (cxt );
310
+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (MyControllerAdviceConfig .class );
311
+ this .resolver .setApplicationContext (ctx );
310
312
this .resolver .afterPropertiesSet ();
311
313
312
314
IllegalStateException ex = new IllegalStateException ();
@@ -317,6 +319,21 @@ public void resolveExceptionControllerAdviceNoHandler() throws Exception {
317
319
assertEquals ("DefaultTestExceptionResolver: IllegalStateException" , this .response .getContentAsString ());
318
320
}
319
321
322
+ @ Test // SPR-16496
323
+ public void resolveExceptionControllerAdviceAgainstProxy () throws Exception {
324
+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (MyControllerAdviceConfig .class );
325
+ this .resolver .setApplicationContext (ctx );
326
+ this .resolver .afterPropertiesSet ();
327
+
328
+ IllegalStateException ex = new IllegalStateException ();
329
+ HandlerMethod handlerMethod = new HandlerMethod (new ProxyFactory (new ResponseBodyController ()).getProxy (), "handle" );
330
+ ModelAndView mav = this .resolver .resolveException (this .request , this .response , handlerMethod , ex );
331
+
332
+ assertNotNull ("Exception was not handled" , mav );
333
+ assertTrue (mav .isEmpty ());
334
+ assertEquals ("BasePackageTestExceptionResolver: IllegalStateException" , this .response .getContentAsString ());
335
+ }
336
+
320
337
321
338
private void assertMethodProcessorCount (int resolverCount , int handlerCount ) {
322
339
assertEquals (resolverCount , this .resolver .getArgumentResolvers ().getResolvers ().size ());
@@ -348,8 +365,18 @@ public void handleException(Exception ex, Writer writer) throws IOException {
348
365
}
349
366
350
367
368
+ interface ResponseBodyInterface {
369
+
370
+ void handle ();
371
+
372
+ @ ExceptionHandler
373
+ @ ResponseBody
374
+ String handleException (IllegalArgumentException ex );
375
+ }
376
+
377
+
351
378
@ Controller
352
- static class ResponseBodyController {
379
+ static class ResponseBodyController extends WebApplicationObjectSupport implements ResponseBodyInterface {
353
380
354
381
public void handle () {}
355
382
@@ -454,7 +481,7 @@ public String handleException(IllegalStateException ex) {
454
481
}
455
482
456
483
457
- @ RestControllerAdvice ("org.springframework.web.servlet.mvc.method.annotation" )
484
+ @ RestControllerAdvice (assignableTypes = WebApplicationObjectSupport . class )
458
485
@ Order (2 )
459
486
static class BasePackageTestExceptionResolver {
460
487
0 commit comments