BlankConstraint 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 && 0 != value.trim().length()) { violations.add(new Violation(propertyName, value, message, ERROR_CODE_IS_NOT_BLANK)); } return violations; } BlankConstraint(@NonNull Object object, @NonNull String propertyName); @NonNull @Override Set<Violation> validate(); void setMessage(String message); static final String ERROR_CODE_IS_NOT_BLANK; }
@Test public void isValid() throws Exception { assertEmpty("is valid with String", new BlankConstraint(BLANK, PROPERTY_NAME).validate()); assertEmpty("is valid with EditText", new BlankConstraint(generateEditText(BLANK), PROPERTY_NAME).validate()); assertEmpty("is valid with Spinner", new BlankConstraint(generateSpinner(BLANK), PROPERTY_NAME).validate()); }
@Test public void isInvalidWithNotBlank() throws Exception { assertNotEmpty("is invalid with String", new BlankConstraint(NOT_BLANK, PROPERTY_NAME).validate()); assertNotEmpty("is invalid with EditText", new BlankConstraint(generateEditText(NOT_BLANK), PROPERTY_NAME).validate()); assertNotEmpty("is invalid with Spinner", new BlankConstraint(generateSpinner(NOT_BLANK), PROPERTY_NAME).validate()); }
@Test public void isValidWithNull() throws Exception { assertEmpty("is valid with String", new BlankConstraint(NULL, PROPERTY_NAME).validate()); assertEmpty("is valid with EditText", new BlankConstraint(generateEditText(NULL), PROPERTY_NAME).validate()); assertEmpty("is valid with Spinner", new BlankConstraint(generateSpinner(NULL), PROPERTY_NAME).validate()); }