[hibernate-issues] [Hibernate-JIRA] Created: (HV-458) Wrong behaviour determining default group constraints in conjunction with @GroupSequence

Hardy Ferentschik (JIRA) noreply at atlassian.com
Tue Mar 22 13:37:08 EDT 2011


Wrong behaviour determining default group constraints in conjunction with @GroupSequence
----------------------------------------------------------------------------------------

                 Key: HV-458
                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HV-458
             Project: Hibernate Validator
          Issue Type: Bug
            Reporter: Hardy Ferentschik


See https://forum.hibernate.org/viewtopic.php?f=9&t=1010136&start=0

In a class hierarchy with more than two classes where the middle class overrides the default group sequence the wrong constraints are executed.
This can be reproduces with
{code}
public class GroupTest {


	private static Validator validator;

	@BeforeClass
	public static void setUp() {
		ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
		validator = factory.getValidator();
	}

	@Test
	public void testDriveAway() {
		// create a car and check that everything is ok with it.
		Car car = new Car( "Morris", "DD-AB-123", 2 );
		Set<ConstraintViolation<Car>> constraintViolations = validator.validate( car );
		assertEquals( 0, constraintViolations.size() );

		// but has it passed the vehicle inspection?
		constraintViolations = validator.validate( car, CarChecks.class );
		assertEquals( 1, constraintViolations.size() );
		assertEquals(
				"The car has to pass the vehicle inspection first", constraintViolations.iterator().next().getMessage()
		);

		// let's go to the vehicle inspection
		car.setPassedVehicleInspection( true );
		assertEquals( 0, validator.validate( car ).size() );

		// now let's add a driver. He is 18, but has not passed the driving test yet
		Driver john = new Driver( "John Doe" );
		john.setAge( 18 );
		car.setDriver( john );
		constraintViolations = validator.validate( car, DriverChecks.class );
		assertEquals( 1, constraintViolations.size() );
		assertEquals( "You first have to pass the driving test", constraintViolations.iterator().next().getMessage() );

		// ok, John passes the test
		john.passedDrivingTest( true );
		assertEquals( 0, validator.validate( car, DriverChecks.class ).size() );

		// just checking that everything is in order now
		assertEquals( 0, validator.validate( car, Default.class, CarChecks.class, DriverChecks.class ).size() );
	}

	@Test
	public void testOrderedChecks() {
		Car car = new Car( "Morris", "DD-AB-123", 2 );
		car.setPassedVehicleInspection( true );

		Driver john = new Driver( "John Doe" );
		john.setAge( 18 );
		john.passedDrivingTest( true );
		car.setDriver( john );

		assertEquals( 0, validator.validate( car, OrderedChecks.class ).size() );
	}

	@Test
	public void testOrderedChecksWithRedefinedDefault() {
		RentalCar rentalCar = new RentalCar( "Morris", "DD-AB-123", 2 );
		rentalCar.setPassedVehicleInspection( true );

		Driver john = new Driver( "John Doe" );
		john.setAge( 18 );
		john.passedDrivingTest( true );
		rentalCar.setDriver( john );

		assertEquals( 0, validator.validate( rentalCar, Default.class, DriverChecks.class ).size() );
	}

	@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"
		);
	}
}
{code}



 

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list