Search in sources :

Example 56 with IndexedTestBean

use of org.springframework.beans.testfixture.beans.IndexedTestBean in project spring-framework by spring-projects.

the class BindTagTests method bindTagWithIndexedPropertiesAndCustomEditor.

@Test
void bindTagWithIndexedPropertiesAndCustomEditor() throws JspException {
    PageContext pc = createPageContext();
    IndexedTestBean tb = new IndexedTestBean();
    DataBinder binder = new ServletRequestDataBinder(tb, "tb");
    binder.registerCustomEditor(TestBean.class, null, new PropertyEditorSupport() {

        @Override
        public String getAsText() {
            return "something";
        }
    });
    Errors errors = binder.getBindingResult();
    errors.rejectValue("array[0]", "code1", "message1");
    errors.rejectValue("array[0]", "code2", "message2");
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
    BindTag tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb.array[0]");
    assertThat(tag.doStartTag() == Tag.EVAL_BODY_INCLUDE).as("Correct doStartTag return value").isTrue();
    BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertThat(status != null).as("Has status variable").isTrue();
    assertThat("array[0]".equals(status.getExpression())).as("Correct expression").isTrue();
    // because of the custom editor getValue() should return a String
    boolean condition = status.getValue() instanceof String;
    assertThat(condition).as("Value is TestBean").isTrue();
    assertThat("something".equals(status.getValue())).as("Correct value").isTrue();
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) Errors(org.springframework.validation.Errors) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) PageContext(jakarta.servlet.jsp.PageContext) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) DataBinder(org.springframework.validation.DataBinder) BindStatus(org.springframework.web.servlet.support.BindStatus) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.jupiter.api.Test)

Example 57 with IndexedTestBean

use of org.springframework.beans.testfixture.beans.IndexedTestBean in project spring-framework by spring-projects.

the class DataBinderTests method testBindingStringArrayToIntegerSet.

@Test
void testBindingStringArrayToIntegerSet() {
    IndexedTestBean tb = new IndexedTestBean();
    DataBinder binder = new DataBinder(tb, "tb");
    binder.registerCustomEditor(Set.class, new CustomCollectionEditor(TreeSet.class) {

        @Override
        protected Object convertElement(Object element) {
            return Integer.valueOf(element.toString());
        }
    });
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("set", new String[] { "10", "20", "30" });
    binder.bind(pvs);
    assertThat(binder.getBindingResult().getFieldValue("set")).isEqualTo(tb.getSet());
    boolean condition = tb.getSet() instanceof TreeSet;
    assertThat(condition).isTrue();
    assertThat(tb.getSet().size()).isEqualTo(3);
    assertThat(tb.getSet().contains(10)).isTrue();
    assertThat(tb.getSet().contains(20)).isTrue();
    assertThat(tb.getSet().contains(30)).isTrue();
    pvs = new MutablePropertyValues();
    pvs.add("set", null);
    binder.bind(pvs);
    assertThat(tb.getSet()).isNull();
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) CustomCollectionEditor(org.springframework.beans.propertyeditors.CustomCollectionEditor) TreeSet(java.util.TreeSet) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) Test(org.junit.jupiter.api.Test)

Example 58 with IndexedTestBean

use of org.springframework.beans.testfixture.beans.IndexedTestBean in project spring-framework by spring-projects.

the class DataBinderTests method testDirectBindingToIndexedField.

@Test
void testDirectBindingToIndexedField() {
    IndexedTestBean tb = new IndexedTestBean();
    DataBinder binder = new DataBinder(tb, "tb");
    binder.registerCustomEditor(TestBean.class, "array", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            DerivedTestBean tb = new DerivedTestBean();
            tb.setName("array" + text);
            setValue(tb);
        }

        @Override
        public String getAsText() {
            return ((TestBean) getValue()).getName();
        }
    });
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("array[0]", "a");
    binder.bind(pvs);
    Errors errors = binder.getBindingResult();
    errors.rejectValue("array[0]", "NOT_ROD", "are you sure you're not Rod?");
    errors.rejectValue("map[key1]", "NOT_ROD", "are you sure you're not Rod?");
    errors.rejectValue("map[key0]", "NOT_NULL", "should not be null");
    assertThat(errors.getFieldValue("array[0]")).isEqualTo("arraya");
    assertThat(errors.getFieldErrorCount("array[0]")).isEqualTo(1);
    assertThat(errors.getFieldError("array[0]").getCode()).isEqualTo("NOT_ROD");
    assertThat(errors.getFieldError("array[0]").getCodes()[0]).isEqualTo("NOT_ROD.tb.array[0]");
    assertThat(errors.getFieldError("array[0]").getCodes()[1]).isEqualTo("NOT_ROD.tb.array");
    assertThat(errors.getFieldError("array[0]").getCodes()[2]).isEqualTo("NOT_ROD.array[0]");
    assertThat(errors.getFieldError("array[0]").getCodes()[3]).isEqualTo("NOT_ROD.array");
    assertThat(errors.getFieldError("array[0]").getCodes()[4]).isEqualTo("NOT_ROD.org.springframework.beans.testfixture.beans.DerivedTestBean");
    assertThat(errors.getFieldError("array[0]").getCodes()[5]).isEqualTo("NOT_ROD");
    assertThat(errors.getFieldValue("array[0]")).isEqualTo("arraya");
    assertThat(errors.getFieldErrorCount("map[key1]")).isEqualTo(1);
    assertThat(errors.getFieldError("map[key1]").getCode()).isEqualTo("NOT_ROD");
    assertThat(errors.getFieldError("map[key1]").getCodes()[0]).isEqualTo("NOT_ROD.tb.map[key1]");
    assertThat(errors.getFieldError("map[key1]").getCodes()[1]).isEqualTo("NOT_ROD.tb.map");
    assertThat(errors.getFieldError("map[key1]").getCodes()[2]).isEqualTo("NOT_ROD.map[key1]");
    assertThat(errors.getFieldError("map[key1]").getCodes()[3]).isEqualTo("NOT_ROD.map");
    assertThat(errors.getFieldError("map[key1]").getCodes()[4]).isEqualTo("NOT_ROD.org.springframework.beans.testfixture.beans.TestBean");
    assertThat(errors.getFieldError("map[key1]").getCodes()[5]).isEqualTo("NOT_ROD");
    assertThat(errors.getFieldErrorCount("map[key0]")).isEqualTo(1);
    assertThat(errors.getFieldError("map[key0]").getCode()).isEqualTo("NOT_NULL");
    assertThat(errors.getFieldError("map[key0]").getCodes()[0]).isEqualTo("NOT_NULL.tb.map[key0]");
    assertThat(errors.getFieldError("map[key0]").getCodes()[1]).isEqualTo("NOT_NULL.tb.map");
    assertThat(errors.getFieldError("map[key0]").getCodes()[2]).isEqualTo("NOT_NULL.map[key0]");
    assertThat(errors.getFieldError("map[key0]").getCodes()[3]).isEqualTo("NOT_NULL.map");
    assertThat(errors.getFieldError("map[key0]").getCodes()[4]).isEqualTo("NOT_NULL");
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.jupiter.api.Test)

