Hi ,
In our project we are about to update a validation library from
4.1.CR1 version to
4.2.Final.
Update to 4.1 is not an option for us due to some other problems.
Unfortunately we have encounter that a at least
DecimalMax constraint is not working as it was in former version
See the example below
A bean Code:
public class SpecificBean extends Bean {
private Double doubleTrouble;
@DecimalMax("1.2")
public Double getDoubleTrouble() {
return doubleTrouble;
}
public void setDoubleTrouble(Double doubleTrouble) {
this.doubleTrouble = doubleTrouble;
}
public String getQualifier() {
return "qualifier";
}
}
And a simple testing class
Code:
public class ValidatorCheck {
/**
* @param args
*/
public static void main(String[] args) {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
SpecificBean bean2 = new SpecificBean();
bean2.setDoubleTrouble(Double.valueOf(1.0));
Set<ConstraintViolation<SpecificBean>> viols2 = validator.validate(bean2);
checkIfNoViolation(viols2);
bean2.setDoubleTrouble(Double.valueOf(1.1));
viols2 = validator.validate(bean2);
checkIfNoViolation(viols2);
bean2.setDoubleTrouble(Double.valueOf(1.19));
viols2 = validator.validate(bean2);
checkIfNoViolation(viols2);
bean2.setDoubleTrouble(Double.valueOf(1.20));
viols2 = validator.validate(bean2);
checkIfNoViolation(viols2);
bean2.setDoubleTrouble(Double.valueOf(1.21));
viols2 = validator.validate(bean2);
checkIfViolation(viols2);
bean2.setDoubleTrouble(Double.valueOf(1.3));
viols2 = validator.validate(bean2);
checkIfViolation(viols2);
bean2.setDoubleTrouble(Double.valueOf(1.51));
viols2 = validator.validate(bean2);
checkIfViolation(viols2);
bean2.setDoubleTrouble(Double.valueOf(1.9));
viols2 = validator.validate(bean2);
checkIfViolation(viols2);
bean2.setDoubleTrouble(Double.valueOf(2.000000001));
viols2 = validator.validate(bean2);
checkIfViolation(viols2);
}
private static void checkIfNoViolation(
Set<ConstraintViolation<SpecificBean>> viols2) {
Assert.assertTrue(viols2.isEmpty());
}
private static void checkIfViolation(
Set<ConstraintViolation<SpecificBean>> viols2) {
Assert.assertFalse("Expected violations ",viols2.isEmpty());
}
}
It is obvious if you see in the
DecimalMaxValidatorForNumberCode:
public boolean isValid(Number value, ConstraintValidatorContext constraintValidatorContext) {
//null values are valid
if ( value == null ) {
return true;
}
if ( value instanceof BigDecimal ) {
return ( ( BigDecimal ) value ).compareTo( maxValue ) != 1;
}
else if ( value instanceof BigInteger ) {
return ( new BigDecimal( ( BigInteger ) value ) ).compareTo( maxValue ) != 1;
}
else {
return ( BigDecimal.valueOf( value.longValue() ).compareTo( maxValue ) ) != 1;
}
}
I'm suspecting the cause of this is a
https://issues.apache.org/jira/browse/BVAL-15Does some one have the same problems ???
Should we use different constraints or this should rather be considered as a bug ???