use of org.springframework.core.convert.TypeDescriptor 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());
}
use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.
the class CollectionToCollectionConverterTests method differentImpls.
@Test
public void differentImpls() throws Exception {
List<Resource> resources = new ArrayList<>();
resources.add(new ClassPathResource("test"));
resources.add(new FileSystemResource("test"));
resources.add(new TestResource());
TypeDescriptor sourceType = TypeDescriptor.forObject(resources);
assertSame(resources, conversionService.convert(resources, sourceType, new TypeDescriptor(getClass().getField("resources"))));
}
use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.
the class CollectionToCollectionConverterTests method nothingInCommon.
@Test(expected = ConversionFailedException.class)
public void nothingInCommon() throws Exception {
List<Object> resources = new ArrayList<>();
resources.add(new ClassPathResource("test"));
resources.add(3);
TypeDescriptor sourceType = TypeDescriptor.forObject(resources);
assertEquals(resources, conversionService.convert(resources, sourceType, new TypeDescriptor(getClass().getField("resources"))));
}
use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.
the class MapToMapConverterTests method noDefaultConstructorCopyNotRequired.
@Test
public void noDefaultConstructorCopyNotRequired() throws Exception {
// SPR-9284
NoDefaultConstructorMap<String, Integer> map = new NoDefaultConstructorMap<>(Collections.<String, Integer>singletonMap("1", 1));
TypeDescriptor sourceType = TypeDescriptor.map(NoDefaultConstructorMap.class, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class));
TypeDescriptor targetType = TypeDescriptor.map(NoDefaultConstructorMap.class, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class));
assertTrue(conversionService.canConvert(sourceType, targetType));
@SuppressWarnings("unchecked") Map<String, Integer> result = (Map<String, Integer>) conversionService.convert(map, sourceType, targetType);
assertEquals(map, result);
assertEquals(NoDefaultConstructorMap.class, result.getClass());
}
use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.
the class StreamConverterTests method convertFromStreamToList.
@Test
public void convertFromStreamToList() throws NoSuchFieldException {
this.conversionService.addConverter(Number.class, String.class, new ObjectToStringConverter());
Stream<Integer> stream = Arrays.asList(1, 2, 3).stream();
TypeDescriptor listOfStrings = new TypeDescriptor(Types.class.getField("listOfStrings"));
;
Object result = this.conversionService.convert(stream, listOfStrings);
assertNotNull("Converted object must not be null", result);
assertTrue("Converted object must be a list", result instanceof List);
@SuppressWarnings("unchecked") List<String> content = (List<String>) result;
assertEquals("1", content.get(0));
assertEquals("2", content.get(1));
assertEquals("3", content.get(2));
assertEquals("Wrong number of elements", 3, content.size());
}
Aggregations