Example 59 with IndexedTestBean

use of org.springframework.beans.testfixture.beans.IndexedTestBean in project spring-framework by spring-projects.

the class DataBinderTests method testBindingWithResortedList.

@Test
@SuppressWarnings("unchecked")
void testBindingWithResortedList() {
    IndexedTestBean tb = new IndexedTestBean();
    DataBinder binder = new DataBinder(tb, "tb");
    MutablePropertyValues pvs = new MutablePropertyValues();
    TestBean tb1 = new TestBean("tb1", 99);
    TestBean tb2 = new TestBean("tb2", 99);
    pvs.add("list[0]", tb1);
    pvs.add("list[1]", tb2);
    binder.bind(pvs);
    assertThat(binder.getBindingResult().getFieldValue("list[0].name")).isEqualTo(tb1.getName());
    assertThat(binder.getBindingResult().getFieldValue("list[1].name")).isEqualTo(tb2.getName());
    tb.getList().set(0, tb2);
    tb.getList().set(1, tb1);
    assertThat(binder.getBindingResult().getFieldValue("list[0].name")).isEqualTo(tb2.getName());
    assertThat(binder.getBindingResult().getFieldValue("list[1].name")).isEqualTo(tb1.getName());
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) Test(org.junit.jupiter.api.Test)

Example 60 with IndexedTestBean

use of org.springframework.beans.testfixture.beans.IndexedTestBean in project spring-framework by spring-projects.

the class DataBinderTests method testCustomEditorWithSubclass.

@Test
void testCustomEditorWithSubclass() {
    IndexedTestBean tb = new IndexedTestBean();
    DataBinder binder = new DataBinder(tb, "tb");
    binder.registerCustomEditor(TestBean.class, new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            DerivedTestBean tb = new DerivedTestBean();
            tb.setName("array" + text);
            setValue(tb);
        }

        @Override
        public String getAsText() {
            return ((TestBean) getValue()).getName();
        }
    });
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("array[0]", "a");
    binder.bind(pvs);
    Errors errors = binder.getBindingResult();
    errors.rejectValue("array[0]", "NOT_ROD", "are you sure you're not Rod?");
    assertThat(errors.getFieldValue("array[0]")).isEqualTo("arraya");
    assertThat(errors.getFieldErrorCount("array[0]")).isEqualTo(1);
    assertThat(errors.getFieldError("array[0]").getCode()).isEqualTo("NOT_ROD");
    assertThat(errors.getFieldError("array[0]").getCodes()[0]).isEqualTo("NOT_ROD.tb.array[0]");
    assertThat(errors.getFieldError("array[0]").getCodes()[1]).isEqualTo("NOT_ROD.tb.array");
    assertThat(errors.getFieldError("array[0]").getCodes()[2]).isEqualTo("NOT_ROD.array[0]");
    assertThat(errors.getFieldError("array[0]").getCodes()[3]).isEqualTo("NOT_ROD.array");
    assertThat(errors.getFieldError("array[0]").getCodes()[4]).isEqualTo("NOT_ROD.org.springframework.beans.testfixture.beans.DerivedTestBean");
    assertThat(errors.getFieldError("array[0]").getCodes()[5]).isEqualTo("NOT_ROD");
    assertThat(errors.getFieldValue("array[0]")).isEqualTo("arraya");
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.jupiter.api.Test)

Aggregations

IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)62 Test (org.junit.jupiter.api.Test)61 TestBean (org.springframework.beans.testfixture.beans.TestBean)39 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)31 PropertyEditorSupport (java.beans.PropertyEditorSupport)22 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)17 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)16 BooleanTestBean (org.springframework.beans.testfixture.beans.BooleanTestBean)15 NumberTestBean (org.springframework.beans.testfixture.beans.NumberTestBean)15 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)12 DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)11 NestedTestBean (org.springframework.beans.testfixture.beans.NestedTestBean)11 BeanWrapper (org.springframework.beans.BeanWrapper)10 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)10 ArrayList (java.util.ArrayList)7 BeanDefinitionBuilder.genericBeanDefinition (org.springframework.beans.factory.support.BeanDefinitionBuilder.genericBeanDefinition)7 ChildBeanDefinition (org.springframework.beans.factory.support.ChildBeanDefinition)7 HashSet (java.util.HashSet)6 HashMap (java.util.HashMap)4 Properties (java.util.Properties)4