001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.codec.language;
019    
020    import static org.junit.Assert.assertEquals;
021    
022    import org.apache.commons.codec.EncoderException;
023    import org.apache.commons.codec.StringEncoder;
024    import org.apache.commons.codec.StringEncoderAbstractTest;
025    import org.junit.Test;
026    
027    /**
028     * Tests RefinedSoundex.
029     *
030     * @version $Id: RefinedSoundexTest.html 889935 2013-12-11 05:05:13Z ggregory $
031     */
032    public class RefinedSoundexTest extends StringEncoderAbstractTest {
033    
034        @Override
035        protected StringEncoder createStringEncoder() {
036            return new RefinedSoundex();
037        }
038    
039        /**
040         * @return Returns the encoder.
041         */
042        private RefinedSoundex getRefinedSoundex() {
043            return (RefinedSoundex)this.getStringEncoder();
044        }
045    
046        @Test
047        public void testDifference() throws EncoderException {
048            // Edge cases
049            assertEquals(0, this.getRefinedSoundex().difference(null, null));
050            assertEquals(0, this.getRefinedSoundex().difference("", ""));
051            assertEquals(0, this.getRefinedSoundex().difference(" ", " "));
052            // Normal cases
053            assertEquals(6, this.getRefinedSoundex().difference("Smith", "Smythe"));
054            assertEquals(3, this.getRefinedSoundex().difference("Ann", "Andrew"));
055            assertEquals(1, this.getRefinedSoundex().difference("Margaret", "Andrew"));
056            assertEquals(1, this.getRefinedSoundex().difference("Janet", "Margaret"));
057            // Examples from
058            // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_de-dz_8co5.asp
059            assertEquals(5, this.getRefinedSoundex().difference("Green", "Greene"));
060            assertEquals(1, this.getRefinedSoundex().difference("Blotchet-Halls", "Greene"));
061            // Examples from
062            // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_setu-sus_3o6w.asp
063            assertEquals(6, this.getRefinedSoundex().difference("Smith", "Smythe"));
064            assertEquals(8, this.getRefinedSoundex().difference("Smithers", "Smythers"));
065            assertEquals(5, this.getRefinedSoundex().difference("Anothers", "Brothers"));
066        }
067    
068        @Test
069        public void testEncode() {
070            assertEquals("T6036084", this.getRefinedSoundex().encode("testing"));
071            assertEquals("T6036084", this.getRefinedSoundex().encode("TESTING"));
072            assertEquals("T60", this.getRefinedSoundex().encode("The"));
073            assertEquals("Q503", this.getRefinedSoundex().encode("quick"));
074            assertEquals("B1908", this.getRefinedSoundex().encode("brown"));
075            assertEquals("F205", this.getRefinedSoundex().encode("fox"));
076            assertEquals("J408106", this.getRefinedSoundex().encode("jumped"));
077            assertEquals("O0209", this.getRefinedSoundex().encode("over"));
078            assertEquals("T60", this.getRefinedSoundex().encode("the"));
079            assertEquals("L7050", this.getRefinedSoundex().encode("lazy"));
080            assertEquals("D6043", this.getRefinedSoundex().encode("dogs"));
081    
082            // Testing CODEC-56
083            assertEquals("D6043", RefinedSoundex.US_ENGLISH.encode("dogs"));
084        }
085    
086        @Test
087        public void testGetMappingCodeNonLetter() {
088            char code = this.getRefinedSoundex().getMappingCode('#');
089            assertEquals("Code does not equals zero", 0, code);
090        }
091    
092        @Test
093        public void testNewInstance() {
094            assertEquals("D6043", new RefinedSoundex().soundex("dogs"));
095        }
096    
097        @Test
098        public void testNewInstance2() {
099            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    }