1
1
/*
2
- * Copyright 2002-2015 the original author or authors.
2
+ * Copyright 2002-2016 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.
16
16
17
17
package org .springframework .web .servlet .mvc .method .annotation ;
18
18
19
- import static org .junit .Assert .*;
20
-
21
19
import java .lang .reflect .Method ;
22
20
import java .util .HashMap ;
23
21
import java .util .Map ;
22
+ import java .util .Optional ;
24
23
25
24
import org .junit .Before ;
26
25
import org .junit .Test ;
27
26
28
27
import org .springframework .core .MethodParameter ;
28
+ import org .springframework .core .annotation .SynthesizingMethodParameter ;
29
+ import org .springframework .core .convert .support .DefaultConversionService ;
29
30
import org .springframework .mock .web .test .MockHttpServletRequest ;
30
31
import org .springframework .mock .web .test .MockHttpServletResponse ;
32
+ import org .springframework .util .ReflectionUtils ;
31
33
import org .springframework .web .bind .MissingPathVariableException ;
32
34
import org .springframework .web .bind .annotation .PathVariable ;
35
+ import org .springframework .web .bind .support .ConfigurableWebBindingInitializer ;
36
+ import org .springframework .web .bind .support .DefaultDataBinderFactory ;
37
+ import org .springframework .web .bind .support .WebDataBinderFactory ;
33
38
import org .springframework .web .context .request .ServletWebRequest ;
34
39
import org .springframework .web .method .support .ModelAndViewContainer ;
35
40
import org .springframework .web .servlet .HandlerMapping ;
36
41
import org .springframework .web .servlet .View ;
37
42
43
+ import static org .junit .Assert .*;
44
+
38
45
/**
39
46
* Test fixture with {@link PathVariableMethodArgumentResolver}.
40
47
*
41
48
* @author Rossen Stoyanchev
49
+ * @author Juergen Hoeller
42
50
*/
43
51
public class PathVariableMethodArgumentResolverTests {
44
52
@@ -48,25 +56,33 @@ public class PathVariableMethodArgumentResolverTests {
48
56
49
57
private MethodParameter paramString ;
50
58
59
+ private MethodParameter paramNotRequired ;
60
+
61
+ private MethodParameter paramOptional ;
62
+
51
63
private ModelAndViewContainer mavContainer ;
52
64
53
65
private ServletWebRequest webRequest ;
54
66
55
67
private MockHttpServletRequest request ;
56
68
69
+
57
70
@ Before
58
71
public void setUp () throws Exception {
59
72
resolver = new PathVariableMethodArgumentResolver ();
60
73
61
- Method method = getClass ().getMethod ("handle" , String .class , String .class );
62
- paramNamedString = new MethodParameter (method , 0 );
63
- paramString = new MethodParameter (method , 1 );
74
+ Method method = ReflectionUtils .findMethod (getClass (), "handle" , (Class <?>[]) null );
75
+ paramNamedString = new SynthesizingMethodParameter (method , 0 );
76
+ paramString = new SynthesizingMethodParameter (method , 1 );
77
+ paramNotRequired = new SynthesizingMethodParameter (method , 2 );
78
+ paramOptional = new SynthesizingMethodParameter (method , 3 );
64
79
65
80
mavContainer = new ModelAndViewContainer ();
66
81
request = new MockHttpServletRequest ();
67
82
webRequest = new ServletWebRequest (request , new MockHttpServletResponse ());
68
83
}
69
84
85
+
70
86
@ Test
71
87
public void supportsParameter () {
72
88
assertTrue ("Parameter with @PathVariable annotation" , resolver .supportsParameter (paramNamedString ));
@@ -89,21 +105,58 @@ public void resolveArgument() throws Exception {
89
105
assertEquals ("value" , pathVars .get ("name" ));
90
106
}
91
107
92
- @ SuppressWarnings ("unchecked" )
108
+ @ Test
109
+ public void resolveArgumentNotRequired () throws Exception {
110
+ Map <String , String > uriTemplateVars = new HashMap <>();
111
+ uriTemplateVars .put ("name" , "value" );
112
+ request .setAttribute (HandlerMapping .URI_TEMPLATE_VARIABLES_ATTRIBUTE , uriTemplateVars );
113
+
114
+ String result = (String ) resolver .resolveArgument (paramNotRequired , mavContainer , webRequest , null );
115
+ assertEquals ("PathVariable not resolved correctly" , "value" , result );
116
+
117
+ @ SuppressWarnings ("unchecked" )
118
+ Map <String , Object > pathVars = (Map <String , Object >) request .getAttribute (View .PATH_VARIABLES );
119
+ assertNotNull (pathVars );
120
+ assertEquals (1 , pathVars .size ());
121
+ assertEquals ("value" , pathVars .get ("name" ));
122
+ }
123
+
124
+ @ Test
125
+ public void resolveArgumentWrappedAsOptional () throws Exception {
126
+ Map <String , String > uriTemplateVars = new HashMap <>();
127
+ uriTemplateVars .put ("name" , "value" );
128
+ request .setAttribute (HandlerMapping .URI_TEMPLATE_VARIABLES_ATTRIBUTE , uriTemplateVars );
129
+
130
+ ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer ();
131
+ initializer .setConversionService (new DefaultConversionService ());
132
+ WebDataBinderFactory binderFactory = new DefaultDataBinderFactory (initializer );
133
+
134
+ @ SuppressWarnings ("unchecked" )
135
+ Optional <String > result = (Optional <String >)
136
+ resolver .resolveArgument (paramOptional , mavContainer , webRequest , binderFactory );
137
+ assertEquals ("PathVariable not resolved correctly" , "value" , result .get ());
138
+
139
+ @ SuppressWarnings ("unchecked" )
140
+ Map <String , Object > pathVars = (Map <String , Object >) request .getAttribute (View .PATH_VARIABLES );
141
+ assertNotNull (pathVars );
142
+ assertEquals (1 , pathVars .size ());
143
+ assertEquals (Optional .of ("value" ), pathVars .get ("name" ));
144
+ }
145
+
93
146
@ Test
94
147
public void resolveArgumentWithExistingPathVars () throws Exception {
95
148
Map <String , String > uriTemplateVars = new HashMap <String , String >();
96
149
uriTemplateVars .put ("name" , "value" );
97
150
request .setAttribute (HandlerMapping .URI_TEMPLATE_VARIABLES_ATTRIBUTE , uriTemplateVars );
98
151
99
- Map <String , Object > pathVars ;
100
152
uriTemplateVars .put ("oldName" , "oldValue" );
101
153
request .setAttribute (View .PATH_VARIABLES , uriTemplateVars );
102
154
103
155
String result = (String ) resolver .resolveArgument (paramNamedString , mavContainer , webRequest , null );
104
156
assertEquals ("PathVariable not resolved correctly" , "value" , result );
105
157
106
- pathVars = (Map <String , Object >) request .getAttribute (View .PATH_VARIABLES );
158
+ @ SuppressWarnings ("unchecked" )
159
+ Map <String , Object > pathVars = (Map <String , Object >) request .getAttribute (View .PATH_VARIABLES );
107
160
assertNotNull (pathVars );
108
161
assertEquals (2 , pathVars .size ());
109
162
assertEquals ("value" , pathVars .get ("name" ));
@@ -113,11 +166,28 @@ public void resolveArgumentWithExistingPathVars() throws Exception {
113
166
@ Test (expected = MissingPathVariableException .class )
114
167
public void handleMissingValue () throws Exception {
115
168
resolver .resolveArgument (paramNamedString , mavContainer , webRequest , null );
116
- fail ("Unresolved path variable should lead to exception." );
169
+ fail ("Unresolved path variable should lead to exception" );
170
+ }
171
+
172
+ @ Test
173
+ public void nullIfNotRequired () throws Exception {
174
+ assertNull (resolver .resolveArgument (paramNotRequired , mavContainer , webRequest , null ));
117
175
}
118
176
177
+ @ Test
178
+ public void wrapEmptyWithOptional () throws Exception {
179
+ ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer ();
180
+ initializer .setConversionService (new DefaultConversionService ());
181
+ WebDataBinderFactory binderFactory = new DefaultDataBinderFactory (initializer );
182
+
183
+ assertEquals (Optional .empty (), resolver .resolveArgument (paramOptional , mavContainer , webRequest , binderFactory ));
184
+ }
185
+
186
+
119
187
@ SuppressWarnings ("unused" )
120
- public void handle (@ PathVariable (value = "name" ) String param1 , String param2 ) {
188
+ public void handle (@ PathVariable ("name" ) String param1 , String param2 ,
189
+ @ PathVariable (name ="name" , required = false ) String param3 ,
190
+ @ PathVariable ("name" ) Optional <String > param4 ) {
121
191
}
122
192
123
- }
193
+ }
0 commit comments