1
1
/*
2
- * Copyright 2012-2017 the original author or authors.
2
+ * Copyright 2012-2018 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.
40
40
import org .springframework .mock .web .test .MockServletContext ;
41
41
import org .springframework .stereotype .Controller ;
42
42
import org .springframework .util .MultiValueMap ;
43
+ import org .springframework .web .bind .annotation .GetMapping ;
43
44
import org .springframework .web .bind .annotation .PathVariable ;
44
45
import org .springframework .web .bind .annotation .RequestBody ;
45
46
import org .springframework .web .bind .annotation .RequestMapping ;
54
55
import org .springframework .web .util .UriComponents ;
55
56
import org .springframework .web .util .UriComponentsBuilder ;
56
57
57
- import static org .hamcrest .Matchers .contains ;
58
- import static org .hamcrest .Matchers .containsInAnyOrder ;
59
- import static org .hamcrest .Matchers .endsWith ;
60
- import static org .hamcrest .Matchers .is ;
61
- import static org .hamcrest .Matchers .startsWith ;
62
- import static org .junit .Assert .assertEquals ;
63
- import static org .junit .Assert .assertThat ;
64
- import static org .springframework .web .servlet .mvc .method .annotation .MvcUriComponentsBuilder .fromController ;
65
- import static org .springframework .web .servlet .mvc .method .annotation .MvcUriComponentsBuilder .fromMethodCall ;
66
- import static org .springframework .web .servlet .mvc .method .annotation .MvcUriComponentsBuilder .fromMethodName ;
67
- import static org .springframework .web .servlet .mvc .method .annotation .MvcUriComponentsBuilder .on ;
58
+ import static org .hamcrest .Matchers .*;
59
+ import static org .junit .Assert .*;
60
+ import static org .springframework .web .servlet .mvc .method .annotation .MvcUriComponentsBuilder .*;
68
61
69
62
/**
70
63
* Unit tests for {@link org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder}.
@@ -142,15 +135,15 @@ public void testFromControllerWithCustomBaseUrlViaInstance() {
142
135
}
143
136
144
137
@ Test
145
- public void testFromMethodNamePathVariable () throws Exception {
146
- UriComponents uriComponents = fromMethodName (
147
- ControllerWithMethods . class , "methodWithPathVariable" , new Object []{ "1" } ).build ();
138
+ public void testFromMethodNamePathVariable () {
139
+ UriComponents uriComponents = fromMethodName (ControllerWithMethods . class ,
140
+ "methodWithPathVariable" , "1" ).build ();
148
141
149
142
assertThat (uriComponents .toUriString (), is ("http://localhost/something/1/foo" ));
150
143
}
151
144
152
145
@ Test
153
- public void testFromMethodNameTypeLevelPathVariable () throws Exception {
146
+ public void testFromMethodNameTypeLevelPathVariable () {
154
147
this .request .setContextPath ("/myapp" );
155
148
UriComponents uriComponents = fromMethodName (
156
149
PersonsAddressesController .class , "getAddressesForCountry" , "DE" ).buildAndExpand ("1" );
@@ -159,7 +152,7 @@ public void testFromMethodNameTypeLevelPathVariable() throws Exception {
159
152
}
160
153
161
154
@ Test
162
- public void testFromMethodNameTwoPathVariables () throws Exception {
155
+ public void testFromMethodNameTwoPathVariables () {
163
156
DateTime now = DateTime .now ();
164
157
UriComponents uriComponents = fromMethodName (
165
158
ControllerWithMethods .class , "methodWithTwoPathVariables" , 1 , now ).build ();
@@ -168,7 +161,7 @@ public void testFromMethodNameTwoPathVariables() throws Exception {
168
161
}
169
162
170
163
@ Test
171
- public void testFromMethodNameWithPathVarAndRequestParam () throws Exception {
164
+ public void testFromMethodNameWithPathVarAndRequestParam () {
172
165
UriComponents uriComponents = fromMethodName (
173
166
ControllerWithMethods .class , "methodForNextPage" , "1" , 10 , 5 ).build ();
174
167
@@ -178,59 +171,56 @@ public void testFromMethodNameWithPathVarAndRequestParam() throws Exception {
178
171
assertThat (queryParams .get ("offset" ), contains ("10" ));
179
172
}
180
173
181
- // SPR-12977
182
-
183
- @ Test
184
- public void fromMethodNameWithBridgedMethod () throws Exception {
174
+ @ Test // SPR-12977
175
+ public void fromMethodNameWithBridgedMethod () {
185
176
UriComponents uriComponents = fromMethodName (PersonCrudController .class , "get" , (long ) 42 ).build ();
177
+
186
178
assertThat (uriComponents .toUriString (), is ("http://localhost/42" ));
187
179
}
188
180
189
- // SPR-11391
190
-
191
- @ Test
192
- public void testFromMethodNameTypeLevelPathVariableWithoutArgumentValue () throws Exception {
181
+ @ Test // SPR-11391
182
+ public void testFromMethodNameTypeLevelPathVariableWithoutArgumentValue () {
193
183
UriComponents uriComponents = fromMethodName (UserContactController .class , "showCreate" , 123 ).build ();
194
184
195
185
assertThat (uriComponents .getPath (), is ("/user/123/contacts/create" ));
196
186
}
197
187
198
188
@ Test
199
- public void testFromMethodNameNotMapped () throws Exception {
189
+ public void testFromMethodNameNotMapped () {
200
190
UriComponents uriComponents = fromMethodName (UnmappedController .class , "unmappedMethod" ).build ();
201
191
202
192
assertThat (uriComponents .toUriString (), is ("http://localhost/" ));
203
193
}
204
194
205
195
@ Test
206
- public void testFromMethodNameWithCustomBaseUrlViaStaticCall () throws Exception {
196
+ public void testFromMethodNameWithCustomBaseUrlViaStaticCall () {
207
197
UriComponentsBuilder builder = UriComponentsBuilder .fromUriString ("http://example.org:9090/base" );
208
198
UriComponents uriComponents = fromMethodName (builder , ControllerWithMethods .class ,
209
- "methodWithPathVariable" , new Object [] { "1" } ).build ();
199
+ "methodWithPathVariable" , "1" ).build ();
210
200
211
201
assertEquals ("http://example.org:9090/base/something/1/foo" , uriComponents .toString ());
212
202
assertEquals ("http://example.org:9090/base" , builder .toUriString ());
213
203
}
214
204
215
205
@ Test
216
- public void testFromMethodNameWithCustomBaseUrlViaInstance () throws Exception {
206
+ public void testFromMethodNameWithCustomBaseUrlViaInstance () {
217
207
UriComponentsBuilder builder = UriComponentsBuilder .fromUriString ("http://example.org:9090/base" );
218
208
MvcUriComponentsBuilder mvcBuilder = MvcUriComponentsBuilder .relativeTo (builder );
219
209
UriComponents uriComponents = mvcBuilder .withMethodName (ControllerWithMethods .class ,
220
- "methodWithPathVariable" , new Object [] { "1" } ).build ();
210
+ "methodWithPathVariable" , "1" ).build ();
221
211
222
212
assertEquals ("http://example.org:9090/base/something/1/foo" , uriComponents .toString ());
223
213
assertEquals ("http://example.org:9090/base" , builder .toUriString ());
224
214
}
225
215
226
216
@ Test
227
- public void testFromMethodNameWithMetaAnnotation () throws Exception {
217
+ public void testFromMethodNameWithMetaAnnotation () {
228
218
UriComponents uriComponents = fromMethodName (MetaAnnotationController .class , "handleInput" ).build ();
229
219
assertThat (uriComponents .toUriString (), is ("http://localhost/input" ));
230
220
}
231
221
232
- @ Test // SPR-14405
233
- public void testFromMappingNameWithOptionalParam () throws Exception {
222
+ @ Test // SPR-14405
223
+ public void testFromMappingNameWithOptionalParam () {
234
224
UriComponents uriComponents = fromMethodName (ControllerWithMethods .class ,
235
225
"methodWithOptionalParam" , new Object [] {null }).build ();
236
226
@@ -255,26 +245,26 @@ public void testFromMethodCallOnSubclass() {
255
245
256
246
@ Test
257
247
public void testFromMethodCallWithTypeLevelUriVars () {
258
- UriComponents uriComponents = fromMethodCall (on (
259
- PersonsAddressesController .class ).getAddressesForCountry ("DE" )).buildAndExpand (15 );
248
+ UriComponents uriComponents = fromMethodCall (
249
+ on ( PersonsAddressesController .class ).getAddressesForCountry ("DE" )).buildAndExpand (15 );
260
250
261
251
assertThat (uriComponents .toUriString (), endsWith ("/people/15/addresses/DE" ));
262
252
}
263
253
264
254
265
255
@ Test
266
256
public void testFromMethodCallWithPathVar () {
267
- UriComponents uriComponents = fromMethodCall (on (
268
- ControllerWithMethods .class ).methodWithPathVariable ("1" )).build ();
257
+ UriComponents uriComponents = fromMethodCall (
258
+ on ( ControllerWithMethods .class ).methodWithPathVariable ("1" )).build ();
269
259
270
260
assertThat (uriComponents .toUriString (), startsWith ("http://localhost" ));
271
261
assertThat (uriComponents .toUriString (), endsWith ("/something/1/foo" ));
272
262
}
273
263
274
264
@ Test
275
265
public void testFromMethodCallWithPathVarAndRequestParams () {
276
- UriComponents uriComponents = fromMethodCall (on (
277
- ControllerWithMethods .class ).methodForNextPage ("1" , 10 , 5 )).build ();
266
+ UriComponents uriComponents = fromMethodCall (
267
+ on ( ControllerWithMethods .class ).methodForNextPage ("1" , 10 , 5 )).build ();
278
268
279
269
assertThat (uriComponents .getPath (), is ("/something/1/foo" ));
280
270
@@ -285,8 +275,8 @@ public void testFromMethodCallWithPathVarAndRequestParams() {
285
275
286
276
@ Test
287
277
public void testFromMethodCallWithPathVarAndMultiValueRequestParams () {
288
- UriComponents uriComponents = fromMethodCall (on (
289
- ControllerWithMethods .class ).methodWithMultiValueRequestParams ("1" , Arrays .asList (3 , 7 ), 5 )).build ();
278
+ UriComponents uriComponents = fromMethodCall (
279
+ on ( ControllerWithMethods .class ).methodWithMultiValueRequestParams ("1" , Arrays .asList (3 , 7 ), 5 )).build ();
290
280
291
281
assertThat (uriComponents .getPath (), is ("/something/1/foo" ));
292
282
@@ -315,7 +305,7 @@ public void testFromMethodCallWithCustomBaseUrlViaInstance() {
315
305
}
316
306
317
307
@ Test
318
- public void testFromMappingName () throws Exception {
308
+ public void testFromMappingName () {
319
309
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext ();
320
310
context .setServletContext (new MockServletContext ());
321
311
context .register (WebConfig .class );
@@ -332,7 +322,7 @@ public void testFromMappingName() throws Exception {
332
322
}
333
323
334
324
@ Test
335
- public void testFromMappingNameWithCustomBaseUrl () throws Exception {
325
+ public void testFromMappingNameWithCustomBaseUrl () {
336
326
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext ();
337
327
context .setServletContext (new MockServletContext ());
338
328
context .register (WebConfig .class );
@@ -370,6 +360,13 @@ public void usesFirstHostOfXForwardedHost() {
370
360
assertThat (uriComponents .toUriString (), startsWith ("http://barfoo:8888" ));
371
361
}
372
362
363
+ @ Test // SPR-16710
364
+ public void withStringReturnType () {
365
+ UriComponents uriComponents = MvcUriComponentsBuilder .fromMethodCall (
366
+ on (BookingController .class ).getBooking (21L )).buildAndExpand (42 );
367
+ assertEquals ("http://localhost/hotels/42/bookings/21" , uriComponents .encode ().toUri ().toString ());
368
+ }
369
+
373
370
374
371
static class Person {
375
372
@@ -516,4 +513,15 @@ public PersonsAddressesController controller() {
516
513
}
517
514
}
518
515
516
+
517
+ @ Controller
518
+ @ RequestMapping ("/hotels/{hotel}" )
519
+ public class BookingController {
520
+
521
+ @ GetMapping ("/bookings/{booking}" )
522
+ public Object getBooking (@ PathVariable Long booking ) {
523
+ return "url" ;
524
+ }
525
+ }
526
+
519
527
}
0 commit comments