18
18
19
19
import java .util .Collections ;
20
20
import java .util .Properties ;
21
-
22
21
import javax .servlet .http .HttpServletResponse ;
23
22
24
- import junit .framework .TestCase ;
23
+ import static org .junit .Assert .*;
24
+ import org .junit .Before ;
25
+ import org .junit .Test ;
25
26
26
27
import org .springframework .mock .web .MockHttpServletRequest ;
27
28
import org .springframework .mock .web .MockHttpServletResponse ;
31
32
/**
32
33
* @author Seth Ladd
33
34
* @author Juergen Hoeller
35
+ * @author Arjen Poutsma
34
36
*/
35
- public class SimpleMappingExceptionResolverTests extends TestCase {
37
+ public class SimpleMappingExceptionResolverTests {
36
38
37
39
private SimpleMappingExceptionResolver exceptionResolver ;
38
40
private MockHttpServletRequest request ;
@@ -41,7 +43,8 @@ public class SimpleMappingExceptionResolverTests extends TestCase {
41
43
private Object handler2 ;
42
44
private Exception genericException ;
43
45
44
- protected void setUp () throws Exception {
46
+ @ Before
47
+ public void setUp () throws Exception {
45
48
exceptionResolver = new SimpleMappingExceptionResolver ();
46
49
handler1 = new String ();
47
50
handler2 = new Object ();
@@ -51,69 +54,90 @@ protected void setUp() throws Exception {
51
54
genericException = new Exception ();
52
55
}
53
56
54
- public void testSetOrder () {
57
+ @ Test
58
+ public void setOrder () {
55
59
exceptionResolver .setOrder (2 );
56
60
assertEquals (2 , exceptionResolver .getOrder ());
57
61
}
58
62
59
- public void testDefaultErrorView () {
63
+ @ Test
64
+ public void defaultErrorView () {
60
65
exceptionResolver .setDefaultErrorView ("default-view" );
61
66
ModelAndView mav = exceptionResolver .resolveException (request , response , handler1 , genericException );
62
67
assertEquals ("default-view" , mav .getViewName ());
63
68
assertEquals (genericException , mav .getModel ().get (SimpleMappingExceptionResolver .DEFAULT_EXCEPTION_ATTRIBUTE ));
64
69
}
65
70
66
- public void testDefaultErrorViewDifferentHandler () {
71
+ @ Test
72
+ public void defaultErrorViewDifferentHandler () {
67
73
exceptionResolver .setDefaultErrorView ("default-view" );
68
74
exceptionResolver .setMappedHandlers (Collections .singleton (handler1 ));
69
75
ModelAndView mav = exceptionResolver .resolveException (request , response , handler2 , genericException );
70
76
assertNull (mav );
71
77
}
72
78
73
- public void testDefaultErrorViewDifferentHandlerClass () {
79
+ @ Test
80
+ public void defaultErrorViewDifferentHandlerClass () {
74
81
exceptionResolver .setDefaultErrorView ("default-view" );
75
82
exceptionResolver .setMappedHandlerClasses (new Class [] {String .class });
76
83
ModelAndView mav = exceptionResolver .resolveException (request , response , handler2 , genericException );
77
84
assertNull (mav );
78
85
}
79
86
80
- public void testNullExceptionAttribute () {
87
+ @ Test
88
+ public void nullExceptionAttribute () {
81
89
exceptionResolver .setDefaultErrorView ("default-view" );
82
90
exceptionResolver .setExceptionAttribute (null );
83
91
ModelAndView mav = exceptionResolver .resolveException (request , response , handler1 , genericException );
84
92
assertEquals ("default-view" , mav .getViewName ());
85
93
assertNull (mav .getModel ().get (SimpleMappingExceptionResolver .DEFAULT_EXCEPTION_ATTRIBUTE ));
86
94
}
87
95
88
- public void testNullExceptionMappings () {
96
+ @ Test
97
+ public void nullExceptionMappings () {
89
98
exceptionResolver .setExceptionMappings (null );
90
99
exceptionResolver .setDefaultErrorView ("default-view" );
91
100
ModelAndView mav = exceptionResolver .resolveException (request , response , handler1 , genericException );
92
101
assertEquals ("default-view" , mav .getViewName ());
93
102
}
94
103
95
- public void testNoDefaultStatusCode () {
104
+ @ Test
105
+ public void noDefaultStatusCode () {
96
106
exceptionResolver .setDefaultErrorView ("default-view" );
97
- ModelAndView mav = exceptionResolver .resolveException (request , response , handler1 , genericException );
107
+ exceptionResolver .resolveException (request , response , handler1 , genericException );
98
108
assertEquals (HttpServletResponse .SC_OK , response .getStatus ());
99
109
}
100
110
101
- public void testSetDefaultStatusCode () {
111
+ @ Test
112
+ public void setDefaultStatusCode () {
102
113
exceptionResolver .setDefaultErrorView ("default-view" );
103
114
exceptionResolver .setDefaultStatusCode (HttpServletResponse .SC_BAD_REQUEST );
104
- ModelAndView mav = exceptionResolver .resolveException (request , response , handler1 , genericException );
115
+ exceptionResolver .resolveException (request , response , handler1 , genericException );
105
116
assertEquals (HttpServletResponse .SC_BAD_REQUEST , response .getStatus ());
106
117
}
107
118
108
- public void testNoDefaultStatusCodeInInclude () {
119
+ @ Test
120
+ public void noDefaultStatusCodeInInclude () {
109
121
exceptionResolver .setDefaultErrorView ("default-view" );
110
122
exceptionResolver .setDefaultStatusCode (HttpServletResponse .SC_BAD_REQUEST );
111
123
request .setAttribute (WebUtils .INCLUDE_REQUEST_URI_ATTRIBUTE , "some path" );
112
- ModelAndView mav = exceptionResolver .resolveException (request , response , handler1 , genericException );
124
+ exceptionResolver .resolveException (request , response , handler1 , genericException );
113
125
assertEquals (HttpServletResponse .SC_OK , response .getStatus ());
114
126
}
115
127
116
- public void testSimpleExceptionMapping () {
128
+ @ Test
129
+ public void specificStatusCode () {
130
+ exceptionResolver .setDefaultErrorView ("default-view" );
131
+ exceptionResolver .setDefaultStatusCode (HttpServletResponse .SC_BAD_REQUEST );
132
+ Properties statusCodes = new Properties ();
133
+ statusCodes .setProperty ("default-view" , "406" );
134
+ exceptionResolver .setStatusCodes (statusCodes );
135
+ exceptionResolver .resolveException (request , response , handler1 , genericException );
136
+ assertEquals (HttpServletResponse .SC_NOT_ACCEPTABLE , response .getStatus ());
137
+ }
138
+
139
+ @ Test
140
+ public void simpleExceptionMapping () {
117
141
Properties props = new Properties ();
118
142
props .setProperty ("Exception" , "error" );
119
143
exceptionResolver .setWarnLogCategory ("HANDLER_EXCEPTION" );
@@ -122,7 +146,8 @@ public void testSimpleExceptionMapping() {
122
146
assertEquals ("error" , mav .getViewName ());
123
147
}
124
148
125
- public void testExactExceptionMappingWithHandlerSpecified () {
149
+ @ Test
150
+ public void exactExceptionMappingWithHandlerSpecified () {
126
151
Properties props = new Properties ();
127
152
props .setProperty ("java.lang.Exception" , "error" );
128
153
exceptionResolver .setMappedHandlers (Collections .singleton (handler1 ));
@@ -131,7 +156,8 @@ public void testExactExceptionMappingWithHandlerSpecified() {
131
156
assertEquals ("error" , mav .getViewName ());
132
157
}
133
158
134
- public void testExactExceptionMappingWithHandlerClassSpecified () {
159
+ @ Test
160
+ public void exactExceptionMappingWithHandlerClassSpecified () {
135
161
Properties props = new Properties ();
136
162
props .setProperty ("java.lang.Exception" , "error" );
137
163
exceptionResolver .setMappedHandlerClasses (new Class [] {String .class });
@@ -140,7 +166,8 @@ public void testExactExceptionMappingWithHandlerClassSpecified() {
140
166
assertEquals ("error" , mav .getViewName ());
141
167
}
142
168
143
- public void testExactExceptionMappingWithHandlerInterfaceSpecified () {
169
+ @ Test
170
+ public void exactExceptionMappingWithHandlerInterfaceSpecified () {
144
171
Properties props = new Properties ();
145
172
props .setProperty ("java.lang.Exception" , "error" );
146
173
exceptionResolver .setMappedHandlerClasses (new Class [] {Comparable .class });
@@ -149,7 +176,8 @@ public void testExactExceptionMappingWithHandlerInterfaceSpecified() {
149
176
assertEquals ("error" , mav .getViewName ());
150
177
}
151
178
152
- public void testSimpleExceptionMappingWithHandlerSpecifiedButWrongHandler () {
179
+ @ Test
180
+ public void simpleExceptionMappingWithHandlerSpecifiedButWrongHandler () {
153
181
Properties props = new Properties ();
154
182
props .setProperty ("Exception" , "error" );
155
183
exceptionResolver .setMappedHandlers (Collections .singleton (handler1 ));
@@ -158,7 +186,8 @@ public void testSimpleExceptionMappingWithHandlerSpecifiedButWrongHandler() {
158
186
assertNull (mav );
159
187
}
160
188
161
- public void testSimpleExceptionMappingWithHandlerClassSpecifiedButWrongHandler () {
189
+ @ Test
190
+ public void simpleExceptionMappingWithHandlerClassSpecifiedButWrongHandler () {
162
191
Properties props = new Properties ();
163
192
props .setProperty ("Exception" , "error" );
164
193
exceptionResolver .setMappedHandlerClasses (new Class [] {String .class });
@@ -167,7 +196,8 @@ public void testSimpleExceptionMappingWithHandlerClassSpecifiedButWrongHandler()
167
196
assertNull (mav );
168
197
}
169
198
170
- public void testMissingExceptionInMapping () {
199
+ @ Test
200
+ public void missingExceptionInMapping () {
171
201
Properties props = new Properties ();
172
202
props .setProperty ("SomeFooThrowable" , "error" );
173
203
exceptionResolver .setWarnLogCategory ("HANDLER_EXCEPTION" );
@@ -176,7 +206,8 @@ public void testMissingExceptionInMapping() {
176
206
assertNull (mav );
177
207
}
178
208
179
- public void testTwoMappings () {
209
+ @ Test
210
+ public void twoMappings () {
180
211
Properties props = new Properties ();
181
212
props .setProperty ("java.lang.Exception" , "error" );
182
213
props .setProperty ("AnotherException" , "another-error" );
@@ -186,7 +217,8 @@ public void testTwoMappings() {
186
217
assertEquals ("error" , mav .getViewName ());
187
218
}
188
219
189
- public void testTwoMappingsOneShortOneLong () {
220
+ @ Test
221
+ public void twoMappingsOneShortOneLong () {
190
222
Properties props = new Properties ();
191
223
props .setProperty ("Exception" , "error" );
192
224
props .setProperty ("AnotherException" , "another-error" );
@@ -196,7 +228,8 @@ public void testTwoMappingsOneShortOneLong() {
196
228
assertEquals ("error" , mav .getViewName ());
197
229
}
198
230
199
- public void testTwoMappingsOneShortOneLongThrowOddException () {
231
+ @ Test
232
+ public void twoMappingsOneShortOneLongThrowOddException () {
200
233
Exception oddException = new SomeOddException ();
201
234
Properties props = new Properties ();
202
235
props .setProperty ("Exception" , "error" );
@@ -207,7 +240,8 @@ public void testTwoMappingsOneShortOneLongThrowOddException() {
207
240
assertEquals ("error" , mav .getViewName ());
208
241
}
209
242
210
- public void testTwoMappingsThrowOddExceptionUseLongExceptionMapping () {
243
+ @ Test
244
+ public void twoMappingsThrowOddExceptionUseLongExceptionMapping () {
211
245
Exception oddException = new SomeOddException ();
212
246
Properties props = new Properties ();
213
247
props .setProperty ("java.lang.Exception" , "error" );
@@ -218,7 +252,8 @@ public void testTwoMappingsThrowOddExceptionUseLongExceptionMapping() {
218
252
assertEquals ("another-error" , mav .getViewName ());
219
253
}
220
254
221
- public void testThreeMappings () {
255
+ @ Test
256
+ public void threeMappings () {
222
257
Exception oddException = new AnotherOddException ();
223
258
Properties props = new Properties ();
224
259
props .setProperty ("java.lang.Exception" , "error" );
0 commit comments