Search in sources :

Example 11 with ConversionFailedException

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

the class MapToMapConverterTests method scalarMapNotGenericSourceField.

@Test
public void scalarMapNotGenericSourceField() throws Exception {
    Map<String, String> map = new HashMap<>();
    map.put("1", "9");
    map.put("2", "37");
    TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("notGenericMapSource"));
    TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarMapTarget"));
    assertTrue(conversionService.canConvert(sourceType, targetType));
    try {
        conversionService.convert(map, sourceType, targetType);
    } catch (ConversionFailedException ex) {
        assertTrue(ex.getCause() instanceof ConverterNotFoundException);
    }
    conversionService.addConverterFactory(new StringToNumberConverterFactory());
    assertTrue(conversionService.canConvert(sourceType, targetType));
    @SuppressWarnings("unchecked") Map<Integer, Integer> result = (Map<Integer, Integer>) conversionService.convert(map, sourceType, targetType);
    assertFalse(map.equals(result));
    assertEquals((Integer) 9, result.get(1));
    assertEquals((Integer) 37, result.get(2));
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ConversionFailedException(org.springframework.core.convert.ConversionFailedException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ConverterNotFoundException(org.springframework.core.convert.ConverterNotFoundException) EnumMap(java.util.EnumMap) MultiValueMap(org.springframework.util.MultiValueMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.Test)

Example 12 with ConversionFailedException

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

the class FormattingConversionServiceFactoryBeanTests method testDefaultFormattersOff.

@Test
public void testDefaultFormattersOff() throws Exception {
    FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean();
    factory.setRegisterDefaultFormatters(false);
    factory.afterPropertiesSet();
    FormattingConversionService fcs = factory.getObject();
    TypeDescriptor descriptor = new TypeDescriptor(TestBean.class.getDeclaredField("pattern"));
    try {
        fcs.convert("15,00", TypeDescriptor.valueOf(String.class), descriptor);
        fail("This format should not be parseable");
    } catch (ConversionFailedException ex) {
        assertTrue(ex.getCause() instanceof NumberFormatException);
    }
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ConversionFailedException(org.springframework.core.convert.ConversionFailedException) Test(org.junit.Test)

Aggregations

ConversionFailedException (org.springframework.core.convert.ConversionFailedException)12 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)10 Test (org.junit.Test)6 Map (java.util.Map)5 ConverterNotFoundException (org.springframework.core.convert.ConverterNotFoundException)5 HashMap (java.util.HashMap)4 EnumMap (java.util.EnumMap)3 LinkedHashMap (java.util.LinkedHashMap)3 ConversionService (org.springframework.core.convert.ConversionService)3 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)3 MultiValueMap (org.springframework.util.MultiValueMap)3 Constructor (java.lang.reflect.Constructor)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Collection (java.util.Collection)2 List (java.util.List)2 JsonObjectBase (com.baidu.dsp.common.vo.JsonObjectBase)1 PropertyEditor (java.beans.PropertyEditor)1 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 Member (java.lang.reflect.Member)1