1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.codec.language.bm;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertTrue;
22
23 import java.util.stream.Stream;
24
25 import org.junit.jupiter.params.ParameterizedTest;
26 import org.junit.jupiter.params.provider.Arguments;
27 import org.junit.jupiter.params.provider.MethodSource;
28
29
30
31
32 public class PhoneticEngineTest {
33
34 private static final Integer TEN = Integer.valueOf(10);
35
36 public static Stream<Arguments> data() {
37
38 return Stream.of(
39 Arguments.of("Renault", "rinD|rinDlt|rina|rinalt|rino|rinolt|rinu|rinult", NameType.GENERIC, RuleType.APPROX, Boolean.TRUE, TEN),
40 Arguments.of("Renault", "rYnDlt|rYnalt|rYnult|rinDlt|rinalt|rinolt|rinult", NameType.ASHKENAZI, RuleType.APPROX, Boolean.TRUE, TEN),
41 Arguments.of("Renault", "rinDlt", NameType.ASHKENAZI, RuleType.APPROX, Boolean.TRUE, Integer.valueOf(1)),
42 Arguments.of("Renault", "rinDlt", NameType.SEPHARDIC, RuleType.APPROX, Boolean.TRUE, TEN),
43 Arguments.of("SntJohn-Smith", "sntjonsmit", NameType.GENERIC, RuleType.EXACT, Boolean.TRUE, TEN),
44 Arguments.of("d'ortley", "(ortlaj|ortlej)-(dortlaj|dortlej)", NameType.GENERIC, RuleType.EXACT, Boolean.TRUE, TEN),
45 Arguments.of("van helsing", "(elSink|elsink|helSink|helsink|helzink|xelsink)-(banhelsink|fanhelsink|fanhelzink|vanhelsink|vanhelzink|vanjelsink)", NameType.GENERIC, RuleType.EXACT, Boolean.FALSE, TEN),
46 Arguments.of("Judenburg", "iudnbYrk|iudnbirk|iudnburk|xudnbirk|xudnburk|zudnbirk|zudnburk", NameType.GENERIC, RuleType.APPROX, Boolean.TRUE, TEN),
47 Arguments.of("Judenburg", "iudnbYrk|iudnbirk|iudnburk|xudnbirk|xudnburk|zudnbirk|zudnburk", NameType.GENERIC, RuleType.APPROX, Boolean.TRUE, Integer.MAX_VALUE)
48 );
49
50 }
51
52 public static Stream<Arguments> invalidData() {
53
54 return Stream.of(
55 Arguments.of("bar", "bar|bor|var|vor", NameType.ASHKENAZI, RuleType.APPROX, Boolean.FALSE, TEN),
56 Arguments.of("al", "|al", NameType.SEPHARDIC, RuleType.APPROX, Boolean.FALSE, TEN),
57 Arguments.of("da", "da|di", NameType.GENERIC, RuleType.EXACT, Boolean.FALSE, TEN),
58 Arguments.of("'''", "", NameType.SEPHARDIC, RuleType.APPROX, Boolean.FALSE, TEN),
59 Arguments.of("'''", "", NameType.SEPHARDIC, RuleType.APPROX, Boolean.FALSE, Integer.MAX_VALUE)
60 );
61
62 }
63
64
65 @ParameterizedTest
66 @MethodSource("data")
67 public void testEncode(final String name, final String phoneticExpected, final NameType nameType,
68 final RuleType ruleType, final boolean concat, final int maxPhonemes) {
69 final PhoneticEngine engine = new PhoneticEngine(nameType, ruleType, concat, maxPhonemes);
70
71 final String phoneticActual = engine.encode(name);
72
73 assertEquals(phoneticExpected, phoneticActual, "phoneme incorrect");
74
75 if (concat) {
76 final String[] split = phoneticActual.split("\\|");
77 assertTrue(split.length <= maxPhonemes);
78 } else {
79 final String[] words = phoneticActual.split("-");
80 for (final String word : words) {
81 final String[] split = word.split("\\|");
82 assertTrue(split.length <= maxPhonemes);
83 }
84 }
85 }
86
87 @ParameterizedTest
88 @MethodSource("invalidData")
89 public void testInvalidEncode(final String input, final String phoneticExpected, final NameType nameType,
90 final RuleType ruleType, final boolean concat, final int maxPhonemes) {
91 final PhoneticEngine engine = new PhoneticEngine(nameType, ruleType, concat, maxPhonemes);
92
93 assertEquals(engine.encode(input), phoneticExpected);
94 }
95 }