This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-codec.git
The following commit(s) were added to refs/heads/master by this push:
new 72210738 Parameterize some unit tests
72210738 is described below
commit 72210738423ebdd766e433d7839d401436793639
Author: Gary Gregory <[email protected]>
AuthorDate: Sat Sep 26 22:19:40 2026 +0000
Parameterize some unit tests
---
.../commons/codec/binary/StringUtilsTest.java | 254 ++++++-------
.../codec/language/DoubleMetaphoneTest.java | 50 +--
.../apache/commons/codec/language/NysiisTest.java | 403 ++++++++++++---------
.../commons/codec/language/RefinedSoundexTest.java | 94 +++--
4 files changed, 428 insertions(+), 373 deletions(-)
diff --git a/src/test/java/org/apache/commons/codec/binary/StringUtilsTest.java
b/src/test/java/org/apache/commons/codec/binary/StringUtilsTest.java
index 6e36234b..3d73f4a7 100644
--- a/src/test/java/org/apache/commons/codec/binary/StringUtilsTest.java
+++ b/src/test/java/org/apache/commons/codec/binary/StringUtilsTest.java
@@ -19,16 +19,21 @@ package org.apache.commons.codec.binary;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
-import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
+import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
+import java.util.function.Function;
+import java.util.stream.Stream;
+import org.junit.jupiter.api.Named;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
/**
* Tests {@link StringUtils}
@@ -45,6 +50,77 @@ class StringUtilsTest {
private static final String STRING_FIXTURE = "ABC";
+ static Stream<Arguments> getBytesCases() {
+ return Stream.of(
+ Arguments.of(StandardCharsets.ISO_8859_1,
+ Named.<Function<String,
byte[]>>of("getBytesIso8859_1", StringUtils::getBytesIso8859_1)),
+ Arguments.of(StandardCharsets.US_ASCII,
+ Named.<Function<String, byte[]>>of("getBytesUsAscii",
StringUtils::getBytesUsAscii)),
+ Arguments.of(StandardCharsets.UTF_16,
+ Named.<Function<String, byte[]>>of("getBytesUtf16",
StringUtils::getBytesUtf16)),
+ Arguments.of(StandardCharsets.UTF_16BE,
+ Named.<Function<String, byte[]>>of("getBytesUtf16Be",
StringUtils::getBytesUtf16Be)),
+ Arguments.of(StandardCharsets.UTF_16LE,
+ Named.<Function<String, byte[]>>of("getBytesUtf16Le",
StringUtils::getBytesUtf16Le)),
+ Arguments.of(StandardCharsets.UTF_8,
+ Named.<Function<String, byte[]>>of("getBytesUtf8",
StringUtils::getBytesUtf8)));
+ }
+
+ static Stream<Arguments> mixedEqualityCases() {
+ return Stream.of(
+ Arguments.of(Named.of("String(abc)", "abc"),
Named.of("StringBuilder(abc)", new StringBuilder("abc")), true),
+ Arguments.of(Named.of("StringBuilder(abc)", new
StringBuilder("abc")), Named.of("String(abcd)", "abcd"), false),
+ Arguments.of(Named.of("String(abcd)", "abcd"),
Named.of("StringBuilder(abc)", new StringBuilder("abc")), false),
+ Arguments.of(Named.of("StringBuilder(abc)", new
StringBuilder("abc")), Named.of("String(ABC)", "ABC"), false));
+ }
+
+ static Stream<Arguments> newStringCases() {
+ return Stream.of(
+ Arguments.of(StandardCharsets.ISO_8859_1,
+ Named.<Function<byte[],
String>>of("newStringIso8859_1", StringUtils::newStringIso8859_1),
BYTES_FIXTURE),
+ Arguments.of(StandardCharsets.US_ASCII,
+ Named.<Function<byte[], String>>of("newStringUsAscii",
StringUtils::newStringUsAscii), BYTES_FIXTURE),
+ Arguments.of(StandardCharsets.UTF_16,
+ Named.<Function<byte[], String>>of("newStringUtf16",
StringUtils::newStringUtf16), BYTES_FIXTURE),
+ Arguments.of(StandardCharsets.UTF_16BE,
+ Named.<Function<byte[], String>>of("newStringUtf16Be",
StringUtils::newStringUtf16Be), BYTES_FIXTURE_16BE),
+ Arguments.of(StandardCharsets.UTF_16LE,
+ Named.<Function<byte[], String>>of("newStringUtf16Le",
StringUtils::newStringUtf16Le), BYTES_FIXTURE_16LE),
+ Arguments.of(StandardCharsets.UTF_8,
+ Named.<Function<byte[], String>>of("newStringUtf8",
StringUtils::newStringUtf8), BYTES_FIXTURE));
+ }
+
+ static Stream<Named<Function<byte[], String>>> newStringFunctions() {
+ return Stream.of(
+ Named.<Function<byte[], String>>of("newStringIso8859_1",
StringUtils::newStringIso8859_1),
+ Named.<Function<byte[], String>>of("newStringUsAscii",
StringUtils::newStringUsAscii),
+ Named.<Function<byte[], String>>of("newStringUtf16",
StringUtils::newStringUtf16),
+ Named.<Function<byte[], String>>of("newStringUtf16Be",
StringUtils::newStringUtf16Be),
+ Named.<Function<byte[], String>>of("newStringUtf16Le",
StringUtils::newStringUtf16Le),
+ Named.<Function<byte[], String>>of("newStringUtf8",
StringUtils::newStringUtf8));
+ }
+
+ static Stream<Arguments> stringBuilderEqualityCases() {
+ return Stream.of(
+ Arguments.of(Named.of("StringBuilder(abc)", new
StringBuilder("abc")), null, false),
+ Arguments.of(null, Named.of("StringBuilder(abc)", new
StringBuilder("abc")), false),
+ Arguments.of(Named.of("StringBuilder(abc)", new
StringBuilder("abc")), Named.of("StringBuilder(abc)", new
StringBuilder("abc")), true),
+ Arguments.of(Named.of("StringBuilder(abc)", new
StringBuilder("abc")), Named.of("StringBuilder(abcd)", new
StringBuilder("abcd")), false),
+ Arguments.of(Named.of("StringBuilder(abcd)", new
StringBuilder("abcd")), Named.of("StringBuilder(abc)", new
StringBuilder("abc")), false),
+ Arguments.of(Named.of("StringBuilder(abc)", new
StringBuilder("abc")), Named.of("StringBuilder(ABC)", new
StringBuilder("ABC")), false));
+ }
+
+ static Stream<Arguments> stringEqualityCases() {
+ return Stream.of(
+ Arguments.of(null, null, true),
+ Arguments.of(Named.of("String(abc)", "abc"), null, false),
+ Arguments.of(null, Named.of("String(abc)", "abc"), false),
+ Arguments.of(Named.of("String(abc)", "abc"),
Named.of("String(abc)", "abc"), true),
+ Arguments.of(Named.of("String(abc)", "abc"),
Named.of("String(abcd)", "abcd"), false),
+ Arguments.of(Named.of("String(abcd)", "abcd"),
Named.of("String(abc)", "abc"), false),
+ Arguments.of(Named.of("String(abc)", "abc"),
Named.of("String(ABC)", "ABC"), false));
+ }
+
@Test
void testByteBufferUtf8() {
assertNull(StringUtils.getByteBufferUtf8(null), "Should be null safe");
@@ -62,48 +138,31 @@ class StringUtilsTest {
new StringUtils();
}
- @Test
- void testEqualsCS1() {
- assertFalse(StringUtils.equals(new StringBuilder("abc"), null));
- assertFalse(StringUtils.equals(null, new StringBuilder("abc")));
- assertTrue(StringUtils.equals(new StringBuilder("abc"), new
StringBuilder("abc")));
- assertFalse(StringUtils.equals(new StringBuilder("abc"), new
StringBuilder("abcd")));
- assertFalse(StringUtils.equals(new StringBuilder("abcd"), new
StringBuilder("abc")));
- assertFalse(StringUtils.equals(new StringBuilder("abc"), new
StringBuilder("ABC")));
- }
-
- @Test
- void testEqualsCS2() {
- assertTrue(StringUtils.equals("abc", new StringBuilder("abc")));
- assertFalse(StringUtils.equals(new StringBuilder("abc"), "abcd"));
- assertFalse(StringUtils.equals("abcd", new StringBuilder("abc")));
- assertFalse(StringUtils.equals(new StringBuilder("abc"), "ABC"));
+ @ParameterizedTest(name = "{0}, {1}: {2}")
+ @MethodSource("mixedEqualityCases")
+ void testEqualsMixedCharSequences(final CharSequence left, final
CharSequence right, final boolean expected) {
+ assertEquals(expected, StringUtils.equals(left, right));
}
- @Test
- void testEqualsString() {
- assertTrue(StringUtils.equals(null, null));
- assertFalse(StringUtils.equals("abc", null));
- assertFalse(StringUtils.equals(null, "abc"));
- assertTrue(StringUtils.equals("abc", "abc"));
- assertFalse(StringUtils.equals("abc", "abcd"));
- assertFalse(StringUtils.equals("abcd", "abc"));
- assertFalse(StringUtils.equals("abc", "ABC"));
+ @ParameterizedTest(name = "{0}, {1}: {2}")
+ @MethodSource("stringBuilderEqualityCases")
+ void testEqualsStringBuilders(final CharSequence left, final CharSequence
right, final boolean expected) {
+ assertEquals(expected, StringUtils.equals(left, right));
}
- @Test
- void testGetBytesIso8859_1() throws UnsupportedEncodingException {
- final String charsetName = StandardCharsets.ISO_8859_1.name();
- testGetBytesUnchecked(charsetName);
- final byte[] expected = STRING_FIXTURE.getBytes(charsetName);
- final byte[] actual = StringUtils.getBytesIso8859_1(STRING_FIXTURE);
- assertArrayEquals(expected, actual);
+ @ParameterizedTest(name = "{0}, {1}: {2}")
+ @MethodSource("stringEqualityCases")
+ void testEqualsStrings(final CharSequence left, final CharSequence right,
final boolean expected) {
+ assertEquals(expected, StringUtils.equals(left, right));
}
- private void testGetBytesUnchecked(final String charsetName) throws
UnsupportedEncodingException {
+ @ParameterizedTest(name = "{0}: {1}")
+ @MethodSource("getBytesCases")
+ void testGetBytes(final Charset charset, final Function<String, byte[]>
encoder) throws UnsupportedEncodingException {
+ final String charsetName = charset.name();
final byte[] expected = STRING_FIXTURE.getBytes(charsetName);
- final byte[] actual = StringUtils.getBytesUnchecked(STRING_FIXTURE,
charsetName);
- assertArrayEquals(expected, actual);
+ assertArrayEquals(expected,
StringUtils.getBytesUnchecked(STRING_FIXTURE, charsetName));
+ assertArrayEquals(expected, encoder.apply(STRING_FIXTURE));
}
@Test
@@ -116,55 +175,12 @@ class StringUtilsTest {
assertNull(StringUtils.getBytesUnchecked(null, "UNKNOWN"));
}
- @Test
- void testGetBytesUsAscii() throws UnsupportedEncodingException {
- final String charsetName = StandardCharsets.US_ASCII.name();
- testGetBytesUnchecked(charsetName);
- final byte[] expected = STRING_FIXTURE.getBytes(charsetName);
- final byte[] actual = StringUtils.getBytesUsAscii(STRING_FIXTURE);
- assertArrayEquals(expected, actual);
- }
-
- @Test
- void testGetBytesUtf16() throws UnsupportedEncodingException {
- final String charsetName = StandardCharsets.UTF_16.name();
- testGetBytesUnchecked(charsetName);
- final byte[] expected = STRING_FIXTURE.getBytes(charsetName);
- final byte[] actual = StringUtils.getBytesUtf16(STRING_FIXTURE);
- assertArrayEquals(expected, actual);
- }
-
- @Test
- void testGetBytesUtf16Be() throws UnsupportedEncodingException {
- final String charsetName = StandardCharsets.UTF_16BE.name();
- testGetBytesUnchecked(charsetName);
- final byte[] expected = STRING_FIXTURE.getBytes(charsetName);
- final byte[] actual = StringUtils.getBytesUtf16Be(STRING_FIXTURE);
- assertArrayEquals(expected, actual);
- }
-
- @Test
- void testGetBytesUtf16Le() throws UnsupportedEncodingException {
- final String charsetName = StandardCharsets.UTF_16LE.name();
- testGetBytesUnchecked(charsetName);
- final byte[] expected = STRING_FIXTURE.getBytes(charsetName);
- final byte[] actual = StringUtils.getBytesUtf16Le(STRING_FIXTURE);
- assertArrayEquals(expected, actual);
- }
-
- @Test
- void testGetBytesUtf8() throws UnsupportedEncodingException {
- final String charsetName = StandardCharsets.UTF_8.name();
- testGetBytesUnchecked(charsetName);
- final byte[] expected = STRING_FIXTURE.getBytes(charsetName);
- final byte[] actual = StringUtils.getBytesUtf8(STRING_FIXTURE);
- assertArrayEquals(expected, actual);
- }
-
- private void testNewString(final String charsetName) throws
UnsupportedEncodingException {
- final String expected = new String(BYTES_FIXTURE, charsetName);
- final String actual = StringUtils.newString(BYTES_FIXTURE,
charsetName);
- assertEquals(expected, actual);
+ @ParameterizedTest(name = "{0}: {1}")
+ @MethodSource("newStringCases")
+ void testNewString(final Charset charset, final Function<byte[], String>
decoder, final byte[] input) throws UnsupportedEncodingException {
+ final String charsetName = charset.name();
+ assertEquals(new String(BYTES_FIXTURE, charsetName),
StringUtils.newString(BYTES_FIXTURE, charsetName));
+ assertEquals(new String(input, charsetName), decoder.apply(input));
}
@Test
@@ -172,72 +188,14 @@ class StringUtilsTest {
assertThrows(IllegalStateException.class, () ->
StringUtils.newString(BYTES_FIXTURE, "UNKNOWN"));
}
- @Test
- void testNewStringIso8859_1() throws UnsupportedEncodingException {
- final String charsetName = StandardCharsets.ISO_8859_1.name();
- testNewString(charsetName);
- final String expected = new String(BYTES_FIXTURE, charsetName);
- final String actual = StringUtils.newStringIso8859_1(BYTES_FIXTURE);
- assertEquals(expected, actual);
- }
-
@Test
void testNewStringNullInput() {
assertNull(StringUtils.newString(null, "UNKNOWN"));
}
- @Test
- void testNewStringNullInput_CODEC229() {
- assertNull(StringUtils.newStringUtf8(null));
- assertNull(StringUtils.newStringIso8859_1(null));
- assertNull(StringUtils.newStringUsAscii(null));
- assertNull(StringUtils.newStringUtf16(null));
- assertNull(StringUtils.newStringUtf16Be(null));
- assertNull(StringUtils.newStringUtf16Le(null));
- }
-
- @Test
- void testNewStringUsAscii() throws UnsupportedEncodingException {
- final String charsetName = StandardCharsets.US_ASCII.name();
- testNewString(charsetName);
- final String expected = new String(BYTES_FIXTURE, charsetName);
- final String actual = StringUtils.newStringUsAscii(BYTES_FIXTURE);
- assertEquals(expected, actual);
- }
-
- @Test
- void testNewStringUtf16() throws UnsupportedEncodingException {
- final String charsetName = StandardCharsets.UTF_16.name();
- testNewString(charsetName);
- final String expected = new String(BYTES_FIXTURE, charsetName);
- final String actual = StringUtils.newStringUtf16(BYTES_FIXTURE);
- assertEquals(expected, actual);
- }
-
- @Test
- void testNewStringUtf16Be() throws UnsupportedEncodingException {
- final String charsetName = StandardCharsets.UTF_16BE.name();
- testNewString(charsetName);
- final String expected = new String(BYTES_FIXTURE_16BE, charsetName);
- final String actual = StringUtils.newStringUtf16Be(BYTES_FIXTURE_16BE);
- assertEquals(expected, actual);
- }
-
- @Test
- void testNewStringUtf16Le() throws UnsupportedEncodingException {
- final String charsetName = StandardCharsets.UTF_16LE.name();
- testNewString(charsetName);
- final String expected = new String(BYTES_FIXTURE_16LE, charsetName);
- final String actual = StringUtils.newStringUtf16Le(BYTES_FIXTURE_16LE);
- assertEquals(expected, actual);
- }
-
- @Test
- void testNewStringUtf8() throws UnsupportedEncodingException {
- final String charsetName = StandardCharsets.UTF_8.name();
- testNewString(charsetName);
- final String expected = new String(BYTES_FIXTURE, charsetName);
- final String actual = StringUtils.newStringUtf8(BYTES_FIXTURE);
- assertEquals(expected, actual);
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("newStringFunctions")
+ void testNewStringNullInput_CODEC229(final Function<byte[], String>
decoder) {
+ assertNull(decoder.apply(null));
}
}
diff --git
a/src/test/java/org/apache/commons/codec/language/DoubleMetaphoneTest.java
b/src/test/java/org/apache/commons/codec/language/DoubleMetaphoneTest.java
index 3aaf7407..ec41955f 100644
--- a/src/test/java/org/apache/commons/codec/language/DoubleMetaphoneTest.java
+++ b/src/test/java/org/apache/commons/codec/language/DoubleMetaphoneTest.java
@@ -23,9 +23,14 @@ import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
+import java.util.stream.Stream;
+
import org.apache.commons.codec.AbstractStringEncoderTest;
import org.apache.commons.codec.EncoderException;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
/**
* Tests {@link DoubleMetaphone}.
@@ -1006,6 +1011,22 @@ class DoubleMetaphoneTest extends
AbstractStringEncoderTest<DoubleMetaphone> {
"wundeews", "windows" }, {
"yeild", "yield" }, };
+ static Stream<Arguments> matchingNames() {
+ validateFixture(MATCHES);
+ return Stream.of(MATCHES).map(pair -> Arguments.of(pair[0], pair[1]));
+ }
+
+ private static void validateFixture(final String[][] pairs) {
+ if (pairs.length == 0) {
+ fail("Test fixture is empty");
+ }
+ for (int i = 0; i < pairs.length; i++) {
+ if (pairs[i].length != 2) {
+ fail("Error in test fixture in the data array at index " + i);
+ }
+ }
+ }
+
/**
* Tests encoding APIs in one place.
*/
@@ -1199,18 +1220,12 @@ class DoubleMetaphoneTest extends
AbstractStringEncoderTest<DoubleMetaphone> {
}
}
- @Test
- void testIsDoubleMetaphoneEqualWithMATCHES() {
- validateFixture(MATCHES);
- for (int i = 0; i < MATCHES.length; i++) {
- final String name0 = MATCHES[i][0];
- final String name1 = MATCHES[i][1];
- final boolean match1 =
getStringEncoder().isDoubleMetaphoneEqual(name0, name1, false);
- final boolean match2 =
getStringEncoder().isDoubleMetaphoneEqual(name0, name1, true);
- if (!match1 && !match2) {
- fail("Expected match [" + i + "] " + name0 + " and " + name1);
- }
- }
+ @ParameterizedTest(name = "[{index}] {0}, {1}")
+ @MethodSource("matchingNames")
+ void testIsDoubleMetaphoneEqualWithMATCHES(final String name0, final
String name1) {
+ final boolean primaryMatch =
getStringEncoder().isDoubleMetaphoneEqual(name0, name1, false);
+ final boolean alternateMatch =
getStringEncoder().isDoubleMetaphoneEqual(name0, name1, true);
+ assertTrue(primaryMatch || alternateMatch, "Expected a primary or
alternate match");
}
@Test
@@ -1244,15 +1259,4 @@ class DoubleMetaphoneTest extends
AbstractStringEncoderTest<DoubleMetaphone> {
assertEquals("JMP", doubleMetaphone.doubleMetaphone(value, false),
"Max=3 Primary");
assertEquals("AMP", doubleMetaphone.doubleMetaphone(value, true),
"Max=3 Alternate");
}
-
- private void validateFixture(final String[][] pairs) {
- if (pairs.length == 0) {
- fail("Test fixture is empty");
- }
- for (int i = 0; i < pairs.length; i++) {
- if (pairs[i].length != 2) {
- fail("Error in test fixture in the data array at index " + i);
- }
- }
- }
}
diff --git a/src/test/java/org/apache/commons/codec/language/NysiisTest.java
b/src/test/java/org/apache/commons/codec/language/NysiisTest.java
index 5e594eac..52f212b9 100644
--- a/src/test/java/org/apache/commons/codec/language/NysiisTest.java
+++ b/src/test/java/org/apache/commons/codec/language/NysiisTest.java
@@ -20,246 +20,313 @@ package org.apache.commons.codec.language;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import java.util.stream.Stream;
+
import org.apache.commons.codec.AbstractStringEncoderTest;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.CsvSource;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.junit.jupiter.params.provider.ValueSource;
/**
* Tests {@link Nysiis}
*/
class NysiisTest extends AbstractStringEncoderTest<Nysiis> {
- private final Nysiis fullNysiis = new Nysiis(false);
-
- /**
- * Takes an array of String pairs where each pair's first element is the
input and the second element the expected
- * encoding.
- *
- * @param testValues
- * an array of String pairs where each pair's first element is
the input and the second element the
- * expected encoding.
- */
- private void assertEncodings(final String[]... testValues) {
- for (final String[] arr : testValues) {
- assertEquals(arr[1], this.fullNysiis.encode(arr[0]), "Problem with
" + arr[0]);
- }
- }
-
- @Override
- protected Nysiis createStringEncoder() {
- return new Nysiis();
- }
-
- private void encodeAll(final String[] strings, final String
expectedEncoding) {
- for (final String string : strings) {
- assertEquals(expectedEncoding, getStringEncoder().encode(string),
"Problem with " + string);
- }
- }
-
- @Test
- void testBran() {
- encodeAll(new String[] { "Brian", "Brown", "Brun" }, "BRAN");
- }
-
- @Test
- void testCap() {
- encodeAll(new String[] { "Capp", "Cope", "Copp", "Kipp" }, "CAP");
- }
-
- @Test
- void testDad() {
- // Data Quality and Record Linkage Techniques P.121 claims this is DAN,
- // but it should be DAD, verified also with dropby.com
- encodeAll(new String[] { "Dent" }, "DAD");
- }
-
- @Test
- void testDan() {
- encodeAll(new String[] { "Dane", "Dean", "Dionne" }, "DAN");
- }
-
- /**
- * Tests data gathered from around the internet.
- *
- * @see <a
href="https://www.dropby.com/NYSIISTextStrings.html">http://www.dropby.com/NYSIISTextStrings.html</a>*/
- @Test
- void testDropBy() {
+ static Stream<Arguments> dropByCases() {
// Explanation of differences between this implementation and the one
at dropby.com is
// prepended to the test string. The referenced rules refer to the
outlined steps the
// class description for Nysiis.
-
- assertEncodings(
+ // @formatter:off
+ return Stream.of(
// 1. Transcode first characters of name
- new String[] { "MACINTOSH", "MCANT" },
+ Arguments.of("MACINTOSH", "MCANT"),
// violates 4j: the second N should not be added, as the first
// key char is already a N
- new String[] { "KNUTH", "NAT" }, // Original: NNAT;
modified: NATH
+ Arguments.of("KNUTH", "NAT"), // Original: NNAT;
modified: NATH
// O and E are transcoded to A because of rule 4a
// H also to A because of rule 4h
// the N gets mysteriously lost, maybe because of a wrongly
implemented rule 4h
// that skips the next char in such a case?
// the remaining A is removed because of rule 7
- new String[] { "KOEHN", "CAN" }, // Original: C
+ Arguments.of("KOEHN", "CAN"), // Original: C
// violates 4j: see also KNUTH
- new String[] { "PHILLIPSON", "FALAPSAN" }, // Original:
FFALAP[SAN]
+ Arguments.of("PHILLIPSON", "FALAPSAN"), // Original:
FFALAP[SAN]
// violates 4j: see also KNUTH
- new String[] { "PFEISTER", "FASTAR" }, // Original:
FFASTA[R]
+ Arguments.of("PFEISTER", "FASTAR"), // Original: FFASTA[R]
// violates 4j: see also KNUTH
- new String[] { "SCHOENHOEFT", "SANAFT" }, // Original:
SSANAF[T]
+ Arguments.of("SCHOENHOEFT", "SANAFT"), // Original: SSANAF[T]
// 2. Transcode last characters of name:
- new String[] { "MCKEE", "MCY" },
- new String[] { "MACKIE", "MCY" },
- new String[] { "HEITSCHMIDT", "HATSNAD" },
- new String[] { "BART", "BAD" },
- new String[] { "HURD", "HAD" },
- new String[] { "HUNT", "HAD" },
- new String[] { "WESTERLUND", "WASTARLAD" },
+ Arguments.of("MCKEE", "MCY"),
+ Arguments.of("MACKIE", "MCY"),
+ Arguments.of("HEITSCHMIDT", "HATSNAD"),
+ Arguments.of("BART", "BAD"),
+ Arguments.of("HURD", "HAD"),
+ Arguments.of("HUNT", "HAD"),
+ Arguments.of("WESTERLUND", "WASTARLAD"),
// 4. Transcode remaining characters by following these rules,
// incrementing by one character each time:
- new String[] { "CASSTEVENS", "CASTAFAN" },
- new String[] { "VASQUEZ", "VASG" },
- new String[] { "FRAZIER", "FRASAR" },
- new String[] { "BOWMAN", "BANAN" },
- new String[] { "MCKNIGHT", "MCNAGT" },
- new String[] { "RICKERT", "RACAD" },
+ Arguments.of("CASSTEVENS", "CASTAFAN"),
+ Arguments.of("VASQUEZ", "VASG"),
+ Arguments.of("FRAZIER", "FRASAR"),
+ Arguments.of("BOWMAN", "BANAN"),
+ Arguments.of("MCKNIGHT", "MCNAGT"),
+ Arguments.of("RICKERT", "RACAD"),
// violates 5: the last S is not removed
// when comparing to DEUTS, which is phonetically similar
// the result it also DAT, which is correct for DEUTSCH too imo
- new String[] { "DEUTSCH", "DAT" }, // Original: DATS
- new String[] { "WESTPHAL", "WASTFAL" },
+ Arguments.of("DEUTSCH", "DAT"), // Original: DATS
+ Arguments.of("WESTPHAL", "WASTFAL"),
// violates 4h: the H should be transcoded to S and thus
ignored as
// the first key character is also S
- new String[] { "SHRIVER", "SRAVAR" }, // Original:
SHRAVA[R]
+ Arguments.of("SHRIVER", "SRAVAR"), // Original: SHRAVA[R]
// same as KOEHN, the L gets mysteriously lost
- new String[] { "KUHL", "CAL" }, // Original: C
- new String[] { "RAWSON", "RASAN" },
+ Arguments.of("KUHL", "CAL"), // Original: C
+ Arguments.of("RAWSON", "RASAN"),
// If last character is S, remove it
- new String[] { "JILES", "JAL" },
+ Arguments.of("JILES", "JAL"),
// violates 6: if the last two characters are AY, remove A
- new String[] { "CARRAWAY", "CARY" }, // Original: CARAY
- new String[] { "YAMADA", "YANAD" });
+ Arguments.of("CARRAWAY", "CARY"), // Original: CARAY
+ Arguments.of("YAMADA", "YANAD"));
+ // @formatter:on
+ }
+
+ static Stream<Arguments> othersCases() {
+ // @formatter:off
+ return Stream.of(
+ Arguments.of("O'Daniel", "ODANAL"),
+ Arguments.of("O'Donnel", "ODANAL"),
+ Arguments.of("Cory", "CARY"),
+ Arguments.of("Corey", "CARY"),
+ Arguments.of("Kory", "CARY"),
+ //
+ Arguments.of("FUZZY", "FASY"));
+ // @formatter:on
+ }
+
+ static Stream<Arguments> rule1Cases() {
+ // @formatter:off
+ return Stream.of(
+ Arguments.of("MACX", "MCX"),
+ Arguments.of("KNX", "NX"),
+ Arguments.of("KX", "CX"),
+ Arguments.of("PHX", "FX"),
+ Arguments.of("PFX", "FX"),
+ Arguments.of("SCHX", "SX"));
+ // @formatter:on
+ }
+
+ static Stream<Arguments> rule2Cases() {
+ // @formatter:off
+ return Stream.of(
+ Arguments.of("XEE", "XY"),
+ Arguments.of("XIE", "XY"),
+ Arguments.of("XDT", "XD"),
+ Arguments.of("XRT", "XD"),
+ Arguments.of("XRD", "XD"),
+ Arguments.of("XNT", "XD"),
+ Arguments.of("XND", "XD"));
+ // @formatter:on
+ }
+
+ static Stream<Arguments> rule4Dot1Cases() {
+ // @formatter:off
+ return Stream.of(
+ Arguments.of("XEV", "XAF"),
+ Arguments.of("XAX", "XAX"),
+ Arguments.of("XEX", "XAX"),
+ Arguments.of("XIX", "XAX"),
+ Arguments.of("XOX", "XAX"),
+ Arguments.of("XUX", "XAX"));
+ // @formatter:on
+ }
+
+ static Stream<Arguments> rule4Dot2Cases() {
+ // @formatter:off
+ return Stream.of(
+ Arguments.of("XQ", "XG"),
+ Arguments.of("XZ", "X"),
+ Arguments.of("XM", "XN"));
+ // @formatter:on
+ }
+
+ static Stream<Arguments> rule5Cases() {
+ // @formatter:off
+ return Stream.of(
+ Arguments.of("XS", "X"),
+ Arguments.of("XSS", "X"));
+ // @formatter:on
+ }
+
+ static Stream<Arguments> rule6Cases() {
+ // @formatter:off
+ return Stream.of(
+ Arguments.of("XAY", "XY"),
+ Arguments.of("XAYS", "XY")); // Rules 5, 6
+ // @formatter:on
+ }
+
+ static Stream<Arguments> rule7Cases() {
+ // @formatter:off
+ return Stream.of(
+ Arguments.of("XA", "X"),
+ Arguments.of("XAS", "X")); // Rules 5, 7
+ // @formatter:on
+ }
+
+ private final Nysiis fullNysiis = new Nysiis(false);
+
+ @Override
+ protected Nysiis createStringEncoder() {
+ return new Nysiis();
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @ValueSource(strings = { "Brian", "Brown", "Brun" })
+ void testBran(final String input) {
+ assertEquals("BRAN", getStringEncoder().encode(input));
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @ValueSource(strings = { "Capp", "Cope", "Copp", "Kipp" })
+ void testCap(final String input) {
+ assertEquals("CAP", getStringEncoder().encode(input));
}
@Test
- void testFal() {
- encodeAll(new String[] { "Phil" }, "FAL");
+ void testDad() {
+ // Data Quality and Record Linkage Techniques P.121 claims this is DAN,
+ // but it should be DAD, verified also with dropby.com
+ assertEquals("DAD", getStringEncoder().encode("Dent"));
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @ValueSource(strings = { "Dane", "Dean", "Dionne" })
+ void testDan(final String input) {
+ assertEquals("DAN", getStringEncoder().encode(input));
}
/**
- * Tests data gathered from around the internets.*/
+ * Tests data gathered from around the internet.
+ *
+ * @see <a
href="https://www.dropby.com/NYSIISTextStrings.html">http://www.dropby.com/NYSIISTextStrings.html</a>
+ */
+ @ParameterizedTest(name = "{0}: {1}")
+ @MethodSource("dropByCases")
+ void testDropBy(final String input, final String expected) {
+ assertEquals(expected, fullNysiis.encode(input));
+ }
+
@Test
- void testOthers() {
- assertEncodings(
- new String[] { "O'Daniel", "ODANAL" },
- new String[] { "O'Donnel", "ODANAL" },
- new String[] { "Cory", "CARY" },
- new String[] { "Corey", "CARY" },
- new String[] { "Kory", "CARY" },
- //
- new String[] { "FUZZY", "FASY" });
+ void testFal() {
+ assertEquals("FAL", getStringEncoder().encode("Phil"));
}
/**
- * Tests rule 1: Translate first characters of name: MAC → MCC, KN → N, K
→ C, PH, PF → FF, SCH → SSS*/
- @Test
- void testRule1() {
- assertEncodings(
- new String[] { "MACX", "MCX" },
- new String[] { "KNX", "NX" },
- new String[] { "KX", "CX" },
- new String[] { "PHX", "FX" },
- new String[] { "PFX", "FX" },
- new String[] { "SCHX", "SX" });
+ * Tests data gathered from around the internets.
+ */
+ @ParameterizedTest(name = "{0}: {1}")
+ @MethodSource("othersCases")
+ void testOthers(final String input, final String expected) {
+ assertEquals(expected, fullNysiis.encode(input));
}
/**
- * Tests rule 2: Translate last characters of name: EE → Y, IE → Y, DT,
RT, RD, NT, ND → D*/
- @Test
- void testRule2() {
- assertEncodings(
- new String[] { "XEE", "XY" },
- new String[] { "XIE", "XY" },
- new String[] { "XDT", "XD" },
- new String[] { "XRT", "XD" },
- new String[] { "XRD", "XD" },
- new String[] { "XNT", "XD" },
- new String[] { "XND", "XD" });
+ * Tests rule 1: Translate first characters of name: MAC → MCC, KN → N, K
→ C, PH, PF → FF, SCH → SSS
+ */
+ @ParameterizedTest(name = "{0}: {1}")
+ @MethodSource("rule1Cases")
+ void testRule1(final String input, final String expected) {
+ assertEquals(expected, fullNysiis.encode(input));
}
/**
- * Tests rule 4.1: EV → AF else A, E, I, O, U → A*/
- @Test
- void testRule4Dot1() {
- assertEncodings(
- new String[] { "XEV", "XAF" },
- new String[] { "XAX", "XAX" },
- new String[] { "XEX", "XAX" },
- new String[] { "XIX", "XAX" },
- new String[] { "XOX", "XAX" },
- new String[] { "XUX", "XAX" });
+ * Tests rule 2: Translate last characters of name: EE → Y, IE → Y, DT,
RT, RD, NT, ND → D
+ */
+ @ParameterizedTest(name = "{0}: {1}")
+ @MethodSource("rule2Cases")
+ void testRule2(final String input, final String expected) {
+ assertEquals(expected, fullNysiis.encode(input));
}
/**
- * Tests rule 4.2: Q → G, Z → S, M → N*/
- @Test
- void testRule4Dot2() {
- assertEncodings(
- new String[] { "XQ", "XG" },
- new String[] { "XZ", "X" },
- new String[] { "XM", "XN" });
+ * Tests rule 4.1: EV → AF else A, E, I, O, U → A
+ */
+ @ParameterizedTest(name = "{0}: {1}")
+ @MethodSource("rule4Dot1Cases")
+ void testRule4Dot1(final String input, final String expected) {
+ assertEquals(expected, fullNysiis.encode(input));
}
/**
- * Tests rule 5: If last character is S, remove it.*/
- @Test
- void testRule5() {
- assertEncodings(
- new String[] { "XS", "X" },
- new String[] { "XSS", "X" });
+ * Tests rule 4.2: Q → G, Z → S, M → N
+ */
+ @ParameterizedTest(name = "{0}: {1}")
+ @MethodSource("rule4Dot2Cases")
+ void testRule4Dot2(final String input, final String expected) {
+ assertEquals(expected, fullNysiis.encode(input));
}
/**
- * Tests rule 6: If last characters are AY, replace with Y.*/
- @Test
- void testRule6() {
- assertEncodings(
- new String[] { "XAY", "XY" },
- new String[] { "XAYS", "XY" }); // Rules 5, 6
+ * Tests rule 5: If last character is S, remove it.
+ */
+ @ParameterizedTest(name = "{0}: {1}")
+ @MethodSource("rule5Cases")
+ void testRule5(final String input, final String expected) {
+ assertEquals(expected, fullNysiis.encode(input));
}
/**
- * Tests rule 7: If last character is A, remove it.*/
- @Test
- void testRule7() {
- assertEncodings(
- new String[] { "XA", "X" },
- new String[] { "XAS", "X" }); // Rules 5, 7
+ * Tests rule 6: If last characters are AY, replace with Y.
+ */
+ @ParameterizedTest(name = "{0}: {1}")
+ @MethodSource("rule6Cases")
+ void testRule6(final String input, final String expected) {
+ assertEquals(expected, fullNysiis.encode(input));
+ }
+
+ /**
+ * Tests rule 7: If last character is A, remove it.
+ */
+ @ParameterizedTest(name = "{0}: {1}")
+ @MethodSource("rule7Cases")
+ void testRule7(final String input, final String expected) {
+ assertEquals(expected, fullNysiis.encode(input));
}
+
@Test
void testSnad() {
// Data Quality and Record Linkage Techniques P.121 claims this is
SNAT,
// but it should be SNAD
- encodeAll(new String[] { "Schmidt" }, "SNAD");
+ assertEquals("SNAD", getStringEncoder().encode("Schmidt"));
}
- @Test
- void testSnat() {
- encodeAll(new String[] { "Smith", "Schmit" }, "SNAT");
+ @ParameterizedTest(name = "{0}")
+ @ValueSource(strings = { "Smith", "Schmit" })
+ void testSnat(final String input) {
+ assertEquals("SNAT", getStringEncoder().encode(input));
}
- @Test
- void testSpecialBranches() {
- encodeAll(new String[] { "Kobwick" }, "CABWAC");
- encodeAll(new String[] { "Kocher" }, "CACAR");
- encodeAll(new String[] { "Fesca" }, "FASC");
- encodeAll(new String[] { "Shom" }, "SAN");
- encodeAll(new String[] { "Ohlo" }, "OL");
- encodeAll(new String[] { "Uhu" }, "UH");
- encodeAll(new String[] { "Um" }, "UN");
+ @ParameterizedTest(name = "{0}: {1}")
+ @CsvSource({
+ "Kobwick, CABWAC",
+ "Kocher, CACAR",
+ "Fesca, FASC",
+ "Shom, SAN",
+ "Ohlo, OL",
+ "Uhu, UH",
+ "Um, UN"
+ })
+ void testSpecialBranches(final String input, final String expected) {
+ assertEquals(expected, getStringEncoder().encode(input));
}
- @Test
- void testTranan() {
- encodeAll(new String[] { "Trueman", "Truman" }, "TRANAN");
+ @ParameterizedTest(name = "{0}")
+ @ValueSource(strings = { "Trueman", "Truman" })
+ void testTranan(final String input) {
+ assertEquals("TRANAN", getStringEncoder().encode(input));
}
@Test
diff --git
a/src/test/java/org/apache/commons/codec/language/RefinedSoundexTest.java
b/src/test/java/org/apache/commons/codec/language/RefinedSoundexTest.java
index fe8f0c54..26418665 100644
--- a/src/test/java/org/apache/commons/codec/language/RefinedSoundexTest.java
+++ b/src/test/java/org/apache/commons/codec/language/RefinedSoundexTest.java
@@ -19,57 +19,83 @@ package org.apache.commons.codec.language;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import java.util.stream.Stream;
+
import org.apache.commons.codec.AbstractStringEncoderTest;
import org.apache.commons.codec.EncoderException;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.CsvSource;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.junit.jupiter.params.provider.NullAndEmptySource;
+import org.junit.jupiter.params.provider.ValueSource;
/**
* Tests RefinedSoundex.
*/
class RefinedSoundexTest extends AbstractStringEncoderTest<RefinedSoundex> {
+ static Stream<Arguments> differenceCases() {
+ // @formatter:off
+ return Stream.of(
+ // Normal cases
+ Arguments.of("Smith", "Smythe", 6),
+ Arguments.of("Ann", "Andrew", 3),
+ Arguments.of("Margaret", "Andrew", 1),
+ Arguments.of("Janet", "Margaret", 1),
+ // Examples from
+ //
https://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_de-dz_8co5.asp
+ Arguments.of("Green", "Greene", 5),
+ Arguments.of("Blotchet-Halls", "Greene", 1),
+ // Examples from
+ //
https://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_setu-sus_3o6w.asp
+ Arguments.of("Smith", "Smythe", 6),
+ Arguments.of("Smithers", "Smythers", 8),
+ Arguments.of("Anothers", "Brothers", 5));
+ // @formatter:on
+ }
+
@Override
protected RefinedSoundex createStringEncoder() {
return new RefinedSoundex();
}
- @Test
- void testDifference() throws EncoderException {
- // Edge cases
- assertEquals(0, getStringEncoder().difference(null, null));
- assertEquals(0, getStringEncoder().difference("", ""));
- assertEquals(0, getStringEncoder().difference(" ", " "));
- // Normal cases
- assertEquals(6, getStringEncoder().difference("Smith", "Smythe"));
- assertEquals(3, getStringEncoder().difference("Ann", "Andrew"));
- assertEquals(1, getStringEncoder().difference("Margaret", "Andrew"));
- assertEquals(1, getStringEncoder().difference("Janet", "Margaret"));
- // Examples from
- //
https://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_de-dz_8co5.asp
- assertEquals(5, getStringEncoder().difference("Green", "Greene"));
- assertEquals(1, getStringEncoder().difference("Blotchet-Halls",
"Greene"));
- // Examples from
- //
https://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_setu-sus_3o6w.asp
- assertEquals(6, getStringEncoder().difference("Smith", "Smythe"));
- assertEquals(8, getStringEncoder().difference("Smithers", "Smythers"));
- assertEquals(5, getStringEncoder().difference("Anothers", "Brothers"));
+ @ParameterizedTest(name = "{0}, {1}: difference {2}")
+ @MethodSource("differenceCases")
+ void testDifference(final String left, final String right, final int
expected) throws EncoderException {
+ assertEquals(expected, getStringEncoder().difference(left, right));
+ }
+
+ @ParameterizedTest
+ @NullAndEmptySource
+ @ValueSource(strings = { " " })
+ void testDifferenceEmpty(final String input) throws EncoderException {
+ assertEquals(0, getStringEncoder().difference(input, input));
+ }
+
+ @ParameterizedTest(name = "{0}: {1}")
+ // @formatter:off
+ @CsvSource({
+ "testing, T6036084",
+ "TESTING, T6036084",
+ "The, T60",
+ "quick, Q503",
+ "brown, B1908",
+ "fox, F205",
+ "jumped, J408106",
+ "over, O0209",
+ "the, T60",
+ "lazy, L7050",
+ "dogs, D6043"
+ })
+ // @formatter:on
+ void testEncode(final String input, final String expected) {
+ assertEquals(expected, getStringEncoder().encode(input));
}
@Test
- void testEncode() {
- assertEquals("T6036084", getStringEncoder().encode("testing"));
- assertEquals("T6036084", getStringEncoder().encode("TESTING"));
- assertEquals("T60", getStringEncoder().encode("The"));
- assertEquals("Q503", getStringEncoder().encode("quick"));
- assertEquals("B1908", getStringEncoder().encode("brown"));
- assertEquals("F205", getStringEncoder().encode("fox"));
- assertEquals("J408106", getStringEncoder().encode("jumped"));
- assertEquals("O0209", getStringEncoder().encode("over"));
- assertEquals("T60", getStringEncoder().encode("the"));
- assertEquals("L7050", getStringEncoder().encode("lazy"));
- assertEquals("D6043", getStringEncoder().encode("dogs"));
-
- // Testing CODEC-56
+ void testEncodeCodec56() {
assertEquals("D6043", RefinedSoundex.US_ENGLISH.encode("dogs"));
}