Search in sources :

Example 1 with ConverterNotFoundException

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

the class MapToMapConverterTests method scalarMap.

@Test
public void scalarMap() throws Exception {
    Map<String, String> map = new HashMap<>();
    map.put("1", "9");
    map.put("2", "37");
    TypeDescriptor sourceType = TypeDescriptor.forObject(map);
    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 2 with ConverterNotFoundException

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

the class MapToMapConverterTests method collectionMap.

@Test
public void collectionMap() throws Exception {
    Map<String, List<String>> map = new HashMap<>();
    map.put("1", Arrays.asList("9", "12"));
    map.put("2", Arrays.asList("37", "23"));
    TypeDescriptor sourceType = TypeDescriptor.forObject(map);
    TypeDescriptor targetType = new TypeDescriptor(getClass().getField("collectionMapTarget"));
    assertTrue(conversionService.canConvert(sourceType, targetType));
    try {
        conversionService.convert(map, sourceType, targetType);
    } catch (ConversionFailedException ex) {
        assertTrue(ex.getCause() instanceof ConverterNotFoundException);
    }
    conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
    conversionService.addConverterFactory(new StringToNumberConverterFactory());
    assertTrue(conversionService.canConvert(sourceType, targetType));
    @SuppressWarnings("unchecked") Map<Integer, List<Integer>> result = (Map<Integer, List<Integer>>) conversionService.convert(map, sourceType, targetType);
    assertFalse(map.equals(result));
    assertEquals(Arrays.asList(9, 12), result.get(1));
    assertEquals(Arrays.asList(37, 23), result.get(2));
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ConverterNotFoundException(org.springframework.core.convert.ConverterNotFoundException) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ConversionFailedException(org.springframework.core.convert.ConversionFailedException) List(java.util.List) 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 3 with ConverterNotFoundException

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

the class CollectionToCollectionConverterTests method scalarList.

@Test
public void scalarList() throws Exception {
    List<String> list = new ArrayList<>();
    list.add("9");
    list.add("37");
    TypeDescriptor sourceType = TypeDescriptor.forObject(list);
    TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarListTarget"));
    assertTrue(conversionService.canConvert(sourceType, targetType));
    try {
        conversionService.convert(list, sourceType, targetType);
    } catch (ConversionFailedException ex) {
        assertTrue(ex.getCause() instanceof ConverterNotFoundException);
    }
    conversionService.addConverterFactory(new StringToNumberConverterFactory());
    assertTrue(conversionService.canConvert(sourceType, targetType));
    @SuppressWarnings("unchecked") List<Integer> result = (List<Integer>) conversionService.convert(list, sourceType, targetType);
    assertFalse(list.equals(result));
    assertEquals(9, result.get(0).intValue());
    assertEquals(37, result.get(1).intValue());
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ConversionFailedException(org.springframework.core.convert.ConversionFailedException) ConverterNotFoundException(org.springframework.core.convert.ConverterNotFoundException) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 4 with ConverterNotFoundException

use of org.springframework.core.convert.ConverterNotFoundException in project camel by apache.

the class SpringTypeConverter method convertTo.

@Override
public <T> T convertTo(Class<T> type, Exchange exchange, Object value) throws TypeConversionException {
    // do not attempt to convert Camel types
    if (type.getCanonicalName().startsWith("org.apache")) {
        return null;
    }
    // do not attempt to convert List -> Map. Ognl expression may use this converter as a fallback expecting null
    if (type.isAssignableFrom(Map.class) && (value.getClass().isArray() || value instanceof Collection)) {
        return null;
    }
    TypeDescriptor sourceType = types.computeIfAbsent(value.getClass(), TypeDescriptor::valueOf);
    TypeDescriptor targetType = types.computeIfAbsent(type, TypeDescriptor::valueOf);
    for (ConversionService conversionService : conversionServices) {
        if (conversionService.canConvert(sourceType, targetType)) {
            try {
                return (T) conversionService.convert(value, sourceType, targetType);
            } catch (ConversionFailedException e) {
                //
                if (e.getCause() instanceof ConverterNotFoundException && isArrayOrCollection(value)) {
                    return null;
                } else {
                    throw new TypeConversionException(value, type, e);
                }
            }
        }
    }
    return null;
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ConversionFailedException(org.springframework.core.convert.ConversionFailedException) TypeConversionException(org.apache.camel.TypeConversionException) ConversionService(org.springframework.core.convert.ConversionService) ConverterNotFoundException(org.springframework.core.convert.ConverterNotFoundException) Collection(java.util.Collection) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 5 with ConverterNotFoundException

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

the class MapToMapConverterTests method collectionMapSourceTarget.

@Test
public void collectionMapSourceTarget() throws Exception {
    Map<String, List<String>> map = new HashMap<>();
    map.put("1", Arrays.asList("9", "12"));
    map.put("2", Arrays.asList("37", "23"));
    TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("sourceCollectionMapTarget"));
    TypeDescriptor targetType = new TypeDescriptor(getClass().getField("collectionMapTarget"));
    assertFalse(conversionService.canConvert(sourceType, targetType));
    try {
        conversionService.convert(map, sourceType, targetType);
        fail("Should have failed");
    } catch (ConverterNotFoundException ex) {
    // expected
    }
    conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
    conversionService.addConverterFactory(new StringToNumberConverterFactory());
    assertTrue(conversionService.canConvert(sourceType, targetType));
    @SuppressWarnings("unchecked") Map<Integer, List<Integer>> result = (Map<Integer, List<Integer>>) conversionService.convert(map, sourceType, targetType);
    assertFalse(map.equals(result));
    assertEquals(Arrays.asList(9, 12), result.get(1));
    assertEquals(Arrays.asList(37, 23), result.get(2));
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ConverterNotFoundException(org.springframework.core.convert.ConverterNotFoundException) List(java.util.List) 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