[hibernate-commits] Hibernate SVN: r17582 - in validator/trunk/hibernate-validator/src: main/java/org/hibernate/validator/constraints/impl and 3 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Wed Sep 30 09:09:41 EDT 2009


Author: hardy.ferentschik
Date: 2009-09-30 09:09:40 -0400 (Wed, 30 Sep 2009)
New Revision: 17582

Added:
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/Email.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/Range.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/EmailValidator.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/ConstraintTest.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/Elevator.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/impl/EmailValidatorTest.java
Modified:
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/Length.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/NotEmpty.java
   validator/trunk/hibernate-validator/src/main/resources/org/hibernate/validator/ValidationMessages.properties
   validator/trunk/hibernate-validator/src/main/resources/org/hibernate/validator/ValidationMessages_de.properties
   validator/trunk/hibernate-validator/src/main/resources/org/hibernate/validator/ValidationMessages_fr.properties
Log:
HV-241 poerted @Email and @Range from the legacy codebase

Copied: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/Email.java (from rev 17580, validator/trunk/hibernate-validator-legacy/src/main/java/org/hibernate/validator/Email.java)
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/Email.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/Email.java	2009-09-30 13:09:40 UTC (rev 17582)
@@ -0,0 +1,50 @@
+//$Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, 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.constraints;
+
+import java.lang.annotation.Documented;
+import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
+import static java.lang.annotation.ElementType.CONSTRUCTOR;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.PARAMETER;
+import java.lang.annotation.Retention;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import java.lang.annotation.Target;
+import javax.validation.Constraint;
+import javax.validation.Payload;
+
+import org.hibernate.validator.constraints.impl.EmailValidator;
+
+/**
+ * The string has to be a well-formed email address.
+ *
+ * @author Emmanuel Bernard
+ * @author Hardy Ferentschik
+ */
+ at Documented
+ at Constraint(validatedBy = EmailValidator.class)
+ at Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
+ at Retention(RUNTIME)
+public @interface Email {
+	String message() default "{org.hibernate.validator.constraints.Email.message}";
+
+	Class<?>[] groups() default { };
+
+	Class<? extends Payload>[] payload() default { };
+}
\ No newline at end of file

Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/Length.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/Length.java	2009-09-30 13:06:48 UTC (rev 17581)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/Length.java	2009-09-30 13:09:40 UTC (rev 17582)
@@ -21,6 +21,9 @@
 import static java.lang.annotation.ElementType.FIELD;
 import static java.lang.annotation.ElementType.METHOD;
 import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
+import static java.lang.annotation.ElementType.CONSTRUCTOR;
+import static java.lang.annotation.ElementType.PARAMETER;
 import java.lang.annotation.Retention;
 import static java.lang.annotation.RetentionPolicy.RUNTIME;
 import java.lang.annotation.Target;
@@ -30,14 +33,14 @@
 import org.hibernate.validator.constraints.impl.LengthValidator;
 
 /**
- * Validate that the string is between min and max included
+ * Validate that the string is between min and max included.
  *
  * @author Emmanuel Bernard
  * @author Hardy Ferentschik
  */
 @Documented
 @Constraint(validatedBy = LengthValidator.class)
