use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.
the class GenericConversionServiceTests method stringToCollectionCanConvert.
@Test
public void stringToCollectionCanConvert() throws Exception {
conversionService.addConverter(new StringToCollectionConverter(conversionService));
assertTrue(conversionService.canConvert(String.class, Collection.class));
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("integerCollection"));
assertFalse(conversionService.canConvert(TypeDescriptor.valueOf(String.class), targetType));
conversionService.addConverterFactory(new StringToNumberConverterFactory());
assertTrue(conversionService.canConvert(TypeDescriptor.valueOf(String.class), targetType));
}
use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.
the class GenericConversionServiceTests method emptyListToArray.
@Test
public void emptyListToArray() {
conversionService.addConverter(new CollectionToArrayConverter(conversionService));
conversionService.addConverterFactory(new StringToNumberConverterFactory());
List<String> list = new ArrayList<>();
TypeDescriptor sourceType = TypeDescriptor.forObject(list);
TypeDescriptor targetType = TypeDescriptor.valueOf(String[].class);
assertTrue(conversionService.canConvert(sourceType, targetType));
assertEquals(0, ((String[]) conversionService.convert(list, sourceType, targetType)).length);
}
use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.
the class GenericConversionServiceTests method convertNullAnnotatedStringToString.
@Test
public void convertNullAnnotatedStringToString() throws Exception {
String source = null;
TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("annotatedString"));
TypeDescriptor targetType = TypeDescriptor.valueOf(String.class);
conversionService.convert(source, sourceType, targetType);
}
use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.
the class MapToMapConverterTests method emptyMap.
@Test
public void emptyMap() throws Exception {
Map<String, String> map = new HashMap<>();
TypeDescriptor sourceType = TypeDescriptor.forObject(map);
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("emptyMapTarget"));
assertTrue(conversionService.canConvert(sourceType, targetType));
assertSame(map, conversionService.convert(map, sourceType, targetType));
}
use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.
the class MapToMapConverterTests method mapToMultiValueMap.
@Test
@SuppressWarnings("unchecked")
public void mapToMultiValueMap() throws Exception {
DefaultConversionService.addDefaultConverters(conversionService);
Map<String, Integer> source = new HashMap<>();
source.put("a", 1);
source.put("b", 2);
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("multiValueMapTarget"));
MultiValueMap<String, String> converted = (MultiValueMap<String, String>) conversionService.convert(source, targetType);
assertThat(converted.size(), equalTo(2));
assertThat(converted.get("a"), equalTo(Arrays.asList("1")));
assertThat(converted.get("b"), equalTo(Arrays.asList("2")));
}
Aggregations