diff --git a/.github/workflows/github-actions-build.yml b/.github/workflows/github-actions-build.yml new file mode 100644 index 0000000..9e77453 --- /dev/null +++ b/.github/workflows/github-actions-build.yml @@ -0,0 +1,40 @@ +name: Java CI + +# Trigger workflow on push or pull_request +# Note - the first pull_request from a forked repo will need to be given approval to run +on: [push, pull_request] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - name: Set up JDK 11 + uses: actions/setup-java@v2 + with: + java-version: '11' + distribution: 'adopt' + + - name: Cache Maven Repo + uses: actions/cache@v2 + with: + path: ~/.m2 + key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} + restore-keys: ${{ runner.os }}-m2 + + - name: Build Test and Verify + run: mvn -B -U clean install -Dmaven.javadoc.skip=true -Pdisplay-versions + + - name: SonarCloud Scan + run: | + if ["$SONAR_TOKEN" == ""]; then + echo "Sonar secure variables NOT available" + else + echo "Sonar secure variables ARE available" + mvn -B sonar:sonar -Dsonar.projectKey="bordertech-mailcheck-java" -Dsonar.organization="bordertech-github" -Dsonar.host.url="https://sonarcloud.io" + fi + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} diff --git a/README.md b/README.md index cd590be..d07a137 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,15 @@ mailcheck-java ============== +This is a fork of the original for the purpose of publishing it to maven-central. + +[![Build Status](https://github.com/BorderTech/mailcheck-java/actions/workflows/github-actions-build.yml/badge.svg)](https://github.com/BorderTech/mailcheck-java/actions/workflows/github-actions-build.yml) +[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=bordertech-mailcheck-java&metric=alert_status)](https://sonarcloud.io/dashboard?id=bordertech-mailcheck-java) +[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=bordertech-mailcheck-java&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=bordertech-mailcheck-java) +[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=bordertech-mailcheck-java&metric=coverage)](https://sonarcloud.io/dashboard?id=bordertech-mailcheck-java) +[![javadoc](https://javadoc.io/badge2/com.github.bordertech.mailcheck/mailcheck-java/javadoc.svg)](https://javadoc.io/doc/com.github.bordertech.mailcheck/mailcheck-java) +[![Maven Central](https://img.shields.io/maven-central/v/com.github.bordertech.mailcheck/mailcheck-java.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22com.github.bordertech.mailcheck%22%20AND%20a:%22mailcheck-java%22) + Introduction ------------ I noticed there was a useful project by Derrick Ko of Kicksend at , @@ -18,25 +27,25 @@ glue code, but I haven't put this in yet because an email address that has a sug Installation ------------ -I haven't put this in any maven repositories, so your best bet is to grab the code and install the project -in your maven repository. - -You can then add it your project (assuming you use Maven) by referring to it using the maven coordinates: - - - com.intercognition - mailcheck-java - 0.1 - +Assuming maven, in your pom.xml: +```xml + + com.github.bordertech.mailcheck + mailcheck-java + + +``` Usage ----- I've created it so that it can be easily used programmatically or configured by an IOC container like Spring. You can get up and running quickly by doing the following: - MailCheck mailCheck = new MailCheck(new DefaultConfiguration()); - EmailAddress address = mailCheck.suggest("user@hotmail.cod"); +```java +MailCheck mailCheck = new MailCheck(new DefaultConfiguration()); +EmailAddress address = mailCheck.suggest("user@hotmail.cod"); +``` This will return you an EmailAddress object giving you a suggestion. If a suggestion could not be found, a null EmailAddress will be returned. @@ -45,13 +54,13 @@ EmailAddress will be returned. Author ------- -Konrad Bloor () +[Konrad Bloor]() Contributors ------------ -Quenio dos Santos () +[Quenio dos Santos]() License @@ -59,4 +68,4 @@ License Copyright (c) 2012 Konrad Bloor -Licensed under the MIT License. \ No newline at end of file +Licensed under the MIT License. diff --git a/pom.xml b/pom.xml index a386398..2bf5efb 100644 --- a/pom.xml +++ b/pom.xml @@ -1,71 +1,72 @@ - - 4.0.0 + + 4.0.0 - com.intercognition - mailcheck-java - 0.1 + com.github.bordertech.mailcheck + mailcheck-java + 1.0.1-SNAPSHOT - - - Konrad Bloor - konrad@intercognition.com - Developer - GMT - http://konradbloor.com - - + + com.github.bordertech.common + qa-parent + 1.0.19 + - - - MIT license - http://www.opensource.org/licenses/mit-license.php - - + jar - - scm:git:git://github.com/konradbloor/mailcheck-java.git - scm:git:git://github.com/konradbloor/mailcheck-java.git - + + false + + 7.4.3.sb + ${basedir}/spotbugs-excludes.xml + - - - junit - junit - 4.10 - test - - + Java port of mailcheck - - - - org.apache.maven.plugins - maven-source-plugin - - - attach-sources - - jar - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - - - attach-javadocs - - jar - - - - - - + https://github.com/BorderTech/mailcheck-java - \ No newline at end of file + + https://github.com/bordertech/mailcheck-java + scm:git:https://github.com/bordertech/mailcheck-java.git + scm:git:https://github.com/bordertech/mailcheck-java.git + mailcheck-java-1.0.0 + + + + GitHub + https://github.com/bordertech/mailcheck-java/issues + + + + Github Actions + https://github.com/BorderTech/mailcheck-java/actions + + + + + + Konrad Bloor + konrad@intercognition.com + Developer + GMT + http://konradbloor.com + + + + + + MIT license + http://www.opensource.org/licenses/mit-license.php + + + + + + junit + junit + 4.13.2 + test + + + + diff --git a/spotbugs-excludes.xml b/spotbugs-excludes.xml new file mode 100644 index 0000000..abb5080 --- /dev/null +++ b/spotbugs-excludes.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/src/main/java/com/intercognition/mailcheck/EmailAddress.java b/src/main/java/com/intercognition/mailcheck/EmailAddress.java index 4a29113..2255f22 100644 --- a/src/main/java/com/intercognition/mailcheck/EmailAddress.java +++ b/src/main/java/com/intercognition/mailcheck/EmailAddress.java @@ -2,119 +2,142 @@ public class EmailAddress { - private String address; - private String domain; - private String tld = ""; - private boolean valid = true; - - public EmailAddress(String email) { - email = email.toLowerCase(); - - String[] parts = email.split("@"); - if(!emailAddressHasAtLeastTwoNonEmptyParts(parts)) valid = false; - - domain = parts[parts.length-1]; - - final String[] tmp = new String[parts.length-1]; - System.arraycopy(parts, 0, tmp, 0, parts.length-1); + private String address; + private String domain; + private String tld; + private boolean valid = true; + + /** + * Honestly this is self-documenting code and doesn't need javadoc. + * @param email Can you guess what to pass here? + */ + public EmailAddress(final String email) { + String lcEmail = email.toLowerCase(); + + String[] parts = lcEmail.split("@"); + if (!emailAddressHasAtLeastTwoNonEmptyParts(parts)) { + valid = false; + } + + domain = parts[parts.length - 1]; + + final String[] tmp = new String[parts.length - 1]; + System.arraycopy(parts, 0, tmp, 0, parts.length - 1); parts = tmp; - address = join(parts,"@"); - - tld = extractTld(domain); - if(tld == null) valid = false; - } - - public EmailAddress emailAddressWithDifferentDomain(String newDomain) { - return new EmailAddress(address+"@"+newDomain); - } - - /** - * @return true if domain, address, and tld have been successfully extracted - */ - public boolean isValid() { - return valid; - } - - /** - * @return top level domain, i.e. 'com', 'co.uk' - */ - public String getTLD() { - return tld; - } - - /** - * @return domain, i.e. 'hotmail.com' - */ - public String getDomain() { - return domain; - } - - /** - * @return email address, i.e. 'test@example.com' - */ - public String getAddress() { - return address; - } - - /** - * @param domain i.e. 'hotmail.com' - * @return true if this email address has a valid domain that is the same as the given domain parameter - */ - public boolean hasDomain(String domain) { - return !(this.domain == null || domain == null) && this.domain.equals(domain); - } - - private boolean emailAddressHasAtLeastTwoNonEmptyParts(String[] parts) { - if(parts.length < 2) { - return false; - } - for(String part : parts) { - if(part.length() == 0) { - return false; - } - } - return true; - } - - private String join(String[] components, String delimeter) { - StringBuilder builder = new StringBuilder(); - boolean first = true; - for(String component: components) { - if(first) - first = false; - else - builder.append(delimeter); - - builder.append(component); - } - return builder.toString(); - } - - private String extractTld(String domain) { - String[] domainParts = domain.split("\\."); - if (domainParts.length == 0) return null; - return extractTld(domainParts, domain); - } - - private String extractTld(String[] domainParts, String domain) { - if(domainParts == null || domainParts.length == 0) return null; - - String tld = ""; - if (domainParts.length == 1) { - //valid because has tld - tld = domain; - } else { - //has both tld and domain - for (int i = 1; i < domainParts.length; i++) { - tld += domainParts[i]; - if(i < domainParts.length-1) tld+= "."; - } - } - return tld; - } - - public String toString() { - return address+"@"+domain; - } + address = join(parts, "@"); + + tld = extractTld(domain); + if (tld == null) { + valid = false; + } + } + + /** + * Create a version of the current EmailAddress with a different domain. + * @param newDomain The new domain. + * @return An email address. + */ + public EmailAddress emailAddressWithDifferentDomain(final String newDomain) { + return new EmailAddress(address + "@" + newDomain); + } + + /** + * @return true if domain, address, and tld have been successfully extracted + */ + public boolean isValid() { + return valid; + } + + /** + * @return top level domain, i.e. 'com', 'co.uk' + */ + public String getTLD() { + return tld; + } + + /** + * @return domain, i.e. 'hotmail.com' + */ + public String getDomain() { + return domain; + } + + /** + * @return email address, i.e. 'test@example.com' + */ + public String getAddress() { + return address; + } + + /** + * @param domain i.e. 'hotmail.com' + * @return true if this email address has a valid domain that is the same as the given domain parameter + */ + public boolean hasDomain(final String domain) { + return !(this.domain == null || domain == null) && this.domain.equals(domain); + } + + private boolean emailAddressHasAtLeastTwoNonEmptyParts(final String[] parts) { + if (parts.length < 2) { + return false; + } + for (String part : parts) { + if (part.length() == 0) { + return false; + } + } + return true; + } + + private String join(final String[] components, final String delimeter) { + StringBuilder builder = new StringBuilder(); + boolean first = true; + for (String component : components) { + if (first) { + first = false; + } else { + builder.append(delimeter); + } + builder.append(component); + } + return builder.toString(); + } + + private String extractTld(final String domain) { + String[] domainParts = domain.split("\\."); + if (domainParts.length == 0) { + return null; + } + return extractTld(domainParts, domain); + } + + private String extractTld(final String[] domainParts, final String domain) { + if (domainParts == null || domainParts.length == 0) { + return null; + } + + String tld = ""; + if (domainParts.length == 1) { + //valid because has tld + tld = domain; + } else { + //has both tld and domain + for (int i = 1; i < domainParts.length; i++) { + tld += domainParts[i]; + if (i < domainParts.length - 1) { + tld += "."; + } + } + } + return tld; + } + + /** + * Returns a string representation of this email address. + * @return An email address. + */ + public String toString() { + return address + "@" + domain; + } } diff --git a/src/main/java/com/intercognition/mailcheck/MailCheck.java b/src/main/java/com/intercognition/mailcheck/MailCheck.java index 0a3b005..8025957 100644 --- a/src/main/java/com/intercognition/mailcheck/MailCheck.java +++ b/src/main/java/com/intercognition/mailcheck/MailCheck.java @@ -7,86 +7,92 @@ /** * Usage: - * + *

