FileConstraint extends Constraint<File> { @NonNull @Override public Set<Violation> validate() throws UnsupportedException { final Set<Violation> violations = new HashSet<>(); final long size = object.length(); if (size <= min) { violations.add(new Violation(propertyName, object, String.format(minMessage, min), ERROR_CODE_TOO_SMALL)); } else if (size >= max) { violations.add(new Violation(propertyName, object, String.format(maxMessage, max), ERROR_CODE_TOO_LARGE)); } return violations; } FileConstraint(@NonNull File object, @NonNull String propertyName); @NonNull @Override Set<Violation> validate(); void setMin(double min); void setMax(double max); void setMinMessage(String minMessage); void setMaxMessage(String maxMessage); static final String ERROR_CODE_TOO_SMALL; static final String ERROR_CODE_TOO_LARGE; }
@Test public void testIsValid() throws Exception { assertEmpty("Is valid", getFileConstraint(0, Double.MAX_VALUE).validate()); }
@Test public void testValidateWithMinConstraint() throws Exception { assertNotEmpty("Is invalid", getFileConstraint(100000000d, Double.MAX_VALUE).validate()); }
@Test public void testValidateWithMaxConstraint() throws Exception { assertNotEmpty("Is invalid", getFileConstraint(0, 0).validate()); }