Search in sources :

Example 66 with DefaultConversionService

use of org.springframework.core.convert.support.DefaultConversionService in project spring-framework by spring-projects.

the class DefaultListableBeanFactoryTests method customConverter.

@Test
void customConverter() {
    GenericConversionService conversionService = new DefaultConversionService();
    conversionService.addConverter(String.class, Float.class, source -> {
        try {
            NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
            return nf.parse(source).floatValue();
        } catch (ParseException ex) {
            throw new IllegalArgumentException(ex);
        }
    });
    lbf.setConversionService(conversionService);
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("myFloat", "1,1");
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    bd.setPropertyValues(pvs);
    lbf.registerBeanDefinition("testBean", bd);
    TestBean testBean = (TestBean) lbf.getBean("testBean");
    assertThat(testBean.getMyFloat().floatValue() == 1.1f).isTrue();
}
Also used : DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) ParseException(java.text.ParseException) GenericConversionService(org.springframework.core.convert.support.GenericConversionService) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) NumberFormat(java.text.NumberFormat) Test(org.junit.jupiter.api.Test)

Example 67 with DefaultConversionService

use of org.springframework.core.convert.support.DefaultConversionService in project spring-framework by spring-projects.

the class BeanWrapperEnumTests method testStandardEnumSetWithMultipleValues.

@Test
public void testStandardEnumSetWithMultipleValues() {
    GenericBean<?> gb = new GenericBean<>();
    BeanWrapper bw = new BeanWrapperImpl(gb);
    bw.setConversionService(new DefaultConversionService());
    assertThat(gb.getStandardEnumSet()).isNull();
    bw.setPropertyValue("standardEnumSet", new String[] { "VALUE_1", "VALUE_2" });
    assertThat(gb.getStandardEnumSet().size()).isEqualTo(2);
    assertThat(gb.getStandardEnumSet().contains(CustomEnum.VALUE_1)).isTrue();
    assertThat(gb.getStandardEnumSet().contains(CustomEnum.VALUE_2)).isTrue();
}
Also used : DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) GenericBean(org.springframework.beans.testfixture.beans.GenericBean) Test(org.junit.jupiter.api.Test)

Example 68 with DefaultConversionService

use of org.springframework.core.convert.support.DefaultConversionService in project spring-framework by spring-projects.

the class BeanWrapperEnumTests method testStandardEnumMapWithMultipleValues.

@Test
public void testStandardEnumMapWithMultipleValues() {
    GenericBean<?> gb = new GenericBean<>();
    BeanWrapper bw = new BeanWrapperImpl(gb);
    bw.setConversionService(new DefaultConversionService());
    assertThat(gb.getStandardEnumMap()).isNull();
    Map<String, Integer> map = new LinkedHashMap<>();
    map.put("VALUE_1", 1);
    map.put("VALUE_2", 2);
    bw.setPropertyValue("standardEnumMap", map);
    assertThat(gb.getStandardEnumMap().size()).isEqualTo(2);
    assertThat(gb.getStandardEnumMap().get(CustomEnum.VALUE_1)).isEqualTo(1);
    assertThat(gb.getStandardEnumMap().get(CustomEnum.VALUE_2)).isEqualTo(2);
}
Also used : DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) GenericBean(org.springframework.beans.testfixture.beans.GenericBean) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.jupiter.api.Test)

Example 69 with DefaultConversionService

use of org.springframework.core.convert.support.DefaultConversionService in project spring-framework by spring-projects.

the class DataBinderTests method testOptionalProperty.

@Test
void testOptionalProperty() {
    OptionalHolder bean = new OptionalHolder();
    DataBinder binder = new DataBinder(bean);
    binder.setConversionService(new DefaultConversionService());
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("id", "1");
    pvs.add("name", null);
    binder.bind(pvs);
    assertThat(bean.getId()).isEqualTo("1");
    assertThat(bean.getName()).isEmpty();
    pvs = new MutablePropertyValues();
    pvs.add("id", "2");
    pvs.add("name", "myName");
    binder.bind(pvs);
    assertThat(bean.getId()).isEqualTo("2");
    assertThat(bean.getName().get()).isEqualTo("myName");
}
Also used : MutablePropertyValues(org.springframework.beans.MutablePropertyValues) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) Test(org.junit.jupiter.api.Test)

Example 70 with DefaultConversionService

use of org.springframework.core.convert.support.DefaultConversionService in project spring-framework by spring-projects.

the class PathVariableMethodArgumentResolverTests method wrapEmptyWithOptional.

@Test
public void wrapEmptyWithOptional() throws Exception {
    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(new DefaultConversionService());
    WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);
    assertThat(resolver.resolveArgument(paramOptional, mavContainer, webRequest, binderFactory)).isEqualTo(Optional.empty());
}
Also used : ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) DefaultDataBinderFactory(org.springframework.web.bind.support.DefaultDataBinderFactory) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultConversionService (org.springframework.core.convert.support.DefaultConversionService)84 Test (org.junit.jupiter.api.Test)37 Test (org.junit.Test)29 ConversionService (org.springframework.core.convert.ConversionService)13 MethodParameter (org.springframework.core.MethodParameter)12 GenericConversionService (org.springframework.core.convert.support.GenericConversionService)12 ConfigurableWebBindingInitializer (org.springframework.web.bind.support.ConfigurableWebBindingInitializer)12 WebDataBinderFactory (org.springframework.web.bind.support.WebDataBinderFactory)12 DefaultDataBinderFactory (org.springframework.web.bind.support.DefaultDataBinderFactory)11 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)9 RequestParam (org.springframework.web.bind.annotation.RequestParam)9 Optional (java.util.Optional)8 BeforeEach (org.junit.jupiter.api.BeforeEach)6 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)6 HashMap (java.util.HashMap)5 DefaultDatatypeChannelMessageConverter (org.springframework.integration.support.converter.DefaultDatatypeChannelMessageConverter)5 GenericMessage (org.springframework.messaging.support.GenericMessage)5 Before (org.junit.Before)4 ContentConverter (com.synopsys.integration.alert.common.ContentConverter)3 ConfigurationModelConfigurationAccessor (com.synopsys.integration.alert.common.persistence.accessor.ConfigurationModelConfigurationAccessor)3