Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

HV-472 Allow IDNs in emails #102

Merged
merged 3 commits into from

2 participants

This page is out of date. Refresh to see the latest.
View
7 ...nate-validator/src/main/java/org/hibernate/validator/constraints/impl/EmailValidator.java
@@ -1,5 +1,6 @@
package org.hibernate.validator.constraints.impl;
+import java.net.IDN;
import java.util.regex.Matcher;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
@@ -17,6 +18,9 @@
* 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>
+ * <p>
+ * Note: This validator required Java 6, because it uses {@code java.lang.IDN} to support internationalized domain names (IDNs).
+ * </p>
*
* @author Emmanuel Bernard
* @author Hardy Ferentschik
@@ -42,7 +46,8 @@ public boolean isValid(CharSequence value, ConstraintValidatorContext context) {
if ( value == null || value.length() == 0 ) {
return true;
}
- Matcher m = pattern.matcher( value );
+ String asciiString = IDN.toASCII( value.toString() );
+ Matcher m = pattern.matcher( asciiString );
return m.matches();
}
}
View
84 ...dator/src/test/java/org/hibernate/validator/test/constraints/impl/EmailValidatorTest.java
@@ -39,61 +39,77 @@ public static void init() {
@Test
public void testNullAndEmptyString() throws Exception {
- isRightEmail( "" );
- isRightEmail( null );
+ isValidEmail( "" );
+ isValidEmail( null );
}
@Test
public void testValidEmail() throws Exception {
- isRightEmail( "emmanuel@hibernate.org" );
- isRightEmail( "emmanuel@hibernate" );
- isRightEmail( "emma-n_uel@hibernate" );
- isRightEmail( "emma+nuel@hibernate.org" );
- isRightEmail( "emma=nuel@hibernate.org" );
- isRightEmail( "emmanuel@[123.12.2.11]" );
- isRightEmail( "*@example.net" );
- isRightEmail( "fred&barny@example.com" );
- isRightEmail( "---@example.com" );
- isRightEmail( "foo-bar@example.net" );
- isRightEmail( "mailbox.sub1.sub2@this-domain" );
+ isValidEmail( "emmanuel@hibernate.org" );
+ isValidEmail( "emmanuel@hibernate" );
+ isValidEmail( "emma-n_uel@hibernate" );
+ isValidEmail( "emma+nuel@hibernate.org" );
+ isValidEmail( "emma=nuel@hibernate.org" );
+ isValidEmail( "emmanuel@[123.12.2.11]" );
+ isValidEmail( "*@example.net" );
+ isValidEmail( "fred&barny@example.com" );
+ isValidEmail( "---@example.com" );
+ isValidEmail( "foo-bar@example.net" );
+ isValidEmail( "mailbox.sub1.sub2@this-domain" );
}
@Test
public void testInValidEmail() throws Exception {
- isWrongEmail( "emmanuel.hibernate.org" );
- isWrongEmail( "emma nuel@hibernate.org" );
- isWrongEmail( "emma(nuel@hibernate.org" );
- isWrongEmail( "emmanuel@" );
- isWrongEmail( "emma\nnuel@hibernate.org" );
- isWrongEmail( "emma@nuel@hibernate.org" );
- isWrongEmail( "Just a string" );
- isWrongEmail( "string" );
- isWrongEmail( "me@" );
- isWrongEmail( "@example.com" );
- isWrongEmail( "me.@example.com" );
- isWrongEmail( ".me@example.com" );
- isWrongEmail( "me@example..com" );
- isWrongEmail( "me\\@example.com" );
+ isInvalidEmail( "emmanuel.hibernate.org" );
+ isInvalidEmail( "emma nuel@hibernate.org" );
+ isInvalidEmail( "emma(nuel@hibernate.org" );
+ isInvalidEmail( "emmanuel@" );
+ isInvalidEmail( "emma\nnuel@hibernate.org" );
+ isInvalidEmail( "emma@nuel@hibernate.org" );
+ isInvalidEmail( "Just a string" );
+ isInvalidEmail( "string" );
+ isInvalidEmail( "me@" );
+ isInvalidEmail( "@example.com" );
+ isInvalidEmail( "me.@example.com" );
+ isInvalidEmail( ".me@example.com" );
+ isInvalidEmail( "me@example..com" );
+ isInvalidEmail( "me\\@example.com" );
}
@Test
@TestForIssue(jiraKey = "HV-339")
public void testAccent() {
- isRightEmail( "Test^Email@example.com" );
+ isValidEmail( "Test^Email@example.com" );
}
@Test
@TestForIssue(jiraKey = "HV-502")
public void testValidEmailCharSequence() throws Exception {
- isRightEmail( new MyCustomStringImpl( "emmanuel@hibernate.org" ) );
- isWrongEmail( new MyCustomStringImpl( "@example.com" ) );
+ isValidEmail( new MyCustomStringImpl( "emmanuel@hibernate.org" ) );
+ isInvalidEmail( new MyCustomStringImpl( "@example.com" ) );
}
- private void isRightEmail(CharSequence email) {
- assertTrue( validator.isValid( email, null ), "Expected a valid email." );
+ @Test
+ @TestForIssue(jiraKey = "HV-472")
+ public void testMailWithInternationalDomainName() throws Exception {
+ isValidEmail( "myname@östereich.at", "A valid email address with umlaut" );
+ isValidEmail( "θσερ@εχαμπλε.ψομ", "A valid greek email address" );
+ isInvalidEmail( "θσερ.εχαμπλε.ψομ", "Email does not contain an @ character and should be invalid" );
+ }
+
+ private void isValidEmail(CharSequence email, String message) {
+ assertTrue( validator.isValid( email, null ), message );
+ }
+
+ private void isValidEmail(CharSequence email) {
+ isValidEmail( email, "Expected a valid email." );
+ }
+
+ private void isInvalidEmail(CharSequence email, String message) {
+ assertFalse( validator.isValid( email, null ), message );
}
- private void isWrongEmail(CharSequence email) {
- assertFalse( validator.isValid( email, null ), "Expected a invalid email." );
+ private void isInvalidEmail(CharSequence email) {
+ isInvalidEmail( email, "Expected a invalid email." );
}
}
View
30 pom.xml
@@ -185,7 +185,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
- <version>1.0-beta-1</version>
+ <version>1.0.1</version>
<executions>
<execution>
<id>enforce-java</id>
@@ -206,7 +206,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
- <version>2.0</version>
+ <version>2.2.2</version>
<configuration>
<goals>deploy</goals>
</configuration>
@@ -217,7 +217,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
- <version>2.3.2</version>
+ <version>2.4</version>
<configuration>
<archive>
<manifestEntries>
@@ -242,7 +242,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
- <version>2.11</version>
+ <version>2.12</version>
<configuration>
<forkMode>once</forkMode>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
@@ -254,7 +254,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
- <version>2.11</version>
+ <version>2.12</version>
<executions>
<execution>
<id>generate-test-report</id>
@@ -272,12 +272,12 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
- <version>2.11</version>
+ <version>2.12</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
- <version>2.1.1</version>
+ <version>2.1.2</version>
<executions>
<execution>
<id>attach-sources</id>
@@ -290,17 +290,17 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
- <version>2.2</version>
+ <version>2.4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
- <version>2.2</version>
+ <version>2.3</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
- <version>2.0</version>
+ <version>2.2.2</version>
<configuration>
<preparationGoals>clean install</preparationGoals>
<autoVersionSubmodules>true</autoVersionSubmodules>
@@ -310,12 +310,12 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
- <version>2.5</version>
+ <version>2.7</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
- <version>1.1.1</version>
+ <version>1.2.1</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
@@ -344,12 +344,12 @@
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
- <version>2.0.1</version>
+ <version>2.3.6</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-archetype-plugin</artifactId>
- <version>2.0-alpha-5</version>
+ <version>2.2</version>
<executions>
<execution>
<id>generate-archetype-install</id>
@@ -378,7 +378,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
- <version>2.7</version>
+ <version>2.8.1</version>
<configuration>
<docfilessubdirs>true</docfilessubdirs>
<javadocDirectory>${project.basedir}/../src/main/javadoc</javadocDirectory>
Something went wrong with that request. Please try again.