Search in sources :

Example 1 with ConfigurableWebBindingInitializer

use of cn.taketoday.web.bind.support.ConfigurableWebBindingInitializer 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);
}
Also used : ConfigurableWebBindingInitializer(cn.taketoday.web.bind.support.ConfigurableWebBindingInitializer) RequestParam(cn.taketoday.web.annotation.RequestParam) Optional(java.util.Optional) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) DefaultConversionService(cn.taketoday.core.conversion.support.DefaultConversionService) BindingContext(cn.taketoday.web.BindingContext) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 2 with ConfigurableWebBindingInitializer

use of cn.taketoday.web.bind.support.ConfigurableWebBindingInitializer in project today-framework by TAKETODAY.

the class RequestParamMethodArgumentResolverTests method optionalMultipartFileWithoutMultipartRequest.

@Test
public void optionalMultipartFileWithoutMultipartRequest() 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, MultipartFile.class);
    Object actual = resolver.resolveArgument(webRequest, param);
    assertThat(actual).isEqualTo(Optional.empty());
}
Also used : ConfigurableWebBindingInitializer(cn.taketoday.web.bind.support.ConfigurableWebBindingInitializer) RequestParam(cn.taketoday.web.annotation.RequestParam) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) DefaultConversionService(cn.taketoday.core.conversion.support.DefaultConversionService) BindingContext(cn.taketoday.web.BindingContext) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 3 with ConfigurableWebBindingInitializer

use of cn.taketoday.web.bind.support.ConfigurableWebBindingInitializer in project today-framework by TAKETODAY.

the class RequestParamMethodArgumentResolverTests method missingOptionalParamValue.

@Test
@SuppressWarnings("rawtypes")
public void missingOptionalParamValue() 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());
    result = resolver.resolveArgument(webRequest, param);
    assertThat(result.getClass()).isEqualTo(Optional.class);
    assertThat(((Optional) result).isPresent()).isFalse();
}
Also used : ConfigurableWebBindingInitializer(cn.taketoday.web.bind.support.ConfigurableWebBindingInitializer) RequestParam(cn.taketoday.web.annotation.RequestParam) Optional(java.util.Optional) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) DefaultConversionService(cn.taketoday.core.conversion.support.DefaultConversionService) BindingContext(cn.taketoday.web.BindingContext) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 4 with ConfigurableWebBindingInitializer

use of cn.taketoday.web.bind.support.ConfigurableWebBindingInitializer in project today-framework by TAKETODAY.

the class RequestParamMethodArgumentResolverTests method resolveOptionalParamArray.

@Test
@SuppressWarnings("rawtypes")
public void resolveOptionalParamArray() 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", "456");
    result = resolver.resolveArgument(webRequest, param);
    assertThat(result.getClass()).isEqualTo(Optional.class);
    assertThat((Integer[]) ((Optional) result).get()).isEqualTo(new Integer[] { 123, 456 });
}
Also used : ConfigurableWebBindingInitializer(cn.taketoday.web.bind.support.ConfigurableWebBindingInitializer) RequestParam(cn.taketoday.web.annotation.RequestParam) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) DefaultConversionService(cn.taketoday.core.conversion.support.DefaultConversionService) BindingContext(cn.taketoday.web.BindingContext) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 5 with ConfigurableWebBindingInitializer

use of cn.taketoday.web.bind.support.ConfigurableWebBindingInitializer in project today-framework by TAKETODAY.

the class RequestParamMethodArgumentResolverTests method resolveOptionalMultipartFile.

@Test
public void resolveOptionalMultipartFile() 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);
    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(initializer));
    ResolvableMethodParameter param = testMethod.annotPresent(RequestParam.class).arg(Optional.class, MultipartFile.class);
    Object result = resolver.resolveArgument(webRequest, param);
    boolean condition = result instanceof Optional;
    assertThat(condition).isTrue();
    assertThat(((Optional<?>) result).get()).as("Invalid result").isEqualTo(expected);
}
Also used : MockMultipartFile(cn.taketoday.web.testfixture.servlet.MockMultipartFile) RequestParam(cn.taketoday.web.annotation.RequestParam) Optional(java.util.Optional) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) DefaultConversionService(cn.taketoday.core.conversion.support.DefaultConversionService) BindingContext(cn.taketoday.web.BindingContext) ConfigurableWebBindingInitializer(cn.taketoday.web.bind.support.ConfigurableWebBindingInitializer) MockMultipartFile(cn.taketoday.web.testfixture.servlet.MockMultipartFile) MultipartFile(cn.taketoday.web.multipart.MultipartFile) MockMultipartHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockMultipartHttpServletRequest) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Aggregations

BindingContext (cn.taketoday.web.BindingContext)17 ConfigurableWebBindingInitializer (cn.taketoday.web.bind.support.ConfigurableWebBindingInitializer)17 Test (org.junit.jupiter.api.Test)15 DefaultConversionService (cn.taketoday.core.conversion.support.DefaultConversionService)11 RequestParam (cn.taketoday.web.annotation.RequestParam)9 ResolvableMethodParameter (cn.taketoday.web.handler.method.ResolvableMethodParameter)9 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)9 MockHttpServletResponse (cn.taketoday.web.testfixture.servlet.MockHttpServletResponse)9 Optional (java.util.Optional)7 DefaultFormattingConversionService (cn.taketoday.format.support.DefaultFormattingConversionService)6 MethodArgumentTypeMismatchException (cn.taketoday.web.handler.method.MethodArgumentTypeMismatchException)1 MultipartFile (cn.taketoday.web.multipart.MultipartFile)1 MockMultipartFile (cn.taketoday.web.testfixture.servlet.MockMultipartFile)1 MockMultipartHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockMultipartHttpServletRequest)1 Date (java.util.Date)1 UUID (java.util.UUID)1