use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.
the class MapToMapConverterTests method emptyMapDifferentTargetImplType.
@Test
public void emptyMapDifferentTargetImplType() throws Exception {
Map<String, String> map = new HashMap<>();
TypeDescriptor sourceType = TypeDescriptor.forObject(map);
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("emptyMapDifferentTarget"));
assertTrue(conversionService.canConvert(sourceType, targetType));
@SuppressWarnings("unchecked") LinkedHashMap<String, String> result = (LinkedHashMap<String, String>) conversionService.convert(map, sourceType, targetType);
assertEquals(map, result);
assertEquals(LinkedHashMap.class, result.getClass());
}
use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.
the class DefaultConversionServiceTests method convertCollectionToCollection.
@Test
public void convertCollectionToCollection() throws Exception {
Set<String> foo = new LinkedHashSet<>();
foo.add("1");
foo.add("2");
foo.add("3");
@SuppressWarnings("unchecked") List<Integer> bar = (List<Integer>) conversionService.convert(foo, TypeDescriptor.forObject(foo), new TypeDescriptor(getClass().getField("genericList")));
assertEquals(new Integer(1), bar.get(0));
assertEquals(new Integer(2), bar.get(1));
assertEquals(new Integer(3), bar.get(2));
}
use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.
the class DefaultConversionServiceTests method convertObjectToCollectionWithElementConversion.
@Test
public void convertObjectToCollectionWithElementConversion() throws Exception {
@SuppressWarnings("unchecked") List<Integer> result = (List<Integer>) conversionService.convert(3L, TypeDescriptor.valueOf(Long.class), new TypeDescriptor(getClass().getField("genericList")));
assertEquals(1, result.size());
assertEquals(new Integer(3), result.get(0));
}
use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.
the class DefaultConversionServiceTests method convertCollectionToCollectionSpecialCaseSourceImpl.
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void convertCollectionToCollectionSpecialCaseSourceImpl() throws Exception {
Map map = new LinkedHashMap();
map.put("1", "1");
map.put("2", "2");
map.put("3", "3");
Collection values = map.values();
List<Integer> bar = (List<Integer>) conversionService.convert(values, TypeDescriptor.forObject(values), new TypeDescriptor(getClass().getField("genericList")));
assertEquals(3, bar.size());
assertEquals(new Integer(1), bar.get(0));
assertEquals(new Integer(2), bar.get(1));
assertEquals(new Integer(3), bar.get(2));
}
use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.
the class DefaultConversionServiceTests method convertObjectToOptional.
@Test
@SuppressWarnings("unchecked")
public void convertObjectToOptional() {
Method method = ClassUtils.getMethod(TestEntity.class, "handleOptionalValue", Optional.class);
MethodParameter parameter = new MethodParameter(method, 0);
TypeDescriptor descriptor = new TypeDescriptor(parameter);
Object actual = conversionService.convert("1,2,3", TypeDescriptor.valueOf(String.class), descriptor);
assertEquals(Optional.class, actual.getClass());
assertEquals(Arrays.asList(1, 2, 3), ((Optional<List<Integer>>) actual).get());
}
Aggregations