Polish RedirectViewTests

This commit is contained in:
Rossen Stoyanchev
2016-02-10 12:23:17 -05:00
parent 8558cbc9bf
commit aba04d576f

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -17,12 +17,13 @@
package org.springframework.web.servlet.view; package org.springframework.web.servlet.view;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
@@ -35,15 +36,17 @@ import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.support.StaticWebApplicationContext; import org.springframework.web.context.support.StaticWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.FlashMap; import org.springframework.web.servlet.FlashMap;
import org.springframework.web.servlet.FlashMapManager;
import org.springframework.web.servlet.View; import org.springframework.web.servlet.View;
import org.springframework.web.servlet.support.RequestDataValueProcessor; import org.springframework.web.servlet.support.RequestDataValueProcessor;
import org.springframework.web.servlet.support.RequestDataValueProcessorWrapper; import org.springframework.web.servlet.support.RequestDataValueProcessorWrapper;
import org.springframework.web.servlet.support.SessionFlashMapManager; import org.springframework.web.servlet.support.SessionFlashMapManager;
import org.springframework.web.util.WebUtils; import org.springframework.web.util.WebUtils;
import static org.junit.Assert.*; import static org.junit.Assert.assertEquals;
import static org.mockito.BDDMockito.*; import static org.junit.Assert.assertTrue;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.mock;
import static org.mockito.BDDMockito.verify;
/** /**
* Tests for redirect view, and query string construction. * Tests for redirect view, and query string construction.
@@ -53,10 +56,28 @@ import static org.mockito.BDDMockito.*;
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Sam Brannen * @author Sam Brannen
* @author Arjen Poutsma * @author Arjen Poutsma
* @author Rossen Stoyanchev
* @since 27.05.2003 * @since 27.05.2003
*/ */
public class RedirectViewTests { public class RedirectViewTests {
private MockHttpServletRequest request;
private MockHttpServletResponse response;
@Before
public void setUp() throws Exception {
this.request = new MockHttpServletRequest();
this.request.setContextPath("/context");
this.request.setCharacterEncoding(WebUtils.DEFAULT_CHARACTER_ENCODING);
this.request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());
this.request.setAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE, new SessionFlashMapManager());
this.response = new MockHttpServletResponse();
}
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void noUrlSet() throws Exception { public void noUrlSet() throws Exception {
RedirectView rv = new RedirectView(); RedirectView rv = new RedirectView();
@@ -68,31 +89,18 @@ public class RedirectViewTests {
RedirectView rv = new RedirectView(); RedirectView rv = new RedirectView();
rv.setUrl("http://url.somewhere.com"); rv.setUrl("http://url.somewhere.com");
rv.setHttp10Compatible(false); rv.setHttp10Compatible(false);
MockHttpServletRequest request = createRequest(); rv.render(new HashMap<>(), request, response);
MockHttpServletResponse response = new MockHttpServletResponse();
request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());
request.setAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE, new SessionFlashMapManager());
rv.render(new HashMap<String, Object>(), request, response);
assertEquals(303, response.getStatus()); assertEquals(303, response.getStatus());
assertEquals("http://url.somewhere.com", response.getHeader("Location")); assertEquals("http://url.somewhere.com", response.getHeader("Location"));
} }
private MockHttpServletRequest createRequest() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());
request.setAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE, new SessionFlashMapManager());
return request;
}
@Test @Test
public void explicitStatusCodeHttp11() throws Exception { public void explicitStatusCodeHttp11() throws Exception {
RedirectView rv = new RedirectView(); RedirectView rv = new RedirectView();
rv.setUrl("http://url.somewhere.com"); rv.setUrl("http://url.somewhere.com");
rv.setHttp10Compatible(false); rv.setHttp10Compatible(false);
rv.setStatusCode(HttpStatus.MOVED_PERMANENTLY); rv.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
MockHttpServletRequest request = createRequest(); rv.render(new HashMap<>(), request, response);
MockHttpServletResponse response = new MockHttpServletResponse();
rv.render(new HashMap<String, Object>(), request, response);
assertEquals(301, response.getStatus()); assertEquals(301, response.getStatus());
assertEquals("http://url.somewhere.com", response.getHeader("Location")); assertEquals("http://url.somewhere.com", response.getHeader("Location"));
} }
@@ -102,9 +110,7 @@ public class RedirectViewTests {
RedirectView rv = new RedirectView(); RedirectView rv = new RedirectView();
rv.setUrl("http://url.somewhere.com"); rv.setUrl("http://url.somewhere.com");
rv.setStatusCode(HttpStatus.MOVED_PERMANENTLY); rv.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
MockHttpServletRequest request = createRequest(); rv.render(new HashMap<>(), request, response);
MockHttpServletResponse response = new MockHttpServletResponse();
rv.render(new HashMap<String, Object>(), request, response);
assertEquals(301, response.getStatus()); assertEquals(301, response.getStatus());
assertEquals("http://url.somewhere.com", response.getHeader("Location")); assertEquals("http://url.somewhere.com", response.getHeader("Location"));
} }
@@ -113,10 +119,8 @@ public class RedirectViewTests {
public void attributeStatusCodeHttp10() throws Exception { public void attributeStatusCodeHttp10() throws Exception {
RedirectView rv = new RedirectView(); RedirectView rv = new RedirectView();
rv.setUrl("http://url.somewhere.com"); rv.setUrl("http://url.somewhere.com");
MockHttpServletRequest request = createRequest();
request.setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, HttpStatus.CREATED); request.setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, HttpStatus.CREATED);
MockHttpServletResponse response = new MockHttpServletResponse(); rv.render(new HashMap<>(), request, response);
rv.render(new HashMap<String, Object>(), request, response);
assertEquals(201, response.getStatus()); assertEquals(201, response.getStatus());
assertEquals("http://url.somewhere.com", response.getHeader("Location")); assertEquals("http://url.somewhere.com", response.getHeader("Location"));
} }
@@ -126,21 +130,18 @@ public class RedirectViewTests {
RedirectView rv = new RedirectView(); RedirectView rv = new RedirectView();
rv.setUrl("http://url.somewhere.com"); rv.setUrl("http://url.somewhere.com");
rv.setHttp10Compatible(false); rv.setHttp10Compatible(false);
MockHttpServletRequest request = createRequest();
request.setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, HttpStatus.CREATED); request.setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, HttpStatus.CREATED);
MockHttpServletResponse response = new MockHttpServletResponse(); rv.render(new HashMap<>(), request, response);
rv.render(new HashMap<String, Object>(), request, response);
assertEquals(201, response.getStatus()); assertEquals(201, response.getStatus());
assertEquals("http://url.somewhere.com", response.getHeader("Location")); assertEquals("http://url.somewhere.com", response.getHeader("Location"));
} }
@SuppressWarnings("AssertEqualsBetweenInconvertibleTypes")
@Test @Test
public void flashMap() throws Exception { public void flashMap() throws Exception {
RedirectView rv = new RedirectView(); RedirectView rv = new RedirectView();
rv.setUrl("http://url.somewhere.com/path"); rv.setUrl("http://url.somewhere.com/path");
rv.setHttp10Compatible(false); rv.setHttp10Compatible(false);
MockHttpServletRequest request = createRequest();
HttpServletResponse response = new MockHttpServletResponse();
FlashMap flashMap = new FlashMap(); FlashMap flashMap = new FlashMap();
flashMap.put("successMessage", "yay!"); flashMap.put("successMessage", "yay!");
request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, flashMap); request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, flashMap);
@@ -167,9 +168,7 @@ public class RedirectViewTests {
rv.setApplicationContext(wac); // Init RedirectView with WebAppCxt rv.setApplicationContext(wac); // Init RedirectView with WebAppCxt
rv.setUrl("/path"); rv.setUrl("/path");
MockHttpServletRequest request = createRequest();
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
HttpServletResponse response = new MockHttpServletResponse();
given(mockProcessor.processUrl(request, "/path")).willReturn("/path?key=123"); given(mockProcessor.processUrl(request, "/path")).willReturn("/path?key=123");
@@ -195,9 +194,6 @@ public class RedirectViewTests {
RedirectView rv = new RedirectView(); RedirectView rv = new RedirectView();
rv.setUrl("/path"); rv.setUrl("/path");
MockHttpServletRequest request = createRequest();
HttpServletResponse response = new MockHttpServletResponse();
given(mockProcessor.processUrl(request, "/path")).willReturn("/path?key=123"); given(mockProcessor.processUrl(request, "/path")).willReturn("/path?key=123");
rv.render(new ModelMap(), request, response); rv.render(new ModelMap(), request, response);
@@ -212,13 +208,13 @@ public class RedirectViewTests {
@Test @Test
public void emptyMap() throws Exception { public void emptyMap() throws Exception {
String url = "/myUrl"; String url = "/myUrl";
doTest(new HashMap<String, Object>(), url, false, url); doTest(new HashMap<>(), url, false, url);
} }
@Test @Test
public void emptyMapWithContextRelative() throws Exception { public void emptyMapWithContextRelative() throws Exception {
String url = "/myUrl"; String url = "/myUrl";
doTest(new HashMap<String, Object>(), url, true, url); doTest(new HashMap<>(), url, true, "/context" + url);
} }
@Test @Test
@@ -226,7 +222,7 @@ public class RedirectViewTests {
String url = "http://url.somewhere.com"; String url = "http://url.somewhere.com";
String key = "foo"; String key = "foo";
String val = "bar"; String val = "bar";
Map<String, String> model = new HashMap<String, String>(); Map<String, String> model = new HashMap<>();
model.put(key, val); model.put(key, val);
String expectedUrlForEncoding = url + "?" + key + "=" + val; String expectedUrlForEncoding = url + "?" + key + "=" + val;
doTest(model, url, false, expectedUrlForEncoding); doTest(model, url, false, expectedUrlForEncoding);
@@ -235,12 +231,13 @@ public class RedirectViewTests {
@Test @Test
public void singleParamWithoutExposingModelAttributes() throws Exception { public void singleParamWithoutExposingModelAttributes() throws Exception {
String url = "http://url.somewhere.com"; String url = "http://url.somewhere.com";
String key = "foo"; Map<String, String> model = Collections.singletonMap("foo", "bar");
String val = "bar";
Map<String, String> model = new HashMap<String, String>(); TestRedirectView rv = new TestRedirectView(url, false, model);
model.put(key, val); rv.setExposeModelAttributes(false);
String expectedUrlForEncoding = url; // + "?" + key + "=" + val; rv.render(model, request, response);
doTest(model, url, false, false, expectedUrlForEncoding);
assertEquals(url, this.response.getRedirectedUrl());
} }
@Test @Test
@@ -248,7 +245,7 @@ public class RedirectViewTests {
String url = "http://url.somewhere.com/test.htm#myAnchor"; String url = "http://url.somewhere.com/test.htm#myAnchor";
String key = "foo"; String key = "foo";
String val = "bar"; String val = "bar";
Map<String, String> model = new HashMap<String, String>(); Map<String, String> model = new HashMap<>();
model.put(key, val); model.put(key, val);
String expectedUrlForEncoding = "http://url.somewhere.com/test.htm" + "?" + key + "=" + val + "#myAnchor"; String expectedUrlForEncoding = "http://url.somewhere.com/test.htm" + "?" + key + "=" + val + "#myAnchor";
doTest(model, url, false, expectedUrlForEncoding); doTest(model, url, false, expectedUrlForEncoding);
@@ -257,7 +254,7 @@ public class RedirectViewTests {
@Test @Test
public void contextRelativeQueryParam() throws Exception { public void contextRelativeQueryParam() throws Exception {
String url = "/test.html?id=1"; String url = "/test.html?id=1";
doTest(new HashMap<String, Object>(), url, true, url); doTest(new HashMap<>(), url, true, "/context" + url);
} }
@Test @Test
@@ -267,16 +264,16 @@ public class RedirectViewTests {
String val = "bar"; String val = "bar";
String key2 = "thisIsKey2"; String key2 = "thisIsKey2";
String val2 = "andThisIsVal2"; String val2 = "andThisIsVal2";
Map<String, String> model = new HashMap<String, String>(); Map<String, String> model = new HashMap<>();
model.put(key, val); model.put(key, val);
model.put(key2, val2); model.put(key2, val2);
try { try {
String expectedUrlForEncoding = "http://url.somewhere.com?" + key + "=" + val + "&" + key2 + "=" + val2; String expectedUrlForEncoding = url + "?" + key + "=" + val + "&" + key2 + "=" + val2;
doTest(model, url, false, expectedUrlForEncoding); doTest(model, url, false, expectedUrlForEncoding);
} }
catch (AssertionError err) { catch (AssertionError err) {
// OK, so it's the other order... probably on Sun JDK 1.6 or IBM JDK 1.5 // OK, so it's the other order... probably on Sun JDK 1.6 or IBM JDK 1.5
String expectedUrlForEncoding = "http://url.somewhere.com?" + key2 + "=" + val2 + "&" + key + "=" + val; String expectedUrlForEncoding = url + "?" + key2 + "=" + val2 + "&" + key + "=" + val;
doTest(model, url, false, expectedUrlForEncoding); doTest(model, url, false, expectedUrlForEncoding);
} }
} }
@@ -286,15 +283,15 @@ public class RedirectViewTests {
String url = "http://url.somewhere.com"; String url = "http://url.somewhere.com";
String key = "foo"; String key = "foo";
String[] val = new String[] {"bar", "baz"}; String[] val = new String[] {"bar", "baz"};
Map<String, String[]> model = new HashMap<String, String[]>(); Map<String, String[]> model = new HashMap<>();
model.put(key, val); model.put(key, val);
try { try {
String expectedUrlForEncoding = "http://url.somewhere.com?" + key + "=" + val[0] + "&" + key + "=" + val[1]; String expectedUrlForEncoding = url + "?" + key + "=" + val[0] + "&" + key + "=" + val[1];
doTest(model, url, false, expectedUrlForEncoding); doTest(model, url, false, expectedUrlForEncoding);
} }
catch (AssertionError err) { catch (AssertionError err) {
// OK, so it's the other order... probably on Sun JDK 1.6 or IBM JDK 1.5 // OK, so it's the other order... probably on Sun JDK 1.6 or IBM JDK 1.5
String expectedUrlForEncoding = "http://url.somewhere.com?" + key + "=" + val[1] + "&" + key + "=" + val[0]; String expectedUrlForEncoding = url + "?" + key + "=" + val[1] + "&" + key + "=" + val[0];
doTest(model, url, false, expectedUrlForEncoding); doTest(model, url, false, expectedUrlForEncoding);
} }
} }
@@ -303,18 +300,18 @@ public class RedirectViewTests {
public void collectionParam() throws Exception { public void collectionParam() throws Exception {
String url = "http://url.somewhere.com"; String url = "http://url.somewhere.com";
String key = "foo"; String key = "foo";
List<String> val = new ArrayList<String>(); List<String> val = new ArrayList<>();
val.add("bar"); val.add("bar");
val.add("baz"); val.add("baz");
Map<String, List<String>> model = new HashMap<String, List<String>>(); Map<String, List<String>> model = new HashMap<>();
model.put(key, val); model.put(key, val);
try { try {
String expectedUrlForEncoding = "http://url.somewhere.com?" + key + "=" + val.get(0) + "&" + key + "=" + val.get(1); String expectedUrlForEncoding = url + "?" + key + "=" + val.get(0) + "&" + key + "=" + val.get(1);
doTest(model, url, false, expectedUrlForEncoding); doTest(model, url, false, expectedUrlForEncoding);
} }
catch (AssertionError err) { catch (AssertionError err) {
// OK, so it's the other order... probably on Sun JDK 1.6 or IBM JDK 1.5 // OK, so it's the other order... probably on Sun JDK 1.6 or IBM JDK 1.5
String expectedUrlForEncoding = "http://url.somewhere.com?" + key + "=" + val.get(1) + "&" + key + "=" + val.get(0); String expectedUrlForEncoding = url + "?" + key + "=" + val.get(1) + "&" + key + "=" + val.get(0);
doTest(model, url, false, expectedUrlForEncoding); doTest(model, url, false, expectedUrlForEncoding);
} }
} }
@@ -325,14 +322,14 @@ public class RedirectViewTests {
String key = "foo"; String key = "foo";
String val = "bar"; String val = "bar";
String key2 = "int2"; String key2 = "int2";
Object val2 = new Long(611); Object val2 = 611;
String key3 = "tb"; String key3 = "tb";
Object val3 = new TestBean(); Object val3 = new TestBean();
Map<String, Object> model = new HashMap<String, Object>(); Map<String, Object> model = new LinkedHashMap<>();
model.put(key, val); model.put(key, val);
model.put(key2, val2); model.put(key2, val2);
model.put(key3, val3); model.put(key3, val3);
String expectedUrlForEncoding = "http://url.somewhere.com?" + key + "=" + val + "&" + key2 + "=" + val2; String expectedUrlForEncoding = url + "?" + key + "=" + val + "&" + key2 + "=" + val2;
doTest(model, url, false, expectedUrlForEncoding); doTest(model, url, false, expectedUrlForEncoding);
} }
@@ -341,64 +338,43 @@ public class RedirectViewTests {
RedirectView rv = new RedirectView(); RedirectView rv = new RedirectView();
rv.setPropagateQueryParams(true); rv.setPropagateQueryParams(true);
rv.setUrl("http://url.somewhere.com?foo=bar#bazz"); rv.setUrl("http://url.somewhere.com?foo=bar#bazz");
MockHttpServletRequest request = createRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
request.setQueryString("a=b&c=d"); request.setQueryString("a=b&c=d");
rv.render(new HashMap<String, Object>(), request, response); rv.render(new HashMap<>(), request, response);
assertEquals(302, response.getStatus()); assertEquals(302, response.getStatus());
assertEquals("http://url.somewhere.com?foo=bar&a=b&c=d#bazz", response.getHeader("Location")); assertEquals("http://url.somewhere.com?foo=bar&a=b&c=d#bazz", response.getHeader("Location"));
} }
private void doTest(Map<String, ?> map, String url, boolean contextRelative, String expectedUrlForEncoding) private void doTest(Map<String, ?> map, String url, boolean contextRelative, String expectedUrl)
throws Exception { throws Exception {
doTest(map, url, contextRelative, true, expectedUrlForEncoding);
TestRedirectView rv = new TestRedirectView(url, contextRelative, map);
rv.render(map, request, response);
assertTrue("queryProperties() should have been called.", rv.queryPropertiesCalled);
assertEquals(expectedUrl, this.response.getRedirectedUrl());
} }
private void doTest(final Map<String, ?> map, final String url, final boolean contextRelative,
final boolean exposeModelAttributes, String expectedUrlForEncoding) throws Exception {
class TestRedirectView extends RedirectView { private static class TestRedirectView extends RedirectView {
public boolean queryPropertiesCalled = false; private Map<String, ?> expectedModel;
/** private boolean queryPropertiesCalled = false;
* Test whether this callback method is called with correct args
*/
@Override public TestRedirectView(String url, boolean contextRelative, Map<String, ?> expectedModel) {
protected Map<String, Object> queryProperties(Map<String, Object> model) { super(url, contextRelative);
// They may not be the same model instance, but they're still equal this.expectedModel = expectedModel;
assertTrue("Map and model must be equal.", map.equals(model));
this.queryPropertiesCalled = true;
return super.queryProperties(model);
}
} }
TestRedirectView rv = new TestRedirectView(); /**
rv.setUrl(url); * Test whether this callback method is called with correct args
rv.setContextRelative(contextRelative); */
rv.setExposeModelAttributes(exposeModelAttributes); @Override
protected Map<String, Object> queryProperties(Map<String, Object> model) {
HttpServletRequest request = mock(HttpServletRequest.class, "request"); assertTrue("Map and model must be equal.", this.expectedModel.equals(model));
if (exposeModelAttributes) { this.queryPropertiesCalled = true;
given(request.getCharacterEncoding()).willReturn(WebUtils.DEFAULT_CHARACTER_ENCODING); return super.queryProperties(model);
}
if (contextRelative) {
expectedUrlForEncoding = "/context" + expectedUrlForEncoding;
given(request.getContextPath()).willReturn("/context");
}
given(request.getAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE)).willReturn(new FlashMap());
FlashMapManager flashMapManager = new SessionFlashMapManager();
given(request.getAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE)).willReturn(flashMapManager);
HttpServletResponse response = mock(HttpServletResponse.class, "response");
given(response.encodeRedirectURL(expectedUrlForEncoding)).willReturn(expectedUrlForEncoding);
response.sendRedirect(expectedUrlForEncoding);
rv.render(map, request, response);
if (exposeModelAttributes) {
assertTrue("queryProperties() should have been called.", rv.queryPropertiesCalled);
} }
} }