Length implements Validation { public static Validation range(final int minLength, final int maxLength) { if (minLength <= 0) { throw new IllegalArgumentException("minLength must be positive"); } if (maxLength <= 0) { throw new IllegalArgumentException("maxLength must be positive"); } return new Length(minLength, 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 testRange() throws Exception { final Validation validation = Length.range(2, 4); assertFalse(validation.isValid("a")); assertTrue(validation.isValid("ab")); assertTrue(validation.isValid("abc")); assertTrue(validation.isValid("abcd")); assertFalse(validation.isValid("abcde")); }
@Test(expected = IllegalArgumentException.class) public void testNegativeMinValueRangeIsNotPossible() throws Exception { Length.range(-1, 1); }
@Test(expected = IllegalArgumentException.class) public void testNegativeMaxValueRangeIsNotPossible() throws Exception { Length.range(1, -1); }