- at Target({ METHOD, FIELD, TYPE })
+ at Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
 @Retention(RUNTIME)
 public @interface Length {
 	int min() default 0;

Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/NotEmpty.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/NotEmpty.java	2009-09-30 13:06:48 UTC (rev 17581)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/NotEmpty.java	2009-09-30 13:09:40 UTC (rev 17582)
@@ -20,6 +20,9 @@
 import java.lang.annotation.Documented;
 import static java.lang.annotation.ElementType.FIELD;
 import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
+import static java.lang.annotation.ElementType.CONSTRUCTOR;
+import static java.lang.annotation.ElementType.PARAMETER;
 import java.lang.annotation.Retention;
 import static java.lang.annotation.RetentionPolicy.RUNTIME;
 import java.lang.annotation.Target;
@@ -34,7 +37,7 @@
  */
 @Documented
 @Constraint(validatedBy = { })
- at Target({ METHOD, FIELD })
+ at Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
 @Retention(RUNTIME)
 @ReportAsSingleViolation
 @NotNull

Copied: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/Range.java (from rev 17580, validator/trunk/hibernate-validator-legacy/src/main/java/org/hibernate/validator/Range.java)
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/Range.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/Range.java	2009-09-30 13:09:40 UTC (rev 17582)
@@ -0,0 +1,61 @@
+//$Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, 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.constraints;
+
+import java.lang.annotation.Documented;
+import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
+import static java.lang.annotation.ElementType.CONSTRUCTOR;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.PARAMETER;
+import java.lang.annotation.Retention;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import java.lang.annotation.Target;
+import javax.validation.Constraint;
+import javax.validation.OverridesAttribute;
+import javax.validation.Payload;
+import javax.validation.ReportAsSingleViolation;
+import javax.validation.constraints.Max;
+import javax.validation.constraints.Min;
+
+/**
+ * The annotated element has to be in the appropriate range. Apply on numeric values or string
+ * representation of the numeric value.
+ *
+ * @author Hardy Ferentschik
+ */
+ at Documented
+ at Constraint(validatedBy = { })
+ at Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
+ at Retention(RUNTIME)
+ at Min(0)
+ at Max(Long.MAX_VALUE)
+ at ReportAsSingleViolation
+public @interface Range {
+	@OverridesAttribute(constraint = Min.class, name = "value")
+	long min() default 0;
+
+	@OverridesAttribute(constraint = Max.class, name = "value")
+	long max() default Long.MAX_VALUE;
+
+	String message() default "{org.hibernate.validator.constraints.Range.message}";
+
+	Class<?>[] groups() default { };
+
+	Class<? extends Payload>[] payload() default { };
+}
\ No newline at end of file

Copied: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/EmailValidator.java (from rev 17580, validator/trunk/hibernate-validator-legacy/src/main/java/org/hibernate/validator/EmailValidator.java)
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/EmailValidator.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/EmailValidator.java	2009-09-30 13:09:40 UTC (rev 17582)
@@ -0,0 +1,40 @@
+//$Id$
+package org.hibernate.validator.constraints.impl;
+
+import java.util.regex.Matcher;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+import org.hibernate.validator.constraints.Email;
+
+/**
+ * Check that a given string is a well-formed email address.
+ *
+ * @author Emmanuel Bernard
+ */
+public class EmailValidator implements ConstraintValidator<Email, String> {
+	//TODO: Implement this http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html regex in java
+	private static String ATOM = "[^\\x00-\\x1F^\\(^\\)^\\<^\\>^\\@^\\,^\\;^\\:^\\\\^\\\"^\\.^\\[^\\]^\\s]";
+	private static String DOMAIN = "(" + ATOM + "+(\\." + ATOM + "+)*";
+	private static String IP_DOMAIN = "\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\]";
+
+	private java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(
+			"^" + ATOM + "+(\\." + ATOM + "+)*@"
+					+ DOMAIN
+					+ "|"
+					+ IP_DOMAIN
+					+ ")$",
+			java.util.regex.Pattern.CASE_INSENSITIVE
+	);
+
+	public void initialize(Email annotation) {
+	}
+
+	public boolean isValid(String value, ConstraintValidatorContext context) {
+		if ( value == null || value.length() == 0 ) {
+			return true;
+		}
+		Matcher m = pattern.matcher( value );
+		return m.matches();
+	}
+}
\ No newline at end of file

Modified: validator/trunk/hibernate-validator/src/main/resources/org/hibernate/validator/ValidationMessages.properties
===================================================================
--- validator/trunk/hibernate-validator/src/main/resources/org/hibernate/validator/ValidationMessages.properties	2009-09-30 13:06:48 UTC (rev 17581)
+++ validator/trunk/hibernate-validator/src/main/resources/org/hibernate/validator/ValidationMessages.properties	2009-09-30 13:09:40 UTC (rev 17582)
@@ -12,6 +12,8 @@
 javax.validation.constraints.Past.message=must be in the past
 javax.validation.constraints.Pattern.message=must match "{regexp}"
 javax.validation.constraints.Size.message=size must be between {min} and {max}
+org.hibernate.validator.constraints.Email.message="{value}" is not a valid email address
 org.hibernate.validator.constraints.Length.message=length must be between {min} and {max}
 org.hibernate.validator.constraints.NotEmpty.message=may not be empty
+org.hibernate.validator.constraints.Range.message={value} must be between {min} and {max}
 

Modified: validator/trunk/hibernate-validator/src/main/resources/org/hibernate/validator/ValidationMessages_de.properties
===================================================================
--- validator/trunk/hibernate-validator/src/main/resources/org/hibernate/validator/ValidationMessages_de.properties	2009-09-30 13:06:48 UTC (rev 17581)
+++ validator/trunk/hibernate-validator/src/main/resources/org/hibernate/validator/ValidationMessages_de.properties	2009-09-30 13:09:40 UTC (rev 17582)
@@ -11,4 +11,8 @@
 javax.validation.constraints.Future.message=muss in der Zukunft liegen
 javax.validation.constraints.AssertTrue.message=muss wahr sein
 javax.validation.constraints.AssertFalse.message=muss falsch sein
-javax.validation.constraints.Digits.message=numerischer Wert aus\u00DFerhalb erlaubten Wertebereichs (<{integer} Ziffern>.<{fraction} Ziffern> erwarted)
\ No newline at end of file
+javax.validation.constraints.Digits.message=numerischer Wert au\u00DFerhalb erlaubten Wertebereichs (<{integer} Ziffern>.<{fraction} Ziffern> erwarted)
+javax.validation.constraints.DecimalMin.message=muss gr\u00F6ssergleich {value} sein
+javax.validation.constraints.DecimalMax.message=muss kleinergleich {value} sein
+org.hibernate.validator.constraints.Email.message="{value}" ist keine g\u00FCltige E-Mail-Adresse
+org.hibernate.validator.constraints.Range.message={value} muss zwischen {min} und {max} liegen
\ No newline at end of file

Modified: validator/trunk/hibernate-validator/src/main/resources/org/hibernate/validator/ValidationMessages_fr.properties
===================================================================
--- validator/trunk/hibernate-validator/src/main/resources/org/hibernate/validator/ValidationMessages_fr.properties	2009-09-30 13:06:48 UTC (rev 17581)
+++ validator/trunk/hibernate-validator/src/main/resources/org/hibernate/validator/ValidationMessages_fr.properties	2009-09-30 13:09:40 UTC (rev 17582)
@@ -12,3 +12,7 @@
 javax.validation.constraints.AssertTrue.message=doit \u00EAtre vrai
 javax.validation.constraints.AssertFalse.message=doit \u00EAtre faux
 javax.validation.constraints.Digits.message=Valeur num\u00E9rique hors limite (<{integer} chiffres>.<{fraction} chiffres> attendus)
+javax.validation.constraints.DecimalMin.message=doit \u00EAtre plus grand que {value}
+javax.validation.constraints.DecimalMax.message=doit \u00EAtre plus petit que {value}
+org.hibernate.validator.constraints.Email.message=Address email "{value}" mal formée
+org.hibernate.validator.constraints.Range.message={value} doit être entre {min} et {max}
\ No newline at end of file

Copied: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/ConstraintTest.java (from rev 17580, validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/ValidatorResolutionTest.java)
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/ConstraintTest.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/ConstraintTest.java	2009-09-30 13:09:40 UTC (rev 17582)
@@ -0,0 +1,67 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, 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.constraints;
+
+import java.util.Set;
+import javax.validation.ConstraintViolation;
+import javax.validation.Validator;
+
+import org.testng.annotations.Test;
+
+import org.hibernate.validator.util.TestUtil;
+import static org.hibernate.validator.util.TestUtil.assertConstraintViolation;
+import static org.hibernate.validator.util.TestUtil.assertNumberOfViolations;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class ConstraintTest {
+
+	@Test
+	public void testRangeConstraint() {
+		Validator validator = TestUtil.getValidator();
+
+		Elevator elevator = new Elevator();
+		elevator.setCurrentFloor( -3 );
+		Set<ConstraintViolation<Elevator>> constraintViolations = validator.validate( elevator );
+
+		assertNumberOfViolations( constraintViolations, 1 );
+		assertConstraintViolation( constraintViolations.iterator().next(), "Invalid floor" );
+
+		elevator.setCurrentFloor( -2 );
+		constraintViolations = validator.validate( elevator );
+
+		assertNumberOfViolations( constraintViolations, 0 );
+
+		elevator.setCurrentFloor( 45 );
+		constraintViolations = validator.validate( elevator );
+
+		assertNumberOfViolations( constraintViolations, 0 );
+
+		elevator.setCurrentFloor( 50 );
+		constraintViolations = validator.validate( elevator );
+
+		assertNumberOfViolations( constraintViolations, 0 );
+
+		elevator.setCurrentFloor( 51 );
+		constraintViolations = validator.validate( elevator );
+
+		assertNumberOfViolations( constraintViolations, 1 );
+		assertConstraintViolation( constraintViolations.iterator().next(), "Invalid floor" );
+	}
+}

Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/Elevator.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/Elevator.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/Elevator.java	2009-09-30 13:09:40 UTC (rev 17582)
@@ -0,0 +1,35 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, 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.constraints;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class Elevator {
+
+	@Range(min = -2, max = 50, message = "Invalid floor")
+	private int currentFloor;
+
+	public int getCurrentFloor() {
+		return currentFloor;
+	}
+
+	public void setCurrentFloor(int currentFloor) {
+		this.currentFloor = currentFloor;
+	}
+}


Property changes on: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/Elevator.java
___________________________________________________________________
Name: svn:keywords
   + Id

Copied: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/impl/EmailValidatorTest.java (from rev 17580, validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/impl/AssertFalseValidatorTest.java)
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/impl/EmailValidatorTest.java	                        (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/constraints/impl/EmailValidatorTest.java	2009-09-30 13:09:40 UTC (rev 17582)
@@ -0,0 +1,62 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, 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.constraints.impl;
+
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class EmailValidatorTest {
+
+	private static EmailValidator validator;
+
+	@BeforeClass
+	public static void init() {
+		validator = new EmailValidator();
+	}
+
+	@Test
+	public void testEmail() throws Exception {
+		isRightEmail( "emmanuel at hibernate.org" );
+		isRightEmail( "" );
+		isRightEmail( null );
+		isRightEmail( "emmanuel at hibernate" );
+		isRightEmail( "emma-n_uel at hibernate" );
+		isRightEmail( "emma+nuel at hibernate.org" );
+		isRightEmail( "emma=nuel at hibernate.org" );
+		isRightEmail( "emmanuel@[123.12.2.11]" );
+		isWrongEmail( "emmanuel.hibernate.org" );
+		isWrongEmail( "emma nuel at hibernate.org" );
+		isWrongEmail( "emma(nuel at hibernate.org" );
+		isWrongEmail( "emmanuel@" );
+		isWrongEmail( "emma\nnuel at hibernate.org" );
+		isWrongEmail( "emma at nuel@hibernate.org" );
+	}
+
+	private void isRightEmail(String email) {
+		assertTrue( validator.isValid( email, null ), "Expected a valid email." );
+	}
+
+	private void isWrongEmail(String email) {
+		assertFalse( validator.isValid( email, null ), "Expected a invalid email." );
+	}
+}
\ No newline at end of file



More information about the hibernate-commits mailing list