EnumValidator implements ConstraintValidator<IsEnum, String> { @Override public boolean isValid(String value, ConstraintValidatorContext context) { if (value == null) { return allowNull; } else { return availableEnumNames.stream().anyMatch(o -> { if (ignoreCase) { return o.equalsIgnoreCase(value); } else { return o.equals(value); } }); } }  @Override @SuppressWarnings("rawtypes") void initialize(IsEnum annotation); @Override boolean isValid(String value, ConstraintValidatorContext context);  }
@Test public void shouldValidateSuccessfully() throws Exception { EnumValidator enumValidator = createAndInitializeValidator(false, false); boolean valid = enumValidator.isValid("foo", null); assertThat(valid, is(true)); }
@Test public void shouldFailForInvalidValue() throws Exception { EnumValidator enumValidator = createAndInitializeValidator(false, false); boolean valid = enumValidator.isValid("xxx", null); assertThat(valid, is(false)); }
@Test public void shouldFailWhenNotIgnoringCase() throws Exception { EnumValidator enumValidator = createAndInitializeValidator(false, false); boolean valid = enumValidator.isValid("Foo", null); assertThat(valid, is(false)); }
@Test public void shouldSucceedWhenIgnoringCase() throws Exception { EnumValidator enumValidator = createAndInitializeValidator(true, false); boolean valid = enumValidator.isValid("Foo", null); assertThat(valid, is(true)); }
@Test public void shouldFailForNull() { EnumValidator enumValidator = createAndInitializeValidator(true, false); boolean valid = enumValidator.isValid(null, null); assertThat(valid, is(false)); }
@Test public void shouldAllowNullWhenFlagIsSet() { EnumValidator enumValidator = createAndInitializeValidator(true, true); boolean valid = enumValidator.isValid(null, null); assertThat(valid, is(true)); }
@Test public void shouldFailForEmptyString() { EnumValidator enumValidator = createAndInitializeValidator(true, false); boolean valid = enumValidator.isValid("", null); assertThat(valid, is(false)); }