Search in sources :

Example 11 with BindingContext

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

the class ModelAttributeMethodProcessorTests method resolveArgumentOrdering.

// SPR-9378
@Test
public void resolveArgumentOrdering() throws Throwable {
    String name = "testBean";
    Object testBean = new TestBean(name);
    StubRequestDataBinder dataBinder = new StubRequestDataBinder(testBean, name);
    BindingContext factory = new ModelFactoryTests.BindingContext0(dataBinder);
    factory.addAttribute(name, testBean);
    factory.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, testBean);
    Object anotherTestBean = new TestBean();
    factory.addAttribute("anotherTestBean", anotherTestBean);
    request.setBindingContext(factory);
    this.processor.resolveArgument(request, this.paramModelAttr);
    Object[] values = factory.getModel().values().toArray();
    assertThat(values[1]).as("Resolved attribute should be updated to be last").isSameAs(testBean);
    assertThat(values[2]).as("BindingResult of resolved attr should be last").isSameAs(dataBinder.getBindingResult());
}
Also used : TestBean(cn.taketoday.beans.testfixture.beans.TestBean) BindingContext(cn.taketoday.web.BindingContext) Test(org.junit.jupiter.api.Test)

Example 12 with BindingContext

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

the class ModelAttributeMethodProcessorTests method setup.

@BeforeEach
public void setup() throws Throwable {
    this.request = new ServletRequestContext(null, new MockHttpServletRequest(), null);
    this.container = new BindingContext();
    request.setBindingContext(container);
    this.processor = new ModelAttributeMethodProcessor(false);
    Method method = ModelAttributeHandler.class.getDeclaredMethod("modelAttribute", TestBean.class, Errors.class, int.class, TestBean.class, TestBean.class, TestBean.class, TestBeanWithConstructorArgs.class);
    this.paramNamedValidModelAttr = new ResolvableMethodParameter(new SynthesizingMethodParameter(method, 0));
    this.paramErrors = new ResolvableMethodParameter(new SynthesizingMethodParameter(method, 1));
    this.paramInt = new ResolvableMethodParameter(new SynthesizingMethodParameter(method, 2));
    this.paramModelAttr = new ResolvableMethodParameter(new SynthesizingMethodParameter(method, 3));
    this.paramBindingDisabledAttr = new ResolvableMethodParameter(new SynthesizingMethodParameter(method, 4));
    this.paramNonSimpleType = new ResolvableMethodParameter(new SynthesizingMethodParameter(method, 5));
    this.beanWithConstructorArgs = new ResolvableMethodParameter(new SynthesizingMethodParameter(method, 6));
    method = getClass().getDeclaredMethod("annotatedReturnValue");
    this.returnParamNamedModelAttrHandler = new HandlerMethod(this, method);
    method = getClass().getDeclaredMethod("notAnnotatedReturnValue");
    this.returnParamNonSimpleTypeHandler = new HandlerMethod(this, method);
}
Also used : SynthesizingMethodParameter(cn.taketoday.core.annotation.SynthesizingMethodParameter) MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) Method(java.lang.reflect.Method) BindingContext(cn.taketoday.web.BindingContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 13 with BindingContext

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

the class ModelFactoryOrderingTests method runTest.

private void runTest(Object controller) throws Throwable {
    ParameterResolvingRegistry resolvers = new ParameterResolvingRegistry();
    resolvers.addStrategies(new ModelAttributeMethodProcessor(false));
    resolvers.addStrategies(new ModelMethodProcessor());
    BindingContext dataBinderFactory = new BindingContext(null);
    Class<?> type = controller.getClass();
    Set<Method> methods = MethodIntrospector.filterMethods(type, METHOD_FILTER);
    List<InvocableHandlerMethod> modelMethods = new ArrayList<>();
    for (Method method : methods) {
        InvocableHandlerMethod modelMethod = new InvocableHandlerMethod(controller, method);
        modelMethod.setResolvingRegistry(resolvers);
        modelMethods.add(modelMethod);
    }
    Collections.shuffle(modelMethods);
    SessionAttributesHandler sessionHandler = new SessionAttributesHandler(type, this.sessionAttributeStore);
    ModelFactory factory = new ModelFactory(modelMethods, dataBinderFactory, sessionHandler);
    factory.initModel(this.webRequest, this.mavContainer, new HandlerMethod(controller, "handle"));
    if (logger.isDebugEnabled()) {
        logger.debug(String.join(" >> ", getInvokedMethods()));
    }
}
Also used : ModelMethodProcessor(cn.taketoday.web.bind.resolver.ModelMethodProcessor) ArrayList(java.util.ArrayList) ParameterResolvingRegistry(cn.taketoday.web.bind.resolver.ParameterResolvingRegistry) Method(java.lang.reflect.Method) BindingContext(cn.taketoday.web.BindingContext)

Example 14 with BindingContext

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

the class ErrorsMethodArgumentResolverTests method bindingResultNotFound.

@Test
public void bindingResultNotFound() throws Exception {
    BindingContext mavContainer = new BindingContext();
    mavContainer.addAllAttributes(bindingResult.getModel());
    mavContainer.addAttribute("ignore1", "value1");
    webRequest.setBindingContext(mavContainer);
    assertThatIllegalStateException().isThrownBy(() -> resolver.resolveArgument(webRequest, paramErrors));
}
Also used : BindingContext(cn.taketoday.web.BindingContext) Test(org.junit.jupiter.api.Test)

Example 15 with BindingContext

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

the class ErrorsMethodArgumentResolverTests method noBindingResult.

@Test
public void noBindingResult() throws Exception {
    BindingContext mavContainer = new BindingContext();
    webRequest.setBindingContext(mavContainer);
    assertThatIllegalStateException().isThrownBy(() -> resolver.resolveArgument(webRequest, paramErrors));
}
Also used : 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