use of cn.taketoday.web.BindingContext in project today-framework by TAKETODAY.
the class ErrorsMethodArgumentResolverTests method bindingResult.
@Test
public void bindingResult() throws Throwable {
BindingContext mavContainer = new BindingContext();
mavContainer.addAttribute("ignore1", "value1");
mavContainer.addAttribute("ignore2", "value2");
mavContainer.addAttribute("ignore3", "value3");
mavContainer.addAttribute("ignore4", "value4");
mavContainer.addAttribute("ignore5", "value5");
mavContainer.addAllAttributes(bindingResult.getModel());
webRequest.setBindingContext(mavContainer);
Object actual = resolver.resolveArgument(webRequest, paramErrors);
assertThat(bindingResult).isSameAs(actual);
}
use of cn.taketoday.web.BindingContext in project today-framework by TAKETODAY.
the class RequestParamMethodArgumentResolverTests method missingRequestParamEmptyValueConvertedToNull.
// SPR-10578
@Test
public void missingRequestParamEmptyValueConvertedToNull() throws Throwable {
RequestContextDataBinder binder = new RequestContextDataBinder(null);
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
ServletRequestContext webRequest = new ServletRequestContext(null, request, new MockHttpServletResponse());
BindingContext binderFactory = mock(BindingContext.class);
given(binderFactory.createBinder(webRequest, null, "stringNotAnnot")).willReturn(binder);
given(binderFactory.createBinder(webRequest, "stringNotAnnot")).willReturn(binder);
webRequest.setBindingContext(binderFactory);
request.addParameter("stringNotAnnot", "");
ResolvableMethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
// Object arg = resolver.resolveArgument(webRequest, param);
// assertThat(arg).isNull();
assertThatThrownBy(() -> resolver.resolveArgument(webRequest, param)).isInstanceOf(MissingRequestParameterException.class).hasMessage("Required request parameter 'stringNotAnnot' for method parameter type String is present but converted to null");
}
use of cn.taketoday.web.BindingContext in project today-framework by TAKETODAY.
the class RequestParamMethodArgumentResolverTests method resolveMultipartFile.
@Test
public void resolveMultipartFile() throws Throwable {
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
MultipartFile expected = new MockMultipartFile("mfile", "Hello World".getBytes());
request.addFile(expected);
webRequest = new ServletRequestContext(null, request, null);
webRequest.setBindingContext(new BindingContext());
ResolvableMethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile.class);
Object result = resolver.resolveArgument(webRequest, param);
boolean condition = result instanceof MultipartFile;
assertThat(condition).isTrue();
assertThat(result).as("Invalid result").isEqualTo(expected);
}
use of cn.taketoday.web.BindingContext in project today-framework by TAKETODAY.
the class RequestParamMethodArgumentResolverTests method resolveOptionalParamValue.
@Test
@SuppressWarnings("rawtypes")
public void resolveOptionalParamValue() throws Throwable {
ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
initializer.setConversionService(new DefaultConversionService());
ServletRequestContext webRequest = new ServletRequestContext(null, request, new MockHttpServletResponse());
BindingContext binderFactory = new BindingContext(initializer);
webRequest.setBindingContext(binderFactory);
ResolvableMethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, Integer.class);
Object result = resolver.resolveArgument(webRequest, param);
assertThat(result).isEqualTo(Optional.empty());
request.addParameter("name", "123");
result = resolver.resolveArgument(webRequest, param);
assertThat(result.getClass()).isEqualTo(Optional.class);
assertThat(((Optional) result).get()).isEqualTo(123);
}
use of cn.taketoday.web.BindingContext in project today-framework by TAKETODAY.
the class RequestParamMethodArgumentResolverTests method resolveMultipartFileNotAnnot.
@Test
public void resolveMultipartFileNotAnnot() throws Throwable {
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
MultipartFile expected = new MockMultipartFile("multipartFileNotAnnot", "Hello World".getBytes());
request.addFile(expected);
ServletRequestContext webRequest = new ServletRequestContext(null, request, null);
webRequest.setBindingContext(new BindingContext());
ResolvableMethodParameter param = this.testMethod.annotNotPresent().arg(MultipartFile.class);
Object result = resolver.resolveArgument(webRequest, param);
boolean condition = result instanceof MultipartFile;
assertThat(condition).isTrue();
assertThat(result).as("Invalid result").isEqualTo(expected);
}
Aggregations