Search in sources :

Example 1 with DefaultConversionService

use of cn.taketoday.core.conversion.support.DefaultConversionService in project today-infrastructure by TAKETODAY.

the class ConfigurationPropertiesTests method loadWhenBeanFactoryConversionServiceAndConverterBean.

@Test
void loadWhenBeanFactoryConversionServiceAndConverterBean() {
    DefaultConversionService conversionService = new DefaultConversionService();
    conversionService.addConverter(new AlienConverter());
    this.context.getBeanFactory().setConversionService(conversionService);
    load(new Class<?>[] { ConverterConfiguration.class, PersonAndAlienProperties.class }, "test.person=John Smith", "test.alien=Alf Tanner");
    PersonAndAlienProperties properties = this.context.getBean(PersonAndAlienProperties.class);
    assertThat(properties.getPerson().firstName).isEqualTo("John");
    assertThat(properties.getPerson().lastName).isEqualTo("Smith");
    assertThat(properties.getAlien().firstName).isEqualTo("Alf");
    assertThat(properties.getAlien().lastName).isEqualTo("Tanner");
}
Also used : DefaultConversionService(cn.taketoday.core.conversion.support.DefaultConversionService) Test(org.junit.jupiter.api.Test)

Example 2 with DefaultConversionService

use of cn.taketoday.core.conversion.support.DefaultConversionService in project today-infrastructure by TAKETODAY.

the class PropertySourcesPlaceholderConfigurerTests method optionalPropertyWithValue.

@Test
public void optionalPropertyWithValue() {
    StandardBeanFactory bf = new StandardBeanFactory();
    bf.setConversionService(new DefaultConversionService());
    bf.registerBeanDefinition("testBean", genericBeanDefinition(OptionalTestBean.class).addPropertyValue("name", "${my.name}").getBeanDefinition());
    MockEnvironment env = new MockEnvironment();
    env.setProperty("my.name", "myValue");
    PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
    ppc.setEnvironment(env);
    ppc.setIgnoreUnresolvablePlaceholders(true);
    ppc.postProcessBeanFactory(bf);
    assertThat(bf.getBean(OptionalTestBean.class).getName()).isEqualTo(Optional.of("myValue"));
}
Also used : DefaultConversionService(cn.taketoday.core.conversion.support.DefaultConversionService) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) Test(org.junit.jupiter.api.Test)

Example 3 with DefaultConversionService

use of cn.taketoday.core.conversion.support.DefaultConversionService in project today-infrastructure by TAKETODAY.

the class AbstractPropertyResolver method getConversionService.

@Override
public ConfigurableConversionService getConversionService() {
    // Need to provide an independent DefaultConversionService, not the
    // shared DefaultConversionService used by PropertySourcesPropertyResolver.
    ConfigurableConversionService cs = this.conversionService;
    if (cs == null) {
        synchronized (this) {
            cs = this.conversionService;
            if (cs == null) {
                cs = new DefaultConversionService();
                this.conversionService = cs;
            }
        }
    }
    return cs;
}
Also used : ConfigurableConversionService(cn.taketoday.core.conversion.support.ConfigurableConversionService) DefaultConversionService(cn.taketoday.core.conversion.support.DefaultConversionService)

Example 4 with DefaultConversionService

use of cn.taketoday.core.conversion.support.DefaultConversionService in project today-infrastructure by TAKETODAY.

the class ObjectToStringHttpMessageConverterTests method defaultCharsetModified.

@Test
public void defaultCharsetModified() throws IOException {
    ConversionService cs = new DefaultConversionService();
    ObjectToStringHttpMessageConverter converter = new ObjectToStringHttpMessageConverter(cs, StandardCharsets.UTF_16);
    converter.write((byte) 31, null, this.response);
    assertThat(this.servletResponse.getCharacterEncoding()).isEqualTo("UTF-16");
}
Also used : ConversionService(cn.taketoday.core.conversion.ConversionService) DefaultConversionService(cn.taketoday.core.conversion.support.DefaultConversionService) DefaultConversionService(cn.taketoday.core.conversion.support.DefaultConversionService) Test(org.junit.jupiter.api.Test)

Example 5 with DefaultConversionService

use of cn.taketoday.core.conversion.support.DefaultConversionService in project today-infrastructure by TAKETODAY.

the class MapBinderTests method bindToMapWithCustomConverterAndChildElements.

@Test
void bindToMapWithCustomConverterAndChildElements() {
    // gh-11892
    DefaultConversionService conversionService = new DefaultConversionService();
    conversionService.addConverter(new MapConverter());
    Binder binder = new Binder(this.sources, null, conversionService, null);
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo", "boom");
    source.put("foo.a", "a");
    source.put("foo.b", "b");
    this.sources.add(source);
    Map<String, String> map = binder.bind("foo", STRING_STRING_MAP).get();
    assertThat(map.get("a")).isEqualTo("a");
    assertThat(map.get("b")).isEqualTo("b");
}
Also used : MockConfigurationPropertySource(cn.taketoday.context.properties.source.MockConfigurationPropertySource) DefaultConversionService(cn.taketoday.core.conversion.support.DefaultConversionService) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultConversionService (cn.taketoday.core.conversion.support.DefaultConversionService)42 Test (org.junit.jupiter.api.Test)38 BindingContext (cn.taketoday.web.BindingContext)11 ConfigurableWebBindingInitializer (cn.taketoday.web.bind.support.ConfigurableWebBindingInitializer)11 MockHttpServletResponse (cn.taketoday.web.testfixture.servlet.MockHttpServletResponse)10 RequestParam (cn.taketoday.web.annotation.RequestParam)9 ResolvableMethodParameter (cn.taketoday.web.handler.method.ResolvableMethodParameter)9 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)9 Optional (java.util.Optional)7 StandardBeanFactory (cn.taketoday.beans.factory.support.StandardBeanFactory)4 GenericBean (cn.taketoday.beans.testfixture.beans.GenericBean)4 MockConfigurationPropertySource (cn.taketoday.context.properties.source.MockConfigurationPropertySource)4 ConversionService (cn.taketoday.core.conversion.ConversionService)4 PropertyValues (cn.taketoday.beans.PropertyValues)3 ConfigurableConversionService (cn.taketoday.core.conversion.support.ConfigurableConversionService)2 ServletServerHttpResponse (cn.taketoday.http.server.ServletServerHttpResponse)2 BeanPropertyBindingResult (cn.taketoday.validation.BeanPropertyBindingResult)2 FieldError (cn.taketoday.validation.FieldError)2 ResultSet (java.sql.ResultSet)2 ResultSetMetaData (java.sql.ResultSetMetaData)2