Skip to content

Commit

Permalink
Solving a bad test (suggesting from .it to .com at commit 69dea91), a…
Browse files Browse the repository at this point in the history
…nd adding the feature to convert things like gmail.CO to gmail.COM, but ignoring transformations for other .CO domains (#16)
  • Loading branch information
rocboronat committed Jul 15, 2015
1 parent 01c71c1 commit 4593c31
Show file tree
Hide file tree
Showing 7 changed files with 257 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package at.rocboron.emailverifier;

public class LastWordCorrection {
public class EmailCorrection {
private String badEnd;
private String goodEnd;

public LastWordCorrection(String badEnd, String goodEnd) {
public EmailCorrection(String badEnd, String goodEnd) {
this.badEnd = badEnd;
this.goodEnd = goodEnd;
}
Expand Down
40 changes: 40 additions & 0 deletions src/at/rocboron/emailverifier/EmailParts.java
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)), "");
}


}
98 changes: 61 additions & 37 deletions src/at/rocboron/emailverifier/EmailSuggester.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,86 @@
import at.rocboron.emailverifier.exception.InvalidEmailException;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmailSuggester {

public static final String YAHOO = "yahoo.com";
public static final String GMAIL = "gmail.com";
public static final String HOTMAIL = "hotmail.com";
public static final String YAHOO = "yahoo";
public static final String GMAIL = "gmail";
public static final String HOTMAIL = "hotmail";

public static final String DOTCOM = ".com";

public List<LastWordCorrection> lastWordCorrections = Arrays.asList(
new LastWordCorrection(".cpm", DOTCOM),
new LastWordCorrection(".cpn", DOTCOM),
new LastWordCorrection(".con", DOTCOM),
new LastWordCorrection(".col", DOTCOM),
new LastWordCorrection(".comm", DOTCOM),

new LastWordCorrection("gnail.com", GMAIL),
new LastWordCorrection("gmial.com", GMAIL),
new LastWordCorrection("gmaail.com", GMAIL),
new LastWordCorrection("gnail.com", GMAIL),
new LastWordCorrection("gamil.com", GMAIL),
new LastWordCorrection("gmal.com", GMAIL),
new LastWordCorrection("ygmail.com", GMAIL),
new LastWordCorrection("gmail.co", GMAIL),
new LastWordCorrection("gmail.cxom", GMAIL),

new LastWordCorrection("hotmaail.com", HOTMAIL),
new LastWordCorrection("hotmal.com", HOTMAIL),
new LastWordCorrection("hotmai.com", HOTMAIL),
new LastWordCorrection("hotmali.com", HOTMAIL),
new LastWordCorrection("hitmail.com", HOTMAIL),
new LastWordCorrection("hitmail.it", HOTMAIL),

new LastWordCorrection("yaho.com", YAHOO),
new LastWordCorrection("yaboo.com", YAHOO)
public List<EmailCorrection> tldCorrections = Arrays.asList(
new EmailCorrection(".cpm", DOTCOM),
new EmailCorrection(".cpn", DOTCOM),
new EmailCorrection(".con", DOTCOM),
new EmailCorrection(".col", DOTCOM),
new EmailCorrection(".comm", DOTCOM),
new EmailCorrection(".cxom", DOTCOM)
);

public List<EmailCorrection> domainCorrections = Arrays.asList(
new EmailCorrection("gnail", GMAIL),
new EmailCorrection("gmial", GMAIL),
new EmailCorrection("gmaail", GMAIL),
new EmailCorrection("gnail", GMAIL),
new EmailCorrection("gamil", GMAIL),
new EmailCorrection("gmal", GMAIL),
new EmailCorrection("ygmail", GMAIL),
new EmailCorrection("gmail", GMAIL),

new EmailCorrection("hotmaail", HOTMAIL),
new EmailCorrection("hotmal", HOTMAIL),
new EmailCorrection("hotmai", HOTMAIL),
new EmailCorrection("hotmali", HOTMAIL),
new EmailCorrection("hitmail", HOTMAIL),

new EmailCorrection("yaho", YAHOO),
new EmailCorrection("yaboo", YAHOO)
);

public List<EmailCorrection> domainAndTldCorrections = Arrays.asList(
new EmailCorrection("gmail.co", GMAIL + DOTCOM)
);

public String getSuggestedEmail(String email) throws InvalidEmailException {
if (email == null) {
throw new InvalidEmailException();
}

for (LastWordCorrection correction : lastWordCorrections){
email = fixDomainByEnd(email, correction.getBadEnd(), correction.getGoodEnd());
for (EmailCorrection correction : tldCorrections) {
email = fixTld(email, correction.getBadEnd(), correction.getGoodEnd());
}
for (EmailCorrection correction : domainCorrections) {
email = fixDomain(email, correction.getBadEnd(), correction.getGoodEnd());
}
for (EmailCorrection correction : domainAndTldCorrections) {
email = fixDomainAndTld(email, correction.getBadEnd(), correction.getGoodEnd());
}

return email;
}

private String fixTld(String email, String badTld, String goodTld) {
if (email.endsWith(badTld)) {
email = email.substring(0, email.length() - badTld.length()).concat(goodTld);
}
return email;
}

private String fixDomain(String email, String badDomain, String goodDomain) throws InvalidEmailException {
EmailParts ep = new EmailParts();
String domain = ep.getDomainWithoutTld(email);
if (domain.equals(badDomain)) {
email = email.replaceAll(domain, goodDomain);
}
return email;
}

private String fixDomainByEnd(String email, String badEnd, String goodEnd) {
if (email.endsWith(badEnd)) {
email = email.substring(0, email.length() - badEnd.length()).concat(goodEnd);
private String fixDomainAndTld(String email, String badDomainAndTld, String goodDomainAndTld) {
if (email.endsWith(badDomainAndTld)) {
email = email.substring(0, email.length() - badDomainAndTld.length()).concat(goodDomainAndTld);
}
return email;
}
Expand Down
7 changes: 7 additions & 0 deletions src/at/rocboron/emailverifier/util/StringUtils.java
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);
}
}
106 changes: 106 additions & 0 deletions src/test/EmailPartsTest.java
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();
}
}
}
13 changes: 11 additions & 2 deletions src/test/EmailSuggestorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
}
Expand Down
30 changes: 30 additions & 0 deletions src/test/StringUtilsTest.java
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"));
}
}

0 comments on commit 4593c31

Please sign in to comment.