[hibernate-commits] Hibernate SVN: r19796 - in validator/trunk/hibernate-validator/src: test/java/org/hibernate/validator/test/constraints/impl and 1 other directory.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Wed Jun 23 07:23:08 EDT 2010


Author: hardy.ferentschik
Date: 2010-06-23 07:23:07 -0400 (Wed, 23 Jun 2010)
New Revision: 19796

Modified:
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/EmailValidator.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/constraints/impl/EmailValidatorTest.java
Log:
HV-339 Updated the ATOM regular expression to explicitly include allowed characters. The old expression tried to exclude characters which were not allowed. In my opinion the RFC is contradicting itself in the 'atext' definition.

Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/EmailValidator.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/EmailValidator.java	2010-06-23 07:36:32 UTC (rev 19795)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/EmailValidator.java	2010-06-23 11:23:07 UTC (rev 19796)
@@ -8,13 +8,22 @@
 import org.hibernate.validator.constraints.Email;
 
 /**
- * Check that a given string is a well-formed email address.
+ * Checks that a given string is a well-formed email address.
+ * <p>
+ * The specification of a valid email can be found in
+ * <a href="http://www.faqs.org/rfcs/rfc2822.html">RFC 2822</a>
+ * and one can come up with a regular expression matching <a href="http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html">
+ * all valid email addresses</a> as per specification. However, as this
+ * <a href="http://www.regular-expressions.info/email.html">article</a> discusses it is not necessarily practical to
+ * implement a 100% compliant email validator. This implementation is a trade-off trying to match most email while ignoring
+ * for example emails with double quotes or comments.
+ * </p>
  *
  * @author Emmanuel Bernard
+ * @author Hardy Ferentschik
  */
 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 ATOM = "[a-z0-9!#$%&'*+/=?^_`{|}~-]";
 	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}\\]";
 

Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/constraints/impl/EmailValidatorTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/constraints/impl/EmailValidatorTest.java	2010-06-23 07:36:32 UTC (rev 19795)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/constraints/impl/EmailValidatorTest.java	2010-06-23 11:23:07 UTC (rev 19796)
@@ -17,13 +17,14 @@
 */
 package org.hibernate.validator.test.constraints.impl;
 
-import static org.testng.Assert.assertFalse;
-import static org.testng.Assert.assertTrue;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
 import org.hibernate.validator.constraints.impl.EmailValidator;
 
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+
 /**
  * @author Hardy Ferentschik
  */
@@ -37,23 +38,52 @@
 	}
 
 	@Test
-	public void testEmail() throws Exception {
-		isRightEmail( "emmanuel at hibernate.org" );
+	public void testNullAndEmptyString() throws Exception {
 		isRightEmail( "" );
 		isRightEmail( null );
+	}
+
+	@Test
+	public void testValidEmail() throws Exception {
+		isRightEmail( "emmanuel at hibernate.org" );
 		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]" );
+		isRightEmail( "*@example.net" );
+		isRightEmail( "fred&barny at example.com" );
+		isRightEmail( "--- at example.com" );
+		isRightEmail( "foo-bar at example.net" );
+		isRightEmail( "mailbox.sub1.sub2 at this-domain" );
+	}
+
+	@Test
+	public void testInValidEmail() throws Exception {
 		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" );
+		isWrongEmail( "Just a string" );
+		isWrongEmail( "string" );
+		isWrongEmail( "me@");
+		isWrongEmail( "@example.com");
+		isWrongEmail( "me. at example.com");
+		isWrongEmail( ".me at example.com");
+		isWrongEmail( "me at example..com");
+		isWrongEmail( "me\\@example.com");
 	}
 
+	/**
+	 * HV-339
+	 */
+	@Test
+	public void testAccent() {
+		isRightEmail( "Test^Email at example.com" );
+	}
+
 	private void isRightEmail(String email) {
 		assertTrue( validator.isValid( email, null ), "Expected a valid email." );
 	}



More information about the hibernate-commits mailing list