1
|
/**
|
2
|
* Copyright (C) 2015 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.validation;
|
10
|
|
11
|
import java.util.Set;
|
12
|
|
13
|
import javax.validation.ConstraintViolation;
|
14
|
import javax.validation.Validation;
|
15
|
import javax.validation.Validator;
|
16
|
import javax.validation.ValidatorFactory;
|
17
|
|
18
|
import org.junit.Assert;
|
19
|
|
20
|
import eu.etaxonomy.cdm.test.TermTestBase;
|
21
|
|
22
|
/**
|
23
|
* Class providing some base functionality for validator testing
|
24
|
*
|
25
|
* @author a.mueller
|
26
|
* @since 18.02.2015
|
27
|
*/
|
28
|
public abstract class ValidationTestBase extends TermTestBase{
|
29
|
|
30
|
protected Validator validator;
|
31
|
|
32
|
{
|
33
|
ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
|
34
|
validator = validatorFactory.getValidator();
|
35
|
}
|
36
|
|
37
|
|
38
|
protected void validateHasConstraint(Object cdmBase, Class validatorClass, Class group) {
|
39
|
Set<ConstraintViolation<Object>> constraintViolations = validator.validate(cdmBase, group);
|
40
|
assertHasConstraintOnValidator(constraintViolations, validatorClass);
|
41
|
}
|
42
|
|
43
|
protected void validateHasNoConstraint(Object cdmBase, Class validatorClass, Class group) {
|
44
|
Set<ConstraintViolation<Object>> constraintViolations = validator.validate(cdmBase, group);
|
45
|
assertNoConstraintOnValidator(constraintViolations, validatorClass);
|
46
|
}
|
47
|
|
48
|
protected void assertNoConstraintOnValidator(Set<ConstraintViolation<Object>> constraintViolations, Class validatorClass) {
|
49
|
assertHasConstraintOnValidator(constraintViolations, validatorClass, false);
|
50
|
}
|
51
|
|
52
|
protected void assertHasConstraintOnValidator(Set<ConstraintViolation<Object>> constraintViolations, Class validatorClass) {
|
53
|
assertHasConstraintOnValidator(constraintViolations, validatorClass, true);
|
54
|
}
|
55
|
|
56
|
private void assertHasConstraintOnValidator(Set<ConstraintViolation<Object>> constraintViolations, Class validatorClass, boolean requiresViolation) {
|
57
|
boolean hasViolation = false;
|
58
|
for (ConstraintViolation<?> violation : constraintViolations){
|
59
|
Class<?> validatedValidatorClass = violation.getConstraintDescriptor().getConstraintValidatorClasses().iterator().next();
|
60
|
if (validatedValidatorClass.equals(validatorClass)){
|
61
|
hasViolation = true;
|
62
|
}
|
63
|
}
|
64
|
if (! hasViolation && requiresViolation){
|
65
|
Assert.fail("constraint violations are missing an validator class " + validatorClass.getSimpleName());
|
66
|
}else if (hasViolation && ! requiresViolation){
|
67
|
Assert.fail("constraint violations should not exist for validator class " + validatorClass.getSimpleName());
|
68
|
}
|
69
|
return;
|
70
|
}
|
71
|
}
|