Skip to content

Commit

Permalink
CODEC-312: Fix possible StringIndexOutOfBoundException thrown by Matc…
Browse files Browse the repository at this point in the history
…hRatingApproachEncoder.encode() method (#220)

* CODEC-312: Fix possible StringIndexOutOfBoundException

Signed-off-by: Arthur Chan <arthur.chan@adalogics.com>

* CODEC-312: Add unit test

Signed-off-by: Arthur Chan <arthur.chan@adalogics.com>

* Remove unmaintained comments

---------

Signed-off-by: Arthur Chan <arthur.chan@adalogics.com>
Co-authored-by: Gary Gregory <garydgregory@users.noreply.github.com>
  • Loading branch information
arthurscchan and garydgregory committed Nov 25, 2023
1 parent a2e542d commit 047d247
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,20 @@ public final String encode(String name) {
// Preprocessing
name = cleanName(name);

// Bulletproof if name becomes empty after cleanName(name)
if (SPACE.equals(name) || name.isEmpty()) {
return EMPTY;
}

// BEGIN: Actual encoding part of the algorithm...
// 1. Delete all vowels unless the vowel begins the word
name = removeVowels(name);

// Bulletproof if name becomes empty after removeVowels(name)
if (SPACE.equals(name) || name.isEmpty()) {
return EMPTY;
}

// 2. Remove second consonant from any double consonant
name = removeDoubleConsonants(name);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
*/
public class MatchRatingApproachEncoderTest extends AbstractStringEncoderTest<MatchRatingApproachEncoder> {

// ********** BEGIN REGION - TEST SUPPORT METHODS

@Override
protected MatchRatingApproachEncoder createStringEncoder() {
return new MatchRatingApproachEncoder();
Expand Down Expand Up @@ -248,10 +246,6 @@ public final void testCompare_Surname_OSULLIVAN_OSUILLEABHAIN_SuccessfulMatch()
assertTrue(this.getStringEncoder().isEncodeEquals("O'Sullivan", "Ó ' Súilleabháin"));
}

// ***** END REGION - TEST SUPPORT METHODS

// ***** BEGIN REGION - TEST GET MRA ENCODING

@Test
public final void testCompare_Surname_PRZEMYSL_PSHEMESHIL_SuccessfullyMatched() {
assertTrue(this.getStringEncoder().isEncodeEquals(" P rz e m y s l", " P sh e m e sh i l"));
Expand Down Expand Up @@ -297,10 +291,6 @@ public final void testCompare_ZACH_ZAKARIA_SuccessfullyMatched() {
assertTrue(this.getStringEncoder().isEncodeEquals("Zach", "Zacharia"));
}

// ***** END REGION - TEST GET MRA ENCODING

// ***** BEGIN REGION - TEST GET MRA COMPARISONS

@Test
public final void testCompareNameNullSpace_ReturnsFalseSuccessfully() {
assertFalse(getStringEncoder().isEncodeEquals(null, " "));
Expand Down Expand Up @@ -433,8 +423,6 @@ public final void testIsEncodeEquals_CornerCase_FirstNameNothing_ReturnsFalse()
assertFalse(this.getStringEncoder().isEncodeEquals("", "test"));
}

// **** BEGIN YIDDISH/SLAVIC SECTION ****

@Test
public final void testIsEncodeEquals_CornerCase_FirstNameNull_ReturnsFalse() {
assertFalse(this.getStringEncoder().isEncodeEquals(null, "test"));
Expand Down Expand Up @@ -470,8 +458,6 @@ public final void testIsVowel_SingleVowel_ReturnsTrue() {
assertTrue(this.getStringEncoder().isVowel("I"));
}

// **** END YIDDISH/SLAVIC SECTION ****

@Test
public final void testIsVowel_SmallD_ReturnsFalse() {
assertFalse(this.getStringEncoder().isVowel("d"));
Expand Down Expand Up @@ -519,4 +505,18 @@ public final void testRemoveVowel_ALESSANDRA_Returns_ALSSNDR() {

// ***** END REGION - TEST GET MRA COMPARISONS

@Test
public final void testPunctuationOnly() {
assertEquals(this.getStringEncoder().encode(".,-"), "");
}

@Test
public final void testVowelOnly() {
assertEquals(this.getStringEncoder().encode("aeiouAEIOU"), "A");
}

@Test
public final void testVowelAndPunctuationOnly() {
assertEquals(this.getStringEncoder().encode("uoiea.,-AEIOU"), "U");
}
}

0 comments on commit 047d247

Please sign in to comment.