use of org.springframework.core.convert.TypeDescriptor 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));
}
use of org.springframework.core.convert.TypeDescriptor 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));
}
use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.
the class DefaultConversionServiceTests method convertCollectionToObjectAssignableTarget.
@Test
public void convertCollectionToObjectAssignableTarget() throws Exception {
Collection<String> source = new ArrayList<>();
source.add("foo");
Object result = conversionService.convert(source, new TypeDescriptor(getClass().getField("assignableTarget")));
assertEquals(source, result);
}
use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.
the class DefaultConversionServiceTests method testSpr7766.
@Test
public void testSpr7766() throws Exception {
ConverterRegistry registry = (conversionService);
registry.addConverter(new ColorConverter());
@SuppressWarnings("unchecked") List<Color> colors = (List<Color>) conversionService.convert(new String[] { "ffffff", "#000000" }, TypeDescriptor.valueOf(String[].class), new TypeDescriptor(new MethodParameter(getClass().getMethod("handlerMethod", List.class), 0)));
assertEquals(2, colors.size());
assertEquals(Color.WHITE, colors.get(0));
assertEquals(Color.BLACK, colors.get(1));
}
use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.
the class DefaultConversionServiceTests method convertArrayToCollectionGenericTypeConversion.
@Test
public void convertArrayToCollectionGenericTypeConversion() throws Exception {
@SuppressWarnings("unchecked") List<Integer> result = (List<Integer>) conversionService.convert(new String[] { "1", "2", "3" }, TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getDeclaredField("genericList")));
assertEquals(new Integer("1"), result.get(0));
assertEquals(new Integer("2"), result.get(1));
assertEquals(new Integer("3"), result.get(2));
}
Aggregations