use of cn.taketoday.core.conversion.support.GenericConversionService in project today-infrastructure by TAKETODAY.
the class BooleanExpressionTests method testConvertAndHandleNull.
@Test
public void testConvertAndHandleNull() {
// SPR-9445
// without null conversion
evaluateAndCheckError("null or true", SpelMessage.TYPE_CONVERSION_ERROR, 0, "null", "boolean");
evaluateAndCheckError("null and true", SpelMessage.TYPE_CONVERSION_ERROR, 0, "null", "boolean");
evaluateAndCheckError("!null", SpelMessage.TYPE_CONVERSION_ERROR, 1, "null", "boolean");
evaluateAndCheckError("null ? 'foo' : 'bar'", SpelMessage.TYPE_CONVERSION_ERROR, 0, "null", "boolean");
// with null conversion (null -> false)
GenericConversionService conversionService = new GenericConversionService() {
@Override
protected Object convertNullSource(TypeDescriptor sourceType, TypeDescriptor targetType) {
return targetType.getType() == Boolean.class ? false : null;
}
};
context.setTypeConverter(new StandardTypeConverter(conversionService));
evaluate("null or true", Boolean.TRUE, Boolean.class, false);
evaluate("null and true", Boolean.FALSE, Boolean.class, false);
evaluate("!null", Boolean.TRUE, Boolean.class, false);
evaluate("null ? 'foo' : 'bar'", "bar", String.class, false);
}
use of cn.taketoday.core.conversion.support.GenericConversionService in project today-infrastructure by TAKETODAY.
the class BindConverterTests method getBindConverter.
private BindConverter getBindConverter(Converter<?, ?> converter) {
GenericConversionService conversionService = new GenericConversionService();
conversionService.addConverter(converter);
return BindConverter.get(Collections.singletonList(conversionService), null);
}
use of cn.taketoday.core.conversion.support.GenericConversionService in project today-infrastructure by TAKETODAY.
the class AbstractPropertyAccessorTests method setPropertyIntermediateListIsNullWithBadConversionService.
@Test
void setPropertyIntermediateListIsNullWithBadConversionService() {
Foo target = new Foo();
AbstractPropertyAccessor accessor = createAccessor(target);
accessor.setConversionService(new GenericConversionService() {
@Override
public Object convert(@Nullable Object source, @Nullable TypeDescriptor sourceType, TypeDescriptor targetType) {
throw new ConversionFailedException(sourceType, targetType, source, null);
}
});
accessor.setAutoGrowNestedPaths(true);
accessor.setPropertyValue("listOfMaps[0]['luckyNumber']", "9");
assertThat(target.listOfMaps.get(0).get("luckyNumber")).isEqualTo("9");
}
use of cn.taketoday.core.conversion.support.GenericConversionService in project today-framework by TAKETODAY.
the class BooleanExpressionTests method testConvertAndHandleNull.
@Test
public void testConvertAndHandleNull() {
// SPR-9445
// without null conversion
evaluateAndCheckError("null or true", SpelMessage.TYPE_CONVERSION_ERROR, 0, "null", "boolean");
evaluateAndCheckError("null and true", SpelMessage.TYPE_CONVERSION_ERROR, 0, "null", "boolean");
evaluateAndCheckError("!null", SpelMessage.TYPE_CONVERSION_ERROR, 1, "null", "boolean");
evaluateAndCheckError("null ? 'foo' : 'bar'", SpelMessage.TYPE_CONVERSION_ERROR, 0, "null", "boolean");
// with null conversion (null -> false)
GenericConversionService conversionService = new GenericConversionService() {
@Override
protected Object convertNullSource(TypeDescriptor sourceType, TypeDescriptor targetType) {
return targetType.getType() == Boolean.class ? false : null;
}
};
context.setTypeConverter(new StandardTypeConverter(conversionService));
evaluate("null or true", Boolean.TRUE, Boolean.class, false);
evaluate("null and true", Boolean.FALSE, Boolean.class, false);
evaluate("!null", Boolean.TRUE, Boolean.class, false);
evaluate("null ? 'foo' : 'bar'", "bar", String.class, false);
}
use of cn.taketoday.core.conversion.support.GenericConversionService in project today-framework by TAKETODAY.
the class BindConverterTests method fallsBackToApplicationConversionService.
@Test
void fallsBackToApplicationConversionService() {
BindConverter bindConverter = BindConverter.get(Collections.singletonList(new GenericConversionService()), null);
Duration result = bindConverter.convert("10s", ResolvableType.fromClass(Duration.class));
assertThat(result.getSeconds()).isEqualTo(10);
}
Aggregations