1
1
/*
2
- * Copyright 2002-2011 the original author or authors.
2
+ * Copyright 2002-2012 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.
36
36
37
37
/**
38
38
* Test fixture for {@link InvocableHandlerMethod} unit tests.
39
- *
39
+ *
40
40
* @author Rossen Stoyanchev
41
41
*/
42
42
public class InvocableHandlerMethodTests {
43
43
44
- private InvocableHandlerMethod handleMethod ;
44
+ private InvocableHandlerMethod handlerMethod ;
45
45
46
46
private NativeWebRequest webRequest ;
47
47
48
48
@ Before
49
49
public void setUp () throws Exception {
50
50
Method method = Handler .class .getDeclaredMethod ("handle" , Integer .class , String .class );
51
- this .handleMethod = new InvocableHandlerMethod (new Handler (), method );
51
+ this .handlerMethod = new InvocableHandlerMethod (new Handler (), method );
52
52
this .webRequest = new ServletWebRequest (new MockHttpServletRequest (), new MockHttpServletResponse ());
53
53
}
54
54
@@ -60,14 +60,14 @@ public void resolveArg() throws Exception {
60
60
HandlerMethodArgumentResolverComposite composite = new HandlerMethodArgumentResolverComposite ();
61
61
composite .addResolver (intResolver );
62
62
composite .addResolver (stringResolver );
63
- handleMethod .setHandlerMethodArgumentResolvers (composite );
64
-
65
- Object returnValue = handleMethod .invokeForRequest (webRequest , null );
66
-
63
+ handlerMethod .setHandlerMethodArgumentResolvers (composite );
64
+
65
+ Object returnValue = handlerMethod .invokeForRequest (webRequest , null );
66
+
67
67
assertEquals (1 , intResolver .getResolvedParameters ().size ());
68
68
assertEquals (1 , stringResolver .getResolvedParameters ().size ());
69
69
assertEquals ("99-value" , returnValue );
70
-
70
+
71
71
assertEquals ("intArg" , intResolver .getResolvedParameters ().get (0 ).getParameterName ());
72
72
assertEquals ("stringArg" , stringResolver .getResolvedParameters ().get (0 ).getParameterName ());
73
73
}
@@ -80,10 +80,10 @@ public void resolveNullArg() throws Exception {
80
80
HandlerMethodArgumentResolverComposite composite = new HandlerMethodArgumentResolverComposite ();
81
81
composite .addResolver (intResolver );
82
82
composite .addResolver (stringResolver );
83
- handleMethod .setHandlerMethodArgumentResolvers (composite );
84
-
85
- Object returnValue = handleMethod .invokeForRequest (webRequest , null );
86
-
83
+ handlerMethod .setHandlerMethodArgumentResolvers (composite );
84
+
85
+ Object returnValue = handlerMethod .invokeForRequest (webRequest , null );
86
+
87
87
assertEquals (1 , intResolver .getResolvedParameters ().size ());
88
88
assertEquals (1 , stringResolver .getResolvedParameters ().size ());
89
89
assertEquals ("null-null" , returnValue );
@@ -92,7 +92,7 @@ public void resolveNullArg() throws Exception {
92
92
@ Test
93
93
public void cannotResolveArg () throws Exception {
94
94
try {
95
- handleMethod .invokeForRequest (webRequest , null );
95
+ handlerMethod .invokeForRequest (webRequest , null );
96
96
fail ("Expected exception" );
97
97
} catch (IllegalStateException ex ) {
98
98
assertTrue (ex .getMessage ().contains ("No suitable resolver for argument [0] [type=java.lang.Integer]" ));
@@ -101,7 +101,7 @@ public void cannotResolveArg() throws Exception {
101
101
102
102
@ Test
103
103
public void resolveProvidedArg () throws Exception {
104
- Object returnValue = handleMethod .invokeForRequest (webRequest , null , 99 , "value" );
104
+ Object returnValue = handlerMethod .invokeForRequest (webRequest , null , 99 , "value" );
105
105
106
106
assertEquals (String .class , returnValue .getClass ());
107
107
assertEquals ("99-value" , returnValue );
@@ -115,21 +115,21 @@ public void resolveProvidedArgFirst() throws Exception {
115
115
HandlerMethodArgumentResolverComposite composite = new HandlerMethodArgumentResolverComposite ();
116
116
composite .addResolver (intResolver );
117
117
composite .addResolver (stringResolver );
118
- handleMethod .setHandlerMethodArgumentResolvers (composite );
118
+ handlerMethod .setHandlerMethodArgumentResolvers (composite );
119
119
120
- Object returnValue = handleMethod .invokeForRequest (webRequest , null , 2 , "value2" );
120
+ Object returnValue = handlerMethod .invokeForRequest (webRequest , null , 2 , "value2" );
121
121
122
122
assertEquals ("2-value2" , returnValue );
123
123
}
124
-
124
+
125
125
@ Test
126
126
public void exceptionInResolvingArg () throws Exception {
127
127
HandlerMethodArgumentResolverComposite composite = new HandlerMethodArgumentResolverComposite ();
128
128
composite .addResolver (new ExceptionRaisingArgumentResolver ());
129
- handleMethod .setHandlerMethodArgumentResolvers (composite );
130
-
129
+ handlerMethod .setHandlerMethodArgumentResolvers (composite );
130
+
131
131
try {
132
- handleMethod .invokeForRequest (webRequest , null );
132
+ handlerMethod .invokeForRequest (webRequest , null );
133
133
fail ("Expected exception" );
134
134
} catch (HttpMessageNotReadableException ex ) {
135
135
// Expected..
@@ -145,10 +145,10 @@ public void illegalArgumentException() throws Exception {
145
145
HandlerMethodArgumentResolverComposite composite = new HandlerMethodArgumentResolverComposite ();
146
146
composite .addResolver (intResolver );
147
147
composite .addResolver (stringResolver );
148
- handleMethod .setHandlerMethodArgumentResolvers (composite );
148
+ handlerMethod .setHandlerMethodArgumentResolvers (composite );
149
149
150
150
try {
151
- handleMethod .invokeForRequest (webRequest , null );
151
+ handlerMethod .invokeForRequest (webRequest , null );
152
152
fail ("Expected exception" );
153
153
} catch (IllegalArgumentException ex ) {
154
154
assertNotNull ("Exception not wrapped" , ex .getCause ());
@@ -200,30 +200,30 @@ private void invokeExceptionRaisingHandler(Throwable expected) throws Exception
200
200
new InvocableHandlerMethod (handler , method ).invokeForRequest (webRequest , null );
201
201
fail ("Expected exception" );
202
202
}
203
-
203
+
204
204
@ SuppressWarnings ("unused" )
205
205
private static class Handler {
206
-
206
+
207
207
public String handle (Integer intArg , String stringArg ) {
208
208
return intArg + "-" + stringArg ;
209
209
}
210
210
}
211
211
212
212
@ SuppressWarnings ("unused" )
213
213
private static class ExceptionRaisingHandler {
214
-
214
+
215
215
private final Throwable t ;
216
216
217
217
public ExceptionRaisingHandler (Throwable t ) {
218
218
this .t = t ;
219
219
}
220
-
220
+
221
221
public void raiseException () throws Throwable {
222
222
throw t ;
223
223
}
224
-
224
+
225
225
}
226
-
226
+
227
227
private static class ExceptionRaisingArgumentResolver implements HandlerMethodArgumentResolver {
228
228
229
229
public boolean supportsParameter (MethodParameter parameter ) {
@@ -235,5 +235,5 @@ public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer m
235
235
throw new HttpMessageNotReadableException ("oops, can't read" );
236
236
}
237
237
}
238
-
238
+
239
239
}
0 commit comments