NotBlankConstraint 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_BLANK)); } return violations; } NotBlankConstraint(@NonNull Object object, @NonNull String propertyName); @NonNull @Override Set<Violation> validate(); void setMessage(String message); static final String ERROR_CODE_IS_BLANK; }
@Test public void isValid() throws Exception { assertEmpty("is valid with String", new NotBlankConstraint(VALID, PROPERTY_NAME).validate()); assertEmpty("is valid with EditText", new NotBlankConstraint(generateEditText(VALID), PROPERTY_NAME).validate()); assertEmpty("is valid with Spinner", new NotBlankConstraint(generateSpinner(VALID), PROPERTY_NAME).validate()); }
@Test public void isInvalidWithBlank() throws Exception { assertNotEmpty("is invalid with String", new NotBlankConstraint(BLANK, PROPERTY_NAME).validate()); assertNotEmpty("is invalid with EditText", new NotBlankConstraint(generateEditText(BLANK), PROPERTY_NAME).validate()); assertNotEmpty("is invalid with Spinner", new NotBlankConstraint(generateSpinner(BLANK), PROPERTY_NAME).validate()); }
@Test public void isInvalidWithNull() throws Exception { assertNotEmpty("is invalid with String", new NotBlankConstraint(NULL, PROPERTY_NAME).validate()); assertNotEmpty("is invalid with EditText", new NotBlankConstraint(generateEditText(NULL), PROPERTY_NAME).validate()); assertNotEmpty("is invalid with Spinner", new NotBlankConstraint(generateSpinner(NULL), PROPERTY_NAME).validate()); }
@Test(expected = UnsupportedException.class) public void testUnsupported() throws Exception { new NotBlankConstraint(new Object(), PROPERTY_NAME).validate(); }