* @throws Exception BAM!
*/
@Test
public void testGroups() throws Exception {
Employee employee = super.newValidBean();
SimpleConstraintValidator defaultValidator = new SimpleConstraintValidator();
Set<ConstraintViolation<Object>> validationResult;
/*
* Validate with implicit default group.
* Expecting NotNull to raise error.
*/
employee.setStricterValueThanOthers(null);
validationResult = defaultValidator.validate(employee);
super.printValidationMessages(validationResult);
assertTrue(validationResult.size() == 1);
/*
* Validate with implicit default group.
* Expecting no issue because value is not null. However, the value is too short but will not be thrown
* because that constraint is only active for the StricterValidationGroup, which is not active here.
*/
employee.setStricterValueThanOthers("A");
validationResult = defaultValidator.validate(employee);
super.printValidationMessages(validationResult);
assertTrue(validationResult.isEmpty());
/*
* Validate with explicit default StricterValidationGroup group.
* Expecting error because value is too short.
*/
employee.setStricterValueThanOthers("A");
validationResult = defaultValidator.validate(employee, StricterValidationGroup.class);
super.printValidationMessages(validationResult);
assertTrue(validationResult.size() == 1);
/*
* Validate with explicit default StricterValidationGroup group.
* Expecting no error because value os long enough.
*/
employee.setStricterValueThanOthers("ABC");
validationResult = defaultValidator.validate(employee, StricterValidationGroup.class);
super.printValidationMessages(validationResult);
assertTrue(validationResult.isEmpty());
/*
* Validate with explicit default StricterValidationGroup group.
* Expecting NotNull NOT to fire because it is in the default group
* and thus does not apply.
*/
employee.setStricterValueThanOthers(null); //notnull does not apply!
validationResult = defaultValidator.validate(employee, StricterValidationGroup.class);
super.printValidationMessages(validationResult);
assertTrue(validationResult.isEmpty());
/*
* Validate with default and StricterValidationGroup groups.
* Expecintg NotNull to apply because we now took the default group back into the game.
*/
employee.setStricterValueThanOthers(null); //notnull does not apply!
validationResult = defaultValidator.validate(employee, Default.class, StricterValidationGroup.class);
super.printValidationMessages(validationResult);
assertTrue(validationResult.size() == 1);
}