use of cn.taketoday.beans.PropertyValue in project today-infrastructure by TAKETODAY.
the class JdbcNamespaceIntegrationTests method assertBeanPropertyValueOf.
private void assertBeanPropertyValueOf(String propertyName, String expected, StandardBeanFactory factory) {
BeanDefinition bean = factory.getBeanDefinition(expected);
PropertyValue value = bean.getPropertyValues().get(propertyName);
assertThat(value).isNotNull();
assertThat(value.getValue().toString()).isEqualTo(expected);
}
use of cn.taketoday.beans.PropertyValue in project today-infrastructure by TAKETODAY.
the class AbstractDataBinderParameterResolver method resolveName.
@Nullable
@Override
protected Object resolveName(String name, ResolvableMethodParameter resolvable, RequestContext context) throws Exception {
final int parameterNameLength = name.length();
// prepare property values
final Map<String, String[]> parameters = context.getParameters();
final DefaultMultiValueMap<String, PropertyValue> propertyValues = new DefaultMultiValueMap<>();
for (final Map.Entry<String, String[]> entry : parameters.entrySet()) {
final String[] paramValues = entry.getValue();
if (ObjectUtils.isNotEmpty(paramValues)) {
final String requestParameterName = entry.getKey();
// users[key].userName=TODAY&users[key].age=20
if (requestParameterName.startsWith(name) && requestParameterName.charAt(parameterNameLength) == '[') {
// userList[0].name '.' 's index
final int separatorIndex = PropertyAccessorUtils.getFirstNestedPropertySeparatorIndex(requestParameterName);
final String property = requestParameterName.substring(separatorIndex + 1);
final int closeKey = requestParameterName.indexOf(']');
final String key = requestParameterName.substring(parameterNameLength + 1, closeKey);
final PropertyValue propertyValue = new PropertyValue(property, paramValues[0]);
propertyValues.add(key, propertyValue);
}
}
}
return doBind(propertyValues, resolvable);
}
use of cn.taketoday.beans.PropertyValue in project today-infrastructure by TAKETODAY.
the class DataBinderParameterResolver method resolveAnnotatedProperty.
/**
* @since 4.0
*/
static void resolveAnnotatedProperty(RequestContext context, ResolvableMethodParameter parameter, RequestContextDataBinder dataBinder) throws Throwable {
Object attribute = parameter.getAttribute(ANNOTATED_RESOLVERS_KEY);
if (attribute instanceof List) {
PropertyValues propertyValues = new PropertyValues();
@SuppressWarnings("unchecked") List<AnnotatedPropertyResolver> resolvers = (List<AnnotatedPropertyResolver>) attribute;
for (final AnnotatedPropertyResolver resolver : resolvers) {
PropertyValue propertyValue = resolver.resolve(context);
propertyValues.add(propertyValue);
}
if (!propertyValues.isEmpty()) {
dataBinder.bind(propertyValues);
}
}
}
use of cn.taketoday.beans.PropertyValue in project today-infrastructure by TAKETODAY.
the class DataBinderFieldAccessTests method bindingWithErrors.
@Test
void bindingWithErrors() throws Exception {
FieldAccessBean rod = new FieldAccessBean();
DataBinder binder = new DataBinder(rod, "person");
binder.initDirectFieldAccess();
PropertyValues pvs = new PropertyValues();
pvs.add(new PropertyValue("name", "Rod"));
pvs.add(new PropertyValue("age", "32x"));
binder.bind(pvs);
assertThatExceptionOfType(BindException.class).isThrownBy(binder::close).satisfies(ex -> {
assertThat(rod.getName()).isEqualTo("Rod");
Map<?, ?> map = binder.getBindingResult().getModel();
FieldAccessBean tb = (FieldAccessBean) map.get("person");
assertThat(tb).isEqualTo(rod);
BindingResult br = (BindingResult) map.get(BindingResult.MODEL_KEY_PREFIX + "person");
assertThat(br).isSameAs(binder.getBindingResult());
assertThat(br.hasErrors()).isTrue();
assertThat(br.getErrorCount()).isEqualTo(1);
assertThat(br.hasFieldErrors()).isTrue();
assertThat(br.getFieldErrorCount("age")).isEqualTo(1);
assertThat(binder.getBindingResult().getFieldValue("age")).isEqualTo("32x");
assertThat(binder.getBindingResult().getFieldError("age").getRejectedValue()).isEqualTo("32x");
assertThat(tb.getAge()).isEqualTo(0);
});
}
use of cn.taketoday.beans.PropertyValue in project today-infrastructure by TAKETODAY.
the class DataBinderFieldAccessTests method bindingNoErrorsNotIgnoreUnknown.
@Test
void bindingNoErrorsNotIgnoreUnknown() throws Exception {
FieldAccessBean rod = new FieldAccessBean();
DataBinder binder = new DataBinder(rod, "person");
binder.initDirectFieldAccess();
binder.setIgnoreUnknownFields(false);
PropertyValues pvs = new PropertyValues();
pvs.add(new PropertyValue("name", "Rod"));
pvs.add(new PropertyValue("age", 32));
pvs.add(new PropertyValue("nonExisting", "someValue"));
assertThatExceptionOfType(NotWritablePropertyException.class).isThrownBy(() -> binder.bind(pvs));
}
Aggregations