Search in sources :

Example 96 with TypeDescriptor

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"));
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor) MultiValueMap(cn.taketoday.core.MultiValueMap) Test(org.junit.jupiter.api.Test)

Example 97 with TypeDescriptor

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);
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor) Resource(cn.taketoday.core.io.Resource) ClassPathResource(cn.taketoday.core.io.ClassPathResource) FileSystemResource(cn.taketoday.core.io.FileSystemResource) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 98 with TypeDescriptor

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));
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 99 with TypeDescriptor

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);
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor) Resource(cn.taketoday.core.io.Resource) ClassPathResource(cn.taketoday.core.io.ClassPathResource) FileSystemResource(cn.taketoday.core.io.FileSystemResource) ArrayList(java.util.ArrayList) FileSystemResource(cn.taketoday.core.io.FileSystemResource) ClassPathResource(cn.taketoday.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 100 with TypeDescriptor

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);
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Aggregations

TypeDescriptor (cn.taketoday.core.TypeDescriptor)221 Test (org.junit.jupiter.api.Test)128 ArrayList (java.util.ArrayList)44 Nullable (cn.taketoday.lang.Nullable)36 List (java.util.List)28 LinkedHashMap (java.util.LinkedHashMap)20 MethodParameter (cn.taketoday.core.MethodParameter)18 ConverterNotFoundException (cn.taketoday.core.conversion.ConverterNotFoundException)18 HashMap (java.util.HashMap)18 ConversionFailedException (cn.taketoday.core.conversion.ConversionFailedException)16 StandardEvaluationContext (cn.taketoday.expression.spel.support.StandardEvaluationContext)16 MultiValueMap (cn.taketoday.core.MultiValueMap)14 EnumMap (java.util.EnumMap)12 MethodExecutor (cn.taketoday.expression.MethodExecutor)10 Collection (java.util.Collection)10 Map (java.util.Map)10 ConversionService (cn.taketoday.core.conversion.ConversionService)8 ClassPathResource (cn.taketoday.core.io.ClassPathResource)8 AccessException (cn.taketoday.expression.AccessException)8 EvaluationException (cn.taketoday.expression.EvaluationException)8