Revised InvocableHandlerMethod exception handling
Issue: SPR-11281
(cherry picked from commit 1a1c72c)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -45,6 +45,7 @@ public class InvocableHandlerMethodTests {
|
||||
|
||||
private NativeWebRequest webRequest;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
Method method = Handler.class.getDeclaredMethod("handle", Integer.class, String.class);
|
||||
@@ -52,6 +53,7 @@ public class InvocableHandlerMethodTests {
|
||||
this.webRequest = new ServletWebRequest(new MockHttpServletRequest(), new MockHttpServletResponse());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void resolveArg() throws Exception {
|
||||
StubArgumentResolver intResolver = new StubArgumentResolver(Integer.class, 99);
|
||||
@@ -63,11 +65,9 @@ public class InvocableHandlerMethodTests {
|
||||
handlerMethod.setHandlerMethodArgumentResolvers(composite);
|
||||
|
||||
Object returnValue = handlerMethod.invokeForRequest(webRequest, null);
|
||||
|
||||
assertEquals(1, intResolver.getResolvedParameters().size());
|
||||
assertEquals(1, stringResolver.getResolvedParameters().size());
|
||||
assertEquals("99-value", returnValue);
|
||||
|
||||
assertEquals("intArg", intResolver.getResolvedParameters().get(0).getParameterName());
|
||||
assertEquals("stringArg", stringResolver.getResolvedParameters().get(0).getParameterName());
|
||||
}
|
||||
@@ -83,7 +83,6 @@ public class InvocableHandlerMethodTests {
|
||||
handlerMethod.setHandlerMethodArgumentResolvers(composite);
|
||||
|
||||
Object returnValue = handlerMethod.invokeForRequest(webRequest, null);
|
||||
|
||||
assertEquals(1, intResolver.getResolvedParameters().size());
|
||||
assertEquals(1, stringResolver.getResolvedParameters().size());
|
||||
assertEquals("null-null", returnValue);
|
||||
@@ -94,7 +93,8 @@ public class InvocableHandlerMethodTests {
|
||||
try {
|
||||
handlerMethod.invokeForRequest(webRequest, null);
|
||||
fail("Expected exception");
|
||||
} catch (IllegalStateException ex) {
|
||||
}
|
||||
catch (IllegalStateException ex) {
|
||||
assertTrue(ex.getMessage().contains("No suitable resolver for argument [0] [type=java.lang.Integer]"));
|
||||
}
|
||||
}
|
||||
@@ -118,7 +118,6 @@ public class InvocableHandlerMethodTests {
|
||||
handlerMethod.setHandlerMethodArgumentResolvers(composite);
|
||||
|
||||
Object returnValue = handlerMethod.invokeForRequest(webRequest, null, 2, "value2");
|
||||
|
||||
assertEquals("2-value2", returnValue);
|
||||
}
|
||||
|
||||
@@ -131,9 +130,9 @@ public class InvocableHandlerMethodTests {
|
||||
try {
|
||||
handlerMethod.invokeForRequest(webRequest, null);
|
||||
fail("Expected exception");
|
||||
} catch (HttpMessageNotReadableException ex) {
|
||||
// Expected..
|
||||
// Allow HandlerMethodArgumentResolver exceptions to propagate..
|
||||
}
|
||||
catch (HttpMessageNotReadableException ex) {
|
||||
// expected - allow HandlerMethodArgumentResolver exceptions to propagate
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,7 +149,8 @@ public class InvocableHandlerMethodTests {
|
||||
try {
|
||||
handlerMethod.invokeForRequest(webRequest, null);
|
||||
fail("Expected exception");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
}
|
||||
catch (IllegalStateException ex) {
|
||||
assertNotNull("Exception not wrapped", ex.getCause());
|
||||
assertTrue(ex.getCause() instanceof IllegalArgumentException);
|
||||
assertTrue(ex.getMessage().contains("Controller ["));
|
||||
@@ -166,28 +166,32 @@ public class InvocableHandlerMethodTests {
|
||||
Throwable expected = new RuntimeException("error");
|
||||
try {
|
||||
invokeExceptionRaisingHandler(expected);
|
||||
} catch (RuntimeException actual) {
|
||||
}
|
||||
catch (RuntimeException actual) {
|
||||
assertSame(expected, actual);
|
||||
}
|
||||
|
||||
expected = new Error("error");
|
||||
try {
|
||||
invokeExceptionRaisingHandler(expected);
|
||||
} catch (Error actual) {
|
||||
}
|
||||
catch (Error actual) {
|
||||
assertSame(expected, actual);
|
||||
}
|
||||
|
||||
expected = new Exception("error");
|
||||
try {
|
||||
invokeExceptionRaisingHandler(expected);
|
||||
} catch (Exception actual) {
|
||||
}
|
||||
catch (Exception actual) {
|
||||
assertSame(expected, actual);
|
||||
}
|
||||
|
||||
expected = new Throwable("error");
|
||||
try {
|
||||
invokeExceptionRaisingHandler(expected);
|
||||
} catch (IllegalStateException actual) {
|
||||
}
|
||||
catch (IllegalStateException actual) {
|
||||
assertNotNull(actual.getCause());
|
||||
assertSame(expected, actual.getCause());
|
||||
assertTrue(actual.getMessage().contains("Failed to invoke controller method"));
|
||||
@@ -201,6 +205,7 @@ public class InvocableHandlerMethodTests {
|
||||
fail("Expected exception");
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static class Handler {
|
||||
|
||||
@@ -209,6 +214,7 @@ public class InvocableHandlerMethodTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static class ExceptionRaisingHandler {
|
||||
|
||||
@@ -221,9 +227,9 @@ public class InvocableHandlerMethodTests {
|
||||
public void raiseException() throws Throwable {
|
||||
throw t;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private static class ExceptionRaisingArgumentResolver implements HandlerMethodArgumentResolver {
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user