ValidBase64Validator implements ConstraintValidator<ValidBase64, String> { @Override public boolean isValid(String value, ConstraintValidatorContext ctx) { try { if (value != null) { Base64.getDecoder().decode(value); } } catch (IllegalArgumentException ignored) { return false; } return true; }  @Override void initialize(ValidBase64 constraintAnnotation); @Override boolean isValid(String value, ConstraintValidatorContext ctx);  }
@Test public void acceptsBase64() { assertThat(validator.isValid("asdfasdfasdf", null)).isTrue(); }
@Test public void acceptsNull() { assertThat(validator.isValid(null, null)).isTrue(); }
@Test public void acceptsEmpty() { assertThat(validator.isValid("", null)).isTrue(); }
@Test public void rejectsWhitespace() { assertThat(validator.isValid("ab cd", null)).isFalse(); }
@Test public void rejectsNonBase64() { assertThat(validator.isValid("+#$", null)).isFalse(); }
@Test public void rejectsUrlSafeBase64() { assertThat(validator.isValid("a+/", null)).isTrue(); assertThat(validator.isValid("b-_", null)).isFalse(); }