Fix issue in AnnotationMethodHandlerExceptionResolver

Caching of resovled exceptions introduced in SPR-7703 also introduced a
side effect whereby if exactly one exception was previously cached, any
other exception would appear as a match to the previously matched
@ExceptionHandler method.

This change ensures use of a fresh map when determining matching
@ExceptionHandler methods while also updating the cache.

Issue: SPR-9209
This commit is contained in:
Rossen Stoyanchev
2012-10-06 22:03:31 -04:00
parent 470c85ade0
commit ec2603de63
4 changed files with 48 additions and 14 deletions
@@ -26,6 +26,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
@@ -110,7 +111,7 @@ public class AnnotationMethodHandlerExceptionResolverTests {
assertEquals("Invalid view name returned", "GenericError", mav.getViewName());
assertEquals("Invalid status code returned", 500, response.getStatus());
}
@Test(expected = IllegalStateException.class)
public void ambiguous() {
IllegalArgumentException ex = new IllegalArgumentException();
@@ -127,7 +128,7 @@ public class AnnotationMethodHandlerExceptionResolverTests {
assertTrue("ModelAndView not empty", mav.isEmpty());
assertEquals("Invalid response written", "IllegalArgumentException", response.getContentAsString());
}
@Test
public void responseBody() throws UnsupportedEncodingException {
IllegalArgumentException ex = new IllegalArgumentException();
@@ -139,6 +140,20 @@ public class AnnotationMethodHandlerExceptionResolverTests {
assertEquals("Invalid response written", "IllegalArgumentException", response.getContentAsString());
}
// SPR-9209
@Test
public void cachingSideEffect() {
IllegalArgumentException ex = new IllegalArgumentException();
SimpleController controller = new SimpleController();
ModelAndView mav = exceptionResolver.resolveException(request, response, controller, ex);
assertNotNull("No ModelAndView returned", mav);
mav = exceptionResolver.resolveException(request, response, controller, new NullPointerException());
assertNull(mav);
}
@Controller
private static class SimpleController {