NotEqualsConstraint extends Constraint<Object> { @Override @NonNull public Set<Violation> validate() { final Set<Violation> violations = new HashSet<>(); final String value = getStringFromObject(object, propertyName); if (expectedValue.equals(value)) { violations.add(new Violation(propertyName, value, String.format(message, value), ERROR_CODE_EQUALS)); } return violations; } NotEqualsConstraint(@NonNull Object object, @NonNull String expectedValue, @NonNull String propertyName); @Override @NonNull Set<Violation> validate(); void setMessage(String message); static final String ERROR_CODE_EQUALS; }
@Test public void isValid() throws Exception { assertEmpty("is valid with String", new NotEqualsConstraint(VALID, EXPECTED_VALUE, PROPERTY_NAME).validate()); assertEmpty("is valid with EditText", new NotEqualsConstraint(generateEditText(VALID), EXPECTED_VALUE, PROPERTY_NAME).validate()); assertEmpty("is valid with Spinner", new NotEqualsConstraint(generateSpinner(VALID), EXPECTED_VALUE, PROPERTY_NAME).validate()); }
@Test public void isInvalid() throws Exception { assertNotEmpty("is invalid with String", new NotEqualsConstraint(VALID, VALID, PROPERTY_NAME).validate()); assertNotEmpty("is invalid with EditText", new NotEqualsConstraint(generateEditText(VALID), VALID, PROPERTY_NAME).validate()); assertNotEmpty("is invalid with Spinner", new NotEqualsConstraint(generateSpinner(VALID), VALID, PROPERTY_NAME).validate()); }
@Test(expected = UnsupportedException.class) public void testUnsupported() throws Exception { new NotEqualsConstraint(new Object(), VALID, PROPERTY_NAME).validate(); }