-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solving a bad test (suggesting from .it to .com at commit 69dea91), a…
…nd adding the feature to convert things like gmail.CO to gmail.COM, but ignoring transformations for other .CO domains (#16)
- Loading branch information
1 parent
01c71c1
commit 4593c31
Showing
7 changed files
with
257 additions
and
41 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
...ron/emailverifier/LastWordCorrection.java → ...cboron/emailverifier/EmailCorrection.java
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,40 @@ | ||
package at.rocboron.emailverifier; | ||
|
||
import at.rocboron.emailverifier.exception.InvalidEmailException; | ||
import at.rocboron.emailverifier.util.StringUtils; | ||
|
||
public class EmailParts { | ||
public String getTld(String email) throws InvalidEmailException { | ||
if (email == null) { | ||
throw new InvalidEmailException(); | ||
} | ||
|
||
String[] domainNameParts = getDomain(email).split("\\."); | ||
|
||
StringBuilder sb = new StringBuilder(); | ||
for (int i = 1; i <= domainNameParts.length - 1; i++) { | ||
sb.append(".").append(domainNameParts[i]); | ||
} | ||
sb.replace(0, 1, ""); | ||
return sb.toString(); | ||
} | ||
|
||
public String getDomain(String email) throws InvalidEmailException { | ||
if (email == null) { | ||
throw new InvalidEmailException(); | ||
} | ||
|
||
return email.substring(email.indexOf('@') + 1); | ||
} | ||
|
||
public String getDomainWithoutTld(String email) throws InvalidEmailException { | ||
if (email == null) { | ||
throw new InvalidEmailException(); | ||
} | ||
|
||
StringUtils stringUtils = new StringUtils(); | ||
return stringUtils.replaceLast(getDomain(email), ".".concat(getTld(email)), ""); | ||
} | ||
|
||
|
||
} |
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,7 @@ | ||
package at.rocboron.emailverifier.util; | ||
|
||
public class StringUtils { | ||
public String replaceLast(String text, String regex, String replacement) { | ||
return text.replaceFirst("(?s)(.*)" + regex, "$1" + replacement); | ||
} | ||
} |
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,106 @@ | ||
package test; | ||
|
||
import at.rocboron.emailverifier.EmailParts; | ||
import at.rocboron.emailverifier.EmailValidator; | ||
import at.rocboron.emailverifier.exception.InvalidEmailException; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static junit.framework.Assert.assertEquals; | ||
import static junit.framework.Assert.assertFalse; | ||
import static junit.framework.Assert.fail; | ||
import static junit.framework.TestCase.assertTrue; | ||
|
||
public class EmailPartsTest { | ||
|
||
EmailParts ep; | ||
|
||
@Before | ||
public void init() { | ||
ep = new EmailParts(); | ||
} | ||
|
||
@Test | ||
public void shouldReturnCom() { | ||
try { | ||
assertEquals("com", ep.getTld("[email protected]")); | ||
} catch (InvalidEmailException e) { | ||
fail(); | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldReturnAt() { | ||
try { | ||
assertEquals("at", ep.getTld("[email protected]")); | ||
} catch (InvalidEmailException e) { | ||
fail(); | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldReturnComDotUk() { | ||
try { | ||
assertEquals("co.uk", ep.getTld("[email protected]")); | ||
} catch (InvalidEmailException e) { | ||
fail(); | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldReturnFewlapsDotCom() { | ||
try { | ||
assertEquals("fewlaps.com", ep.getDomain("[email protected]")); | ||
} catch (InvalidEmailException e) { | ||
fail(); | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldReturnFewlapsDotCoDotUk() { | ||
try { | ||
assertEquals("fewlaps.co.uk", ep.getDomain("[email protected]")); | ||
} catch (InvalidEmailException e) { | ||
fail(); | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldReturnFewlaps() { | ||
try { | ||
assertEquals("fewlaps", ep.getDomainWithoutTld("[email protected]")); | ||
} catch (InvalidEmailException e) { | ||
fail(); | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldReturnRocboron() { | ||
try { | ||
assertEquals("rocboron", ep.getDomainWithoutTld("[email protected]")); | ||
} catch (InvalidEmailException e) { | ||
fail(); | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldReturnGmail() { | ||
try { | ||
assertEquals("gmail", ep.getDomainWithoutTld("[email protected]")); | ||
} catch (InvalidEmailException e) { | ||
fail(); | ||
} | ||
} | ||
|
||
/** | ||
* Key test with IT, 'cause "hitmail" contains "it", the TLD | ||
*/ | ||
@Test | ||
public void shouldReturnHitmail() { | ||
try { | ||
assertEquals("hitmail", ep.getDomainWithoutTld("[email protected]")); | ||
} catch (InvalidEmailException e) { | ||
fail(); | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -151,6 +151,15 @@ public void shouldFixGmailDotCoIssue() { | |
} | ||
} | ||
|
||
@Test | ||
public void shouldNotDoNothingWithYahooDotCo() { | ||
try { | ||
assertEquals("[email protected]", es.getSuggestedEmail("[email protected]")); | ||
} catch (Exception e) { | ||
fail(); | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldFixGmailDotCommIssue() { | ||
try { | ||
|
@@ -242,9 +251,9 @@ public void shouldFixHitmailDotComIssue() { | |
} | ||
|
||
@Test | ||
public void shouldFixHitmailDotComIt() { | ||
public void shouldFixHitmailDotItIssue() { | ||
try { | ||
assertEquals("roc@hotmail.com", es.getSuggestedEmail("[email protected]")); | ||
assertEquals("roc@hotmail.it", es.getSuggestedEmail("[email protected]")); | ||
} catch (Exception e) { | ||
fail(); | ||
} | ||
|
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,30 @@ | ||
package test; | ||
|
||
import at.rocboron.emailverifier.EmailValidator; | ||
import at.rocboron.emailverifier.util.StringUtils; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static junit.framework.Assert.assertEquals; | ||
import static junit.framework.Assert.assertFalse; | ||
import static junit.framework.TestCase.assertTrue; | ||
|
||
public class StringUtilsTest { | ||
|
||
StringUtils su; | ||
|
||
@Before | ||
public void init() { | ||
su = new StringUtils(); | ||
} | ||
|
||
@Test | ||
public void shouldReturnHeo() { | ||
assertEquals("heo", su.replaceLast("hello", "ll", "")); | ||
} | ||
|
||
@Test | ||
public void shouldReturnAaabxx() { | ||
assertEquals("aaabxx", su.replaceLast("aaabbb", "bb", "xx")); | ||
} | ||
} |