use of cn.taketoday.validation.BindingResult in project today-infrastructure by TAKETODAY.
the class ErrorResponseExceptionTests method methodArgumentNotValidException.
@Test
void methodArgumentNotValidException() {
BindingResult bindingResult = new BindException(new Object(), "object");
bindingResult.addError(new FieldError("object", "field", "message"));
ErrorResponse ex = new MethodArgumentNotValidException(this.methodParameter, bindingResult);
assertStatus(ex, HttpStatus.BAD_REQUEST);
assertDetail(ex, "Invalid request content.");
assertThat(ex.getHeaders()).isEmpty();
}
use of cn.taketoday.validation.BindingResult in project today-infrastructure by TAKETODAY.
the class MethodArgumentNotValidException method getMessage.
@Override
public String getMessage() {
StringBuilder sb = new StringBuilder("Validation failed for argument [").append(this.parameter.getParameterIndex()).append("] in ").append(this.parameter.getExecutable().toGenericString());
BindingResult bindingResult = getBindingResult();
if (bindingResult.getErrorCount() > 1) {
sb.append(" with ").append(bindingResult.getErrorCount()).append(" errors");
}
sb.append(": ");
for (ObjectError error : bindingResult.getAllErrors()) {
sb.append('[').append(error).append("] ");
}
return sb.toString();
}
use of cn.taketoday.validation.BindingResult in project today-framework by TAKETODAY.
the class DataBinderTests method testBindingWithErrorsAndCustomEditors.
@Test
void testBindingWithErrorsAndCustomEditors() {
TestBean rod = new TestBean();
DataBinder binder = new DataBinder(rod, "person");
binder.registerCustomEditor(String.class, "touchy", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue("prefix_" + text);
}
@Override
public String getAsText() {
return getValue().toString().substring(7);
}
});
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("name", "Rod");
pvs.add("age", "32x");
pvs.add("touchy", "m.y");
pvs.add("spouse", "Kerry");
binder.bind(pvs);
assertThatExceptionOfType(BindException.class).isThrownBy(binder::close).satisfies(ex -> {
assertThat(rod.getName()).isEqualTo("Rod");
Map<?, ?> model = binder.getBindingResult().getModel();
TestBean tb = (TestBean) model.get("person");
assertThat(tb).isEqualTo(rod);
BindingResult br = (BindingResult) model.get(BindingResult.MODEL_KEY_PREFIX + "person");
assertThat(binder.getBindingResult()).isSameAs(br);
assertThat(br.hasErrors()).isTrue();
assertThat(br.getErrorCount()).isEqualTo(2);
assertThat(br.hasFieldErrors("age")).isTrue();
assertThat(br.getFieldErrorCount("age")).isEqualTo(1);
assertThat(binder.getBindingResult().getFieldValue("age")).isEqualTo("32x");
FieldError ageError = binder.getBindingResult().getFieldError("age");
assertThat(ageError).isNotNull();
assertThat(ageError.getCode()).isEqualTo("typeMismatch");
assertThat(ageError.getRejectedValue()).isEqualTo("32x");
assertThat(tb.getAge()).isEqualTo(0);
assertThat(br.hasFieldErrors("touchy")).isTrue();
assertThat(br.getFieldErrorCount("touchy")).isEqualTo(1);
assertThat(binder.getBindingResult().getFieldValue("touchy")).isEqualTo("m.y");
FieldError touchyError = binder.getBindingResult().getFieldError("touchy");
assertThat(touchyError).isNotNull();
assertThat(touchyError.getCode()).isEqualTo("methodInvocation");
assertThat(touchyError.getRejectedValue()).isEqualTo("m.y");
assertThat(tb.getTouchy()).isNull();
assertThat(br.hasFieldErrors("spouse")).isFalse();
assertThat(binder.getBindingResult().getFieldValue("spouse")).isEqualTo("Kerry");
assertThat(tb.getSpouse()).isNotNull();
});
}
use of cn.taketoday.validation.BindingResult in project today-framework by TAKETODAY.
the class DataBinderTests method testBindingWithRequiredMapFields.
@Test
void testBindingWithRequiredMapFields() {
TestBean tb = new TestBean();
tb.setSpouse(new TestBean());
DataBinder binder = new DataBinder(tb, "person");
binder.setRequiredFields("someMap[key1]", "someMap[key2]", "someMap['key3']", "someMap[key4]");
PropertyValues pvs = new PropertyValues();
pvs.add("someMap[key1]", "value1");
pvs.add("someMap['key2']", "value2");
pvs.add("someMap[key3]", "value3");
binder.bind(pvs);
BindingResult br = binder.getBindingResult();
assertThat(br.getErrorCount()).as("Wrong number of errors").isEqualTo(1);
assertThat(br.getFieldError("someMap[key4]").getCode()).isEqualTo("required");
}
use of cn.taketoday.validation.BindingResult in project today-framework by TAKETODAY.
the class DataBinderTests method testBindingNoErrors.
@Test
void testBindingNoErrors() throws BindException {
TestBean rod = new TestBean();
DataBinder binder = new DataBinder(rod, "person");
assertThat(binder.isIgnoreUnknownFields()).isTrue();
PropertyValues pvs = new PropertyValues();
pvs.add("name", "Rod");
pvs.add("age", "032");
pvs.add("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<?, ?> map = binder.getBindingResult().getModel();
assertThat(map.size() == 2).as("There is one element in map").isTrue();
TestBean tb = (TestBean) map.get("person");
assertThat(tb.equals(rod)).as("Same object").isTrue();
BindingResult other = new BeanPropertyBindingResult(rod, "person");
assertThat(binder.getBindingResult()).isEqualTo(other);
assertThat(other).isEqualTo(binder.getBindingResult());
BindException ex = new BindException(other);
assertThat(other).isEqualTo(ex);
assertThat(ex).isEqualTo(other);
assertThat(binder.getBindingResult()).isEqualTo(ex);
assertThat(ex).isEqualTo(binder.getBindingResult());
other.reject("xxx");
boolean condition = !other.equals(binder.getBindingResult());
assertThat(condition).isTrue();
}
Aggregations