Search in sources :

Example 6 with ConverterNotFoundException

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

the class GenericConversionServiceTests method adaptedCollectionTypesFromSameSourceType.

@Test
public void adaptedCollectionTypesFromSameSourceType() throws Exception {
    conversionService.addConverter(new MyStringToStringCollectionConverter());
    assertEquals(Collections.singleton("testX"), conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection"))));
    assertEquals(Collections.singleton("testX"), conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("genericCollection"))));
    assertEquals(Collections.singleton("testX"), conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("rawCollection"))));
    assertEquals(Collections.singleton("testX"), conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("genericCollection"))));
    assertEquals(Collections.singleton("testX"), conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection"))));
    assertEquals(Collections.singleton("testX"), conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("rawCollection"))));
    try {
        conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("integerCollection")));
        fail("Should have thrown ConverterNotFoundException");
    } catch (ConverterNotFoundException ex) {
    // expected
    }
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ConverterNotFoundException(org.springframework.core.convert.ConverterNotFoundException) Test(org.junit.Test)

Example 7 with ConverterNotFoundException

use of org.springframework.core.convert.ConverterNotFoundException 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)

Aggregations

ConverterNotFoundException (org.springframework.core.convert.ConverterNotFoundException)7 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)7 Test (org.junit.Test)6 Map (java.util.Map)5 ConversionFailedException (org.springframework.core.convert.ConversionFailedException)5 EnumMap (java.util.EnumMap)4 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)4 MultiValueMap (org.springframework.util.MultiValueMap)4 List (java.util.List)3 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 LinkedList (java.util.LinkedList)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 TypeConversionException (org.apache.camel.TypeConversionException)1 ConversionService (org.springframework.core.convert.ConversionService)1