Length implements Validation { public static Validation max(final int maxLength) { if (maxLength <= 0) { throw new IllegalArgumentException("maxLength must be positive"); } return new Length(-1, maxLength); } private  Length(final int minLength, final int maxLength); static Validation min(final int minLength); static Validation max(final int maxLength); static Validation range(final int minLength, final int maxLength); @Override String getErrorMessage(final Context context); @Override boolean isValid(final String text);  }
@Test public void testMax() throws Exception { final Validation validation = Length.max(3); assertTrue(validation.isValid("abc")); assertFalse(validation.isValid("abcd")); }
@Test(expected = IllegalArgumentException.class) public void testNegativeMaxValueIsNotPossible() throws Exception { Length.max(-1); }