1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.web.method.annotation;
18
19 import java.io.FileNotFoundException;
20 import java.io.IOException;
21 import java.net.BindException;
22 import java.net.SocketException;
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
25
26 import org.junit.Test;
27
28 import org.springframework.stereotype.Controller;
29 import org.springframework.util.ClassUtils;
30 import org.springframework.web.bind.annotation.ExceptionHandler;
31
32 import static org.junit.Assert.*;
33
34
35
36
37
38
39 public class ExceptionHandlerMethodResolverTests {
40
41 @Test
42 public void resolveMethodFromAnnotation() {
43 ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(ExceptionController.class);
44 IOException exception = new IOException();
45 assertEquals("handleIOException", resolver.resolveMethod(exception).getName());
46 }
47
48 @Test
49 public void resolveMethodFromArgument() {
50 ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(ExceptionController.class);
51 IllegalArgumentException exception = new IllegalArgumentException();
52 assertEquals("handleIllegalArgumentException", resolver.resolveMethod(exception).getName());
53 }
54
55 @Test
56 public void resolveMethodExceptionSubType() {
57 ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(ExceptionController.class);
58 IOException ioException = new FileNotFoundException();
59 assertEquals("handleIOException", resolver.resolveMethod(ioException).getName());
60 SocketException bindException = new BindException();
61 assertEquals("handleSocketException", resolver.resolveMethod(bindException).getName());
62 }
63
64 @Test
65 public void resolveMethodBestMatch() {
66 ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(ExceptionController.class);
67 SocketException exception = new SocketException();
68 assertEquals("handleSocketException", resolver.resolveMethod(exception).getName());
69 }
70
71 @Test
72 public void resolveMethodNoMatch() {
73 ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(ExceptionController.class);
74 Exception exception = new Exception();
75 assertNull("1st lookup", resolver.resolveMethod(exception));
76 assertNull("2nd lookup from cache", resolver.resolveMethod(exception));
77 }
78
79 @Test
80 public void resolveMethodInherited() {
81 ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(InheritedController.class);
82 IOException exception = new IOException();
83 assertEquals("handleIOException", resolver.resolveMethod(exception).getName());
84 }
85
86 @Test(expected = IllegalStateException.class)
87 public void ambiguousExceptionMapping() {
88 new ExceptionHandlerMethodResolver(AmbiguousController.class);
89 }
90
91 @Test(expected = IllegalArgumentException.class)
92 public void noExceptionMapping() {
93 new ExceptionHandlerMethodResolver(NoExceptionController.class);
94 }
95
96 @Controller
97 static class ExceptionController {
98
99 public void handle() {}
100
101 @ExceptionHandler(IOException.class)
102 public void handleIOException() {
103 }
104
105 @ExceptionHandler(SocketException.class)
106 public void handleSocketException() {
107 }
108
109 @ExceptionHandler
110 public void handleIllegalArgumentException(IllegalArgumentException exception) {
111 }
112 }
113
114 @Controller
115 static class InheritedController extends ExceptionController {
116
117 @Override
118 public void handleIOException() {
119 }
120 }
121
122 @Controller
123 static class AmbiguousController {
124
125 public void handle() {}
126
127 @ExceptionHandler({BindException.class, IllegalArgumentException.class})
128 public String handle1(Exception ex, HttpServletRequest request, HttpServletResponse response)
129 throws IOException {
130 return ClassUtils.getShortName(ex.getClass());
131 }
132
133 @ExceptionHandler
134 public String handle2(IllegalArgumentException ex) {
135 return ClassUtils.getShortName(ex.getClass());
136 }
137 }
138
139 @Controller
140 static class NoExceptionController {
141
142 @ExceptionHandler
143 public void handle() {
144 }
145 }
146
147 }