Allow configuration of custom redirect patterns
This change enables the ability to configure ViewNameMethodReturnValueHandler & ModelAndViewMethodReturnValueHandler with patterns to use to test for a custom redirect view name. Issue: SPR-12054
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -104,7 +104,7 @@ public class ModelAndViewMethodReturnValueHandlerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleRedirectAttributesWithViewInstance() throws Exception {
|
||||
public void handleRedirectAttributesWithViewName() throws Exception {
|
||||
RedirectAttributesModelMap redirectAttributes = new RedirectAttributesModelMap();
|
||||
mavContainer.setRedirectModel(redirectAttributes);
|
||||
|
||||
@@ -114,7 +114,22 @@ public class ModelAndViewMethodReturnValueHandlerTests {
|
||||
ModelMap model = mavContainer.getModel();
|
||||
assertEquals("redirect:viewName", mavContainer.getViewName());
|
||||
assertEquals("attrValue", model.get("attrName"));
|
||||
assertSame("RedirectAttributes should be used if controller redirects", redirectAttributes, model);
|
||||
assertSame(redirectAttributes, model);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleRedirectAttributesWithCustomPrefix() throws Exception {
|
||||
RedirectAttributesModelMap redirectAttributes = new RedirectAttributesModelMap();
|
||||
mavContainer.setRedirectModel(redirectAttributes);
|
||||
|
||||
ModelAndView mav = new ModelAndView("myRedirect:viewName", "attrName", "attrValue");
|
||||
handler.setRedirectPatterns("myRedirect:*");
|
||||
handler.handleReturnValue(mav, returnParamModelAndView, mavContainer, webRequest);
|
||||
|
||||
ModelMap model = mavContainer.getModel();
|
||||
assertEquals("myRedirect:viewName", mavContainer.getViewName());
|
||||
assertEquals("attrValue", model.get("attrName"));
|
||||
assertSame(redirectAttributes, model);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -137,10 +152,12 @@ public class ModelAndViewMethodReturnValueHandlerTests {
|
||||
return new MethodParameter(method, -1);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
ModelAndView modelAndView() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
String viewName() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -20,7 +20,6 @@ import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -29,7 +28,6 @@ import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
import org.springframework.web.method.support.ModelAndViewContainer;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ViewNameMethodReturnValueHandler;
|
||||
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;
|
||||
|
||||
/**
|
||||
@@ -45,23 +43,25 @@ public class ViewNameMethodReturnValueHandlerTests {
|
||||
|
||||
private ServletWebRequest webRequest;
|
||||
|
||||
private MethodParameter param;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
public void setUp() throws NoSuchMethodException {
|
||||
this.handler = new ViewNameMethodReturnValueHandler();
|
||||
this.mavContainer = new ModelAndViewContainer();
|
||||
this.webRequest = new ServletWebRequest(new MockHttpServletRequest());
|
||||
this.param = new MethodParameter(getClass().getDeclaredMethod("viewName"), -1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void supportsReturnType() throws Exception {
|
||||
assertTrue(this.handler.supportsReturnType(createReturnValueParam("viewName")));
|
||||
assertTrue(this.handler.supportsReturnType(this.param));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnViewName() throws Exception {
|
||||
MethodParameter param = createReturnValueParam("viewName");
|
||||
this.handler.handleReturnValue("testView", param, this.mavContainer, this.webRequest);
|
||||
|
||||
this.handler.handleReturnValue("testView", this.param, this.mavContainer, this.webRequest);
|
||||
assertEquals("testView", this.mavContainer.getViewName());
|
||||
}
|
||||
|
||||
@@ -69,18 +69,33 @@ public class ViewNameMethodReturnValueHandlerTests {
|
||||
public void returnViewNameRedirect() throws Exception {
|
||||
ModelMap redirectModel = new RedirectAttributesModelMap();
|
||||
this.mavContainer.setRedirectModel(redirectModel);
|
||||
MethodParameter param = createReturnValueParam("viewName");
|
||||
this.handler.handleReturnValue("redirect:testView", param, this.mavContainer, this.webRequest);
|
||||
|
||||
this.handler.handleReturnValue("redirect:testView", this.param, this.mavContainer, this.webRequest);
|
||||
assertEquals("redirect:testView", this.mavContainer.getViewName());
|
||||
assertSame("Should have switched to the RedirectModel", redirectModel, this.mavContainer.getModel());
|
||||
assertSame(redirectModel, this.mavContainer.getModel());
|
||||
}
|
||||
|
||||
private MethodParameter createReturnValueParam(String methodName) throws Exception {
|
||||
Method method = getClass().getDeclaredMethod(methodName);
|
||||
return new MethodParameter(method, -1);
|
||||
@Test
|
||||
public void returnViewCustomRedirect() throws Exception {
|
||||
ModelMap redirectModel = new RedirectAttributesModelMap();
|
||||
this.mavContainer.setRedirectModel(redirectModel);
|
||||
this.handler.setRedirectPatterns("myRedirect:*");
|
||||
this.handler.handleReturnValue("myRedirect:testView", this.param, this.mavContainer, this.webRequest);
|
||||
assertEquals("myRedirect:testView", this.mavContainer.getViewName());
|
||||
assertSame(redirectModel, this.mavContainer.getModel());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnViewRedirectWithCustomRedirectPattern() throws Exception {
|
||||
ModelMap redirectModel = new RedirectAttributesModelMap();
|
||||
this.mavContainer.setRedirectModel(redirectModel);
|
||||
this.handler.setRedirectPatterns("myRedirect:*");
|
||||
this.handler.handleReturnValue("redirect:testView", this.param, this.mavContainer, this.webRequest);
|
||||
assertEquals("redirect:testView", this.mavContainer.getViewName());
|
||||
assertSame(redirectModel, this.mavContainer.getModel());
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
String viewName() {
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user