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(a)hibernate.org" );
+ public void testNullAndEmptyString() throws Exception {
isRightEmail( "" );
isRightEmail( null );
+ }
+
+ @Test
+ public void testValidEmail() throws Exception {
+ isRightEmail( "emmanuel(a)hibernate.org" );
isRightEmail( "emmanuel@hibernate" );
isRightEmail( "emma-n_uel@hibernate" );
isRightEmail( "emma+nuel(a)hibernate.org" );
isRightEmail( "emma=nuel(a)hibernate.org" );
isRightEmail( "emmanuel(a)[123.12.2.11]" );
+ isRightEmail( "*(a)example.net" );
+ isRightEmail( "fred&barny(a)example.com" );
+ isRightEmail( "---(a)example.com" );
+ isRightEmail( "foo-bar(a)example.net" );
+ isRightEmail( "mailbox.sub1.sub2@this-domain" );
+ }
+
+ @Test
+ public void testInValidEmail() throws Exception {
isWrongEmail( "emmanuel.hibernate.org" );
isWrongEmail( "emma nuel(a)hibernate.org" );
isWrongEmail( "emma(nuel(a)hibernate.org" );
isWrongEmail( "emmanuel@" );
isWrongEmail( "emma\nnuel(a)hibernate.org" );
isWrongEmail( "emma@nuel(a)hibernate.org" );
+ isWrongEmail( "Just a string" );
+ isWrongEmail( "string" );
+ isWrongEmail( "me@");
+ isWrongEmail( "(a)example.com");
+ isWrongEmail( "me.(a)example.com");
+ isWrongEmail( ".me(a)example.com");
+ isWrongEmail( "me@example..com");
+ isWrongEmail( "me\\(a)example.com");
}
+ /**
+ * HV-339
+ */
+ @Test
+ public void testAccent() {
+ isRightEmail( "Test^Email(a)example.com" );
+ }
+
private void isRightEmail(String email) {
assertTrue( validator.isValid( email, null ), "Expected a valid email." );
}