|
16 | 16 |
|
17 | 17 | package org.springframework.web.servlet.view;
|
18 | 18 |
|
19 |
| -import java.util.ArrayList; |
20 |
| -import java.util.Collections; |
21 | 19 | import java.util.HashMap;
|
22 | 20 | import java.util.LinkedHashMap;
|
23 | 21 | import java.util.List;
|
|
49 | 47 | import static org.mockito.Mockito.verify;
|
50 | 48 |
|
51 | 49 | /**
|
52 |
| - * Tests for redirect view, and query string construction. |
53 |
| - * Doesn't test URL encoding, although it does check that it's called. |
| 50 | + * Tests for redirect view and query string construction. |
| 51 | + * |
| 52 | + * <p>Doesn't test URL encoding, although it does check that it's called. |
54 | 53 | *
|
55 | 54 | * @author Rod Johnson
|
56 | 55 | * @author Juergen Hoeller
|
|
61 | 60 | */
|
62 | 61 | class RedirectViewTests {
|
63 | 62 |
|
64 |
| - private MockHttpServletRequest request; |
| 63 | + private MockHttpServletRequest request = new MockHttpServletRequest(); |
65 | 64 |
|
66 |
| - private MockHttpServletResponse response; |
| 65 | + private MockHttpServletResponse response = new MockHttpServletResponse(); |
67 | 66 |
|
68 | 67 |
|
69 | 68 | @BeforeEach
|
70 | 69 | void setUp() {
|
71 |
| - this.request = new MockHttpServletRequest(); |
72 | 70 | this.request.setContextPath("/context");
|
73 | 71 | this.request.setCharacterEncoding(WebUtils.DEFAULT_CHARACTER_ENCODING);
|
74 | 72 | this.request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());
|
75 | 73 | this.request.setAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE, new SessionFlashMapManager());
|
76 |
| - this.response = new MockHttpServletResponse(); |
77 |
| - |
78 | 74 | }
|
79 | 75 |
|
80 | 76 |
|
81 | 77 | @Test
|
82 | 78 | void noUrlSet() {
|
83 | 79 | RedirectView rv = new RedirectView();
|
84 |
| - assertThatIllegalArgumentException().isThrownBy( |
85 |
| - rv::afterPropertiesSet); |
| 80 | + assertThatIllegalArgumentException().isThrownBy(rv::afterPropertiesSet); |
86 | 81 | }
|
87 | 82 |
|
88 | 83 | @Test
|
@@ -175,7 +170,6 @@ void updateTargetUrl() throws Exception {
|
175 | 170 | verify(mockProcessor).processUrl(request, "/path");
|
176 | 171 | }
|
177 | 172 |
|
178 |
| - |
179 | 173 | @Test
|
180 | 174 | void updateTargetUrlWithContextLoader() throws Exception {
|
181 | 175 | StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
@@ -214,7 +208,6 @@ public void remoteHost() {
|
214 | 208 | assertThat(rv.isRemoteHost("https://url.somewhere.com")).isFalse();
|
215 | 209 | assertThat(rv.isRemoteHost("/path")).isFalse();
|
216 | 210 | assertThat(rv.isRemoteHost("http://somewhereelse.example")).isTrue();
|
217 |
| - |
218 | 211 | }
|
219 | 212 |
|
220 | 213 | @Test // SPR-16752
|
@@ -247,16 +240,15 @@ void singleParam() throws Exception {
|
247 | 240 | String url = "https://url.somewhere.com";
|
248 | 241 | String key = "foo";
|
249 | 242 | String val = "bar";
|
250 |
| - Map<String, String> model = new HashMap<>(); |
251 |
| - model.put(key, val); |
| 243 | + Map<String, String> model = Map.of(key, val); |
252 | 244 | String expectedUrlForEncoding = url + "?" + key + "=" + val;
|
253 | 245 | doTest(model, url, false, expectedUrlForEncoding);
|
254 | 246 | }
|
255 | 247 |
|
256 | 248 | @Test
|
257 | 249 | void singleParamWithoutExposingModelAttributes() throws Exception {
|
258 | 250 | String url = "https://url.somewhere.com";
|
259 |
| - Map<String, String> model = Collections.singletonMap("foo", "bar"); |
| 251 | + Map<String, String> model = Map.of("foo", "bar"); |
260 | 252 |
|
261 | 253 | TestRedirectView rv = new TestRedirectView(url, false, model);
|
262 | 254 | rv.setExposeModelAttributes(false);
|
@@ -289,56 +281,31 @@ void twoParams() throws Exception {
|
289 | 281 | String val = "bar";
|
290 | 282 | String key2 = "thisIsKey2";
|
291 | 283 | String val2 = "andThisIsVal2";
|
292 |
| - Map<String, String> model = new HashMap<>(); |
| 284 | + Map<String, String> model = new LinkedHashMap<>(); |
293 | 285 | model.put(key, val);
|
294 | 286 | model.put(key2, val2);
|
295 |
| - try { |
296 |
| - String expectedUrlForEncoding = url + "?" + key + "=" + val + "&" + key2 + "=" + val2; |
297 |
| - doTest(model, url, false, expectedUrlForEncoding); |
298 |
| - } |
299 |
| - catch (AssertionError err) { |
300 |
| - // OK, so it's the other order... probably on Sun JDK 1.6 or IBM JDK 1.5 |
301 |
| - String expectedUrlForEncoding = url + "?" + key2 + "=" + val2 + "&" + key + "=" + val; |
302 |
| - doTest(model, url, false, expectedUrlForEncoding); |
303 |
| - } |
| 287 | + String expectedUrlForEncoding = url + "?" + key + "=" + val + "&" + key2 + "=" + val2; |
| 288 | + doTest(model, url, false, expectedUrlForEncoding); |
304 | 289 | }
|
305 | 290 |
|
306 | 291 | @Test
|
307 | 292 | void arrayParam() throws Exception {
|
308 | 293 | String url = "https://url.somewhere.com";
|
309 | 294 | String key = "foo";
|
310 | 295 | String[] val = new String[] {"bar", "baz"};
|
311 |
| - Map<String, String[]> model = new HashMap<>(); |
312 |
| - model.put(key, val); |
313 |
| - try { |
314 |
| - String expectedUrlForEncoding = url + "?" + key + "=" + val[0] + "&" + key + "=" + val[1]; |
315 |
| - doTest(model, url, false, expectedUrlForEncoding); |
316 |
| - } |
317 |
| - catch (AssertionError err) { |
318 |
| - // OK, so it's the other order... probably on Sun JDK 1.6 or IBM JDK 1.5 |
319 |
| - String expectedUrlForEncoding = url + "?" + key + "=" + val[1] + "&" + key + "=" + val[0]; |
320 |
| - doTest(model, url, false, expectedUrlForEncoding); |
321 |
| - } |
| 296 | + Map<String, String[]> model = Map.of(key, val); |
| 297 | + String expectedUrlForEncoding = url + "?" + key + "=" + val[0] + "&" + key + "=" + val[1]; |
| 298 | + doTest(model, url, false, expectedUrlForEncoding); |
322 | 299 | }
|
323 | 300 |
|
324 | 301 | @Test
|
325 | 302 | void collectionParam() throws Exception {
|
326 | 303 | String url = "https://url.somewhere.com";
|
327 | 304 | String key = "foo";
|
328 |
| - List<String> val = new ArrayList<>(); |
329 |
| - val.add("bar"); |
330 |
| - val.add("baz"); |
331 |
| - Map<String, List<String>> model = new HashMap<>(); |
332 |
| - model.put(key, val); |
333 |
| - try { |
334 |
| - String expectedUrlForEncoding = url + "?" + key + "=" + val.get(0) + "&" + key + "=" + val.get(1); |
335 |
| - doTest(model, url, false, expectedUrlForEncoding); |
336 |
| - } |
337 |
| - catch (AssertionError err) { |
338 |
| - // OK, so it's the other order... probably on Sun JDK 1.6 or IBM JDK 1.5 |
339 |
| - String expectedUrlForEncoding = url + "?" + key + "=" + val.get(1) + "&" + key + "=" + val.get(0); |
340 |
| - doTest(model, url, false, expectedUrlForEncoding); |
341 |
| - } |
| 305 | + List<String> val = List.of("bar", "baz"); |
| 306 | + Map<String, List<String>> model = Map.of(key, val); |
| 307 | + String expectedUrlForEncoding = url + "?" + key + "=" + val.get(0) + "&" + key + "=" + val.get(1); |
| 308 | + doTest(model, url, false, expectedUrlForEncoding); |
342 | 309 | }
|
343 | 310 |
|
344 | 311 | @Test
|
|
0 commit comments