Author: hardy.ferentschik
Date: 2010-06-21 09:30:26 -0400 (Mon, 21 Jun 2010)
New Revision: 19776
Added:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/constraints/impl/DecimalMinMaxValidatorBoundaryTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/constraints/impl/MinMaxValidatorBoundaryTest.java
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/DecimalMaxValidatorForNumber.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/DecimalMaxValidatorForString.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/DecimalMinValidatorForNumber.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/DecimalMinValidatorForString.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/MaxValidatorForNumber.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/MinValidatorForNumber.java
Log:
HV-335
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/DecimalMaxValidatorForNumber.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/DecimalMaxValidatorForNumber.java 2010-06-20
19:55:55 UTC (rev 19775)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/DecimalMaxValidatorForNumber.java 2010-06-21
13:30:26 UTC (rev 19776)
@@ -38,12 +38,13 @@
this.maxValue = new BigDecimal( maxValue.value() );
}
catch ( NumberFormatException nfe ) {
- throw new IllegalArgumentException( maxValue.value() + " does not represent a
valid BigDecimal format" );
+ throw new IllegalArgumentException(
+ maxValue.value() + " does not represent a valid BigDecimal format", nfe
+ );
}
}
public boolean isValid(Number value, ConstraintValidatorContext
constraintValidatorContext) {
-
//null values are valid
if ( value == null ) {
return true;
@@ -56,7 +57,7 @@
return ( new BigDecimal( ( BigInteger ) value ) ).compareTo( maxValue ) != 1;
}
else {
- return ( new BigDecimal( value.doubleValue() ).compareTo( maxValue ) ) != 1;
+ return ( BigDecimal.valueOf( value.longValue() ).compareTo( maxValue ) ) != 1;
}
}
}
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/DecimalMaxValidatorForString.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/DecimalMaxValidatorForString.java 2010-06-20
19:55:55 UTC (rev 19775)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/DecimalMaxValidatorForString.java 2010-06-21
13:30:26 UTC (rev 19776)
@@ -37,7 +37,9 @@
this.maxValue = new BigDecimal( maxValue.value() );
}
catch ( NumberFormatException nfe ) {
- throw new IllegalArgumentException( maxValue.value() + " does not represent a
valid BigDecimal format" );
+ throw new IllegalArgumentException(
+ maxValue.value() + " does not represent a valid BigDecimal format", nfe
+ );
}
}
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/DecimalMinValidatorForNumber.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/DecimalMinValidatorForNumber.java 2010-06-20
19:55:55 UTC (rev 19775)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/DecimalMinValidatorForNumber.java 2010-06-21
13:30:26 UTC (rev 19776)
@@ -38,7 +38,9 @@
this.minValue = new BigDecimal( minValue.value() );
}
catch ( NumberFormatException nfe ) {
- throw new IllegalArgumentException( minValue.value() + " does not represent a
valid BigDecimal format" );
+ throw new IllegalArgumentException(
+ minValue.value() + " does not represent a valid BigDecimal format", nfe
+ );
}
}
@@ -56,7 +58,7 @@
return ( new BigDecimal( ( BigInteger ) value ) ).compareTo( minValue ) != -1;
}
else {
- return ( new BigDecimal( value.doubleValue() ).compareTo( minValue ) ) != -1;
+ return ( BigDecimal.valueOf( value.longValue() ).compareTo( minValue ) ) != -1;
}
}
}
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/DecimalMinValidatorForString.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/DecimalMinValidatorForString.java 2010-06-20
19:55:55 UTC (rev 19775)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/DecimalMinValidatorForString.java 2010-06-21
13:30:26 UTC (rev 19776)
@@ -34,7 +34,9 @@
this.minValue = new BigDecimal( minValue.value() );
}
catch ( NumberFormatException nfe ) {
- throw new IllegalArgumentException( minValue.value() + " does not represent a
valid BigDecimal format" );
+ throw new IllegalArgumentException(
+ minValue.value() + " does not represent a valid BigDecimal format", nfe
+ );
}
}
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/MaxValidatorForNumber.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/MaxValidatorForNumber.java 2010-06-20
19:55:55 UTC (rev 19775)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/MaxValidatorForNumber.java 2010-06-21
13:30:26 UTC (rev 19776)
@@ -1,7 +1,7 @@
// $Id$
/*
* JBoss, Home of Professional Open Source
-* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
+* Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
@@ -28,6 +28,7 @@
* value specified.
*
* @author Alaa Nassef
+ * @author Hardy Ferentschik
*/
public class MaxValidatorForNumber implements ConstraintValidator<Max, Number> {
@@ -49,8 +50,8 @@
return ( ( BigInteger ) value ).compareTo( BigInteger.valueOf( maxValue ) ) != 1;
}
else {
- double doubleValue = value.doubleValue();
- return doubleValue <= maxValue;
+ long longValue = value.longValue();
+ return longValue <= maxValue;
}
}
}
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/MinValidatorForNumber.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/MinValidatorForNumber.java 2010-06-20
19:55:55 UTC (rev 19775)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/MinValidatorForNumber.java 2010-06-21
13:30:26 UTC (rev 19776)
@@ -28,6 +28,7 @@
* value specified.
*
* @author Alaa Nassef
+ * @author Hardy Ferentschik
*/
public class MinValidatorForNumber implements ConstraintValidator<Min, Number> {
@@ -49,9 +50,8 @@
return ( ( BigInteger ) value ).compareTo( BigInteger.valueOf( minValue ) ) != -1;
}
else {
- double doubleValue = value.doubleValue();
- return doubleValue >= minValue;
+ long longValue = value.longValue();
+ return longValue >= minValue;
}
-
}
}
Added:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/constraints/impl/DecimalMinMaxValidatorBoundaryTest.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/constraints/impl/DecimalMinMaxValidatorBoundaryTest.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/constraints/impl/DecimalMinMaxValidatorBoundaryTest.java 2010-06-21
13:30:26 UTC (rev 19776)
@@ -0,0 +1,89 @@
+// $Id:$
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.hibernate.validator.test.constraints.impl;
+
+import java.util.Set;
+import javax.validation.ConstraintViolation;
+import javax.validation.Validator;
+import javax.validation.ValidatorFactory;
+import javax.validation.constraints.DecimalMin;
+
+import org.testng.annotations.Test;
+
+import org.hibernate.validator.HibernateValidator;
+import org.hibernate.validator.HibernateValidatorConfiguration;
+import org.hibernate.validator.cfg.ConstraintMapping;
+import org.hibernate.validator.cfg.defs.DecimalMaxDef;
+import org.hibernate.validator.cfg.defs.DecimalMinDef;
+import org.hibernate.validator.test.util.TestUtil;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static org.hibernate.validator.test.util.TestUtil.assertCorrectConstraintTypes;
+import static org.hibernate.validator.test.util.TestUtil.assertNumberOfViolations;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class DecimalMinMaxValidatorBoundaryTest {
+ public double d;
+
+ @Test
+ public void testDecimalMinValue() {
+
+ // use programmatic mapping api to configure constraint
+ ConstraintMapping mapping = new ConstraintMapping();
+ mapping.type( DecimalMinMaxValidatorBoundaryTest.class )
+ .property( "d", FIELD )
+ .constraint( DecimalMinDef.class )
+ .value( "0.100000000000000005" );
+
+ HibernateValidatorConfiguration config = TestUtil.getConfiguration(
HibernateValidator.class );
+ config.addMapping( mapping );
+
+ ValidatorFactory factory = config.buildValidatorFactory();
+ Validator validator = factory.getValidator();
+
+ this.d = 0.1;
+
+ Set<ConstraintViolation<DecimalMinMaxValidatorBoundaryTest>>
constraintViolations = validator.validate( this );
+ assertNumberOfViolations( constraintViolations, 1 );
+ assertCorrectConstraintTypes( constraintViolations, DecimalMin.class );
+ }
+
+ @Test
+ public void testDecimalMaxValue() {
+
+ // use programmatic mapping api to configure constraint
+ ConstraintMapping mapping = new ConstraintMapping();
+ mapping.type( DecimalMinMaxValidatorBoundaryTest.class )
+ .property( "d", FIELD )
+ .constraint( DecimalMaxDef.class )
+ .value( "0.1" );
+
+ HibernateValidatorConfiguration config = TestUtil.getConfiguration(
HibernateValidator.class );
+ config.addMapping( mapping );
+
+ ValidatorFactory factory = config.buildValidatorFactory();
+ Validator validator = factory.getValidator();
+
+ this.d = 0.1;
+
+ Set<ConstraintViolation<DecimalMinMaxValidatorBoundaryTest>>
constraintViolations = validator.validate( this );
+ assertNumberOfViolations( constraintViolations, 0 );
+ }
+}
\ No newline at end of file
Property changes on:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/constraints/impl/DecimalMinMaxValidatorBoundaryTest.java
___________________________________________________________________
Name: svn:keywords
+ Id
Added:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/constraints/impl/MinMaxValidatorBoundaryTest.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/constraints/impl/MinMaxValidatorBoundaryTest.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/constraints/impl/MinMaxValidatorBoundaryTest.java 2010-06-21
13:30:26 UTC (rev 19776)
@@ -0,0 +1,78 @@
+// $Id:$
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.hibernate.validator.test.constraints.impl;
+
+import java.util.Set;
+import javax.validation.ConstraintViolation;
+import javax.validation.Validator;
+import javax.validation.constraints.Max;
+import javax.validation.constraints.Min;
+
+import org.testng.annotations.Test;
+
+import org.hibernate.validator.test.util.TestUtil;
+
+import static junit.framework.Assert.assertFalse;
+import static org.hibernate.validator.test.util.TestUtil.assertCorrectConstraintTypes;
+import static org.hibernate.validator.test.util.TestUtil.assertNumberOfViolations;
+
+/**
+ * Check correct behaviour of {@link
org.hibernate.validator.constraints.impl.MinValidatorForNumber} and
+ * {@link org.hibernate.validator.constraints.impl.MaxValidatorForNumber} on boundary
values.
+ * <p/>
+ * The chosen numbers: 9223372036854775806l and 9223372036854775807l cast to
+ * the same double value.
+ *
+ * @author Carlos Vara
+ * @author Hardy Ferentschik
+ */
+public class MinMaxValidatorBoundaryTest {
+ @Min(value = 9223372036854775807l)
+ public long min;
+
+ @Max(value = 9223372036854775806l)
+ public long max;
+
+ @Test
+ public void testMinBoundaryValue() {
+ Validator validator = TestUtil.getValidator();
+
+ this.min = 9223372036854775806l;
+ this.max = 0l;
+
+ // Current min value is smaller, should fail, but it doesn't
+ Set<ConstraintViolation<MinMaxValidatorBoundaryTest>> constraintViolations
= validator.validate( this );
+ assertNumberOfViolations( constraintViolations, 1 );
+ assertCorrectConstraintTypes( constraintViolations, Min.class );
+ }
+
+ @Test
+ public void testMaxBoundaryValue() {
+ Validator validator = TestUtil.getValidator();
+
+ this.min = Long.MAX_VALUE;
+ this.max = 9223372036854775807l;
+
+ // Current max value is bigger, should fail, but it doesn't
+ Set<ConstraintViolation<MinMaxValidatorBoundaryTest>> constraintViolations
= validator.validate( this );
+ assertNumberOfViolations( constraintViolations, 1 );
+ assertCorrectConstraintTypes( constraintViolations, Max.class );
+ }
+}
+
+
Property changes on:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/constraints/impl/MinMaxValidatorBoundaryTest.java
___________________________________________________________________
Name: svn:keywords
+ Id