Author: hardy.ferentschik
Date: 2009-06-11 11:51:21 -0400 (Thu, 11 Jun 2009)
New Revision: 16761
Added:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMaxValidatorForNumber.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMaxValidatorForString.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMinValidatorForNumber.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMinValidatorForString.java
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintHelper.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MaxValidatorForNumberTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MaxValidatorForStringTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MinValidatorForNumberTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MinValidatorForStringTest.java
Log:
HV-168 Added validators for DecimalMin and DecimalMax
Added:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMaxValidatorForNumber.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMaxValidatorForNumber.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMaxValidatorForNumber.java 2009-06-11
15:51:21 UTC (rev 16761)
@@ -0,0 +1,63 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, 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.validation.constraints.impl;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import javax.validation.ConstraintDeclarationException;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+import javax.validation.constraints.DecimalMax;
+
+/**
+ * Check that the number being validated is less than or equal to the maximum
+ * value specified.
+ *
+ * @author Hardy Ferentschik
+ */
+public class DecimalMaxValidatorForNumber implements ConstraintValidator<DecimalMax,
Number> {
+
+ private BigDecimal maxValue;
+
+ public void initialize(DecimalMax maxValue) {
+ try {
+ this.maxValue = new BigDecimal( maxValue.value() );
+ }
+ catch ( NumberFormatException nfe ) {
+ throw new ConstraintDeclarationException( maxValue.value() + " does not represent
a valid BigDemcimal formt" );
+ }
+ }
+
+ 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 ( new BigDecimal( value.doubleValue() ).compareTo( maxValue ) ) != 1;
+ }
+ }
+}
\ No newline at end of file
Property changes on:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMaxValidatorForNumber.java
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMaxValidatorForString.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMaxValidatorForString.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMaxValidatorForString.java 2009-06-11
15:51:21 UTC (rev 16761)
@@ -0,0 +1,57 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, 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.validation.constraints.impl;
+
+import java.math.BigDecimal;
+import javax.validation.ConstraintDeclarationException;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+import javax.validation.constraints.DecimalMax;
+
+/**
+ * Check that the String being validated represents a number, and has a value
+ * less than or equal to the maximum value specified.
+ *
+ * @author Alaa Nassef
+ */
+public class DecimalMaxValidatorForString implements ConstraintValidator<DecimalMax,
String> {
+
+ private BigDecimal maxValue;
+
+ public void initialize(DecimalMax maxValue) {
+ try {
+ this.maxValue = new BigDecimal( maxValue.value() );
+ }
+ catch ( NumberFormatException nfe ) {
+ throw new ConstraintDeclarationException( maxValue.value() + " does not represent
a valid BigDemcimal formt" );
+ }
+ }
+
+ public boolean isValid(String value, ConstraintValidatorContext
constraintValidatorContext) {
+ //null values are valid
+ if ( value == null ) {
+ return true;
+ }
+ try {
+ return new BigDecimal( value ).compareTo( maxValue ) != 1;
+ }
+ catch ( NumberFormatException nfe ) {
+ return false;
+ }
+ }
+}
\ No newline at end of file
Property changes on:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMaxValidatorForString.java
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMinValidatorForNumber.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMinValidatorForNumber.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMinValidatorForNumber.java 2009-06-11
15:51:21 UTC (rev 16761)
@@ -0,0 +1,63 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, 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.validation.constraints.impl;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import javax.validation.ConstraintDeclarationException;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+import javax.validation.constraints.DecimalMin;
+
+/**
+ * Check that the number being validated is less than or equal to the maximum
+ * value specified.
+ *
+ * @author Hardy Ferentschik
+ */
+public class DecimalMinValidatorForNumber implements ConstraintValidator<DecimalMin,
Number> {
+
+ private BigDecimal minValue;
+
+ public void initialize(DecimalMin minValue) {
+ try {
+ this.minValue = new BigDecimal( minValue.value() );
+ }
+ catch ( NumberFormatException nfe ) {
+ throw new ConstraintDeclarationException( minValue.value() + " does not represent
a valid BigDemcimal formt" );
+ }
+ }
+
+ public boolean isValid(Number value, ConstraintValidatorContext
constraintValidatorContext) {
+
+ //null values are valid
+ if ( value == null ) {
+ return true;
+ }
+
+ if ( value instanceof BigDecimal ) {
+ return ( ( BigDecimal ) value ).compareTo( minValue ) != -1;
+ }
+ else if ( value instanceof BigInteger ) {
+ return ( new BigDecimal( ( BigInteger ) value ) ).compareTo( minValue ) != -1;
+ }
+ else {
+ return ( new BigDecimal( value.doubleValue() ).compareTo( minValue ) ) != -1;
+ }
+ }
+}
\ No newline at end of file
Property changes on:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMinValidatorForNumber.java
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMinValidatorForString.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMinValidatorForString.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMinValidatorForString.java 2009-06-11
15:51:21 UTC (rev 16761)
@@ -0,0 +1,54 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, 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.validation.constraints.impl;
+
+import java.math.BigDecimal;
+import javax.validation.ConstraintDeclarationException;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+import javax.validation.constraints.DecimalMin;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class DecimalMinValidatorForString implements ConstraintValidator<DecimalMin,
String> {
+
+ private BigDecimal minValue;
+
+ public void initialize(DecimalMin minValue) {
+ try {
+ this.minValue = new BigDecimal( minValue.value() );
+ }
+ catch ( NumberFormatException nfe ) {
+ throw new ConstraintDeclarationException( minValue.value() + " does not represent
a valid BigDemcimal formt" );
+ }
+ }
+
+ public boolean isValid(String value, ConstraintValidatorContext
constraintValidatorContext) {
+ //null values are valid
+ if ( value == null ) {
+ return true;
+ }
+ try {
+ return new BigDecimal( value ).compareTo( minValue ) != -1;
+ }
+ catch ( NumberFormatException nfe ) {
+ return false;
+ }
+ }
+}
\ No newline at end of file
Property changes on:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/impl/DecimalMinValidatorForString.java
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintHelper.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintHelper.java 2009-06-11
10:39:51 UTC (rev 16760)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintHelper.java 2009-06-11
15:51:21 UTC (rev 16761)
@@ -39,6 +39,8 @@
import javax.validation.constraints.Past;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
+import javax.validation.constraints.DecimalMax;
+import javax.validation.constraints.DecimalMin;
import org.hibernate.validation.constraints.impl.AssertFalseValidator;
import org.hibernate.validation.constraints.impl.AssertTrueValidator;
@@ -66,6 +68,10 @@
import org.hibernate.validation.constraints.impl.SizeValidatorForCollection;
import org.hibernate.validation.constraints.impl.SizeValidatorForMap;
import org.hibernate.validation.constraints.impl.SizeValidatorForString;
+import org.hibernate.validation.constraints.impl.DecimalMaxValidatorForNumber;
+import org.hibernate.validation.constraints.impl.DecimalMaxValidatorForString;
+import org.hibernate.validation.constraints.impl.DecimalMinValidatorForNumber;
+import org.hibernate.validation.constraints.impl.DecimalMinValidatorForString;
import org.hibernate.validation.util.ReflectionHelper;
/**
@@ -94,6 +100,21 @@
builtinConstraints.put( AssertTrue.class, constraintList );
constraintList = new ArrayList<Class<? extends ConstraintValidator<?,
?>>>();
+ constraintList.add( DecimalMaxValidatorForNumber.class );
+ constraintList.add( DecimalMaxValidatorForString.class );
+ builtinConstraints.put( DecimalMax.class, constraintList );
+
+ constraintList = new ArrayList<Class<? extends ConstraintValidator<?,
?>>>();
+ constraintList.add( DecimalMinValidatorForNumber.class );
+ constraintList.add( DecimalMinValidatorForString.class );
+ builtinConstraints.put( DecimalMin.class, constraintList );
+
+ constraintList = new ArrayList<Class<? extends ConstraintValidator<?,
?>>>();
+ constraintList.add( DigitsValidatorForString.class );
+ constraintList.add( DigitsValidatorForNumber.class );
+ builtinConstraints.put( Digits.class, constraintList );
+
+ constraintList = new ArrayList<Class<? extends ConstraintValidator<?,
?>>>();
constraintList.add( FutureValidatorForCalendar.class );
constraintList.add( FutureValidatorForDate.class );
builtinConstraints.put( Future.class, constraintList );
@@ -122,9 +143,8 @@
builtinConstraints.put( Past.class, constraintList );
constraintList = new ArrayList<Class<? extends ConstraintValidator<?,
?>>>();
- constraintList.add( DigitsValidatorForString.class );
- constraintList.add( DigitsValidatorForNumber.class );
- builtinConstraints.put( Digits.class, constraintList );
+ constraintList.add( PatternValidator.class );
+ builtinConstraints.put( Pattern.class, constraintList );
constraintList = new ArrayList<Class<? extends ConstraintValidator<?,
?>>>();
constraintList.add( SizeValidatorForString.class );
@@ -139,10 +159,6 @@
constraintList.add( SizeValidatorForArraysOfInt.class );
constraintList.add( SizeValidatorForArraysOfLong.class );
builtinConstraints.put( Size.class, constraintList );
-
- constraintList = new ArrayList<Class<? extends ConstraintValidator<?,
?>>>();
- constraintList.add( PatternValidator.class );
- builtinConstraints.put( Pattern.class, constraintList );
}
public List<Class<? extends ConstraintValidator<? extends Annotation,
?>>> getBuiltInConstraints(Class<? extends Annotation> annotationType) {
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MaxValidatorForNumberTest.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MaxValidatorForNumberTest.java 2009-06-11
10:39:51 UTC (rev 16760)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MaxValidatorForNumberTest.java 2009-06-11
15:51:21 UTC (rev 16761)
@@ -19,11 +19,14 @@
import java.math.BigDecimal;
import java.math.BigInteger;
+import javax.validation.ConstraintDeclarationException;
+import javax.validation.ConstraintValidator;
+import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.Max;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
-import org.testng.annotations.BeforeClass;
+import static org.testng.Assert.fail;
import org.testng.annotations.Test;
import org.hibernate.validation.util.annotationfactory.AnnotationDescriptor;
@@ -35,21 +38,51 @@
*/
public class MaxValidatorForNumberTest {
- private static MaxValidatorForNumber constraint;
+ @Test
+ public void testIsValidMax() {
- @BeforeClass
- public static void init() {
AnnotationDescriptor<Max> descriptor = new AnnotationDescriptor<Max>(
Max.class );
descriptor.setValue( "value", 15l );
descriptor.setValue( "message", "{validator.max}" );
Max m = AnnotationFactory.create( descriptor );
- constraint = new MaxValidatorForNumber();
+ MaxValidatorForNumber constraint = new MaxValidatorForNumber();
constraint.initialize( m );
+ testMaxValidator( constraint );
}
@Test
- public void testIsValid() {
+ public void testIsValidDecimalMax() {
+
+ AnnotationDescriptor<DecimalMax> descriptor = new
AnnotationDescriptor<DecimalMax>( DecimalMax.class );
+ descriptor.setValue( "value", "15.0E0" );
+ descriptor.setValue( "message", "{validator.max}" );
+ DecimalMax m = AnnotationFactory.create( descriptor );
+
+ DecimalMaxValidatorForNumber constraint = new DecimalMaxValidatorForNumber();
+ constraint.initialize( m );
+ testMaxValidator( constraint );
+ }
+
+ @Test
+ public void testInitializeDecimalMaxWithInvalidValue() {
+
+ AnnotationDescriptor<DecimalMax> descriptor = new
AnnotationDescriptor<DecimalMax>( DecimalMax.class );
+ descriptor.setValue( "value", "foobar" );
+ descriptor.setValue( "message", "{validator.max}" );
+ DecimalMax m = AnnotationFactory.create( descriptor );
+
+ DecimalMaxValidatorForNumber constraint = new DecimalMaxValidatorForNumber();
+ try {
+ constraint.initialize( m );
+ fail();
+ }
+ catch ( ConstraintDeclarationException e ) {
+ // success
+ }
+ }
+
+ private void testMaxValidator(ConstraintValidator<?, Number> constraint) {
byte b = 1;
Byte bWrapper = 127;
assertTrue( constraint.isValid( null, null ) );
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MaxValidatorForStringTest.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MaxValidatorForStringTest.java 2009-06-11
10:39:51 UTC (rev 16760)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MaxValidatorForStringTest.java 2009-06-11
15:51:21 UTC (rev 16761)
@@ -17,36 +17,69 @@
*/
package org.hibernate.validation.constraints.impl;
+import javax.validation.ConstraintDeclarationException;
+import javax.validation.ConstraintValidator;
+import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.Max;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
-import org.testng.annotations.BeforeClass;
+import static org.testng.Assert.fail;
import org.testng.annotations.Test;
import org.hibernate.validation.util.annotationfactory.AnnotationDescriptor;
import org.hibernate.validation.util.annotationfactory.AnnotationFactory;
/**
- * @author Alaa Nassef
+ * @author Hardy Ferentschik
*/
public class MaxValidatorForStringTest {
- private static MaxValidatorForString constraint;
+ @Test
+ public void testIsValidMax() {
- @BeforeClass
- public static void init() {
AnnotationDescriptor<Max> descriptor = new AnnotationDescriptor<Max>(
Max.class );
descriptor.setValue( "value", 15l );
descriptor.setValue( "message", "{validator.max}" );
Max m = AnnotationFactory.create( descriptor );
- constraint = new MaxValidatorForString();
+ MaxValidatorForString constraint = new MaxValidatorForString();
constraint.initialize( m );
+ testMaxValidator( constraint );
}
@Test
- public void testIsValid() {
+ public void testIsValidDecimalMax() {
+
+ AnnotationDescriptor<DecimalMax> descriptor = new
AnnotationDescriptor<DecimalMax>( DecimalMax.class );
+ descriptor.setValue( "value", "15.0E0" );
+ descriptor.setValue( "message", "{validator.max}" );
+ DecimalMax m = AnnotationFactory.create( descriptor );
+
+ DecimalMaxValidatorForString constraint = new DecimalMaxValidatorForString();
+ constraint.initialize( m );
+ testMaxValidator( constraint );
+ }
+
+ @Test
+ public void testInitializeDecimalMaxWithInvalidValue() {
+
+ AnnotationDescriptor<DecimalMax> descriptor = new
AnnotationDescriptor<DecimalMax>( DecimalMax.class );
+ descriptor.setValue( "value", "foobar" );
+ descriptor.setValue( "message", "{validator.max}" );
+ DecimalMax m = AnnotationFactory.create( descriptor );
+
+ DecimalMaxValidatorForNumber constraint = new DecimalMaxValidatorForNumber();
+ try {
+ constraint.initialize( m );
+ fail();
+ }
+ catch ( ConstraintDeclarationException e ) {
+ // success
+ }
+ }
+
+ private void testMaxValidator(ConstraintValidator<?, String> constraint) {
assertTrue( constraint.isValid( null, null ) );
assertTrue( constraint.isValid( "15", null ) );
assertTrue( constraint.isValid( "15.0", null ) );
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MinValidatorForNumberTest.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MinValidatorForNumberTest.java 2009-06-11
10:39:51 UTC (rev 16760)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MinValidatorForNumberTest.java 2009-06-11
15:51:21 UTC (rev 16761)
@@ -19,11 +19,14 @@
import java.math.BigDecimal;
import java.math.BigInteger;
+import javax.validation.ConstraintDeclarationException;
+import javax.validation.ConstraintValidator;
+import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.Min;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
-import org.testng.annotations.BeforeClass;
+import static org.testng.Assert.fail;
import org.testng.annotations.Test;
import org.hibernate.validation.util.annotationfactory.AnnotationDescriptor;
@@ -35,21 +38,49 @@
*/
public class MinValidatorForNumberTest {
- private static MinValidatorForNumber constraint;
-
- @BeforeClass
- public static void init() {
+ @Test
+ public void testIsValidMinValidator() {
AnnotationDescriptor<Min> descriptor = new AnnotationDescriptor<Min>(
Min.class );
descriptor.setValue( "value", 15l );
descriptor.setValue( "message", "{validator.min}" );
Min m = AnnotationFactory.create( descriptor );
- constraint = new MinValidatorForNumber();
+ MinValidatorForNumber constraint = new MinValidatorForNumber();
constraint.initialize( m );
+ testMinValidator( constraint );
}
@Test
- public void testIsValid() {
+ public void testIsValidDecimalMinValidator() {
+ AnnotationDescriptor<DecimalMin> descriptor = new
AnnotationDescriptor<DecimalMin>( DecimalMin.class );
+ descriptor.setValue( "value", "1500E-2" );
+ descriptor.setValue( "message", "{validator.min}" );
+ DecimalMin m = AnnotationFactory.create( descriptor );
+
+ DecimalMinValidatorForNumber constraint = new DecimalMinValidatorForNumber();
+ constraint.initialize( m );
+ testMinValidator( constraint );
+ }
+
+ @Test
+ public void testInitializeDecimalMaxWithInvalidValue() {
+
+ AnnotationDescriptor<DecimalMin> descriptor = new
AnnotationDescriptor<DecimalMin>( DecimalMin.class );
+ descriptor.setValue( "value", "foobar" );
+ descriptor.setValue( "message", "{validator.min}" );
+ DecimalMin m = AnnotationFactory.create( descriptor );
+
+ DecimalMinValidatorForNumber constraint = new DecimalMinValidatorForNumber();
+ try {
+ constraint.initialize( m );
+ fail();
+ }
+ catch ( ConstraintDeclarationException e ) {
+ // success
+ }
+ }
+
+ private void testMinValidator(ConstraintValidator<?, Number> constraint) {
byte b = 1;
Byte bWrapper = 127;
assertTrue( constraint.isValid( null, null ) );
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MinValidatorForStringTest.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MinValidatorForStringTest.java 2009-06-11
10:39:51 UTC (rev 16760)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/impl/MinValidatorForStringTest.java 2009-06-11
15:51:21 UTC (rev 16761)
@@ -17,11 +17,14 @@
*/
package org.hibernate.validation.constraints.impl;
+import javax.validation.ConstraintDeclarationException;
+import javax.validation.ConstraintValidator;
+import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.Min;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
-import org.testng.annotations.BeforeClass;
+import static org.testng.Assert.fail;
import org.testng.annotations.Test;
import org.hibernate.validation.util.annotationfactory.AnnotationDescriptor;
@@ -29,24 +32,53 @@
/**
* @author Alaa Nassef
+ * @author Hardy Ferentschik
*/
public class MinValidatorForStringTest {
- private static MinValidatorForString constraint;
-
- @BeforeClass
- public static void init() {
+ @Test
+ public void testIsValidMinValidator() {
AnnotationDescriptor<Min> descriptor = new AnnotationDescriptor<Min>(
Min.class );
descriptor.setValue( "value", 15l );
descriptor.setValue( "message", "{validator.min}" );
Min m = AnnotationFactory.create( descriptor );
- constraint = new MinValidatorForString();
+ MinValidatorForString constraint = new MinValidatorForString();
constraint.initialize( m );
+ testMinValidator( constraint );
}
@Test
- public void testIsValid() {
+ public void testIsValidDecimalMinValidator() {
+ AnnotationDescriptor<DecimalMin> descriptor = new
AnnotationDescriptor<DecimalMin>( DecimalMin.class );
+ descriptor.setValue( "value", "1500E-2" );
+ descriptor.setValue( "message", "{validator.min}" );
+ DecimalMin m = AnnotationFactory.create( descriptor );
+
+ DecimalMinValidatorForString constraint = new DecimalMinValidatorForString();
+ constraint.initialize( m );
+ testMinValidator( constraint );
+ }
+
+ @Test
+ public void testInitializeDecimalMaxWithInvalidValue() {
+
+ AnnotationDescriptor<DecimalMin> descriptor = new
AnnotationDescriptor<DecimalMin>( DecimalMin.class );
+ descriptor.setValue( "value", "foobar" );
+ descriptor.setValue( "message", "{validator.min}" );
+ DecimalMin m = AnnotationFactory.create( descriptor );
+
+ DecimalMinValidatorForNumber constraint = new DecimalMinValidatorForNumber();
+ try {
+ constraint.initialize( m );
+ fail();
+ }
+ catch ( ConstraintDeclarationException e ) {
+ // success
+ }
+ }
+
+ private void testMinValidator(ConstraintValidator<?, String> constraint) {
assertTrue( constraint.isValid( null, null ) );
assertTrue( constraint.isValid( "20", null ) );
assertTrue( constraint.isValid( "15", null ) );
@@ -57,5 +89,4 @@
//number format exception
assertFalse( constraint.isValid( "15l", null ) );
}
-
}