While reading the JSR 303 specification I came to the understanding (Item C of section 3.4.5) that if a class extends another, the subclass inherits the @GroupSequence annotation that the parent class defines on itself if the subclass does not explicitly create a group sequence.
I have tried to test this theory by extending the
https://github.com/hibernate/hibernate-validator/blob/master/hibernate-validator-archetype/src/test/java/com/example/GroupTest.java tests.
Unfortunately these tests are not behaving how I had expected them to based upon my understanding. If someone could please steer me in the right direction of what the intended functionality of GroupSequence is in a hierarchy, and more specifically explain Item C of Section 3.4.5 to me, I would greatly appreciate it.
The following our the unit test cases that I've added to test my theory on this item which may lend more information about how I understand the spec.
Code:
@Test
public void testOrderedChecksFailsFast() {
RentalCar rentalCar = new RentalCar("Morris", "DD-AB-123", 0);
// This should not create a violation exception due to the 0 seat count failing first due to the GroupSequence on RentalCar
rentalCar.setPassedVehicleInspection(false);
Driver john = new Driver("John Doe");
john.setAge(18);
john.passedDrivingTest(true);
rentalCar.setDriver(john);
assertEquals(1, validator.validate(rentalCar).size());
assertEquals(validator.validate(rentalCar).iterator().next()
.getPropertyPath().toString(), "seatCount");
rentalCar.setSeatCount(4);
assertEquals(1, validator.validate(rentalCar).size());
assertEquals(validator.validate(rentalCar).iterator().next()
.getPropertyPath().toString(), "passedVehicleInspection");
}
@Test
public void testSubclassesInheritGroupSequence() {
//Our assertion here is based around Item C from Section 3.4.5 of the JSR 303 Validation Spec
//that class X (MiniRentalCar) without explicitly defining a Default group would then inherit
//it's superclasse's "Default" constraints along with it's own attribute level constraints
//not explicitly tied to a group other than Default.
class MiniRentalCar extends RentalCar{
public MiniRentalCar(String manufacturer, String licencePlate, int seatCount) {
super( manufacturer, licencePlate, seatCount );
}
}
MiniRentalCar miniRentalCar = new MiniRentalCar("Morris", "DD-AB-123", 0);
// This should not create a violation exception due to the 0 seat count.
miniRentalCar.setPassedVehicleInspection(false);
Driver john = new Driver("John Doe");
john.setAge(18);
john.passedDrivingTest(true);
miniRentalCar.setDriver(john);
assertEquals(1, validator.validate(miniRentalCar).size());
assertEquals(validator.validate(miniRentalCar).iterator().next()
.getPropertyPath().toString(), "seatCount");
miniRentalCar.setSeatCount(4);
assertEquals(1, validator.validate(miniRentalCar).size());
assertEquals(validator.validate(miniRentalCar).iterator().next()
.getPropertyPath().toString(), "passedVehicleInspection");
}
@Test
public void testExplicitGroupSequenceOnSubclass(){
//With the testSubclassesInheritGroupSequence test failing, we then try
//a similar test case whereby we explicitly set the Default group for this class.
@GroupSequence({ AnotherMiniRentalCar.class, CarChecks.class })
class AnotherMiniRentalCar extends RentalCar{
public AnotherMiniRentalCar(String manufacturer, String licencePlate, int seatCount) {
super( manufacturer, licencePlate, seatCount );
}
}
AnotherMiniRentalCar anotherMiniRentalCar = new AnotherMiniRentalCar("Morris", "DD-AB-123", 0);
// This should not create a violation exception due to the 0 seat count.
anotherMiniRentalCar.setPassedVehicleInspection(false);
Driver john = new Driver("John Doe");
john.setAge(18);
john.passedDrivingTest(true);
anotherMiniRentalCar.setDriver(john);
assertEquals(1, validator.validate(anotherMiniRentalCar).size());
assertEquals(validator.validate(anotherMiniRentalCar).iterator().next()
.getPropertyPath().toString(), "seatCount");
anotherMiniRentalCar.setSeatCount(4);
assertEquals(1, validator.validate(anotherMiniRentalCar).size());
assertEquals(validator.validate(anotherMiniRentalCar).iterator().next()
.getPropertyPath().toString(), "passedVehicleInspection");
}