-->
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.  [ 3 posts ] 
Author Message
 Post subject: Problems with decimal max constraint
PostPosted: Tue Jul 19, 2011 8:03 am 
Newbie

Joined: Tue Aug 24, 2010 3:56 am
Posts: 8
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 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;
      }
   }


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

Does some one have the same problems ???
Should we use different constraints or this should rather be considered as a bug ???


Top
 Profile  
 
 Post subject: Re: Problems with decimal max constraint
PostPosted: Tue Jul 19, 2011 6:43 pm 
Hibernate Team
Hibernate Team

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

this seems to be caused by HV-335. I think long/double values should be handled separately in the validator. Could you create an issue in JIRA for this?

Note that @DecimalMax isn't supported for doubles as per the specification, so relying on this feature might reduce portability between BV providers of your code.

--Gunnar

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


Top
 Profile  
 
 Post subject: Re: Problems with decimal max constraint
PostPosted: Wed Jul 20, 2011 7:18 am 
Newbie

Joined: Tue Aug 24, 2010 3:56 am
Posts: 8
Hi Gunnar,
Issue https://hibernate.onjira.com/browse/HV-508 has been created for that.

Thanks Mariusz


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.