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