Author: epbernard
Date: 2006-12-19 13:09:33 -0500 (Tue, 19 Dec 2006)
New Revision: 11002
Added:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/Digits.java
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/DigitsValidator.java
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/validator/test/validator/DigitsTest.java
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages.properties
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_da.properties
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_de.properties
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_es.properties
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_fr.properties
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_it.properties
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_nl.properties
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_pt_BR.properties
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_sv.properties
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_zh_CN.properties
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_zh_TW.properties
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/validator/test/validator/Car.java
Log:
ANN-507 @Digits
Added:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/Digits.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/Digits.java 2006-12-19
17:38:43 UTC (rev 11001)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/Digits.java 2006-12-19
18:09:33 UTC (rev 11002)
@@ -0,0 +1,25 @@
+//$Id: $
+package org.hibernate.validator;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Target;
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+/**
+ * Check that a given number has <code>integerDigits</code> integer digits
+ * and <code>fractionalDigits</code> fractional digits
+ * The constraints are defined at the database level too
+ *
+ * @author Norman Richards
+ */
+(a)ValidatorClass(DigitsValidator.class)
+(a)Target({ElementType.METHOD, ElementType.FIELD})
+@Retention( RetentionPolicy.RUNTIME)
+@Documented
+public @interface Digits {
+ int integerDigits();
+ int fractionalDigits() default 0;
+ String message() default "{validator.digits}";
+}
\ No newline at end of file
Added:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/DigitsValidator.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/DigitsValidator.java 2006-12-19
17:38:43 UTC (rev 11001)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/DigitsValidator.java 2006-12-19
18:09:33 UTC (rev 11002)
@@ -0,0 +1,84 @@
+//$Id: $
+package org.hibernate.validator;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+
+import org.hibernate.mapping.Property;
+import org.hibernate.mapping.Column;
+
+/**
+ * Validate a Digit to check if it matches the according pattern
+ *
+ * @authod Norman Richards
+ * @author Emmanuel Bernard
+ */
+public class DigitsValidator implements Validator<Digits>, PropertyConstraint {
+ int integerDigits;
+ int fractionalDigits;
+
+ public void initialize(Digits configuration) {
+ integerDigits = configuration.integerDigits();
+ fractionalDigits = configuration.fractionalDigits();
+ }
+
+ public boolean isValid(Object value) {
+ if ( value == null ) {
+ return true;
+ }
+
+ String stringValue = null;
+
+ if ( value instanceof String ) {
+ try {
+ stringValue = stringValue( new BigDecimal( (String) value ) );
+ }
+ catch (NumberFormatException nfe) {
+ return false;
+ }
+ }
+ else if ( value instanceof BigDecimal ) {
+ stringValue = stringValue( (BigDecimal) value );
+ }
+ else if ( value instanceof BigInteger ) {
+ stringValue = stringValue( (BigInteger) value );
+ }
+ else if ( value instanceof Number ) {
+ //yukky
+ stringValue = stringValue( new BigDecimal( ( (Number) value ).toString() ) );
+ }
+ else {
+ return false;
+ }
+
+ int pos = stringValue.indexOf( "." );
+
+ int left = ( pos == -1 ) ?
+ stringValue.length() :
+ pos;
+ int right = ( pos == -1 ) ?
+ 0 :
+ stringValue.length() - pos - 1;
+
+ if ( left == 1 && stringValue.charAt( 0 ) == '0' ) {
+ left--;
+ }
+
+ return !( left > integerDigits || right > fractionalDigits );
+
+ }
+
+ private String stringValue(BigDecimal number) {
+ return number.abs().stripTrailingZeros().toPlainString();
+ }
+
+ private String stringValue(BigInteger number) {
+ return number.abs().toString();
+ }
+
+ public void apply(Property property) {
+ Column col = (Column) property.getColumnIterator().next();
+ col.setPrecision( integerDigits + fractionalDigits );
+ col.setScale( fractionalDigits );
+ }
+}
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages.properties
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages.properties 2006-12-19
17:38:43 UTC (rev 11001)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages.properties 2006-12-19
18:09:33 UTC (rev 11002)
@@ -10,4 +10,5 @@
validator.range=must be between {min} and {max}
validator.size=size must be between {min} and {max}
validator.email=not a well-formed email address
-validator.notEmpty=may not be null or empty
\ No newline at end of file
+validator.notEmpty=may not be null or empty
+validator.digits=numeric value out of bounds (<{integerDigits}
digits>.<{fractionalDigits} digits> expected)
\ No newline at end of file
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_da.properties
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_da.properties 2006-12-19
17:38:43 UTC (rev 11001)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_da.properties 2006-12-19
18:09:33 UTC (rev 11002)
@@ -10,4 +10,5 @@
validator.range=skal v�re mellem {min} og {max}
validator.size=antallet skal v�re mellem {min} og {max}
validator.email=not a well-formed email address
-validator.notEmpty=m� ikke v�re null eller tom
\ No newline at end of file
+validator.notEmpty=m� ikke v�re null eller tom
+validator.digits=numeric value out of bounds (<{integerDigits}
digits>.<{fractionalDigits} digits> expected)
\ No newline at end of file
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_de.properties
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_de.properties 2006-12-19
17:38:43 UTC (rev 11001)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_de.properties 2006-12-19
18:09:33 UTC (rev 11002)
@@ -10,4 +10,5 @@
validator.range=muss zwischen {min} und {max} sein
validator.size=muss zwischen {min} und {max} gross sein
validator.email=not a well-formed email address
-validator.notEmpty=kann nicht null oder leer sein
\ No newline at end of file
+validator.notEmpty=kann nicht null oder leer sein
+validator.digits=numeric value out of bounds (<{integerDigits}
digits>.<{fractionalDigits} digits> expected)
\ No newline at end of file
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_es.properties
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_es.properties 2006-12-19
17:38:43 UTC (rev 11001)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_es.properties 2006-12-19
18:09:33 UTC (rev 11002)
@@ -10,4 +10,5 @@
validator.range=debe ser entre {min} y {max}
validator.size=el tama�o debe ser entre {min} y {max}
validator.email=no es una direcci�n de correo electr�nico bien formada
-validator.notEmpty=no debe ser nulo o vac�o
\ No newline at end of file
+validator.notEmpty=no debe ser nulo o vac�o
+validator.digits=numeric value out of bounds (<{integerDigits}
digits>.<{fractionalDigits} digits> expected)
\ No newline at end of file
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_fr.properties
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_fr.properties 2006-12-19
17:38:43 UTC (rev 11001)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_fr.properties 2006-12-19
18:09:33 UTC (rev 11002)
@@ -10,4 +10,5 @@
validator.range=doit �tre entre {min} et {max}
validator.size=le nombre d'�l�ments doit �tre entre {min} et {max}
validator.email=Address email mal form�e
-validator.notEmpty=ne peut pas �tre nul ou vide
\ No newline at end of file
+validator.notEmpty=ne peut pas �tre nul ou vide
+validator.digits=valeur num�rique hors limite (<{integerDigits}
chiffres>.<{fractionalDigits} chiffres> attendus)
\ No newline at end of file
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_it.properties
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_it.properties 2006-12-19
17:38:43 UTC (rev 11001)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_it.properties 2006-12-19
18:09:33 UTC (rev 11002)
@@ -11,3 +11,4 @@
validator.size=le dimensioni devono essere tra {min} e {max}
validator.email=not a well-formed email address
validator.notEmpty=non pu� essere nullo o vuoto
+validator.digits=numeric value out of bounds (<{integerDigits}
digits>.<{fractionalDigits} digits> expected)
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_nl.properties
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_nl.properties 2006-12-19
17:38:43 UTC (rev 11001)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_nl.properties 2006-12-19
18:09:33 UTC (rev 11002)
@@ -10,4 +10,5 @@
validator.range=moet tussen {min} en {max} zijn
validator.size=moet tussen {min} en {max} groot zijn
validator.email=moet een geldig e-mail adres zijn
-validator.notEmpty=mag niet null of leeg zijn
\ No newline at end of file
+validator.notEmpty=mag niet null of leeg zijn
+validator.digits=numeric value out of bounds (<{integerDigits}
digits>.<{fractionalDigits} digits> expected)
\ No newline at end of file
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_pt_BR.properties
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_pt_BR.properties 2006-12-19
17:38:43 UTC (rev 11001)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_pt_BR.properties 2006-12-19
18:09:33 UTC (rev 11002)
@@ -10,4 +10,5 @@
validator.range=deve estar entre {min} e {max}
validator.size=tamanho deve estar entre {min} e {max}
validator.email=n�o � um email v�lido
-validator.notEmpty=n�o pode ser nulo ou vazio
\ No newline at end of file
+validator.notEmpty=n�o pode ser nulo ou vazio
+validator.digits=numeric value out of bounds (<{integerDigits}
digits>.<{fractionalDigits} digits> expected)
\ No newline at end of file
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_sv.properties
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_sv.properties 2006-12-19
17:38:43 UTC (rev 11001)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_sv.properties 2006-12-19
18:09:33 UTC (rev 11002)
@@ -10,4 +10,5 @@
validator.range=m�ste vara mellan {min} och {max}
validator.size=storlek m�ste vara mellan {min} och {max}
validator.email=ogiltig e-mail adress
-validator.notEmpty=f�r inte vara tomt
\ No newline at end of file
+validator.notEmpty=f�r inte vara tomt
+validator.digits=numeric value out of bounds (<{integerDigits}
digits>.<{fractionalDigits} digits> expected)
\ No newline at end of file
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_zh_CN.properties
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_zh_CN.properties 2006-12-19
17:38:43 UTC (rev 11001)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_zh_CN.properties 2006-12-19
18:09:33 UTC (rev 11002)
@@ -9,4 +9,6 @@
validator.pattern=\u5fc5\u987b\u7b26\u5408 "{regex}"
validator.range=\u5fc5\u987b\u4ecb\u4e8e {min} \u4e0e {max} \u4e4b\u95f4
validator.size=\u5927\u5c0f\u5fc5\u987b\u4ecb\u4e8e {min} \u4e0e {max} \u4e4b\u95f4
-validator.email=not a well-formed email address
\ No newline at end of file
+validator.email=not a well-formed email address
+validator.notEmpty=may not be null or empty
+validator.digits=numeric value out of bounds (<{integerDigits}
digits>.<{fractionalDigits} digits> expected)
\ No newline at end of file
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_zh_TW.properties
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_zh_TW.properties 2006-12-19
17:38:43 UTC (rev 11001)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_zh_TW.properties 2006-12-19
18:09:33 UTC (rev 11002)
@@ -9,4 +9,6 @@
validator.pattern=\u5fc5\u9808\u7b26\u5408"{regex}"
validator.range=\u5fc5\u9808\u4ecb\u65bc{min} \u8207 {max} \u4e4b\u9593
validator.size=\u5927\u5c0f\u5fc5\u9808\u4ecb\u65bc{min} \u8207 {max} \u4e4b\u9593
-validator.email=not a well-formed email address
\ No newline at end of file
+validator.email=not a well-formed email address
+validator.digits=numeric value out of bounds (<{integerDigits}
digits>.<{fractionalDigits} digits> expected)
+validator.notEmpty=may not be null or empty
\ No newline at end of file
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/validator/test/validator/Car.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/validator/test/validator/Car.java 2006-12-19
17:38:43 UTC (rev 11001)
+++
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/validator/test/validator/Car.java 2006-12-19
18:09:33 UTC (rev 11002)
@@ -1,14 +1,28 @@
//$Id: $
package org.hibernate.validator.test.validator;
+import java.math.BigDecimal;
+
+import javax.persistence.Id;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Entity;
+
import org.hibernate.validator.NotEmpty;
+import org.hibernate.validator.Digits;
/**
* @author Emmanuel Bernard
*/
+@Entity
public class Car {
+ @Id @GeneratedValue
+ public Long id;
@NotEmpty
public String name;
@NotEmpty
public String[] insurances;
+ @Digits(integerDigits = 1, fractionalDigits = 2)
+ public BigDecimal length;
+ @Digits(integerDigits = 2, fractionalDigits = 1)
+ public Double gallons;
}
Added:
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/validator/test/validator/DigitsTest.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/validator/test/validator/DigitsTest.java 2006-12-19
17:38:43 UTC (rev 11001)
+++
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/validator/test/validator/DigitsTest.java 2006-12-19
18:09:33 UTC (rev 11002)
@@ -0,0 +1,60 @@
+//$Id: $
+package org.hibernate.validator.test.validator;
+
+import java.math.BigDecimal;
+
+import org.hibernate.validator.ClassValidator;
+import org.hibernate.validator.InvalidValue;
+import org.hibernate.validator.Environment;
+import org.hibernate.validator.event.ValidatePreInsertEventListener;
+import org.hibernate.validator.event.ValidatePreUpdateEventListener;
+import org.hibernate.validator.test.Address;
+import org.hibernate.validator.test.PrefixMessageInterpolator;
+import org.hibernate.Session;
+import org.hibernate.event.PreInsertEventListener;
+import org.hibernate.event.PreUpdateEventListener;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.mapping.PersistentClass;
+import org.hibernate.mapping.Column;
+import org.hibernate.test.annotations.TestCase;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class DigitsTest extends TestCase {
+
+ public void testDigits() throws Exception {
+ Car car = new Car();
+ car.name = "350Z";
+ car.insurances = new String[] { "random" };
+ car.length = new BigDecimal(10.2);
+ car.gallons = 100.3;
+ ClassValidator<Car> classValidator = new ClassValidator<Car>( Car.class );
+ InvalidValue[] invalidValues = classValidator.getInvalidValues( car );
+ assertEquals( 2, invalidValues.length );
+ car.length = new BigDecimal(1.223); //more than 2
+ car.gallons = 10.300; //1 digit really so not invalid
+ invalidValues = classValidator.getInvalidValues( car );
+ assertEquals( 1, invalidValues.length );
+ }
+
+ public void testApply() throws Exception {
+ PersistentClass classMapping = getCfg().getClassMapping( Car.class.getName() );
+ Column stateColumn = (Column) classMapping.getProperty( "gallons"
).getColumnIterator().next();
+ assertEquals( stateColumn.getPrecision(), 3 );
+ assertEquals( stateColumn.getScale(), 1 );
+ }
+
+ protected void configure(Configuration cfg) {
+ cfg.getEventListeners()
+ .setPreInsertEventListeners( new PreInsertEventListener[]{new
ValidatePreInsertEventListener()} );
+ cfg.getEventListeners()
+ .setPreUpdateEventListeners( new PreUpdateEventListener[]{new
ValidatePreUpdateEventListener()} );
+ }
+
+ protected Class[] getMappings() {
+ return new Class[] {
+ Car.class
+ };
+ }
+}
Show replies by date