Search in sources :

Example 11 with ModelAndViewContainer

use of org.springframework.web.method.support.ModelAndViewContainer in project spring-framework by spring-projects.

the class RequestPartMethodArgumentResolverTests method resolveOptionalRequestPart.

@Test
public void resolveOptionalRequestPart() throws Exception {
    SimpleBean simpleBean = new SimpleBean("foo");
    given(messageConverter.canRead(SimpleBean.class, MediaType.TEXT_PLAIN)).willReturn(true);
    given(messageConverter.read(eq(SimpleBean.class), isA(HttpInputMessage.class))).willReturn(simpleBean);
    ModelAndViewContainer mavContainer = new ModelAndViewContainer();
    Object actualValue = resolver.resolveArgument(optionalRequestPart, mavContainer, webRequest, new ValidatingBinderFactory());
    assertThat(actualValue).as("Invalid argument value").isEqualTo(Optional.of(simpleBean));
    assertThat(mavContainer.isRequestHandled()).as("The requestHandled flag shouldn't change").isFalse();
    actualValue = resolver.resolveArgument(optionalRequestPart, mavContainer, webRequest, new ValidatingBinderFactory());
    assertThat(actualValue).as("Invalid argument value").isEqualTo(Optional.of(simpleBean));
    assertThat(mavContainer.isRequestHandled()).as("The requestHandled flag shouldn't change").isFalse();
}
Also used : HttpInputMessage(org.springframework.http.HttpInputMessage) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) Test(org.junit.jupiter.api.Test)

Example 12 with ModelAndViewContainer

use of org.springframework.web.method.support.ModelAndViewContainer in project spring-framework by spring-projects.

the class RequestPartMethodArgumentResolverTests method testResolveArgument.

private void testResolveArgument(SimpleBean argValue, MethodParameter parameter) throws Exception {
    given(messageConverter.canRead(SimpleBean.class, MediaType.TEXT_PLAIN)).willReturn(true);
    given(messageConverter.read(eq(SimpleBean.class), isA(HttpInputMessage.class))).willReturn(argValue);
    ModelAndViewContainer mavContainer = new ModelAndViewContainer();
    Object actualValue = resolver.resolveArgument(parameter, mavContainer, webRequest, new ValidatingBinderFactory());
    assertThat(actualValue).as("Invalid argument value").isEqualTo(argValue);
    assertThat(mavContainer.isRequestHandled()).as("The requestHandled flag shouldn't change").isFalse();
    assertThat(trackedStream != null && trackedStream.closed).isTrue();
}
Also used : HttpInputMessage(org.springframework.http.HttpInputMessage) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer)

Example 13 with ModelAndViewContainer

use of org.springframework.web.method.support.ModelAndViewContainer in project spring-framework by spring-projects.

the class ModelFactoryTests method updateModelSessionAttributesSaved.

@Test
public void updateModelSessionAttributesSaved() throws Exception {
    String attributeName = "sessionAttr";
    String attribute = "value";
    ModelAndViewContainer container = new ModelAndViewContainer();
    container.addAttribute(attributeName, attribute);
    WebDataBinder dataBinder = new WebDataBinder(attribute, attributeName);
    WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class);
    given(binderFactory.createBinder(this.webRequest, attribute, attributeName)).willReturn(dataBinder);
    ModelFactory modelFactory = new ModelFactory(null, binderFactory, this.attributeHandler);
    modelFactory.updateModel(this.webRequest, container);
    assertThat(container.getModel().get(attributeName)).isEqualTo(attribute);
    assertThat(this.attributeStore.retrieveAttribute(this.webRequest, attributeName)).isEqualTo(attribute);
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.jupiter.api.Test)

Example 14 with ModelAndViewContainer

use of org.springframework.web.method.support.ModelAndViewContainer in project spring-framework by spring-projects.

the class ModelFactoryTests method updateModelWhenRedirecting.

// SPR-12542
@Test
public void updateModelWhenRedirecting() throws Exception {
    String attributeName = "sessionAttr";
    String attribute = "value";
    ModelAndViewContainer container = new ModelAndViewContainer();
    container.addAttribute(attributeName, attribute);
    String queryParam = "123";
    String queryParamName = "q";
    container.setRedirectModel(new ModelMap(queryParamName, queryParam));
    container.setRedirectModelScenario(true);
    WebDataBinder dataBinder = new WebDataBinder(attribute, attributeName);
    WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class);
    given(binderFactory.createBinder(this.webRequest, attribute, attributeName)).willReturn(dataBinder);
    ModelFactory modelFactory = new ModelFactory(null, binderFactory, this.attributeHandler);
    modelFactory.updateModel(this.webRequest, container);
    assertThat(container.getModel().get(queryParamName)).isEqualTo(queryParam);
    assertThat(container.getModel().size()).isEqualTo(1);
    assertThat(this.attributeStore.retrieveAttribute(this.webRequest, attributeName)).isEqualTo(attribute);
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) ModelMap(org.springframework.ui.ModelMap) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.jupiter.api.Test)

Example 15 with ModelAndViewContainer

use of org.springframework.web.method.support.ModelAndViewContainer in project spring-framework by spring-projects.

the class ModelFactoryOrderingTests method setup.

@Before
public void setup() {
    this.sessionAttributeStore = new DefaultSessionAttributeStore();
    this.webRequest = new ServletWebRequest(new MockHttpServletRequest(), new MockHttpServletResponse());
    this.mavContainer = new ModelAndViewContainer();
    this.mavContainer.addAttribute("methods", new ArrayList<String>());
}
Also used : ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) DefaultSessionAttributeStore(org.springframework.web.bind.support.DefaultSessionAttributeStore) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Before(org.junit.Before)

Aggregations

ModelAndViewContainer (org.springframework.web.method.support.ModelAndViewContainer)44 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)29 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)22 BeforeEach (org.junit.jupiter.api.BeforeEach)20 MethodParameter (org.springframework.core.MethodParameter)14 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)12 Method (java.lang.reflect.Method)10 Test (org.junit.jupiter.api.Test)10 WebDataBinderFactory (org.springframework.web.bind.support.WebDataBinderFactory)5 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)4 HandlerMethod (org.springframework.web.method.HandlerMethod)4 HttpMethod (org.springframework.http.HttpMethod)3 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)3 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)3 WebDataBinder (org.springframework.web.bind.WebDataBinder)3 LinkedHashMap (java.util.LinkedHashMap)2 Before (org.junit.Before)2 Test (org.junit.Test)2 HttpInputMessage (org.springframework.http.HttpInputMessage)2 Nullable (org.springframework.lang.Nullable)2