-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: How can I test that validation DID occur in a specific order
PostPosted: Mon Aug 10, 2015 2:53 pm 
Newbie

Joined: Thu May 21, 2015 11:19 am
Posts: 7
Hello,
I'm using a @GroupSequence at the class level to replace the Default group and force validation to be in a specific order. I'd like to set a debug flag to get logged output indicating the order the validation occurred -- this output could also indicate if I'm unintentionally validating something more than once. Is this already possible?


Top
 Profile  
 
 Post subject: Re: How can I test that validation DID occur in a specific order
PostPosted: Thu Sep 17, 2015 4:16 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Let's assume you have:

Code:
@GroupSequence({Group1.class, Group2.class, Group3.class})
public class Foo { ... }


Then set up a Foo instance in a way you expect it to violate constraints of all three groups. Validate it and only the violation(s) of Group1 should be returned. Update the model so it satisfies the Group1 constraints, validate again and you should only see the Group2 constraints being violated. And so on.

Hth,

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: How can I test that validation DID occur in a specific order
PostPosted: Mon Sep 21, 2015 4:02 pm 
Newbie

Joined: Thu May 21, 2015 11:19 am
Posts: 7
Although using the @GroupSequence Annotation will validate in a specific order, how can I write tests that the validation did occur in a specific order? ... or write tests that something wasn't validated twice? I'd like to add a listener into Java Bean Validator such that when a bean is about to be validated, my listener will be notified so my tests can verify the order of validation and that the object wasn't validated twice.

// below is conceptually the idea that a functional or unit test framework can verify that the Car object below is behaving as expected.
class MyValidationListener implements ValidationListener
{
ValidatedTwiceDetector vtd = new ValidatedTwiceDetector();
OrderOfValidationChecker oovc = new OrderOfValidationChecker();
public void validatedObject(ConstraintAnnotation ca, Object ob)
{
vtd.check(ca, ob);
oovc.check(ca, ob);
}
}

ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
validator.addValidationListener(new MyValidationListener());

Car car = Car.createInstance();
Set<ConstraintViolation<Car>> constraintViolations = validator.validate(car);
...
It would be nice to have my listener receive events notifying what constraints were applied to the validator -- and get their respective order as well as be able to detect if something was validated twice unintentionally.

James


Top
 Profile  
 
 Post subject: Re: How can I test that validation DID occur in a specific order
PostPosted: Tue Sep 22, 2015 2:51 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Hi,

The order of validation is testable in the way I described.

Quote:
Set up a Foo instance in a way you expect it to violate constraints of all three groups. Validate it and only the violation(s) of Group1 should be returned.


You would assert on the violations returned by the validator. By that you know that the constraints of Group1 are validated first, otherwise you'd see violations of the Group2 or Group3 constraints.

Quote:
Update the model so it satisfies the Group1 constraints, validate again and you should only see the Group2 constraints being violated.


Again you would assert on the violations returned by the validator. Now you see that the constraints of Group1 are validated before Group2 and Group2 before Group3 (otherwise you'd have seen constraints of Group2 in the first validation and now would see Group3 constraints).

As far as asserting that no constraint is validated twice is concerned, you could add listeners to your model for revealing the order of invocation of their getters (assuming you use getter-level constraints and not field level constraints), which gives an indication of the order of validated constraints. An alternative would be to use something like AspectJ to expose the invocation order (then even including field access) without altering your actual model. Not sure though whether that all is worth the effort really.

Hth,

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.