* First construct with a configuration. A default configuration is supplied to get you started * quickly: - * + *

* MailCheck mailCheck = new MailCheck(new DefaultConfiguration()); - * + *

* Then use the MailCheck object to give you suggestions: - * + *

* EmailAddress suggestion = mailCheck.suggest("test@hitmail.com"); - * + *

* If the returned object is null */ public class MailCheck { - private Configuration configuration; - - public MailCheck(Configuration configuration) { - this.configuration = configuration; - } - - /** - * @param email The user entered email address that you want to check for suggestions with - * @return null, if no suggestions, or an EmailAddress object containing the suggestion - */ - public EmailAddress suggest(String email) { - - EmailAddress emailParts = new EmailAddress(email); - if(!emailParts.isValid()) return null; - - String closestDomain = this.findClosestString(emailParts.getDomain(), configuration.getDomains(), configuration.getDistanceAlgorithm(), configuration.getThreshold()); - - if(closestDomain != null) { - //we have a suggestion - if(!emailParts.hasDomain(closestDomain)) { - //if we have a suggestion different to the actual domain, return it - return emailParts.emailAddressWithDifferentDomain(closestDomain); - } - } else { - //we don't have a suggestion, check tld - String closestTopLevelDomain = this.findClosestString(emailParts.getTLD(), configuration.getTopLevelDomains(), configuration.getDistanceAlgorithm(), configuration.getThreshold()); - if (emailParts.getDomain() != null && closestTopLevelDomain != null && !closestTopLevelDomain.equals(emailParts.getTLD())) { - //return suggestion based off tld - String domain = emailParts.getDomain(); - closestDomain = domain.substring(0, domain.lastIndexOf(emailParts.getTLD())) + closestTopLevelDomain; - return emailParts.emailAddressWithDifferentDomain(closestDomain); - } - } - - //exact match, no match, or invalid email so no suggestion - return null; - } - - protected String findClosestString(String candidate, - Collection possibleMatches, - DistanceAlgorithm distanceAlgorithm, - int threshold) { - float stringDistance; - float smallestMatchDistance = 99; - String closestMatch = null; - - if (candidate == null || candidate.length() == 0 || possibleMatches == null || possibleMatches.isEmpty()) { - return null; - } - - for (String closeDomain : possibleMatches) { - if (candidate.equals(closestMatch)) { - return candidate; - } - stringDistance = distanceAlgorithm.getDistance(candidate, closeDomain); - if (stringDistance < smallestMatchDistance) { - smallestMatchDistance = stringDistance; - closestMatch = closeDomain; - } - } - - if (smallestMatchDistance <= threshold && closestMatch != null) { - return closestMatch; - } - return null; - - } + private Configuration configuration; + + /** + * Creates a new MailCheck instance with the given configuration. + * @param configuration The desired configuration. + */ + public MailCheck(final Configuration configuration) { + this.configuration = configuration; + } + + /** + * @param email The user entered email address that you want to check for suggestions with + * @return null, if no suggestions, or an EmailAddress object containing the suggestion + */ + public EmailAddress suggest(final String email) { + + EmailAddress emailParts = new EmailAddress(email); + if (!emailParts.isValid()) { + return null; + } + + String closestDomain = this.findClosestString(emailParts.getDomain(), configuration.getDomains(), configuration.getDistanceAlgorithm(), configuration.getThreshold()); + + if (closestDomain != null) { + //we have a suggestion + if (!emailParts.hasDomain(closestDomain)) { + //if we have a suggestion different to the actual domain, return it + return emailParts.emailAddressWithDifferentDomain(closestDomain); + } + } else { + //we don't have a suggestion, check tld + String closestTopLevelDomain = this.findClosestString(emailParts.getTLD(), configuration.getTopLevelDomains(), configuration.getDistanceAlgorithm(), configuration.getThreshold()); + if (emailParts.getDomain() != null && closestTopLevelDomain != null && !closestTopLevelDomain.equals(emailParts.getTLD())) { + //return suggestion based off tld + String domain = emailParts.getDomain(); + closestDomain = domain.substring(0, domain.lastIndexOf(emailParts.getTLD())) + closestTopLevelDomain; + return emailParts.emailAddressWithDifferentDomain(closestDomain); + } + } + + //exact match, no match, or invalid email so no suggestion + return null; + } + + protected String findClosestString(final String candidate, + final Collection possibleMatches, + final DistanceAlgorithm distanceAlgorithm, + final int threshold) { + float stringDistance; + float smallestMatchDistance = 99; + String closestMatch = null; + + if (candidate == null || candidate.length() == 0 || possibleMatches == null || possibleMatches.isEmpty()) { + return null; + } + + for (String closeDomain : possibleMatches) { + if (candidate.equals(closestMatch)) { + return candidate; + } + stringDistance = distanceAlgorithm.getDistance(candidate, closeDomain); + if (stringDistance < smallestMatchDistance) { + smallestMatchDistance = stringDistance; + closestMatch = closeDomain; + } + } + + if (smallestMatchDistance <= threshold && closestMatch != null) { + return closestMatch; + } + return null; + + } } diff --git a/src/main/java/com/intercognition/mailcheck/config/Configuration.java b/src/main/java/com/intercognition/mailcheck/config/Configuration.java index 6f5b6c0..8072f08 100644 --- a/src/main/java/com/intercognition/mailcheck/config/Configuration.java +++ b/src/main/java/com/intercognition/mailcheck/config/Configuration.java @@ -5,8 +5,15 @@ import java.util.Collection; public interface Configuration { - public int getThreshold(); - public Collection getDomains(); - public Collection getTopLevelDomains(); - public DistanceAlgorithm getDistanceAlgorithm(); + @SuppressWarnings("checkstyle:MissingJavadocMethod") + int getThreshold(); + + @SuppressWarnings("checkstyle:MissingJavadocMethod") + Collection getDomains(); + + @SuppressWarnings("checkstyle:MissingJavadocMethod") + Collection getTopLevelDomains(); + + @SuppressWarnings("checkstyle:MissingJavadocMethod") + DistanceAlgorithm getDistanceAlgorithm(); } diff --git a/src/main/java/com/intercognition/mailcheck/config/DefaultConfiguration.java b/src/main/java/com/intercognition/mailcheck/config/DefaultConfiguration.java index 35f0a3b..25737b1 100644 --- a/src/main/java/com/intercognition/mailcheck/config/DefaultConfiguration.java +++ b/src/main/java/com/intercognition/mailcheck/config/DefaultConfiguration.java @@ -9,41 +9,42 @@ */ public class DefaultConfiguration extends SimpleConfiguration { - public DefaultConfiguration() { - super( - 3, - Arrays.asList( - "aol.com", - "att.net", - "comcast.net", - "facebook.com", - "gmail.com", - "gmx.com", - "google.com", - "googlemail.com", - "hotmail.co.uk", - "hotmail.com", - "mac.com", - "mail.com", - "me.com", - "live.com", - "msn.com", - "sbcglobal.net", - "verizon.net", - "yahoo.co.uk", - "yahoo.com" - ), - Arrays.asList( - "co.uk", - "com", - "edu", - "gov", - "info", - "mil", - "net", - "org" - ), - new Sift3(5) - ); - } + @SuppressWarnings("checkstyle:MissingJavadocMethod") + public DefaultConfiguration() { + super( + 3, + Arrays.asList( + "aol.com", + "att.net", + "comcast.net", + "facebook.com", + "gmail.com", + "gmx.com", + "google.com", + "googlemail.com", + "hotmail.co.uk", + "hotmail.com", + "mac.com", + "mail.com", + "me.com", + "live.com", + "msn.com", + "sbcglobal.net", + "verizon.net", + "yahoo.co.uk", + "yahoo.com" + ), + Arrays.asList( + "co.uk", + "com", + "edu", + "gov", + "info", + "mil", + "net", + "org" + ), + new Sift3(5) + ); + } } diff --git a/src/main/java/com/intercognition/mailcheck/config/SimpleConfiguration.java b/src/main/java/com/intercognition/mailcheck/config/SimpleConfiguration.java index 0dc2797..1224293 100644 --- a/src/main/java/com/intercognition/mailcheck/config/SimpleConfiguration.java +++ b/src/main/java/com/intercognition/mailcheck/config/SimpleConfiguration.java @@ -1,56 +1,55 @@ package com.intercognition.mailcheck.config; import com.intercognition.mailcheck.stringdistance.DistanceAlgorithm; -import com.intercognition.mailcheck.stringdistance.Sift3; import java.util.Collection; -import java.util.HashSet; -import java.util.List; -import java.util.Set; /** - * Use to hold your own custom configuration + * Use to hold your own custom configuration. */ public class SimpleConfiguration implements Configuration { - private int threshold; - private Collection domains; - private Collection topLevelDomains; - private DistanceAlgorithm distanceAlgorithm; - - /** - * @param threshold String distance above which a domain (i.e. 'hotmail.com') may not be considered a match, - * for example if your threshold is 3, then 'dugmail.com' would be a match but 'hotdogs.com' would - * not be a match, because dug != hot, distance is 3, and mail != dogs, distance is 4. - * @param domains Unordered collection of possible domains, for example a List with the element "gmail.com" - * @param topLevelDomains Unordered collection of possible top level domains, for example a List with the element - * "com" - * @param distanceAlgorithm A distance algorithm to use, Sift3 is supplied. - */ - public SimpleConfiguration(int threshold, - Collection domains, - Collection topLevelDomains, - DistanceAlgorithm distanceAlgorithm) { - this.threshold = threshold; - this.domains = domains; - this.topLevelDomains = topLevelDomains; - this.distanceAlgorithm = distanceAlgorithm; - } - - - public Collection getDomains() { - return domains; - } - - public DistanceAlgorithm getDistanceAlgorithm() { - return distanceAlgorithm; - } - - public int getThreshold() { - return threshold; - } - - public Collection getTopLevelDomains() { - return topLevelDomains; - } + private int threshold; + private Collection domains; + private Collection topLevelDomains; + private DistanceAlgorithm distanceAlgorithm; + + /** + * @param threshold String distance above which a domain (i.e. 'hotmail.com') may not be considered a match, + * for example if your threshold is 3, then 'dugmail.com' would be a match but 'hotdogs.com' would + * not be a match, because dug != hot, distance is 3, and mail != dogs, distance is 4. + * @param domains Unordered collection of possible domains, for example a List with the element "gmail.com" + * @param topLevelDomains Unordered collection of possible top level domains, for example a List with the element + * "com" + * @param distanceAlgorithm A distance algorithm to use, Sift3 is supplied. + */ + public SimpleConfiguration(final int threshold, + final Collection domains, + final Collection topLevelDomains, + final DistanceAlgorithm distanceAlgorithm) { + this.threshold = threshold; + this.domains = domains; + this.topLevelDomains = topLevelDomains; + this.distanceAlgorithm = distanceAlgorithm; + } + + @SuppressWarnings("checkstyle:MissingJavadocMethod") + public Collection getDomains() { + return domains; + } + + @SuppressWarnings("checkstyle:MissingJavadocMethod") + public DistanceAlgorithm getDistanceAlgorithm() { + return distanceAlgorithm; + } + + @SuppressWarnings("checkstyle:MissingJavadocMethod") + public int getThreshold() { + return threshold; + } + + @SuppressWarnings("checkstyle:MissingJavadocMethod") + public Collection getTopLevelDomains() { + return topLevelDomains; + } } diff --git a/src/main/java/com/intercognition/mailcheck/config/package-info.java b/src/main/java/com/intercognition/mailcheck/config/package-info.java new file mode 100644 index 0000000..396395c --- /dev/null +++ b/src/main/java/com/intercognition/mailcheck/config/package-info.java @@ -0,0 +1,4 @@ +/** + * Mailcheck Configuration. + */ +package com.intercognition.mailcheck.config; diff --git a/src/main/java/com/intercognition/mailcheck/package-info.java b/src/main/java/com/intercognition/mailcheck/package-info.java new file mode 100644 index 0000000..1861c86 --- /dev/null +++ b/src/main/java/com/intercognition/mailcheck/package-info.java @@ -0,0 +1,4 @@ +/** + * Mailcheck core package. + */ +package com.intercognition.mailcheck; diff --git a/src/main/java/com/intercognition/mailcheck/stringdistance/DistanceAlgorithm.java b/src/main/java/com/intercognition/mailcheck/stringdistance/DistanceAlgorithm.java index 646f6e9..80601ce 100644 --- a/src/main/java/com/intercognition/mailcheck/stringdistance/DistanceAlgorithm.java +++ b/src/main/java/com/intercognition/mailcheck/stringdistance/DistanceAlgorithm.java @@ -1,8 +1,9 @@ package com.intercognition.mailcheck.stringdistance; /** - * Enables us to use different distance algorithms than Sift3 + * Enables us to use different distance algorithms than Sift3. */ public interface DistanceAlgorithm { - public float getDistance(String firstString, String secondString); + @SuppressWarnings("checkstyle:MissingJavadocMethod") + float getDistance(String firstString, String secondString); } diff --git a/src/main/java/com/intercognition/mailcheck/stringdistance/Sift3.java b/src/main/java/com/intercognition/mailcheck/stringdistance/Sift3.java index fd22023..e330a6d 100644 --- a/src/main/java/com/intercognition/mailcheck/stringdistance/Sift3.java +++ b/src/main/java/com/intercognition/mailcheck/stringdistance/Sift3.java @@ -1,65 +1,64 @@ package com.intercognition.mailcheck.stringdistance; /** - * Ported from C# version - * @see authors page on algorithm + * Ported from C# version. * + * @see authors page on algorithm */ public class Sift3 implements DistanceAlgorithm { - private int maxOffset; + private int maxOffset; - /** - * Calculates string distance - broadly how many characters are different in one string to another - * - * @param maxOffset How far away a different character has to be from another to not count - * as distance. For example, if your max offset is 5, abcde and abcdef should - * have a difference of 0. - */ - public Sift3(int maxOffset) { - this.maxOffset = maxOffset; - } + /** + * Calculates string distance - broadly how many characters are different in one string to another. + * + * @param maxOffset How far away a different character has to be from another to not count + * as distance. For example, if your max offset is 5, abcde and abcdef should + * have a difference of 0. + */ + public Sift3(final int maxOffset) { + this.maxOffset = maxOffset; + } - @Override - public float getDistance(String firstString, String secondString) { - if (isNullOrEmpty(firstString)) - return isNullOrEmpty(secondString) ? 0 : secondString.length(); - if (isNullOrEmpty(secondString)) - return firstString.length(); + @Override + public float getDistance(final String firstString, final String secondString) { + if (isNullOrEmpty(firstString)) { + return isNullOrEmpty(secondString) ? 0 : secondString.length(); + } + if (isNullOrEmpty(secondString)) { + return firstString.length(); + } - int c = 0; - int offset1 = 0; - int offset2 = 0; - int lcs = 0; + int c = 0; + int offset1 = 0; + int offset2 = 0; + int lcs = 0; - while (c + offset1 < firstString.length() && (c + offset2 < secondString.length())) { - if(firstString.charAt(c + offset1) == secondString.charAt(c + offset2)) - lcs++; - else { - offset1 = 0; - offset2 = 0; - for (int i = 0; i < maxOffset; i++) - { - if ((c + i < firstString.length()) - && (firstString.charAt(c + i) == secondString.charAt(c))) - { - offset1 = i; - break; - } - if ((c + i < secondString.length()) - && (firstString.charAt(c) == secondString.charAt(c + i))) - { - offset2 = i; - break; - } - } - } - c++; - } - return (firstString.length() + secondString.length())/2.0f - lcs; - } + while (c + offset1 < firstString.length() && (c + offset2 < secondString.length())) { + if (firstString.charAt(c + offset1) == secondString.charAt(c + offset2)) { + lcs++; + } else { + offset1 = 0; + offset2 = 0; + for (int i = 0; i < maxOffset; i++) { + if ((c + i < firstString.length()) + && (firstString.charAt(c + i) == secondString.charAt(c))) { + offset1 = i; + break; + } + if ((c + i < secondString.length()) + && (firstString.charAt(c) == secondString.charAt(c + i))) { + offset2 = i; + break; + } + } + } + c++; + } + return (firstString.length() + secondString.length()) / 2.0f - lcs; + } - private boolean isNullOrEmpty(String candidate) { - return candidate == null || candidate.length() == 0; - } + private boolean isNullOrEmpty(final String candidate) { + return candidate == null || candidate.length() == 0; + } } diff --git a/src/main/java/com/intercognition/mailcheck/stringdistance/package-info.java b/src/main/java/com/intercognition/mailcheck/stringdistance/package-info.java new file mode 100644 index 0000000..5dfa646 --- /dev/null +++ b/src/main/java/com/intercognition/mailcheck/stringdistance/package-info.java @@ -0,0 +1,4 @@ +/** + * Mailcheck String Distance Algorithms. + */ +package com.intercognition.mailcheck.stringdistance; diff --git a/src/test/java/com/intercognition/mailcheck/EmailAddressTest.java b/src/test/java/com/intercognition/mailcheck/EmailAddressTest.java index 925a86d..1480148 100644 --- a/src/test/java/com/intercognition/mailcheck/EmailAddressTest.java +++ b/src/test/java/com/intercognition/mailcheck/EmailAddressTest.java @@ -7,113 +7,113 @@ public class EmailAddressTest { - //basic cases - - @Test - public void correctlySplitsNormalDomainIntoComponents() { - EmailAddress emailAddress = new EmailAddress("test@example.com"); - assertSplitCorrect(emailAddress, "test", "example.com", "com"); - } - - @Test - public void correctlySplitsDomainWithTwoTldPartsIntoComponents() { - EmailAddress emailAddress = new EmailAddress("test@example.co.uk"); - assertSplitCorrect(emailAddress, "test", "example.co.uk", "co.uk"); - } - - @Test - public void correctlySplitsDomainWithSubdomainIntoComponents() { - EmailAddress emailAddress = new EmailAddress("test@mail.randomsmallcompany.co.uk"); - assertSplitCorrect(emailAddress, "test", "mail.randomsmallcompany.co.uk", "randomsmallcompany.co.uk"); - } - - //rfc compliant cases - - @Test - public void splitDealsWithQuotes() { - EmailAddress emailAddress = new EmailAddress("\"foo@bar\"@example.com"); - assertSplitCorrect(emailAddress, "\"foo@bar\"","example.com","com"); - } - - @Test - public void splitDealsWithNumbers() { - EmailAddress emailAddress = new EmailAddress("containsnumbers1234567890@example.com"); - assertSplitCorrect(emailAddress, "containsnumbers1234567890","example.com","com"); - } - - @Test - public void splitDealsWithSymbolPlusSign() { - EmailAddress emailAddress = new EmailAddress("contains+symbol@example.com"); - assertSplitCorrect(emailAddress, "contains+symbol","example.com","com"); - } - - @Test - public void splitDealsWithSymbolMinusSign() { - EmailAddress emailAddress = new EmailAddress("contains-symbol@example.com"); - assertSplitCorrect(emailAddress, "contains-symbol","example.com","com"); - } - - @Test - public void splitDealsWithPeriodSymbolInAddressAndDomain() { - EmailAddress emailAddress = new EmailAddress("contains.symbol@domain.contains.symbol"); - assertSplitCorrect(emailAddress, "contains.symbol","domain.contains.symbol","contains.symbol"); - } - - @Test - public void splitDealsWithSpacesAndInvertedCommas() { - EmailAddress emailAddress = new EmailAddress("\"contains.and\\ symbols\"@example.com"); - assertSplitCorrect(emailAddress, "\"contains.and\\ symbols\"","example.com","com"); - } - - @Test - public void splitDealsWithPeriodAtSymbolAndInvertedCommas() { - EmailAddress emailAddress = new EmailAddress("\"contains.and.@.symbols.com\"@example.com"); - assertSplitCorrect(emailAddress, "\"contains.and.@.symbols.com\"","example.com","com"); - } - - @Test - public void splitDealsWithAllTheSymbols() { - EmailAddress emailAddress = new EmailAddress("\"()<>[]:;@,\\\\\\\"!#$%&\\'*+-/=?^_`{}|\\ \\ \\ \\ \\ ~\\ \\ \\ \\ \\ \\ \\ ?\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ ^_`{}|~.a\"@allthesymbols.com"); - assertSplitCorrect(emailAddress, - "\"()<>[]:;@,\\\\\\\"!#$%&\\'*+-/=?^_`{}|\\ \\ \\ \\ \\ ~\\ \\ \\ \\ \\ \\ \\ ?\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ ^_`{}|~.a\"", - "allthesymbols.com", - "com"); - } - - @Test - public void splitDealsWithJustTld() { - EmailAddress emailAddress = new EmailAddress("postbox@com"); - assertSplitCorrect(emailAddress, - "postbox", - "com", - "com"); - } - - //non-rfc compliant cases - - @Test - public void withoutUserEmailAddressIsInvalid() { - assertFalse(new EmailAddress("example.com").isValid()); - } - - @Test - public void withoutUserEmailAddressWithSubdomainIsInvalid() { - assertFalse(new EmailAddress("abc.example.com").isValid()); - } - - @Test - public void withoutUserEmailAddressBeginningWithAtSymbolIsInvalid() { - assertFalse(new EmailAddress("@example.com").isValid()); - } - - @Test - public void withoutDomainUserThenAtSymbolIsInvalid() { - assertFalse(new EmailAddress("test@").isValid()); - } - - private void assertSplitCorrect(EmailAddress split, String address, String domain, String tld) { - assertEquals(address, split.getAddress()); - assertEquals(domain, split.getDomain()); - assertEquals(tld, split.getTLD()); - } + //basic cases + + @Test + public void correctlySplitsNormalDomainIntoComponents() { + EmailAddress emailAddress = new EmailAddress("test@example.com"); + assertSplitCorrect(emailAddress, "test", "example.com", "com"); + } + + @Test + public void correctlySplitsDomainWithTwoTldPartsIntoComponents() { + EmailAddress emailAddress = new EmailAddress("test@example.co.uk"); + assertSplitCorrect(emailAddress, "test", "example.co.uk", "co.uk"); + } + + @Test + public void correctlySplitsDomainWithSubdomainIntoComponents() { + EmailAddress emailAddress = new EmailAddress("test@mail.randomsmallcompany.co.uk"); + assertSplitCorrect(emailAddress, "test", "mail.randomsmallcompany.co.uk", "randomsmallcompany.co.uk"); + } + + //rfc compliant cases + + @Test + public void splitDealsWithQuotes() { + EmailAddress emailAddress = new EmailAddress("\"foo@bar\"@example.com"); + assertSplitCorrect(emailAddress, "\"foo@bar\"", "example.com", "com"); + } + + @Test + public void splitDealsWithNumbers() { + EmailAddress emailAddress = new EmailAddress("containsnumbers1234567890@example.com"); + assertSplitCorrect(emailAddress, "containsnumbers1234567890", "example.com", "com"); + } + + @Test + public void splitDealsWithSymbolPlusSign() { + EmailAddress emailAddress = new EmailAddress("contains+symbol@example.com"); + assertSplitCorrect(emailAddress, "contains+symbol", "example.com", "com"); + } + + @Test + public void splitDealsWithSymbolMinusSign() { + EmailAddress emailAddress = new EmailAddress("contains-symbol@example.com"); + assertSplitCorrect(emailAddress, "contains-symbol", "example.com", "com"); + } + + @Test + public void splitDealsWithPeriodSymbolInAddressAndDomain() { + EmailAddress emailAddress = new EmailAddress("contains.symbol@domain.contains.symbol"); + assertSplitCorrect(emailAddress, "contains.symbol", "domain.contains.symbol", "contains.symbol"); + } + + @Test + public void splitDealsWithSpacesAndInvertedCommas() { + EmailAddress emailAddress = new EmailAddress("\"contains.and\\ symbols\"@example.com"); + assertSplitCorrect(emailAddress, "\"contains.and\\ symbols\"", "example.com", "com"); + } + + @Test + public void splitDealsWithPeriodAtSymbolAndInvertedCommas() { + EmailAddress emailAddress = new EmailAddress("\"contains.and.@.symbols.com\"@example.com"); + assertSplitCorrect(emailAddress, "\"contains.and.@.symbols.com\"", "example.com", "com"); + } + + @Test + public void splitDealsWithAllTheSymbols() { + EmailAddress emailAddress = new EmailAddress("\"()<>[]:;@,\\\\\\\"!#$%&\\'*+-/=?^_`{}|\\ \\ \\ \\ \\ ~\\ \\ \\ \\ \\ \\ \\ ?\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ ^_`{}|~.a\"@allthesymbols.com"); + assertSplitCorrect(emailAddress, + "\"()<>[]:;@,\\\\\\\"!#$%&\\'*+-/=?^_`{}|\\ \\ \\ \\ \\ ~\\ \\ \\ \\ \\ \\ \\ ?\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ ^_`{}|~.a\"", + "allthesymbols.com", + "com"); + } + + @Test + public void splitDealsWithJustTld() { + EmailAddress emailAddress = new EmailAddress("postbox@com"); + assertSplitCorrect(emailAddress, + "postbox", + "com", + "com"); + } + + //non-rfc compliant cases + + @Test + public void withoutUserEmailAddressIsInvalid() { + assertFalse(new EmailAddress("example.com").isValid()); + } + + @Test + public void withoutUserEmailAddressWithSubdomainIsInvalid() { + assertFalse(new EmailAddress("abc.example.com").isValid()); + } + + @Test + public void withoutUserEmailAddressBeginningWithAtSymbolIsInvalid() { + assertFalse(new EmailAddress("@example.com").isValid()); + } + + @Test + public void withoutDomainUserThenAtSymbolIsInvalid() { + assertFalse(new EmailAddress("test@").isValid()); + } + + private void assertSplitCorrect(EmailAddress split, String address, String domain, String tld) { + assertEquals(address, split.getAddress()); + assertEquals(domain, split.getDomain()); + assertEquals(tld, split.getTLD()); + } } diff --git a/src/test/java/com/intercognition/mailcheck/MailCheckTest.java b/src/test/java/com/intercognition/mailcheck/MailCheckTest.java index 755b4a2..eb05a1a 100644 --- a/src/test/java/com/intercognition/mailcheck/MailCheckTest.java +++ b/src/test/java/com/intercognition/mailcheck/MailCheckTest.java @@ -13,82 +13,82 @@ public class MailCheckTest { - private SimpleConfiguration configuration; - private MailCheck mailCheck; - - @Before - public void setUp() { - configuration = new SimpleConfiguration(3, - Arrays.asList("yahoo.com", "yahoo.com.tw", "google.com","hotmail.com", "gmail.com", "emaildomain.com", "comcast.net", "facebook.com", "msn.com", "gmx.com"), - Arrays.asList("co.uk", "com", "org", "info"), - new Sift3(5)); - mailCheck = new MailCheck(configuration); - } - - @Test - public void testSimpleSuggestion() { - EmailAddress result = mailCheck.suggest("test@hotmail.co"); - assertEquals("test@hotmail.com",result.toString()); - } - - @Test - public void testNoSuggestionGivenWhenDomainSeemsCorrect() { - EmailAddress result = mailCheck.suggest("contact@kicksend.com"); - assertEquals(null,result); - } - - @Test - public void testSuggestions() { - assertCorrection("test@emaildomain.co","emaildomain.com"); - assertCorrection("test@gmail.con","gmail.com"); - - assertCorrection("test@gnail.con","gmail.com"); - assertCorrection("test@GNAIL.con","gmail.com"); - assertCorrection("test@#gmail.com","gmail.com"); - assertCorrection("test@comcast.com","comcast.net"); - assertCorrection("test@homail.con","hotmail.com"); - assertCorrection("test@hotmail.co","hotmail.com"); - assertCorrection("test@fabecook.com","facebook.com"); - assertCorrection("test@yajoo.com","yahoo.com"); - assertCorrection("test@randomsmallcompany.cmo","randomsmallcompany.com"); - } - - @Test - public void validOrCompletelyIncompleteDomainsHaveNoSuggestion() { - assertNull(mailCheck.suggest("test@yahoo.com.tw")); - assertNull(mailCheck.suggest("")); - assertNull(mailCheck.suggest("test@")); - assertNull(mailCheck.suggest("test")); - - } - - @Test - public void noSuggestionForEmailAddressWithSubdomainAndMisspelledTld() { - assertNull(mailCheck.suggest("test@mail.randomsmallcompany.cmo")); - } - - @Test - public void findClosestStringWithNullCandidateGivesNull() { - assertNull(mailCheck.findClosestString(null, Arrays.asList("possible"), new Sift3(5), 3)); - } - - @Test - public void findClosestStringWithEmptyCandidateGivesNull() { - assertNull(mailCheck.findClosestString("", Arrays.asList("possible"), new Sift3(5), 3)); - } - - @Test - public void findClosestStringWithNullPossibleMatchesGivesNull() { - assertNull(mailCheck.findClosestString("candidate", null, new Sift3(5), 3)); - } - - @Test - @SuppressWarnings("unchecked") - public void findClosestStringWithEmptyPossibleMatchesGivesNull() { - assertNull(mailCheck.findClosestString("candidate", Collections.EMPTY_LIST, new Sift3(5), 3)); - } - - private void assertCorrection(String misspelled, String domain) { - assertEquals(domain, mailCheck.suggest(misspelled).getDomain()); - } + private SimpleConfiguration configuration; + private MailCheck mailCheck; + + @Before + public void setUp() { + configuration = new SimpleConfiguration(3, + Arrays.asList("yahoo.com", "yahoo.com.tw", "google.com", "hotmail.com", "gmail.com", "emaildomain.com", "comcast.net", "facebook.com", "msn.com", "gmx.com"), + Arrays.asList("co.uk", "com", "org", "info"), + new Sift3(5)); + mailCheck = new MailCheck(configuration); + } + + @Test + public void testSimpleSuggestion() { + EmailAddress result = mailCheck.suggest("test@hotmail.co"); + assertEquals("test@hotmail.com", result.toString()); + } + + @Test + public void testNoSuggestionGivenWhenDomainSeemsCorrect() { + EmailAddress result = mailCheck.suggest("contact@kicksend.com"); + assertEquals(null, result); + } + + @Test + public void testSuggestions() { + assertCorrection("test@emaildomain.co", "emaildomain.com"); + assertCorrection("test@gmail.con", "gmail.com"); + + assertCorrection("test@gnail.con", "gmail.com"); + assertCorrection("test@GNAIL.con", "gmail.com"); + assertCorrection("test@#gmail.com", "gmail.com"); + assertCorrection("test@comcast.com", "comcast.net"); + assertCorrection("test@homail.con", "hotmail.com"); + assertCorrection("test@hotmail.co", "hotmail.com"); + assertCorrection("test@fabecook.com", "facebook.com"); + assertCorrection("test@yajoo.com", "yahoo.com"); + assertCorrection("test@randomsmallcompany.cmo", "randomsmallcompany.com"); + } + + @Test + public void validOrCompletelyIncompleteDomainsHaveNoSuggestion() { + assertNull(mailCheck.suggest("test@yahoo.com.tw")); + assertNull(mailCheck.suggest("")); + assertNull(mailCheck.suggest("test@")); + assertNull(mailCheck.suggest("test")); + + } + + @Test + public void noSuggestionForEmailAddressWithSubdomainAndMisspelledTld() { + assertNull(mailCheck.suggest("test@mail.randomsmallcompany.cmo")); + } + + @Test + public void findClosestStringWithNullCandidateGivesNull() { + assertNull(mailCheck.findClosestString(null, Arrays.asList("possible"), new Sift3(5), 3)); + } + + @Test + public void findClosestStringWithEmptyCandidateGivesNull() { + assertNull(mailCheck.findClosestString("", Arrays.asList("possible"), new Sift3(5), 3)); + } + + @Test + public void findClosestStringWithNullPossibleMatchesGivesNull() { + assertNull(mailCheck.findClosestString("candidate", null, new Sift3(5), 3)); + } + + @Test + @SuppressWarnings("unchecked") + public void findClosestStringWithEmptyPossibleMatchesGivesNull() { + assertNull(mailCheck.findClosestString("candidate", Collections.EMPTY_LIST, new Sift3(5), 3)); + } + + private void assertCorrection(String misspelled, String domain) { + assertEquals(domain, mailCheck.suggest(misspelled).getDomain()); + } } diff --git a/src/test/java/com/intercognition/mailcheck/config/DefaultConfigurationTest.java b/src/test/java/com/intercognition/mailcheck/config/DefaultConfigurationTest.java index 0c39de3..6dcce7e 100644 --- a/src/test/java/com/intercognition/mailcheck/config/DefaultConfigurationTest.java +++ b/src/test/java/com/intercognition/mailcheck/config/DefaultConfigurationTest.java @@ -12,59 +12,59 @@ public class DefaultConfigurationTest { - private Configuration configuration; + private Configuration configuration; - @Before - public void setUp() { - configuration = new DefaultConfiguration(); - } + @Before + public void setUp() { + configuration = new DefaultConfiguration(); + } - @Test - public void thresholdAccessor() { - assertEquals(3,configuration.getThreshold()); - } + @Test + public void thresholdAccessor() { + assertEquals(3, configuration.getThreshold()); + } - @Test - public void domainAccessor() { - assertEquals(Arrays.asList("aol.com", - "att.net", - "comcast.net", - "facebook.com", - "gmail.com", - "gmx.com", - "google.com", - "googlemail.com", - "hotmail.co.uk", - "hotmail.com", - "mac.com", - "mail.com", - "me.com", - "live.com", - "msn.com", - "sbcglobal.net", - "verizon.net", - "yahoo.co.uk", - "yahoo.com"), - configuration.getDomains()); - } + @Test + public void domainAccessor() { + assertEquals(Arrays.asList("aol.com", + "att.net", + "comcast.net", + "facebook.com", + "gmail.com", + "gmx.com", + "google.com", + "googlemail.com", + "hotmail.co.uk", + "hotmail.com", + "mac.com", + "mail.com", + "me.com", + "live.com", + "msn.com", + "sbcglobal.net", + "verizon.net", + "yahoo.co.uk", + "yahoo.com"), + configuration.getDomains()); + } - @Test - public void topLevelDomains() { - assertEquals(Arrays.asList( - "co.uk", - "com", - "edu", - "gov", - "info", - "mil", - "net", - "org"), - configuration.getTopLevelDomains()); - } + @Test + public void topLevelDomains() { + assertEquals(Arrays.asList( + "co.uk", + "com", + "edu", + "gov", + "info", + "mil", + "net", + "org"), + configuration.getTopLevelDomains()); + } - @Test - public void distanceAlgorithm() { - assertTrue(configuration.getDistanceAlgorithm() instanceof Sift3); - } + @Test + public void distanceAlgorithm() { + assertTrue(configuration.getDistanceAlgorithm() instanceof Sift3); + } } diff --git a/src/test/java/com/intercognition/mailcheck/config/SimpleConfigurationTest.java b/src/test/java/com/intercognition/mailcheck/config/SimpleConfigurationTest.java index f159120..13c027f 100644 --- a/src/test/java/com/intercognition/mailcheck/config/SimpleConfigurationTest.java +++ b/src/test/java/com/intercognition/mailcheck/config/SimpleConfigurationTest.java @@ -11,33 +11,33 @@ public class SimpleConfigurationTest { - private Configuration configuration; - - @Before - public void setUp() { - configuration = new SimpleConfiguration(5, - Arrays.asList("hotmail.com"), - Arrays.asList("com"), - new Sift3(5)); - } - - @Test - public void thresholdAccessor() { - assertEquals(5,configuration.getThreshold()); - } - - @Test - public void domainAccessor() { - assertEquals(Arrays.asList("hotmail.com"),configuration.getDomains()); - } - - @Test - public void topLevelDomains() { - assertEquals(Arrays.asList("com"),configuration.getTopLevelDomains()); - } - - @Test - public void distanceAlgorithm() { - assertTrue(configuration.getDistanceAlgorithm() instanceof Sift3); - } + private Configuration configuration; + + @Before + public void setUp() { + configuration = new SimpleConfiguration(5, + Arrays.asList("hotmail.com"), + Arrays.asList("com"), + new Sift3(5)); + } + + @Test + public void thresholdAccessor() { + assertEquals(5, configuration.getThreshold()); + } + + @Test + public void domainAccessor() { + assertEquals(Arrays.asList("hotmail.com"), configuration.getDomains()); + } + + @Test + public void topLevelDomains() { + assertEquals(Arrays.asList("com"), configuration.getTopLevelDomains()); + } + + @Test + public void distanceAlgorithm() { + assertTrue(configuration.getDistanceAlgorithm() instanceof Sift3); + } } diff --git a/src/test/java/com/intercognition/mailcheck/stringdistance/Sift3Test.java b/src/test/java/com/intercognition/mailcheck/stringdistance/Sift3Test.java index c6f17d4..a6b95c1 100644 --- a/src/test/java/com/intercognition/mailcheck/stringdistance/Sift3Test.java +++ b/src/test/java/com/intercognition/mailcheck/stringdistance/Sift3Test.java @@ -6,56 +6,56 @@ public class Sift3Test { - DistanceAlgorithm sift3 = new Sift3(5); - - @Test - public void distanceBetweenNullFirstStringAndSecondStringIsLengthOfSecondString() { - assertEquals(6D, sift3.getDistance(null, "abcdef"), 0D); - } - - @Test - public void distanceBetweenEmptyFirstStringAndSecondStringIsLengthOfSecondString() { - assertEquals(6D, sift3.getDistance("", "abcdef"), 0D); - } - - @Test - public void distanceBetweenFirstStringAndNullSecondStringIsLengthOfFirstString() { - assertEquals(5D, sift3.getDistance("abcde", null), 0D); - } - - @Test - public void distanceBetweenFirstStringAndEmptySecondStringIsLengthOfFirstString() { - assertEquals(5D, sift3.getDistance("abcde", ""), 0D); - } - - @Test - public void distanceBetweenIdenticalStringsIsZero() { - assertEquals(0D, sift3.getDistance("abcdef", "abcdef"), 0D); - } - - @Test - public void oneCharacterDifferenceBetweenStringsGivesDistanceOfOne() { - assertEquals(1D, sift3.getDistance("abcdef", "abcdeZ"), 0D); - } - - @Test - public void twoCharacterDifferenceBetweenStringsIs2() { - assertEquals(2D, sift3.getDistance("abcdef", "ZbcdeZ"), 0D); - } - - @Test - public void differenceFurtherThanMaximumOffsetBetweenStringsIsCountedAsDistance() { - assertEquals(0D, sift3.getDistance("abcdef", "abcdefg"), 0.5f); - } - - @Test - public void differenceCloserThanMaximumOffsetBetweenStringsGivesDistanceOfOne() { - assertEquals(1D, sift3.getDistance("abc", "abZ"), 0D); - } - - @Test - public void additionalCharacterWithinOffsetRangeIsCountedAsDistance() { - assertEquals(0D, sift3.getDistance("abc", "abcd"), 0.5f); - } + DistanceAlgorithm sift3 = new Sift3(5); + + @Test + public void distanceBetweenNullFirstStringAndSecondStringIsLengthOfSecondString() { + assertEquals(6D, sift3.getDistance(null, "abcdef"), 0D); + } + + @Test + public void distanceBetweenEmptyFirstStringAndSecondStringIsLengthOfSecondString() { + assertEquals(6D, sift3.getDistance("", "abcdef"), 0D); + } + + @Test + public void distanceBetweenFirstStringAndNullSecondStringIsLengthOfFirstString() { + assertEquals(5D, sift3.getDistance("abcde", null), 0D); + } + + @Test + public void distanceBetweenFirstStringAndEmptySecondStringIsLengthOfFirstString() { + assertEquals(5D, sift3.getDistance("abcde", ""), 0D); + } + + @Test + public void distanceBetweenIdenticalStringsIsZero() { + assertEquals(0D, sift3.getDistance("abcdef", "abcdef"), 0D); + } + + @Test + public void oneCharacterDifferenceBetweenStringsGivesDistanceOfOne() { + assertEquals(1D, sift3.getDistance("abcdef", "abcdeZ"), 0D); + } + + @Test + public void twoCharacterDifferenceBetweenStringsIs2() { + assertEquals(2D, sift3.getDistance("abcdef", "ZbcdeZ"), 0D); + } + + @Test + public void differenceFurtherThanMaximumOffsetBetweenStringsIsCountedAsDistance() { + assertEquals(0D, sift3.getDistance("abcdef", "abcdefg"), 0.5f); + } + + @Test + public void differenceCloserThanMaximumOffsetBetweenStringsGivesDistanceOfOne() { + assertEquals(1D, sift3.getDistance("abc", "abZ"), 0D); + } + + @Test + public void additionalCharacterWithinOffsetRangeIsCountedAsDistance() { + assertEquals(0D, sift3.getDistance("abc", "abcd"), 0.5f); + } }