View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Tests PhoneticEngine.
31   */
32  public class PhoneticEngineTest {
33  
34      private static final Integer TEN = Integer.valueOf(10);
35  
36      public static Stream<Arguments> data() {
37          // @formatter:off
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          // @formatter:on
50      }
51  
52      public static Stream<Arguments> invalidData() {
53          // @formatter:off
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          // @formatter:on
62      }
63  
64      // TODO Identify if there is a need to an assertTimeout(Duration.ofMillis(10000L) in some point, since this method was marked as @Test(timeout = 10000L)
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  }