Search in sources :

Example 11 with PropertyValue

use of cn.taketoday.beans.PropertyValue in project today-infrastructure by TAKETODAY.

the class DataBinderFieldAccessTests method nestedBindingWithDefaultConversionNoErrors.

@Test
void nestedBindingWithDefaultConversionNoErrors() throws Exception {
    FieldAccessBean rod = new FieldAccessBean();
    DataBinder binder = new DataBinder(rod, "person");
    assertThat(binder.isIgnoreUnknownFields()).isTrue();
    binder.initDirectFieldAccess();
    PropertyValues pvs = new PropertyValues();
    pvs.add(new PropertyValue("spouse.name", "Kerry"));
    pvs.add(new PropertyValue("spouse.jedi", "on"));
    binder.bind(pvs);
    binder.close();
    assertThat(rod.getSpouse().getName()).isEqualTo("Kerry");
    assertThat((rod.getSpouse()).isJedi()).isTrue();
}
Also used : PropertyValues(cn.taketoday.beans.PropertyValues) PropertyValue(cn.taketoday.beans.PropertyValue) Test(org.junit.jupiter.api.Test)

Example 12 with PropertyValue

use of cn.taketoday.beans.PropertyValue in project today-framework by TAKETODAY.

the class ViewResolverTests method beanNameViewResolver.

@Test
public void beanNameViewResolver() {
    PropertyValues pvs1 = new PropertyValues();
    pvs1.add(new PropertyValue("url", "/example1.jsp"));
    this.wac.registerSingleton("example1", InternalResourceView.class, pvs1);
    PropertyValues pvs2 = new PropertyValues();
    pvs2.add(new PropertyValue("url", "/example2.jsp"));
    this.wac.registerSingleton("example2", JstlView.class, pvs2);
    BeanNameViewResolver vr = new BeanNameViewResolver();
    vr.setApplicationContext(this.wac);
    this.wac.refresh();
    View view = vr.resolveViewName("example1", Locale.getDefault());
    assertThat(view.getClass()).as("Correct view class").isEqualTo(InternalResourceView.class);
    assertThat(((InternalResourceView) view).getUrl()).as("Correct URL").isEqualTo("/example1.jsp");
    view = vr.resolveViewName("example2", Locale.getDefault());
    assertThat(view).isInstanceOf(JstlView.class);
    assertThat(((JstlView) view).getUrl()).as("Correct URL").isEqualTo("/example2.jsp");
}
Also used : PropertyValues(cn.taketoday.beans.PropertyValues) PropertyValue(cn.taketoday.beans.PropertyValue) JstlView(cn.taketoday.web.servlet.view.JstlView) InternalResourceView(cn.taketoday.web.servlet.view.InternalResourceView) Test(org.junit.jupiter.api.Test)

Example 13 with PropertyValue

use of cn.taketoday.beans.PropertyValue in project today-framework by TAKETODAY.

the class DataBinderFieldAccessTests method bindingNoErrors.

@Test
void bindingNoErrors() throws Exception {
    FieldAccessBean rod = new FieldAccessBean();
    DataBinder binder = new DataBinder(rod, "person");
    assertThat(binder.isIgnoreUnknownFields()).isTrue();
    binder.initDirectFieldAccess();
    PropertyValues pvs = new PropertyValues();
    pvs.add(new PropertyValue("name", "Rod"));
    pvs.add(new PropertyValue("age", 32));
    pvs.add(new PropertyValue("nonExisting", "someValue"));
    binder.bind(pvs);
    binder.close();
    assertThat(rod.getName().equals("Rod")).as("changed name correctly").isTrue();
    assertThat(rod.getAge() == 32).as("changed age correctly").isTrue();
    Map<?, ?> m = binder.getBindingResult().getModel();
    assertThat(m.size() == 2).as("There is one element in map").isTrue();
    FieldAccessBean tb = (FieldAccessBean) m.get("person");
    assertThat(tb.equals(rod)).as("Same object").isTrue();
}
Also used : PropertyValues(cn.taketoday.beans.PropertyValues) PropertyValue(cn.taketoday.beans.PropertyValue) Test(org.junit.jupiter.api.Test)

Example 14 with PropertyValue

use of cn.taketoday.beans.PropertyValue in project today-framework by TAKETODAY.

the class DataBinderFieldAccessTests method nestedBindingWithDefaultConversionNoErrors.

@Test
void nestedBindingWithDefaultConversionNoErrors() throws Exception {
    FieldAccessBean rod = new FieldAccessBean();
    DataBinder binder = new DataBinder(rod, "person");
    assertThat(binder.isIgnoreUnknownFields()).isTrue();
    binder.initDirectFieldAccess();
    PropertyValues pvs = new PropertyValues();
    pvs.add(new PropertyValue("spouse.name", "Kerry"));
    pvs.add(new PropertyValue("spouse.jedi", "on"));
    binder.bind(pvs);
    binder.close();
    assertThat(rod.getSpouse().getName()).isEqualTo("Kerry");
    assertThat((rod.getSpouse()).isJedi()).isTrue();
}
Also used : PropertyValues(cn.taketoday.beans.PropertyValues) PropertyValue(cn.taketoday.beans.PropertyValue) Test(org.junit.jupiter.api.Test)

Example 15 with PropertyValue

use of cn.taketoday.beans.PropertyValue in project today-framework by TAKETODAY.

the class DataBinderFieldAccessTests method bindingWithErrorsAndCustomEditors.

@Test
void bindingWithErrorsAndCustomEditors() throws Exception {
    FieldAccessBean rod = new FieldAccessBean();
    DataBinder binder = new DataBinder(rod, "person");
    binder.initDirectFieldAccess();
    binder.registerCustomEditor(TestBean.class, "spouse", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue(new TestBean(text, 0));
        }

        @Override
        public String getAsText() {
            return ((TestBean) getValue()).getName();
        }
    });
    PropertyValues pvs = new PropertyValues();
    pvs.add(new PropertyValue("name", "Rod"));
    pvs.add(new PropertyValue("age", "32x"));
    pvs.add(new PropertyValue("spouse", "Kerry"));
    binder.bind(pvs);
    assertThatExceptionOfType(BindException.class).isThrownBy(binder::close).satisfies(ex -> {
        assertThat(rod.getName()).isEqualTo("Rod");
        Map<?, ?> model = binder.getBindingResult().getModel();
        FieldAccessBean tb = (FieldAccessBean) model.get("person");
        assertThat(tb).isEqualTo(rod);
        BindingResult br = (BindingResult) model.get(BindingResult.MODEL_KEY_PREFIX + "person");
        assertThat(br).isSameAs(binder.getBindingResult());
        assertThat(br.hasErrors()).isTrue();
        assertThat(br.getErrorCount()).isEqualTo(1);
        assertThat(br.hasFieldErrors("age")).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);
        assertThat(br.hasFieldErrors("spouse")).isFalse();
        assertThat(binder.getBindingResult().getFieldValue("spouse")).isEqualTo("Kerry");
        assertThat(tb.getSpouse()).isNotNull();
    });
}
Also used : PropertyValues(cn.taketoday.beans.PropertyValues) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) PropertyValue(cn.taketoday.beans.PropertyValue) PropertyEditorSupport(java.beans.PropertyEditorSupport) 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