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;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  
22  import org.apache.commons.codec.AbstractStringEncoderTest;
23  import org.apache.commons.codec.EncoderException;
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * Tests RefinedSoundex.
28   */
29  public class RefinedSoundexTest extends AbstractStringEncoderTest<RefinedSoundex> {
30  
31      @Override
32      protected RefinedSoundex createStringEncoder() {
33          return new RefinedSoundex();
34      }
35  
36      @Test
37      public void testDifference() throws EncoderException {
38          // Edge cases
39          assertEquals(0, this.getStringEncoder().difference(null, null));
40          assertEquals(0, this.getStringEncoder().difference("", ""));
41          assertEquals(0, this.getStringEncoder().difference(" ", " "));
42          // Normal cases
43          assertEquals(6, this.getStringEncoder().difference("Smith", "Smythe"));
44          assertEquals(3, this.getStringEncoder().difference("Ann", "Andrew"));
45          assertEquals(1, this.getStringEncoder().difference("Margaret", "Andrew"));
46          assertEquals(1, this.getStringEncoder().difference("Janet", "Margaret"));
47          // Examples from
48          // https://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_de-dz_8co5.asp
49          assertEquals(5, this.getStringEncoder().difference("Green", "Greene"));
50          assertEquals(1, this.getStringEncoder().difference("Blotchet-Halls", "Greene"));
51          // Examples from
52          // https://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_setu-sus_3o6w.asp
53          assertEquals(6, this.getStringEncoder().difference("Smith", "Smythe"));
54          assertEquals(8, this.getStringEncoder().difference("Smithers", "Smythers"));
55          assertEquals(5, this.getStringEncoder().difference("Anothers", "Brothers"));
56      }
57  
58      @Test
59      public void testEncode() {
60          assertEquals("T6036084", this.getStringEncoder().encode("testing"));
61          assertEquals("T6036084", this.getStringEncoder().encode("TESTING"));
62          assertEquals("T60", this.getStringEncoder().encode("The"));
63          assertEquals("Q503", this.getStringEncoder().encode("quick"));
64          assertEquals("B1908", this.getStringEncoder().encode("brown"));
65          assertEquals("F205", this.getStringEncoder().encode("fox"));
66          assertEquals("J408106", this.getStringEncoder().encode("jumped"));
67          assertEquals("O0209", this.getStringEncoder().encode("over"));
68          assertEquals("T60", this.getStringEncoder().encode("the"));
69          assertEquals("L7050", this.getStringEncoder().encode("lazy"));
70          assertEquals("D6043", this.getStringEncoder().encode("dogs"));
71  
72          // Testing CODEC-56
73          assertEquals("D6043", RefinedSoundex.US_ENGLISH.encode("dogs"));
74      }
75  
76      @Test
77      public void testGetMappingCodeNonLetter() {
78          final char code = this.getStringEncoder().getMappingCode('#');
79          assertEquals(0, code, "Code does not equals zero");
80      }
81  
82      @Test
83      public void testInvalidSoundexCharacter() {
84          final char[] invalid = new char[256];
85          for (int i = 0; i < invalid.length; i++) {
86              invalid[i] = (char) i;
87          }
88  
89          assertEquals(new RefinedSoundex().encode(new String(invalid)), "A0136024043780159360205050136024043780159360205053");
90      }
91  
92      @Test
93      public void testNewInstance() {
94          assertEquals("D6043", new RefinedSoundex().soundex("dogs"));
95      }
96  
97      @Test
98      public void testNewInstance2() {
99          assertEquals("D6043", new RefinedSoundex(RefinedSoundex.US_ENGLISH_MAPPING_STRING.toCharArray()).soundex("dogs"));
100     }
101 
102     @Test
103     public void testNewInstance3() {
104         assertEquals("D6043", new RefinedSoundex(RefinedSoundex.US_ENGLISH_MAPPING_STRING).soundex("dogs"));
105     }
106 }