[hibernate-issues] [Hibernate-JIRA] Created: (HV-508) DecimalMaxValidatorForNumber for a double values is not correct

Donigiewicz (JIRA) noreply at atlassian.com
Wed Jul 20 07:16:15 EDT 2011


DecimalMaxValidatorForNumber for a double values is not correct
---------------------------------------------------------------

                 Key: HV-508
                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HV-508
             Project: Hibernate Validator
          Issue Type: Bug
          Components: validators
    Affects Versions: 4.2.0.Final
         Environment: hibernate-validator-4.2.0.Final.jar
validation-api-1.0.0.GA.jar

database independent
            Reporter: Donigiewicz


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 encountered that atleast 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";
   }
}
{code}

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());
   }

}
{code}


It is obvious if you see in the DecimalMaxValidatorForNumber
{code}
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;
      }
   }
{code}


I'm suspecting the cause of this is a https://issues.apache.org/jira/browse/BVAL-15

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list