MatchConstraint extends Constraint<Object> { @NonNull @Override public Set<Violation> validate() throws UnsupportedException { final Set<Violation> violations = new HashSet<>(); final String value = getStringFromObject(object, propertyName); if (null == value || !pattern.matcher(value).matches()) { violations.add(new Violation(propertyName, value, message, ERROR_CODE_NOT_MATCH)); } return violations; } MatchConstraint(@NonNull Object object, @NonNull Pattern pattern, @NonNull String propertyName); @NonNull @Override Set<Violation> validate(); void setMessage(String message); static final String ERROR_CODE_NOT_MATCH; }
@Test public void isInValid() throws Exception { assertNotEmpty("is invalid with String", new MatchConstraint(INVALID, PATTERN, PROPERTY_NAME).validate()); assertNotEmpty("is invalid with String", new MatchConstraint(NULL, PATTERN, PROPERTY_NAME).validate()); assertNotEmpty("is invalid with EdiText", new MatchConstraint(generateEditText(INVALID), PATTERN, PROPERTY_NAME).validate()); assertNotEmpty("is invalid with Spinner", new MatchConstraint(generateSpinner(INVALID), PATTERN, PROPERTY_NAME).validate()); }
@Test public void isValid() throws Exception { assertEmpty("is valid with String", new MatchConstraint(VALID, PATTERN, PROPERTY_NAME).validate()); assertEmpty("is valid with ediText", new MatchConstraint(generateEditText(VALID), PATTERN, PROPERTY_NAME).validate()); assertEmpty("is valid with spinner", new MatchConstraint(generateSpinner(VALID), PATTERN, PROPERTY_NAME).validate()); }