-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2564 from SCADA-LTS/fix/#623_Fixed_email_validati…
…on_in_users_details Fix/#623 fixed email validation in users details
- Loading branch information
Showing
4 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.serotonin.mango.util; | ||
|
||
import org.scada_lts.serorepl.utils.StringUtils; | ||
|
||
import java.util.regex.Pattern; | ||
import java.util.regex.Matcher; | ||
public class EmailValidator { | ||
private static final String EMAIL_PATTERN = | ||
"(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])"; | ||
|
||
public static boolean isValidEmail(String email) { | ||
if(StringUtils.isEmpty(email)) | ||
return false; | ||
Pattern pattern = Pattern.compile(EMAIL_PATTERN); | ||
Matcher matcher = pattern.matcher(email.toLowerCase()); | ||
return matcher.matches(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.serotonin.mango.util; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
@RunWith(Parameterized.class) | ||
public class EmailValidatorTest { | ||
|
||
@Parameterized.Parameters(name= "{index}: email: {0}, valid: {1}") | ||
public static Object[][] data() { | ||
return new Object[][] { | ||
new Object[] {"", false}, | ||
new Object[] {"\t", false}, | ||
new Object[] {" ", false}, | ||
new Object[] {null, false}, | ||
new Object[] {"Email@com.com", true}, | ||
new Object[] {"email@Com.co", true}, | ||
new Object[] {"email@com.c", true}, | ||
new Object[] {"email@com.", false}, | ||
new Object[] {"email@co.com", true}, | ||
new Object[] {"email@c.com", true}, | ||
new Object[] {"email@.com", false}, | ||
new Object[] {"emailcom.com", false}, | ||
new Object[] {"email@@.cm", false}, | ||
new Object[] {"e@mail@com.com", false}, | ||
new Object[] {"@email@com.com", false}, | ||
new Object[] {"emai@l@com.com", false}, | ||
new Object[] {"email@com.c@om", false}, | ||
new Object[] {"email@com.@com", false}, | ||
new Object[] {"email@com@.com", false}, | ||
new Object[] {"email@com.com@", false}, | ||
new Object[] {"email@dewa.c", true}, | ||
new Object[] {"email@@@a.om", false}, | ||
new Object[] {"email@ac.cm", true}, | ||
new Object[] {"ertyu@ac.cm", true}, | ||
new Object[] {"e@ac.cm", true}, | ||
new Object[] {"@ac.cm", false}, | ||
new Object[] {"ac.cm", false}, | ||
new Object[] {"ac.@cm", false}, | ||
new Object[] {"test2@test", false}, | ||
new Object[] {"Email@com.com", true}, | ||
new Object[] {"Email@Com.com", true}, | ||
new Object[] {"Email@com.Com", true}, | ||
new Object[] {"eMAil@com.com", true}, | ||
}; | ||
} | ||
|
||
private final String email; | ||
private final boolean isvalid; | ||
|
||
public EmailValidatorTest(String email, boolean isvalid) { | ||
this.email = email; | ||
this.isvalid = isvalid; | ||
} | ||
|
||
@Test | ||
public void when_isValidEmail() { | ||
//when: | ||
boolean result = EmailValidator.isValidEmail(email); | ||
//then: | ||
Assert.assertEquals(isvalid, result); | ||
} | ||
} |