Search in sources :

Example 1 with DefaultFormattingConversionService

use of cn.taketoday.format.support.DefaultFormattingConversionService in project today-infrastructure by TAKETODAY.

the class DataBinderTests method conversionWithInappropriateStringEditor.

@Test
void conversionWithInappropriateStringEditor() {
    DataBinder dataBinder = new DataBinder(null);
    DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService();
    dataBinder.setConversionService(conversionService);
    dataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    NameBean bean = new NameBean("Fred");
    assertThat(dataBinder.convertIfNecessary(bean, String.class)).as("ConversionService should have invoked toString()").isEqualTo("Fred");
    conversionService.addConverter(new NameBeanConverter());
    assertThat(dataBinder.convertIfNecessary(bean, String.class)).as("Type converter should have been used").isEqualTo("[Fred]");
}
Also used : StringTrimmerEditor(cn.taketoday.beans.propertyeditors.StringTrimmerEditor) DefaultFormattingConversionService(cn.taketoday.format.support.DefaultFormattingConversionService) Test(org.junit.jupiter.api.Test)

Example 2 with DefaultFormattingConversionService

use of cn.taketoday.format.support.DefaultFormattingConversionService in project today-framework by TAKETODAY.

the class DataBinderTests method conversionWithInappropriateStringEditor.

@Test
void conversionWithInappropriateStringEditor() {
    DataBinder dataBinder = new DataBinder(null);
    DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService();
    dataBinder.setConversionService(conversionService);
    dataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    NameBean bean = new NameBean("Fred");
    assertThat(dataBinder.convertIfNecessary(bean, String.class)).as("ConversionService should have invoked toString()").isEqualTo("Fred");
    conversionService.addConverter(new NameBeanConverter());
    assertThat(dataBinder.convertIfNecessary(bean, String.class)).as("Type converter should have been used").isEqualTo("[Fred]");
}
Also used : StringTrimmerEditor(cn.taketoday.beans.propertyeditors.StringTrimmerEditor) DefaultFormattingConversionService(cn.taketoday.format.support.DefaultFormattingConversionService) Test(org.junit.jupiter.api.Test)

Example 3 with DefaultFormattingConversionService

use of cn.taketoday.format.support.DefaultFormattingConversionService in project today-framework by TAKETODAY.

the class RequestHeaderMethodArgumentResolverTests method dateConversion.

@Test
@SuppressWarnings("deprecation")
void dateConversion() throws Throwable {
    String rfc1123val = "Thu, 21 Apr 2016 17:11:08 +0100";
    servletRequest.addHeader("name", rfc1123val);
    ConfigurableWebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer();
    bindingInitializer.setConversionService(new DefaultFormattingConversionService());
    webRequest.setBindingContext(new BindingContext(bindingInitializer));
    Object result = resolver.resolveArgument(webRequest, paramDate);
    assertThat(result).isEqualTo(new Date(rfc1123val));
}
Also used : ConfigurableWebBindingInitializer(cn.taketoday.web.bind.support.ConfigurableWebBindingInitializer) BindingContext(cn.taketoday.web.BindingContext) DefaultFormattingConversionService(cn.taketoday.format.support.DefaultFormattingConversionService) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 4 with DefaultFormattingConversionService

use of cn.taketoday.format.support.DefaultFormattingConversionService in project today-framework by TAKETODAY.

the class RequestHeaderMethodArgumentResolverTests method uuidConversionWithInvalidValue.

@Test
void uuidConversionWithInvalidValue() throws Throwable {
    servletRequest.addHeader("name", "bogus-uuid");
    ConfigurableWebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer();
    bindingInitializer.setConversionService(new DefaultFormattingConversionService());
    webRequest.setBindingContext(new BindingContext(bindingInitializer));
    assertThatExceptionOfType(MethodArgumentTypeMismatchException.class).isThrownBy(() -> resolver.resolveArgument(webRequest, paramUuid));
}
Also used : ConfigurableWebBindingInitializer(cn.taketoday.web.bind.support.ConfigurableWebBindingInitializer) MethodArgumentTypeMismatchException(cn.taketoday.web.handler.method.MethodArgumentTypeMismatchException) BindingContext(cn.taketoday.web.BindingContext) DefaultFormattingConversionService(cn.taketoday.format.support.DefaultFormattingConversionService) Test(org.junit.jupiter.api.Test)

Example 5 with DefaultFormattingConversionService

use of cn.taketoday.format.support.DefaultFormattingConversionService in project today-framework by TAKETODAY.

the class RequestHeaderMethodArgumentResolverTests method uuidConversionWithValidValue.

@Test
void uuidConversionWithValidValue() throws Throwable {
    UUID uuid = UUID.randomUUID();
    servletRequest.addHeader("name", uuid.toString());
    ConfigurableWebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer();
    bindingInitializer.setConversionService(new DefaultFormattingConversionService());
    webRequest.setBindingContext(new BindingContext(bindingInitializer));
    Object result = resolver.resolveArgument(webRequest, paramUuid);
    assertThat(result).isEqualTo(uuid);
}
Also used : ConfigurableWebBindingInitializer(cn.taketoday.web.bind.support.ConfigurableWebBindingInitializer) UUID(java.util.UUID) BindingContext(cn.taketoday.web.BindingContext) DefaultFormattingConversionService(cn.taketoday.format.support.DefaultFormattingConversionService) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultFormattingConversionService (cn.taketoday.format.support.DefaultFormattingConversionService)11 Test (org.junit.jupiter.api.Test)9 BindingContext (cn.taketoday.web.BindingContext)7 ConfigurableWebBindingInitializer (cn.taketoday.web.bind.support.ConfigurableWebBindingInitializer)6 StringTrimmerEditor (cn.taketoday.beans.propertyeditors.StringTrimmerEditor)3 ConversionService (cn.taketoday.core.conversion.ConversionService)1 DataBinder (cn.taketoday.validation.DataBinder)1 RequestContextDataBinder (cn.taketoday.web.bind.RequestContextDataBinder)1 WebDataBinder (cn.taketoday.web.bind.WebDataBinder)1 MethodArgumentTypeMismatchException (cn.taketoday.web.handler.method.MethodArgumentTypeMismatchException)1 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)1 MockHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockHttpServletRequest)1 Date (java.util.Date)1 UUID (java.util.UUID)1