### Question:
PrivilegeValidator implements Validator { @Override public void validate(Object obj, Errors errors) { Privilege privilege = (Privilege) obj; if (privilege == null) { errors.rejectValue("privilege", "error.general"); } else { ValidationUtils.rejectIfEmptyOrWhitespace(errors, "privilege", "error.privilege"); ValidateUtil.validateFieldLengths(errors, obj.getClass(), "privilege", "description"); } }  @Override boolean supports(Class<?> c); @Override void validate(Object obj, Errors errors);  }### Answer:
@Test public void validate_shouldFailValidationIfPrivilegeIsNullOrEmptyOrWhitespace() { Privilege priv = new Privilege(); priv.setPrivilege(null); priv.setDescription("some text"); Errors errors = new BindException(priv, "priv"); new PrivilegeValidator().validate(priv, errors); Assert.assertTrue(errors.hasFieldErrors("privilege")); priv.setPrivilege(""); errors = new BindException(priv, "priv"); new PrivilegeValidator().validate(priv, errors); Assert.assertTrue(errors.hasFieldErrors("privilege")); priv.setPrivilege(" "); errors = new BindException(priv, "priv"); new PrivilegeValidator().validate(priv, errors); Assert.assertTrue(errors.hasFieldErrors("privilege")); }
@Test public void validate_shouldPassValidationIfDescriptionIsNullOrEmptyOrWhitespace() { Privilege priv = new Privilege(); priv.setPrivilege("Wallhacking"); priv.setDescription(null); Errors errors = new BindException(priv, "priv"); new PrivilegeValidator().validate(priv, errors); Assert.assertFalse(errors.hasFieldErrors("description")); priv.setDescription(""); errors = new BindException(priv, "priv"); new PrivilegeValidator().validate(priv, errors); Assert.assertFalse(errors.hasFieldErrors("description")); priv.setDescription(" "); errors = new BindException(priv, "priv"); new PrivilegeValidator().validate(priv, errors); Assert.assertFalse(errors.hasFieldErrors("description")); }
@Test public void validate_shouldPassValidationIfAllRequiredFieldsHaveProperValues() { Privilege priv = new Privilege(); priv.setPrivilege("Wallhacking"); priv.setDescription("idspispopd"); Errors errors = new BindException(priv, "priv"); new PrivilegeValidator().validate(priv, errors); Assert.assertFalse(errors.hasErrors()); Assert.assertNotNull(priv.getName()); Assert.assertEquals(priv.getPrivilege(), "Wallhacking"); }
@Test public void validate_shouldPassValidationIfFieldLengthsAreCorrect() { Privilege priv = new Privilege(); priv.setPrivilege("Wallhacking"); priv.setDescription("description"); Errors errors = new BindException(priv, "priv"); new PrivilegeValidator().validate(priv, errors); Assert.assertFalse(errors.hasErrors()); }
@Test public void validate_shouldFailValidationIfFieldLengthsAreNotCorrect() { Privilege priv = new Privilege(); priv .setPrivilege("too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text"); priv .setDescription("too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text"); Errors errors = new BindException(priv, "priv"); new PrivilegeValidator().validate(priv, errors); Assert.assertTrue(errors.hasFieldErrors("privilege")); Assert.assertTrue(errors.hasFieldErrors("description")); }