40
40
41
41
import static org .hamcrest .Matchers .*;
42
42
import static org .junit .Assert .assertThat ;
43
- import static org .springframework .web .servlet .mvc .method .annotation .MvcUriComponentsBuilder .on ;
43
+ import static org .springframework .web .servlet .mvc .method .annotation .MvcUriComponentsBuilder .* ;
44
44
45
45
/**
46
46
* Unit tests for {@link org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder}.
49
49
* @author Dietrich Schulten
50
50
* @author Rossen Stoyanchev
51
51
*/
52
- public class MvcUriComponentsContributorTests {
52
+ public class MvcUriComponentsBuilderTests {
53
53
54
54
private MockHttpServletRequest request ;
55
55
56
- private MvcUriComponentsBuilder builder ;
57
-
58
56
59
57
@ Before
60
58
public void setUp () {
@@ -69,66 +67,65 @@ public void tearDown() {
69
67
70
68
71
69
@ Test
72
- public void fromController () {
73
- UriComponents uriComponents = this . builder . fromController (PersonControllerImpl .class ).build ();
70
+ public void testFromController () {
71
+ UriComponents uriComponents = fromController (PersonControllerImpl .class ).build ();
74
72
assertThat (uriComponents .toUriString (), Matchers .endsWith ("/people" ));
75
73
}
76
74
77
75
@ Test
78
- public void fromControllerUriTemplate () {
79
- UriComponents uriComponents = this . builder . fromController (PersonsAddressesController .class ).buildAndExpand (15 );
76
+ public void testFromControllerUriTemplate () {
77
+ UriComponents uriComponents = fromController (PersonsAddressesController .class ).buildAndExpand (15 );
80
78
assertThat (uriComponents .toUriString (), endsWith ("/people/15/addresses" ));
81
79
}
82
80
83
81
@ Test
84
- public void fromControllerSubResource () {
85
- UriComponents uriComponents =
86
- this .builder .fromController (PersonControllerImpl .class ).pathSegment ("something" ).build ();
82
+ public void testFromControllerSubResource () {
83
+ UriComponents uriComponents = fromController (PersonControllerImpl .class ).pathSegment ("something" ).build ();
87
84
88
85
assertThat (uriComponents .toUriString (), endsWith ("/people/something" ));
89
86
}
90
87
91
88
@ Test
92
- public void fromControllerTwoTypeLevelMappings () {
93
- UriComponents uriComponents = this . builder . fromController (InvalidController .class ).build ();
89
+ public void testFromControllerTwoTypeLevelMappings () {
90
+ UriComponents uriComponents = fromController (InvalidController .class ).build ();
94
91
assertThat (uriComponents .toUriString (), is ("http://localhost/persons" ));
95
92
}
96
93
97
94
@ Test
98
- public void fromControllerNotMapped () {
99
- UriComponents uriComponents = this . builder . fromController (UnmappedController .class ).build ();
95
+ public void testFromControllerNotMapped () {
96
+ UriComponents uriComponents = fromController (UnmappedController .class ).build ();
100
97
assertThat (uriComponents .toUriString (), is ("http://localhost/" ));
101
98
}
102
99
103
100
@ Test
104
- public void fromMethodPathVariable () throws Exception {
105
- UriComponents uriComponents = this . builder . fromMethodName (
101
+ public void testFromMethodPathVariable () throws Exception {
102
+ UriComponents uriComponents = fromMethodName (
106
103
ControllerWithMethods .class , "methodWithPathVariable" , new Object []{"1" }).build ();
107
104
108
105
assertThat (uriComponents .toUriString (), is ("http://localhost/something/1/foo" ));
109
106
}
110
107
111
108
@ Test
112
- public void fromMethodTypeLevelPathVariable () throws Exception {
109
+ public void testFromMethodTypeLevelPathVariable () throws Exception {
113
110
this .request .setContextPath ("/myapp" );
114
- UriComponents uriComponents = this . builder . fromMethodName (
111
+ UriComponents uriComponents = fromMethodName (
115
112
PersonsAddressesController .class , "getAddressesForCountry" , "DE" ).buildAndExpand ("1" );
116
113
117
114
assertThat (uriComponents .toUriString (), is ("http://localhost/myapp/people/1/addresses/DE" ));
118
115
}
119
116
120
117
@ Test
121
- public void fromMethodTwoPathVariables () throws Exception {
118
+ public void testFromMethodTwoPathVariables () throws Exception {
122
119
DateTime now = DateTime .now ();
123
- UriComponents uriComponents = this . builder . fromMethodName (
120
+ UriComponents uriComponents = fromMethodName (
124
121
ControllerWithMethods .class , "methodWithTwoPathVariables" , 1 , now ).build ();
125
122
126
123
assertThat (uriComponents .getPath (), is ("/something/1/foo/" + ISODateTimeFormat .date ().print (now )));
127
124
}
128
125
129
126
@ Test
130
- public void fromMethodWithPathVarAndRequestParam () throws Exception {
131
- UriComponents uriComponents = this . builder . fromMethodName (
127
+ public void testFromMethodWithPathVarAndRequestParam () throws Exception {
128
+ UriComponents uriComponents = fromMethodName (
132
129
ControllerWithMethods .class , "methodForNextPage" , "1" , 10 , 5 ).build ();
133
130
134
131
assertThat (uriComponents .getPath (), is ("/something/1/foo" ));
@@ -138,43 +135,42 @@ public void fromMethodWithPathVarAndRequestParam() throws Exception {
138
135
}
139
136
140
137
@ Test
141
- public void fromMethodNotMapped () throws Exception {
142
- UriComponents uriComponents = this . builder . fromMethodName (UnmappedController .class , "unmappedMethod" ).build ();
138
+ public void testFromMethodNotMapped () throws Exception {
139
+ UriComponents uriComponents = fromMethodName (UnmappedController .class , "unmappedMethod" ).build ();
143
140
144
141
assertThat (uriComponents .toUriString (), is ("http://localhost/" ));
145
142
}
146
143
147
144
@ Test
148
- public void fromMethodCall () {
149
- UriComponents uriComponents = this .builder .fromMethodCall (
150
- on (ControllerWithMethods .class ).myMethod (null )).build ();
145
+ public void testFromMethodCall () {
146
+ UriComponents uriComponents = fromMethodCall (on (ControllerWithMethods .class ).myMethod (null )).build ();
151
147
152
148
assertThat (uriComponents .toUriString (), startsWith ("http://localhost" ));
153
149
assertThat (uriComponents .toUriString (), endsWith ("/something/else" ));
154
150
}
155
151
156
152
@ Test
157
- public void fromMethodCallWithTypeLevelUriVars () {
158
- UriComponents uriComponents = this . builder . fromMethodCall (
159
- on ( PersonsAddressesController .class ).getAddressesForCountry ("DE" )).buildAndExpand (15 );
153
+ public void testFromMethodCallWithTypeLevelUriVars () {
154
+ UriComponents uriComponents = fromMethodCall ( on (
155
+ PersonsAddressesController .class ).getAddressesForCountry ("DE" )).buildAndExpand (15 );
160
156
161
157
assertThat (uriComponents .toUriString (), endsWith ("/people/15/addresses/DE" ));
162
158
}
163
159
164
160
165
161
@ Test
166
- public void fromMethodCallWithPathVar () {
167
- UriComponents uriComponents = this . builder . fromMethodCall (
168
- on ( ControllerWithMethods .class ).methodWithPathVariable ("1" )).build ();
162
+ public void testFromMethodCallWithPathVar () {
163
+ UriComponents uriComponents = fromMethodCall ( on (
164
+ ControllerWithMethods .class ).methodWithPathVariable ("1" )).build ();
169
165
170
166
assertThat (uriComponents .toUriString (), startsWith ("http://localhost" ));
171
167
assertThat (uriComponents .toUriString (), endsWith ("/something/1/foo" ));
172
168
}
173
169
174
170
@ Test
175
- public void fromMethodCallWithPathVarAndRequestParams () {
176
- UriComponents uriComponents = this . builder . fromMethodCall (
177
- on ( ControllerWithMethods .class ).methodForNextPage ("1" , 10 , 5 )).build ();
171
+ public void testFromMethodCallWithPathVarAndRequestParams () {
172
+ UriComponents uriComponents = fromMethodCall ( on (
173
+ ControllerWithMethods .class ).methodForNextPage ("1" , 10 , 5 )).build ();
178
174
179
175
assertThat (uriComponents .getPath (), is ("/something/1/foo" ));
180
176
@@ -184,10 +180,9 @@ public void fromMethodCallWithPathVarAndRequestParams() {
184
180
}
185
181
186
182
@ Test
187
- public void fromMethodCallWithPathVarAndMultiValueRequestParams () {
188
- UriComponents uriComponents = this .builder .fromMethodCall (
189
- on (ControllerWithMethods .class ).methodWithMultiValueRequestParams (
190
- "1" , Arrays .asList (3 , 7 ), 5 )).build ();
183
+ public void testFromMethodCallWithPathVarAndMultiValueRequestParams () {
184
+ UriComponents uriComponents = fromMethodCall (on (
185
+ ControllerWithMethods .class ).methodWithMultiValueRequestParams ("1" , Arrays .asList (3 , 7 ), 5 )).build ();
191
186
192
187
assertThat (uriComponents .getPath (), is ("/something/1/foo" ));
193
188
@@ -199,23 +194,23 @@ public void fromMethodCallWithPathVarAndMultiValueRequestParams() {
199
194
@ Test
200
195
public void usesForwardedHostAsHostIfHeaderIsSet () {
201
196
this .request .addHeader ("X-Forwarded-Host" , "somethingDifferent" );
202
- UriComponents uriComponents = this . builder . fromController (PersonControllerImpl .class ).build ();
197
+ UriComponents uriComponents = fromController (PersonControllerImpl .class ).build ();
203
198
204
199
assertThat (uriComponents .toUriString (), startsWith ("http://somethingDifferent" ));
205
200
}
206
201
207
202
@ Test
208
203
public void usesForwardedHostAndPortFromHeader () {
209
204
request .addHeader ("X-Forwarded-Host" , "foobar:8088" );
210
- UriComponents uriComponents = this . builder . fromController (PersonControllerImpl .class ).build ();
205
+ UriComponents uriComponents = fromController (PersonControllerImpl .class ).build ();
211
206
212
207
assertThat (uriComponents .toUriString (), startsWith ("http://foobar:8088" ));
213
208
}
214
209
215
210
@ Test
216
211
public void usesFirstHostOfXForwardedHost () {
217
212
request .addHeader ("X-Forwarded-Host" , "barfoo:8888, localhost:8088" );
218
- UriComponents uriComponents = this . builder . fromController (PersonControllerImpl .class ).build ();
213
+ UriComponents uriComponents = fromController (PersonControllerImpl .class ).build ();
219
214
220
215
assertThat (uriComponents .toUriString (), startsWith ("http://barfoo:8888" ));
221
216
}
@@ -239,6 +234,7 @@ class PersonControllerImpl implements PersonController {
239
234
240
235
}
241
236
237
+ @ SuppressWarnings ("unused" )
242
238
@ RequestMapping ("/people/{id}/addresses" )
243
239
static class PersonsAddressesController {
244
240
@@ -253,13 +249,15 @@ class InvalidController {
253
249
254
250
}
255
251
252
+ @ SuppressWarnings ("unused" )
256
253
class UnmappedController {
257
254
258
255
@ RequestMapping
259
256
public void unmappedMethod () {
260
257
}
261
258
}
262
259
260
+ @ SuppressWarnings ("unused" )
263
261
@ RequestMapping ("/something" )
264
262
static class ControllerWithMethods {
265
263
@@ -292,5 +290,4 @@ HttpEntity<Void> methodWithMultiValueRequestParams(@PathVariable String id,
292
290
}
293
291
}
294
292
295
-
296
293
}
0 commit comments