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();
}
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);
}
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();
}
}
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");
}
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");
}
Aggregations