Here are the examples of the java api org.springframework.validation.BeanPropertyBindingResult taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
139 Examples
19
View Source File : FinanceValidationUtil.java
License : MIT License
Project Creator : InnovateUKGitHub
License : MIT License
Project Creator : InnovateUKGitHub
@Transactional(readOnly = true)
public ValidationMessages validateCosreplacedem(FinanceRowItem cosreplacedem) {
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(cosreplacedem, "cosreplacedem");
invokeValidator(cosreplacedem, bindingResult);
return buildValidationMessages(cosreplacedem, bindingResult);
}
19
View Source File : FinanceValidationUtil.java
License : MIT License
Project Creator : InnovateUKGitHub
License : MIT License
Project Creator : InnovateUKGitHub
@Transactional(readOnly = true)
public ValidationMessages validateProjectCosreplacedem(FinanceRowItem cosreplacedem) {
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(cosreplacedem, "cosreplacedem");
invokeProjectCostValidator(cosreplacedem, bindingResult);
return buildValidationMessages(cosreplacedem, bindingResult);
}
18
View Source File : TextareaTagTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void customBind() throws Exception {
BeanPropertyBindingResult result = new BeanPropertyBindingResult(createTestBean(), "testBean");
result.getPropertyAccessor().registerCustomEditor(Float.clreplaced, new SimpleFloatEditor());
exposeBindingResult(result);
this.tag.setPath("myFloat");
replacedertEquals(Tag.SKIP_BODY, this.tag.doStartTag());
String output = getOutput();
replacedertContainsAttribute(output, "name", "myFloat");
replacedertBlockTagContains(output, "12.34f");
}
18
View Source File : SelectTagTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void nestedPathWithListAndEditorAndNullValue() throws Exception {
this.tag.setPath("bean.realCountry");
this.tag.sereplacedems(Country.getCountries());
this.tag.sereplacedemValue("isoCode");
this.tag.sereplacedemLabel("name");
this.tag.setMultiple("false");
TestBeanWrapper testBean = new TestBeanWrapper();
TestBeanWithRealCountry withCountry = (TestBeanWithRealCountry) getTestBean();
withCountry.setRealCountry(null);
testBean.setBean(withCountry);
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(testBean, "testBean");
bindingResult.getPropertyAccessor().registerCustomEditor(Country.clreplaced, new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
if (text == null || text.length() == 0) {
setValue(null);
return;
}
setValue(Country.getCountryWithIsoCode(text));
}
@Override
public String getAsText() {
Country value = (Country) getValue();
if (value == null) {
return null;
}
return value.getName();
}
});
getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "testBean", bindingResult);
this.tag.doStartTag();
String output = getOutput();
replacedertTrue(output.startsWith("<select "));
replacedertTrue(output.endsWith("</select>"));
replacedertFalse(output.contains("selected=\"selected\""));
replacedertFalse(output.contains("multiple=\"multiple\""));
}
18
View Source File : SelectTagTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void withListAndEditorAndNullValue() throws Exception {
this.tag.setPath("realCountry");
this.tag.sereplacedems(Country.getCountries());
this.tag.sereplacedemValue("isoCode");
this.tag.sereplacedemLabel("name");
TestBeanWithRealCountry testBean = (TestBeanWithRealCountry) getTestBean();
testBean.setRealCountry(null);
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(testBean, "testBean");
bindingResult.getPropertyAccessor().registerCustomEditor(Country.clreplaced, new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(Country.getCountryWithIsoCode(text));
}
@Override
public String getAsText() {
Country value = (Country) getValue();
if (value == null) {
return "";
}
return value.getName();
}
});
getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "testBean", bindingResult);
this.tag.doStartTag();
String output = getOutput();
replacedertTrue(output.startsWith("<select "));
replacedertTrue(output.endsWith("</select>"));
replacedertFalse(output.contains("selected=\"selected\""));
}
18
View Source File : SelectTagTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void withListAndEditor() throws Exception {
this.tag.setPath("realCountry");
this.tag.sereplacedems(Country.getCountries());
this.tag.sereplacedemValue("isoCode");
this.tag.sereplacedemLabel("name");
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(getTestBean(), "testBean");
bindingResult.getPropertyAccessor().registerCustomEditor(Country.clreplaced, new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(Country.getCountryWithIsoCode(text));
}
@Override
public String getAsText() {
return ((Country) getValue()).getName();
}
});
getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "testBean", bindingResult);
this.tag.doStartTag();
String output = getOutput();
replacedertTrue(output.startsWith("<select "));
replacedertTrue(output.endsWith("</select>"));
replacedertTrue(output.contains("option value=\"AT\" selected=\"selected\">Austria"));
}
18
View Source File : SelectTagTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void nestedPathWithListAndEditor() throws Exception {
this.tag.setPath("bean.realCountry");
this.tag.sereplacedems(Country.getCountries());
this.tag.sereplacedemValue("isoCode");
this.tag.sereplacedemLabel("name");
TestBeanWrapper testBean = new TestBeanWrapper();
testBean.setBean(getTestBean());
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(testBean, "testBean");
bindingResult.getPropertyAccessor().registerCustomEditor(Country.clreplaced, new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(Country.getCountryWithIsoCode(text));
}
@Override
public String getAsText() {
return ((Country) getValue()).getName();
}
});
getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "testBean", bindingResult);
this.tag.doStartTag();
String output = getOutput();
replacedertTrue(output.startsWith("<select "));
replacedertTrue(output.endsWith("</select>"));
replacedertTrue(output.contains("option value=\"AT\" selected=\"selected\">Austria"));
}
18
View Source File : HiddenInputTagTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void withCustomBinder() throws Exception {
this.tag.setPath("myFloat");
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(this.bean, COMMAND_NAME);
errors.getPropertyAccessor().registerCustomEditor(Float.clreplaced, new SimpleFloatEditor());
exposeBindingResult(errors);
replacedertEquals(Tag.SKIP_BODY, this.tag.doStartTag());
String output = getOutput();
replacedertTagOpened(output);
replacedertTagClosed(output);
replacedertContainsAttribute(output, "type", "hidden");
replacedertContainsAttribute(output, "value", "12.34f");
}
18
View Source File : TextareaTagTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void customBind() throws Exception {
BeanPropertyBindingResult result = new BeanPropertyBindingResult(createTestBean(), "testBean");
result.getPropertyAccessor().registerCustomEditor(Float.clreplaced, new SimpleFloatEditor());
exposeBindingResult(result);
this.tag.setPath("myFloat");
replacedertThat(this.tag.doStartTag()).isEqualTo(Tag.SKIP_BODY);
String output = getOutput();
replacedertContainsAttribute(output, "name", "myFloat");
replacedertBlockTagContains(output, "12.34f");
}
18
View Source File : HiddenInputTagTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void withCustomBinder() throws Exception {
this.tag.setPath("myFloat");
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(this.bean, COMMAND_NAME);
errors.getPropertyAccessor().registerCustomEditor(Float.clreplaced, new SimpleFloatEditor());
exposeBindingResult(errors);
replacedertThat(this.tag.doStartTag()).isEqualTo(Tag.SKIP_BODY);
String output = getOutput();
replacedertTagOpened(output);
replacedertTagClosed(output);
replacedertContainsAttribute(output, "type", "hidden");
replacedertContainsAttribute(output, "value", "12.34f");
}
18
View Source File : SpringValidatorAdapterTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
// SPR-16177
@Test
public void testWithList() {
Parent parent = new Parent();
parent.setName("Parent whit list");
parent.getChildList().addAll(createChildren(parent));
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(parent, "parent");
validatorAdapter.validate(parent, errors);
replacedertThat(errors.getErrorCount() > 0).isTrue();
}
18
View Source File : SpringValidatorAdapterTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
// SPR-16177
@Test
public void testWithSet() {
Parent parent = new Parent();
parent.setName("Parent with set");
parent.getChildSet().addAll(createChildren(parent));
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(parent, "parent");
validatorAdapter.validate(parent, errors);
replacedertThat(errors.getErrorCount() > 0).isTrue();
}
18
View Source File : ValidationHandlerTest.java
License : MIT License
Project Creator : InnovateUKGitHub
License : MIT License
Project Creator : InnovateUKGitHub
@Test
public void testNewValidationHandler() {
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(new TestForm(), "targetName");
ValidationHandler validationHandler = ValidationHandler.newBindingResultHandler(bindingResult);
replacedertFalse(validationHandler.hasErrors());
replacedertTrue(validationHandler.getAllErrors().isEmpty());
}
18
View Source File : ValidationHandlerTest.java
License : MIT License
Project Creator : InnovateUKGitHub
License : MIT License
Project Creator : InnovateUKGitHub
@Test
public void testSucceedOnNoErrors() {
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(new TestForm(), "targetName");
ValidationHandler validationHandler = ValidationHandler.newBindingResultHandler(bindingResult);
String result = validationHandler.failNowOrSucceedWith(() -> "failure", () -> "success");
replacedertEquals("success", result);
}
18
View Source File : FinanceValidationUtil.java
License : MIT License
Project Creator : InnovateUKGitHub
License : MIT License
Project Creator : InnovateUKGitHub
private void invokeProjectCostValidator(FinanceRowItem cosreplacedem, BeanPropertyBindingResult bindingResult) {
FinanceRowHandler financeRowHandler = applicationValidatorService.getProjectCostHandler(cosreplacedem);
financeRowHandler.validate(cosreplacedem, bindingResult);
}
18
View Source File : FinanceValidationUtil.java
License : MIT License
Project Creator : InnovateUKGitHub
License : MIT License
Project Creator : InnovateUKGitHub
private void invokeValidator(FinanceRowItem cosreplacedem, BeanPropertyBindingResult bindingResult) {
FinanceRowHandler financeRowHandler = applicationValidatorService.getCostHandler(cosreplacedem);
financeRowHandler.validate(cosreplacedem, bindingResult);
}
18
View Source File : FinanceValidationUtil.java
License : MIT License
Project Creator : InnovateUKGitHub
License : MIT License
Project Creator : InnovateUKGitHub
private ValidationMessages buildValidationMessages(FinanceRowItem cosreplacedem, BeanPropertyBindingResult bindingResult) {
if (bindingResult.hasErrors()) {
if (LOG.isDebugEnabled()) {
LOG.debug("validated, with messages: ");
bindingResult.getFieldErrors().stream().forEach(e -> LOG.debug("Field Error: " + e.getRejectedValue() + e.getDefaultMessage()));
bindingResult.getAllErrors().stream().forEach(e -> LOG.debug("Error: " + e.getObjectName() + e.getDefaultMessage()));
}
return new ValidationMessages(cosreplacedem.getId(), bindingResult);
} else {
return ValidationMessages.noErrors(cosreplacedem.getId());
}
}
18
View Source File : ValidationUtil.java
License : MIT License
Project Creator : FAIRDataTeam
License : MIT License
Project Creator : FAIRDataTeam
public static <T> void validationFailed(String field, String code, String defaultMessage, T reqDto) throws BindException {
BeanPropertyBindingResult error = new BeanPropertyBindingResult(reqDto, reqDto.getClreplaced().getName());
error.rejectValue(field, code, defaultMessage);
throw new BindException(error);
}
18
View Source File : ValidationUtil.java
License : MIT License
Project Creator : FAIRDataTeam
License : MIT License
Project Creator : FAIRDataTeam
public static <T> void uniquenessValidationFailed(String field, T reqDto) throws BindException {
BeanPropertyBindingResult error = new BeanPropertyBindingResult(reqDto, reqDto.getClreplaced().getName());
error.rejectValue(field, "Uniqueness", "must be unique");
throw new BindException(error);
}
18
View Source File : TransmissionRiskLevelEncoding.java
License : Apache License 2.0
Project Creator : covid-be-app
License : Apache License 2.0
Project Creator : covid-be-app
private void internalValidate() {
BeanPropertyBindingResult errorsContainer = new BeanPropertyBindingResult(this, "transmissionRiskEncoding");
this.validate(this, errorsContainer);
if (errorsContainer.hasErrors()) {
throw new IllegalArgumentException("Errors while loading transmission risk level encoding configuration. Reason: " + StringUtils.join(errorsContainer.getAllErrors().stream().map(ObjectError::getDefaultMessage).collect(Collectors.toList()), ","));
}
}
17
View Source File : PayloadMethodArgumentResolver.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Nullable
private Consumer<Object> getValidator(Message<?> message, MethodParameter parameter) {
if (this.validator == null) {
return null;
}
for (Annotation ann : parameter.getParameterAnnotations()) {
Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.clreplaced);
if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) {
Object hints = (validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann));
Object[] validationHints = (hints instanceof Object[] ? (Object[]) hints : new Object[] { hints });
String name = Conventions.getVariableNameForParameter(parameter);
return target -> {
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(target, name);
if (!ObjectUtils.isEmpty(validationHints) && this.validator instanceof SmartValidator) {
((SmartValidator) this.validator).validate(target, bindingResult, validationHints);
} else {
this.validator.validate(target, bindingResult);
}
if (bindingResult.hasErrors()) {
throw new MethodArgumentNotValidException(message, parameter, bindingResult);
}
};
}
}
return null;
}
17
View Source File : ValidatorFactoryTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void testSpringValidationFieldType() {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.afterPropertiesSet();
ValidPerson person = new ValidPerson();
person.setName("Phil");
person.getAddress().setStreet("Phil's Street");
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(person, "person");
validator.validate(person, errors);
replacedertEquals(1, errors.getErrorCount());
replacedertThat("Field/Value type mismatch", errors.getFieldError("address").getRejectedValue(), instanceOf(ValidAddress.clreplaced));
}
17
View Source File : SpringValidatorAdapterTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
// SPR-16177
@Test
public void testWithList() {
Parent parent = new Parent();
parent.setName("Parent whit list");
parent.getChildList().addAll(createChildren(parent));
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(parent, "parent");
validatorAdapter.validate(parent, errors);
replacedertTrue(errors.getErrorCount() > 0);
}
17
View Source File : SpringValidatorAdapterTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
// SPR-16177
@Test
public void testWithSet() {
Parent parent = new Parent();
parent.setName("Parent with set");
parent.getChildSet().addAll(createChildren(parent));
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(parent, "parent");
validatorAdapter.validate(parent, errors);
replacedertTrue(errors.getErrorCount() > 0);
}
17
View Source File : SelectTagTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void withListAndEditor() throws Exception {
this.tag.setPath("realCountry");
this.tag.sereplacedems(Country.getCountries());
this.tag.sereplacedemValue("isoCode");
this.tag.sereplacedemLabel("name");
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(getTestBean(), "testBean");
bindingResult.getPropertyAccessor().registerCustomEditor(Country.clreplaced, new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(Country.getCountryWithIsoCode(text));
}
@Override
public String getAsText() {
return ((Country) getValue()).getName();
}
});
getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "testBean", bindingResult);
this.tag.doStartTag();
String output = getOutput();
replacedertThat(output.startsWith("<select ")).isTrue();
replacedertThat(output.endsWith("</select>")).isTrue();
replacedertThat(output.contains("option value=\"AT\" selected=\"selected\">Austria")).isTrue();
}
17
View Source File : SelectTagTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void nestedPathWithListAndEditor() throws Exception {
this.tag.setPath("bean.realCountry");
this.tag.sereplacedems(Country.getCountries());
this.tag.sereplacedemValue("isoCode");
this.tag.sereplacedemLabel("name");
TestBeanWrapper testBean = new TestBeanWrapper();
testBean.setBean(getTestBean());
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(testBean, "testBean");
bindingResult.getPropertyAccessor().registerCustomEditor(Country.clreplaced, new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(Country.getCountryWithIsoCode(text));
}
@Override
public String getAsText() {
return ((Country) getValue()).getName();
}
});
getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "testBean", bindingResult);
this.tag.doStartTag();
String output = getOutput();
replacedertThat(output.startsWith("<select ")).isTrue();
replacedertThat(output.endsWith("</select>")).isTrue();
replacedertThat(output.contains("option value=\"AT\" selected=\"selected\">Austria")).isTrue();
}
17
View Source File : SelectTagTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void nestedPathWithListAndEditorAndNullValue() throws Exception {
this.tag.setPath("bean.realCountry");
this.tag.sereplacedems(Country.getCountries());
this.tag.sereplacedemValue("isoCode");
this.tag.sereplacedemLabel("name");
this.tag.setMultiple("false");
TestBeanWrapper testBean = new TestBeanWrapper();
TestBeanWithRealCountry withCountry = (TestBeanWithRealCountry) getTestBean();
withCountry.setRealCountry(null);
testBean.setBean(withCountry);
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(testBean, "testBean");
bindingResult.getPropertyAccessor().registerCustomEditor(Country.clreplaced, new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
if (text == null || text.length() == 0) {
setValue(null);
return;
}
setValue(Country.getCountryWithIsoCode(text));
}
@Override
public String getAsText() {
Country value = (Country) getValue();
if (value == null) {
return null;
}
return value.getName();
}
});
getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "testBean", bindingResult);
this.tag.doStartTag();
String output = getOutput();
replacedertThat(output.startsWith("<select ")).isTrue();
replacedertThat(output.endsWith("</select>")).isTrue();
replacedertThat(output.contains("selected=\"selected\"")).isFalse();
replacedertThat(output.contains("multiple=\"multiple\"")).isFalse();
}
17
View Source File : SelectTagTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void withListAndEditorAndNullValue() throws Exception {
this.tag.setPath("realCountry");
this.tag.sereplacedems(Country.getCountries());
this.tag.sereplacedemValue("isoCode");
this.tag.sereplacedemLabel("name");
TestBeanWithRealCountry testBean = (TestBeanWithRealCountry) getTestBean();
testBean.setRealCountry(null);
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(testBean, "testBean");
bindingResult.getPropertyAccessor().registerCustomEditor(Country.clreplaced, new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(Country.getCountryWithIsoCode(text));
}
@Override
public String getAsText() {
Country value = (Country) getValue();
if (value == null) {
return "";
}
return value.getName();
}
});
getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "testBean", bindingResult);
this.tag.doStartTag();
String output = getOutput();
replacedertThat(output.startsWith("<select ")).isTrue();
replacedertThat(output.endsWith("</select>")).isTrue();
replacedertThat(output.contains("selected=\"selected\"")).isFalse();
}
17
View Source File : SpringValidatorAdapterTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
// SPR-15839
@Test
public void testMapValueConstraint() {
Map<String, String> property = new HashMap<>();
property.put("no value can be", null);
BeanWithMapEntryConstraint bean = new BeanWithMapEntryConstraint();
bean.setProperty(property);
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(bean, "bean");
validatorAdapter.validate(bean, errors);
replacedertThat(errors.getFieldErrorCount("property[no value can be]")).isEqualTo(1);
replacedertThat(errors.getFieldValue("property[no value can be]")).isNull();
}
17
View Source File : SpringValidatorAdapterTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
// SPR-15839
@Test
public void testListElementConstraint() {
BeanWithListElementConstraint bean = new BeanWithListElementConstraint();
bean.setProperty(Arrays.asList("no", "element", "can", "be", null));
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(bean, "bean");
validatorAdapter.validate(bean, errors);
replacedertThat(errors.getFieldErrorCount("property[4]")).isEqualTo(1);
replacedertThat(errors.getFieldValue("property[4]")).isNull();
}
17
View Source File : SpringValidatorAdapterTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
// SPR-13406
@Test
public void testApplyMessageSourceResolvableToStringArgumentValueWithUnresolvedLogicalFieldName() {
TestBean testBean = new TestBean();
testBean.setEmail("[email protected]");
testBean.setConfirmEmail("[email protected]");
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(testBean, "testBean");
validatorAdapter.validate(testBean, errors);
replacedertThat(errors.getFieldErrorCount("email")).isEqualTo(1);
replacedertThat(errors.getFieldValue("email")).isEqualTo("[email protected]");
replacedertThat(errors.getFieldErrorCount("confirmEmail")).isEqualTo(1);
FieldError error1 = errors.getFieldError("email");
FieldError error2 = errors.getFieldError("confirmEmail");
replacedertThat(error1).isNotNull();
replacedertThat(error2).isNotNull();
replacedertThat(messageSource.getMessage(error1, Locale.ENGLISH)).isEqualTo("email must be same value as confirmEmail");
replacedertThat(messageSource.getMessage(error2, Locale.ENGLISH)).isEqualTo("Email required");
replacedertThat(error1.contains(ConstraintViolation.clreplaced)).isTrue();
replacedertThat(error1.unwrap(ConstraintViolation.clreplaced).getPropertyPath().toString()).isEqualTo("email");
replacedertThat(error2.contains(ConstraintViolation.clreplaced)).isTrue();
replacedertThat(error2.unwrap(ConstraintViolation.clreplaced).getPropertyPath().toString()).isEqualTo("confirmEmail");
}
17
View Source File : ValidatorFactoryTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void testSpringValidationFieldType() {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.afterPropertiesSet();
ValidPerson person = new ValidPerson();
person.setName("Phil");
person.getAddress().setStreet("Phil's Street");
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(person, "person");
validator.validate(person, errors);
replacedertThat(errors.getErrorCount()).isEqualTo(1);
replacedertThat(errors.getFieldError("address").getRejectedValue()).as("Field/Value type mismatch").isInstanceOf(ValidAddress.clreplaced);
}
17
View Source File : ValidatorFactoryTests.java
License : MIT License
Project Creator : mindcarver
License : MIT License
Project Creator : mindcarver
@Test
public void testListValidation() {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.afterPropertiesSet();
ListContainer listContainer = new ListContainer();
listContainer.addString("A");
listContainer.addString("X");
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(listContainer, "listContainer");
errors.initConversion(new DefaultConversionService());
validator.validate(listContainer, errors);
FieldError fieldError = errors.getFieldError("list[1]");
replacedertEquals("X", errors.getFieldValue("list[1]"));
}
17
View Source File : ValidatorFactoryTests.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
@Test
public void testSpringValidationFieldType() throws Exception {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.afterPropertiesSet();
ValidPerson person = new ValidPerson();
person.setName("Phil");
person.getAddress().setStreet("Phil's Street");
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(person, "person");
validator.validate(person, errors);
replacedertEquals(1, errors.getErrorCount());
replacedertThat("Field/Value type mismatch", errors.getFieldError("address").getRejectedValue(), instanceOf(ValidAddress.clreplaced));
}
17
View Source File : ValidationHandlerTest.java
License : MIT License
Project Creator : InnovateUKGitHub
License : MIT License
Project Creator : InnovateUKGitHub
@Test
public void testSucceedWhenOnlySkippedFieldErrorPresent() {
String fieldToSkip = "formField2";
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(new TestForm(), "targetName");
bindingResult.rejectValue(fieldToSkip, "Code2", "A validation error message that should not cause failure");
ValidationHandler validationHandler = ValidationHandler.newBindingResultHandler(bindingResult);
String result = validationHandler.failNowOrSucceedWithFilter(e -> !e.getField().contains(fieldToSkip), () -> "failure", () -> "success");
replacedertEquals("success", result);
}
16
View Source File : RadioButtonTagTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void withCheckedObjectValueAndEditor() throws Exception {
this.tag.setPath("myFloat");
this.tag.setValue("F12.99");
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(this.bean, COMMAND_NAME);
MyFloatEditor editor = new MyFloatEditor();
bindingResult.getPropertyEditorRegistry().registerCustomEditor(Float.clreplaced, editor);
getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + COMMAND_NAME, bindingResult);
int result = this.tag.doStartTag();
replacedertEquals(Tag.SKIP_BODY, result);
String output = getOutput();
replacedertTagOpened(output);
replacedertTagClosed(output);
replacedertContainsAttribute(output, "name", "myFloat");
replacedertContainsAttribute(output, "type", "radio");
replacedertContainsAttribute(output, "value", "F" + getFloat().toString());
replacedertContainsAttribute(output, "checked", "checked");
}
16
View Source File : InputTagTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void withErrors() throws Exception {
this.tag.setPath("name");
this.tag.setCssClreplaced("good");
this.tag.setCssErrorClreplaced("bad");
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(this.rob, COMMAND_NAME);
errors.rejectValue("name", "some.code", "Default Message");
errors.rejectValue("name", "too.short", "Too Short");
exposeBindingResult(errors);
replacedertEquals(Tag.SKIP_BODY, this.tag.doStartTag());
String output = getOutput();
replacedertTagOpened(output);
replacedertTagClosed(output);
replacedertContainsAttribute(output, "type", getType());
replacedertValueAttribute(output, "Rob");
replacedertContainsAttribute(output, "clreplaced", "bad");
}
16
View Source File : InputTagTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void withCustomBinder() throws Exception {
this.tag.setPath("myFloat");
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(this.rob, COMMAND_NAME);
errors.getPropertyAccessor().registerCustomEditor(Float.clreplaced, new SimpleFloatEditor());
exposeBindingResult(errors);
replacedertEquals(Tag.SKIP_BODY, this.tag.doStartTag());
String output = getOutput();
replacedertTagOpened(output);
replacedertTagClosed(output);
replacedertContainsAttribute(output, "type", getType());
replacedertValueAttribute(output, "12.34f");
}
16
View Source File : DefaultHandlerExceptionResolverTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void handleMethodArgumentNotValid() throws Exception {
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(new TestBean(), "testBean");
errors.rejectValue("name", "invalid");
MethodParameter parameter = new MethodParameter(this.getClreplaced().getMethod("handle", String.clreplaced), 0);
MethodArgumentNotValidException ex = new MethodArgumentNotValidException(parameter, errors);
ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex);
replacedertNotNull("No ModelAndView returned", mav);
replacedertTrue("No Empty ModelAndView returned", mav.isEmpty());
replacedertEquals("Invalid status code", 400, response.getStatus());
}
16
View Source File : ValidatorFactoryTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void testListValidation() {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.afterPropertiesSet();
ListContainer listContainer = new ListContainer();
listContainer.addString("A");
listContainer.addString("X");
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(listContainer, "listContainer");
errors.initConversion(new DefaultConversionService());
validator.validate(listContainer, errors);
FieldError fieldError = errors.getFieldError("list[1]");
replacedertNotNull(fieldError);
replacedertEquals("X", fieldError.getRejectedValue());
replacedertEquals("X", errors.getFieldValue("list[1]"));
}
16
View Source File : ValidatorFactoryTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void testSpringValidationWithErrorInListElement() {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.afterPropertiesSet();
ValidPerson person = new ValidPerson();
person.getAddressList().add(new ValidAddress());
BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
validator.validate(person, result);
replacedertEquals(3, result.getErrorCount());
FieldError fieldError = result.getFieldError("name");
replacedertEquals("name", fieldError.getField());
fieldError = result.getFieldError("address.street");
replacedertEquals("address.street", fieldError.getField());
fieldError = result.getFieldError("addressList[0].street");
replacedertEquals("addressList[0].street", fieldError.getField());
}
16
View Source File : ValidatorFactoryTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void testSpringValidationWithErrorInSetElement() {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.afterPropertiesSet();
ValidPerson person = new ValidPerson();
person.getAddressSet().add(new ValidAddress());
BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
validator.validate(person, result);
replacedertEquals(3, result.getErrorCount());
FieldError fieldError = result.getFieldError("name");
replacedertEquals("name", fieldError.getField());
fieldError = result.getFieldError("address.street");
replacedertEquals("address.street", fieldError.getField());
fieldError = result.getFieldError("addressSet[].street");
replacedertEquals("addressSet[].street", fieldError.getField());
}
16
View Source File : RadioButtonTagTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void withCheckedObjectValueAndEditor() throws Exception {
this.tag.setPath("myFloat");
this.tag.setValue("F12.99");
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(this.bean, COMMAND_NAME);
MyFloatEditor editor = new MyFloatEditor();
bindingResult.getPropertyEditorRegistry().registerCustomEditor(Float.clreplaced, editor);
getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + COMMAND_NAME, bindingResult);
int result = this.tag.doStartTag();
replacedertThat(result).isEqualTo(Tag.SKIP_BODY);
String output = getOutput();
replacedertTagOpened(output);
replacedertTagClosed(output);
replacedertContainsAttribute(output, "name", "myFloat");
replacedertContainsAttribute(output, "type", "radio");
replacedertContainsAttribute(output, "value", "F" + getFloat().toString());
replacedertContainsAttribute(output, "checked", "checked");
}
16
View Source File : InputTagTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void withErrors() throws Exception {
this.tag.setPath("name");
this.tag.setCssClreplaced("good");
this.tag.setCssErrorClreplaced("bad");
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(this.rob, COMMAND_NAME);
errors.rejectValue("name", "some.code", "Default Message");
errors.rejectValue("name", "too.short", "Too Short");
exposeBindingResult(errors);
replacedertThat(this.tag.doStartTag()).isEqualTo(Tag.SKIP_BODY);
String output = getOutput();
replacedertTagOpened(output);
replacedertTagClosed(output);
replacedertContainsAttribute(output, "type", getType());
replacedertValueAttribute(output, "Rob");
replacedertContainsAttribute(output, "clreplaced", "bad");
}
16
View Source File : InputTagTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void withCustomBinder() throws Exception {
this.tag.setPath("myFloat");
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(this.rob, COMMAND_NAME);
errors.getPropertyAccessor().registerCustomEditor(Float.clreplaced, new SimpleFloatEditor());
exposeBindingResult(errors);
replacedertThat(this.tag.doStartTag()).isEqualTo(Tag.SKIP_BODY);
String output = getOutput();
replacedertTagOpened(output);
replacedertTagClosed(output);
replacedertContainsAttribute(output, "type", getType());
replacedertValueAttribute(output, "12.34f");
}
16
View Source File : ValidatorFactoryTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void testSpringValidationFieldType() {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.afterPropertiesSet();
ValidPerson person = new ValidPerson();
person.setName("Phil");
person.getAddress().setStreet("Phil's Street");
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(person, "person");
validator.validate(person, errors);
replacedertThat(errors.getErrorCount()).isEqualTo(1);
replacedertThat(errors.getFieldError("address").getRejectedValue()).isInstanceOf(ValidAddress.clreplaced);
}
16
View Source File : SpringValidatorAdapterTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
// SPR-15839
@Test
public void testMapEntryConstraint() {
Map<String, String> property = new HashMap<>();
property.put(null, null);
BeanWithMapEntryConstraint bean = new BeanWithMapEntryConstraint();
bean.setProperty(property);
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(bean, "bean");
validatorAdapter.validate(bean, errors);
replacedertThat(errors.hasFieldErrors("property[]")).isTrue();
replacedertThat(errors.getFieldValue("property[]")).isNull();
}
16
View Source File : SpringValidatorAdapterTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void testPatternMessage() {
TestBean testBean = new TestBean();
testBean.setEmail("X");
testBean.setConfirmEmail("X");
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(testBean, "testBean");
validatorAdapter.validate(testBean, errors);
replacedertThat(errors.getFieldErrorCount("email")).isEqualTo(1);
replacedertThat(errors.getFieldValue("email")).isEqualTo("X");
FieldError error = errors.getFieldError("email");
replacedertThat(error).isNotNull();
replacedertThat(messageSource.getMessage(error, Locale.ENGLISH)).contains("[\\w.'-]{1,}@[\\w.'-]{1,}");
replacedertThat(error.contains(ConstraintViolation.clreplaced)).isTrue();
replacedertThat(error.unwrap(ConstraintViolation.clreplaced).getPropertyPath().toString()).isEqualTo("email");
}
16
View Source File : ValidatorFactoryTests.java
License : MIT License
Project Creator : mindcarver
License : MIT License
Project Creator : mindcarver
@Test
public void testListValidation() {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.afterPropertiesSet();
ListContainer listContainer = new ListContainer();
listContainer.addString("A");
listContainer.addString("X");
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(listContainer, "listContainer");
errors.initConversion(new DefaultConversionService());
validator.validate(listContainer, errors);
replacedertEquals("X", errors.getFieldValue("list[1]"));
}
16
View Source File : ValidatorFactoryTests.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
@Test
public void testSpringValidationWithErrorInListElement() throws Exception {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.afterPropertiesSet();
ValidPerson person = new ValidPerson();
person.getAddressList().add(new ValidAddress());
BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
validator.validate(person, result);
replacedertEquals(3, result.getErrorCount());
FieldError fieldError = result.getFieldError("name");
replacedertEquals("name", fieldError.getField());
fieldError = result.getFieldError("address.street");
replacedertEquals("address.street", fieldError.getField());
fieldError = result.getFieldError("addressList[0].street");
replacedertEquals("addressList[0].street", fieldError.getField());
}
See More Examples