Search in sources :

Example 6 with TypeDescriptor

use of cn.taketoday.core.TypeDescriptor in project today-infrastructure by TAKETODAY.

the class DelimitedStringToCollectionConverterTests method matchesWhenHasAnnotationAndConvertibleElementTypeShouldReturnTrue.

@Test
void matchesWhenHasAnnotationAndConvertibleElementTypeShouldReturnTrue() {
    TypeDescriptor sourceType = TypeDescriptor.valueOf(String.class);
    TypeDescriptor targetType = TypeDescriptor.nested(ReflectionUtils.findField(Values.class, "convertibleElementType"), 0);
    assertThat(new DelimitedStringToCollectionConverter(new ApplicationConversionService()).matches(sourceType, targetType)).isTrue();
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor) Test(org.junit.jupiter.api.Test)

Example 7 with TypeDescriptor

use of cn.taketoday.core.TypeDescriptor in project today-infrastructure by TAKETODAY.

the class DelimitedStringToCollectionConverterTests method convertWhenHasConvertibleElementTypeShouldReturnConvertedType.

@Test
@SuppressWarnings("unchecked")
void convertWhenHasConvertibleElementTypeShouldReturnConvertedType() {
    TypeDescriptor sourceType = TypeDescriptor.valueOf(String.class);
    TypeDescriptor targetType = TypeDescriptor.nested(ReflectionUtils.findField(Values.class, "convertibleElementType"), 0);
    List<Integer> converted = (List<Integer>) new ApplicationConversionService().convert(" 1 |  2| 3  ", sourceType, targetType);
    assertThat(converted).containsExactly(1, 2, 3);
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor) List(java.util.List) LinkedList(java.util.LinkedList) Test(org.junit.jupiter.api.Test)

Example 8 with TypeDescriptor

use of cn.taketoday.core.TypeDescriptor in project today-infrastructure by TAKETODAY.

the class FormattingConversionServiceFactoryBeanTests method testDefaultFormattersOn.

@Test
public void testDefaultFormattersOn() throws Exception {
    FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean();
    factory.afterPropertiesSet();
    FormattingConversionService fcs = factory.getObject();
    TypeDescriptor descriptor = new TypeDescriptor(TestBean.class.getDeclaredField("pattern"));
    LocaleContextHolder.setLocale(Locale.GERMAN);
    try {
        Object value = fcs.convert("15,00", TypeDescriptor.valueOf(String.class), descriptor);
        assertThat(value).isEqualTo(15.0);
        value = fcs.convert(15.0, descriptor, TypeDescriptor.valueOf(String.class));
        assertThat(value).isEqualTo("15");
    } finally {
        LocaleContextHolder.resetLocaleContext();
    }
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor) Test(org.junit.jupiter.api.Test)

Example 9 with TypeDescriptor

use of cn.taketoday.core.TypeDescriptor in project today-infrastructure by TAKETODAY.

the class CharSequenceToObjectConverterTests method convertWhenTargetIsListAndNotUsingApplicationConversionService.

@Test
@SuppressWarnings("unchecked")
void convertWhenTargetIsListAndNotUsingApplicationConversionService() {
    FormattingConversionService conversionService = new DefaultFormattingConversionService();
    conversionService.addConverter(new CharSequenceToObjectConverter(conversionService));
    StringBuilder source = new StringBuilder("1,2,3");
    TypeDescriptor sourceType = TypeDescriptor.valueOf(StringBuilder.class);
    TypeDescriptor targetType = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(String.class));
    List<String> conveted = (List<String>) conversionService.convert(source, sourceType, targetType);
    assertThat(conveted).containsExactly("1", "2", "3");
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 10 with TypeDescriptor

use of cn.taketoday.core.TypeDescriptor in project today-infrastructure by TAKETODAY.

the class DelimitedStringToArrayConverterTests method convertWhenHasDelimiterOfNoneShouldReturnWholeString.

@ConversionServiceTest
void convertWhenHasDelimiterOfNoneShouldReturnWholeString(ConversionService conversionService) {
    TypeDescriptor sourceType = TypeDescriptor.valueOf(String.class);
    TypeDescriptor targetType = TypeDescriptor.nested(ReflectionUtils.findField(Values.class, "delimiterNone"), 0);
    String[] converted = (String[]) conversionService.convert("a,b,c", sourceType, targetType);
    assertThat(converted).containsExactly("a,b,c");
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor)

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