validation testing
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
package org.springframework.binding.validation;
|
||||
|
||||
import org.springframework.binding.validation.ConfirmationConstraint.ConfirmationForm;
|
||||
|
||||
public class AccountRegistrationForm {
|
||||
private String username;
|
||||
private String password;
|
||||
private String confirmedPassword;
|
||||
|
||||
public void validate(ValidationContext context) {
|
||||
context.setProperty("username");
|
||||
context.validate(new RequiredConstraint());
|
||||
context.validate(new LengthConstraint(3, 10));
|
||||
|
||||
context.setProperty("password");
|
||||
context.validate(new RequiredConstraint());
|
||||
context.validate(new LengthConstraint(6, 10));
|
||||
context.validate(new PasswordStrengthConstraint());
|
||||
|
||||
context.setProperty("confirmedPassword");
|
||||
context.validate(new ConfirmationConstraint(), new ConfirmationForm(password, confirmedPassword));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package org.springframework.binding.validation;
|
||||
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
public class ConfirmationConstraint {
|
||||
|
||||
public void validate(ConfirmationForm form, ValidationContext context) {
|
||||
if (form.getValue() == null) {
|
||||
return;
|
||||
}
|
||||
if (!ObjectUtils.nullSafeEquals(form.getValue(), form.getConfirmedValue())) {
|
||||
context.addDefaultFailure();
|
||||
}
|
||||
}
|
||||
|
||||
public static class ConfirmationForm {
|
||||
|
||||
private Object value;
|
||||
|
||||
private Object confirmedValue;
|
||||
|
||||
public ConfirmationForm(String value, String confirmedValue) {
|
||||
this.value = value;
|
||||
this.confirmedValue = confirmedValue;
|
||||
}
|
||||
|
||||
public Object getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(Object value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public Object getConfirmedValue() {
|
||||
return confirmedValue;
|
||||
}
|
||||
|
||||
public void setConfirmedValue(Object confirmedValue) {
|
||||
this.confirmedValue = confirmedValue;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.springframework.binding.validation;
|
||||
|
||||
public class LengthConstraint {
|
||||
|
||||
private Integer min;
|
||||
|
||||
private Integer max;
|
||||
|
||||
public LengthConstraint(int min, int max) {
|
||||
this.min = new Integer(min);
|
||||
this.max = new Integer(max);
|
||||
}
|
||||
|
||||
public Integer getMin() {
|
||||
return min;
|
||||
}
|
||||
|
||||
public Integer getMax() {
|
||||
return max;
|
||||
}
|
||||
|
||||
public void validate(String value, ValidationContext context) {
|
||||
if (value == null || value.length() < min.intValue() || value.length() > max.intValue()) {
|
||||
context.addFailure(createFailure());
|
||||
}
|
||||
}
|
||||
|
||||
protected ValidationFailure createFailure() {
|
||||
return new ValidationFailureBuilder().arg("min", getMin()).arg("max", getMax()).build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package org.springframework.binding.validation;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class PasswordStrengthConstraint {
|
||||
|
||||
private Pattern pattern = Pattern.compile("^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]$");
|
||||
|
||||
public void validate(String password, ValidationContext context) {
|
||||
Matcher matcher = pattern.matcher(password);
|
||||
if (!matcher.find()) {
|
||||
context.addFailure(createFailure());
|
||||
}
|
||||
}
|
||||
|
||||
protected ValidationFailure createFailure() {
|
||||
return new ValidationFailureBuilder().warning().detail("cause").detail("recommendedAction")
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package org.springframework.binding.validation;
|
||||
|
||||
public class RequiredConstraint {
|
||||
|
||||
public void validate(Object value, ValidationContext context) {
|
||||
if (value == null) {
|
||||
context.addDefaultFailure();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user