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    *      https://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;
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   * Tests RefinedSoundex.
36   */
37  class RefinedSoundexTest extends AbstractStringEncoderTest<RefinedSoundex> {
38  
39      static Stream<Arguments> differenceCases() {
40          // @formatter:off
41          return Stream.of(
42                  // Normal cases
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                  // Examples from
48                  // https://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_de-dz_8co5.asp
49                  Arguments.of("Green", "Greene", 5),
50                  Arguments.of("Blotchet-Halls", "Greene", 1),
51                  // Examples from
52                  // https://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_setu-sus_3o6w.asp
53                  Arguments.of("Smith", "Smythe", 6),
54                  Arguments.of("Smithers", "Smythers", 8),
55                  Arguments.of("Anothers", "Brothers", 5));
56          // @formatter:on
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      // @formatter:off
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      // @formatter:on
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 }