Search in sources :

Example 6 with PropertyValue

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);
}
Also used : PropertyValue(cn.taketoday.beans.PropertyValue) BeanDefinition(cn.taketoday.beans.factory.config.BeanDefinition)

Example 7 with PropertyValue

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);
}
Also used : DefaultMultiValueMap(cn.taketoday.core.DefaultMultiValueMap) PropertyValue(cn.taketoday.beans.PropertyValue) DefaultMultiValueMap(cn.taketoday.core.DefaultMultiValueMap) Map(java.util.Map) MultiValueMap(cn.taketoday.core.MultiValueMap) Nullable(cn.taketoday.lang.Nullable)

Example 8 with PropertyValue

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);
        }
    }
}
Also used : PropertyValues(cn.taketoday.beans.PropertyValues) PropertyValue(cn.taketoday.beans.PropertyValue) ArrayList(java.util.ArrayList) List(java.util.List)

Example 9 with PropertyValue

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);
    });
}
Also used : PropertyValues(cn.taketoday.beans.PropertyValues) PropertyValue(cn.taketoday.beans.PropertyValue) Test(org.junit.jupiter.api.Test)

Example 10 with PropertyValue

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));
}
Also used : PropertyValues(cn.taketoday.beans.PropertyValues) NotWritablePropertyException(cn.taketoday.beans.NotWritablePropertyException) PropertyValue(cn.taketoday.beans.PropertyValue) Test(org.junit.jupiter.api.Test)

Aggregations

PropertyValue (cn.taketoday.beans.PropertyValue)46 PropertyValues (cn.taketoday.beans.PropertyValues)28 Test (org.junit.jupiter.api.Test)22 ConfigurablePropertyAccessor (cn.taketoday.beans.ConfigurablePropertyAccessor)6 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)6 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)6 MultiValueMap (cn.taketoday.core.MultiValueMap)6 List (java.util.List)6 Map (java.util.Map)6 BeanMetadata (cn.taketoday.beans.BeanMetadata)4 BeanWrapper (cn.taketoday.beans.BeanWrapper)4 BeanWrapperImpl (cn.taketoday.beans.BeanWrapperImpl)4 BooleanTestBean (cn.taketoday.beans.BooleanTestBean)4 NumberTestBean (cn.taketoday.beans.NumberTestBean)4 BeanDefinition (cn.taketoday.beans.factory.config.BeanDefinition)4 IndexedTestBean (cn.taketoday.beans.testfixture.beans.IndexedTestBean)4 Nullable (cn.taketoday.lang.Nullable)4 RequestContextDataBinder (cn.taketoday.web.bind.RequestContextDataBinder)4 ArrayList (java.util.ArrayList)4 AnnotationAwareAspectJAutoProxyCreator (cn.taketoday.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator)2