Search in sources :

Example 6 with BindingContext

use of cn.taketoday.web.BindingContext in project today-framework by TAKETODAY.

the class ModelFactoryTests method modelAttributeMethodWithNullReturnValue.

@Test
public void modelAttributeMethodWithNullReturnValue() throws Throwable {
    BindingContext bindingContext = webRequest.getBindingContext();
    ModelFactory modelFactory = createModelFactory("nullModelAttr");
    HandlerMethod handlerMethod = createHandlerMethod("handle");
    modelFactory.initModel(this.webRequest, bindingContext, handlerMethod);
    assertThat(bindingContext.containsAttribute("name")).isTrue();
    assertThat(bindingContext.getModel().get("name")).isNull();
}
Also used : BindingContext(cn.taketoday.web.BindingContext) Test(org.junit.jupiter.api.Test)

Example 7 with BindingContext

use of cn.taketoday.web.BindingContext in project today-framework by TAKETODAY.

the class ModelFactoryTests method updateModelBindingResult.

@Test
public void updateModelBindingResult() throws Throwable {
    String commandName = "attr1";
    Object command = new Object();
    RequestContextDataBinder dataBinder = new RequestContextDataBinder(command, commandName);
    BindingContext container = new BindingContext0(dataBinder);
    container.addAttribute(commandName, command);
    ModelFactory modelFactory = new ModelFactory(null, container, this.attributeHandler);
    modelFactory.updateModel(this.webRequest, container);
    assertThat(container.getModel().get(commandName)).isEqualTo(command);
    String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + commandName;
    assertThat(container.getModel().get(bindingResultKey)).isSameAs(dataBinder.getBindingResult());
    assertThat(container.getModel().size()).isEqualTo(2);
}
Also used : RequestContextDataBinder(cn.taketoday.web.bind.RequestContextDataBinder) BindingContext(cn.taketoday.web.BindingContext) Test(org.junit.jupiter.api.Test)

Example 8 with BindingContext

use of cn.taketoday.web.BindingContext in project today-framework by TAKETODAY.

the class ModelFactoryTests method updateModelWhenRedirecting.

// SPR-12542
@Test
public void updateModelWhenRedirecting() throws Throwable {
    String attributeName = "sessionAttr";
    String attribute = "value";
    RequestContextDataBinder dataBinder = new RequestContextDataBinder(attribute, attributeName);
    BindingContext container = new BindingContext0(dataBinder);
    container.addAttribute(attributeName, attribute);
    String queryParam = "123";
    String queryParamName = "q";
    container.setRedirectModel(new RedirectModel(queryParamName, queryParam));
    ModelFactory modelFactory = new ModelFactory(null, container, this.attributeHandler);
    modelFactory.updateModel(this.webRequest, container);
    assertThat(container.getRedirectModel().get(queryParamName)).isEqualTo(queryParam);
    assertThat(container.getRedirectModel().size()).isEqualTo(1);
    assertThat(this.attributeStore.retrieveAttribute(this.webRequest, attributeName)).isEqualTo(attribute);
}
Also used : RedirectModel(cn.taketoday.web.view.RedirectModel) RequestContextDataBinder(cn.taketoday.web.bind.RequestContextDataBinder) BindingContext(cn.taketoday.web.BindingContext) Test(org.junit.jupiter.api.Test)

Example 9 with BindingContext

use of cn.taketoday.web.BindingContext in project today-framework by TAKETODAY.

the class ModelFactoryTests method modelAttributeMethod.

@Test
public void modelAttributeMethod() throws Throwable {
    BindingContext container = webRequest.getBindingContext();
    ModelFactory modelFactory = createModelFactory("modelAttr", Model.class);
    HandlerMethod handlerMethod = createHandlerMethod("handle");
    modelFactory.initModel(this.webRequest, container, handlerMethod);
    assertThat(container.getModel().get("modelAttr")).isEqualTo(Boolean.TRUE);
}
Also used : BindingContext(cn.taketoday.web.BindingContext) Test(org.junit.jupiter.api.Test)

Example 10 with BindingContext

use of cn.taketoday.web.BindingContext in project today-framework by TAKETODAY.

the class ModelAttributeMethodProcessorTests method resolveArgumentBindingDisabled.

@Test
public void resolveArgumentBindingDisabled() throws Throwable {
    String name = "noBindAttr";
    Object target = new TestBean();
    StubRequestDataBinder dataBinder = new StubRequestDataBinder(target, name);
    BindingContext factory = new ModelFactoryTests.BindingContext0(dataBinder);
    factory.addAttribute(name, target);
    request.setBindingContext(factory);
    this.processor.resolveArgument(request, this.paramBindingDisabledAttr);
    assertThat(dataBinder.isBindInvoked()).isFalse();
    assertThat(dataBinder.isValidateInvoked()).isTrue();
}
Also used : TestBean(cn.taketoday.beans.testfixture.beans.TestBean) BindingContext(cn.taketoday.web.BindingContext) Test(org.junit.jupiter.api.Test)

Aggregations

BindingContext (cn.taketoday.web.BindingContext)75 Test (org.junit.jupiter.api.Test)53 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)31 ResolvableMethodParameter (cn.taketoday.web.handler.method.ResolvableMethodParameter)30 RequestParam (cn.taketoday.web.annotation.RequestParam)23 ConfigurableWebBindingInitializer (cn.taketoday.web.bind.support.ConfigurableWebBindingInitializer)17 MockHttpServletResponse (cn.taketoday.web.testfixture.servlet.MockHttpServletResponse)13 DefaultConversionService (cn.taketoday.core.conversion.support.DefaultConversionService)11 MockHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockHttpServletRequest)11 RequestContextDataBinder (cn.taketoday.web.bind.RequestContextDataBinder)9 MockMultipartFile (cn.taketoday.web.testfixture.servlet.MockMultipartFile)9 MockMultipartHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockMultipartHttpServletRequest)9 MultipartFile (cn.taketoday.web.multipart.MultipartFile)8 DefaultFormattingConversionService (cn.taketoday.format.support.DefaultFormattingConversionService)7 Nullable (cn.taketoday.lang.Nullable)7 Optional (java.util.Optional)7 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)6 MethodParameter (cn.taketoday.core.MethodParameter)6 MockPart (cn.taketoday.web.testfixture.servlet.MockPart)6 RequestPart (cn.taketoday.web.annotation.RequestPart)5