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();
}
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();
}
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");
}
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());
}
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");
}
Aggregations