1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.codec.language;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21
22 import java.util.stream.Stream;
23
24 import org.apache.commons.codec.AbstractStringEncoderTest;
25 import org.apache.commons.codec.EncoderException;
26 import org.junit.jupiter.api.Test;
27 import org.junit.jupiter.params.ParameterizedTest;
28 import org.junit.jupiter.params.provider.Arguments;
29 import org.junit.jupiter.params.provider.CsvSource;
30 import org.junit.jupiter.params.provider.MethodSource;
31 import org.junit.jupiter.params.provider.NullAndEmptySource;
32 import org.junit.jupiter.params.provider.ValueSource;
33
34
35
36
37 class RefinedSoundexTest extends AbstractStringEncoderTest<RefinedSoundex> {
38
39 static Stream<Arguments> differenceCases() {
40
41 return Stream.of(
42
43 Arguments.of("Smith", "Smythe", 6),
44 Arguments.of("Ann", "Andrew", 3),
45 Arguments.of("Margaret", "Andrew", 1),
46 Arguments.of("Janet", "Margaret", 1),
47
48
49 Arguments.of("Green", "Greene", 5),
50 Arguments.of("Blotchet-Halls", "Greene", 1),
51
52
53 Arguments.of("Smith", "Smythe", 6),
54 Arguments.of("Smithers", "Smythers", 8),
55 Arguments.of("Anothers", "Brothers", 5));
56
57 }
58
59 @Override
60 protected RefinedSoundex createStringEncoder() {
61 return new RefinedSoundex();
62 }
63
64 @ParameterizedTest(name = "{0}, {1}: difference {2}")
65 @MethodSource("differenceCases")
66 void testDifference(final String left, final String right, final int expected) throws EncoderException {
67 assertEquals(expected, getStringEncoder().difference(left, right));
68 }
69
70 @ParameterizedTest
71 @NullAndEmptySource
72 @ValueSource(strings = { " " })
73 void testDifferenceEmpty(final String input) throws EncoderException {
74 assertEquals(0, getStringEncoder().difference(input, input));
75 }
76
77 @ParameterizedTest(name = "{0}: {1}")
78
79 @CsvSource({
80 "testing, T6036084",
81 "TESTING, T6036084",
82 "The, T60",
83 "quick, Q503",
84 "brown, B1908",
85 "fox, F205",
86 "jumped, J408106",
87 "over, O0209",
88 "the, T60",
89 "lazy, L7050",
90 "dogs, D6043"
91 })
92
93 void testEncode(final String input, final String expected) {
94 assertEquals(expected, getStringEncoder().encode(input));
95 }
96
97 @Test
98 void testEncodeCodec56() {
99 assertEquals("D6043", RefinedSoundex.US_ENGLISH.encode("dogs"));
100 }
101
102 @Test
103 void testGetMappingCodeNonLetter() {
104 final char code = getStringEncoder().getMappingCode('#');
105 assertEquals(0, code, "Code does not equals zero");
106 }
107
108 @Test
109 void testInvalidSoundexCharacter() {
110 final char[] invalid = new char[256];
111 for (int i = 0; i < invalid.length; i++) {
112 invalid[i] = (char) i;
113 }
114
115 assertEquals(new RefinedSoundex().encode(new String(invalid)), "A0136024043780159360205050136024043780159360205053");
116 }
117
118 @Test
119 void testNewInstance() {
120 assertEquals("D6043", new RefinedSoundex().soundex("dogs"));
121 }
122
123 @Test
124 void testNewInstance2() {
125 assertEquals("D6043", new RefinedSoundex(RefinedSoundex.US_ENGLISH_MAPPING_STRING.toCharArray()).soundex("dogs"));
126 }
127
128 @Test
129 void testNewInstance3() {
130 assertEquals("D6043", new RefinedSoundex(RefinedSoundex.US_ENGLISH_MAPPING_STRING).soundex("dogs"));
131 }
132 }