use of cn.taketoday.core.TypeDescriptor in project today-framework by TAKETODAY.
the class MapToMapConverterTests method multiValueMapToMultiValueMap.
@Test
@SuppressWarnings("unchecked")
void multiValueMapToMultiValueMap() throws Exception {
DefaultConversionService.addDefaultConverters(conversionService);
MultiValueMap<String, Integer> source = MultiValueMap.fromLinkedHashMap();
source.put("a", Arrays.asList(1, 2, 3));
source.put("b", Arrays.asList(4, 5, 6));
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("multiValueMapTarget"));
MultiValueMap<String, String> converted = (MultiValueMap<String, String>) conversionService.convert(source, targetType);
assertThat(converted.size()).isEqualTo(2);
assertThat(converted.get("a")).isEqualTo(Arrays.asList("1", "2", "3"));
assertThat(converted.get("b")).isEqualTo(Arrays.asList("4", "5", "6"));
}
use of cn.taketoday.core.TypeDescriptor in project today-framework by TAKETODAY.
the class CollectionToCollectionConverterTests method allNulls.
@Test
void allNulls() throws Exception {
List<Resource> resources = new ArrayList<>();
resources.add(null);
resources.add(null);
TypeDescriptor sourceType = TypeDescriptor.fromObject(resources);
assertThat(conversionService.convert(resources, sourceType, new TypeDescriptor(getClass().getField("resources")))).isSameAs(resources);
}
use of cn.taketoday.core.TypeDescriptor in project today-framework by TAKETODAY.
the class CollectionToCollectionConverterTests method stringToEnumSet.
@Test
void stringToEnumSet() throws Exception {
conversionService.addConverterFactory(new StringToEnumConverterFactory());
List<String> list = new ArrayList<>();
list.add("A");
list.add("C");
assertThat(conversionService.convert(list, TypeDescriptor.fromObject(list), new TypeDescriptor(getClass().getField("enumSet")))).isEqualTo(EnumSet.of(MyEnum.A, MyEnum.C));
}
use of cn.taketoday.core.TypeDescriptor in project today-framework by TAKETODAY.
the class CollectionToCollectionConverterTests method mixedInNulls.
@Test
void mixedInNulls() throws Exception {
List<Resource> resources = new ArrayList<>();
resources.add(new ClassPathResource("test"));
resources.add(null);
resources.add(new FileSystemResource("test"));
resources.add(new TestResource());
TypeDescriptor sourceType = TypeDescriptor.fromObject(resources);
assertThat(conversionService.convert(resources, sourceType, new TypeDescriptor(getClass().getField("resources")))).isSameAs(resources);
}
use of cn.taketoday.core.TypeDescriptor in project today-framework by TAKETODAY.
the class CollectionToCollectionConverterTests method objectToCollection.
@Test
@SuppressWarnings("unchecked")
void objectToCollection() throws Exception {
List<List<String>> list = new ArrayList<>();
list.add(Arrays.asList("9", "12"));
list.add(Arrays.asList("37", "23"));
conversionService.addConverterFactory(new StringToNumberConverterFactory());
conversionService.addConverter(new ObjectToCollectionConverter(conversionService));
conversionService.addConverter(new CollectionToObjectConverter(conversionService));
TypeDescriptor sourceType = TypeDescriptor.fromObject(list);
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("objectToCollection"));
assertThat(conversionService.canConvert(sourceType, targetType)).isTrue();
List<List<List<Integer>>> result = (List<List<List<Integer>>>) conversionService.convert(list, sourceType, targetType);
assertThat(result.get(0).get(0).get(0)).isEqualTo((Integer) 9);
assertThat(result.get(0).get(1).get(0)).isEqualTo((Integer) 12);
assertThat(result.get(1).get(0).get(0)).isEqualTo((Integer) 37);
assertThat(result.get(1).get(1).get(0)).isEqualTo((Integer) 23);
}
Aggregations