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
018package org.apache.commons.codec.language;
019
020import static org.junit.Assert.assertEquals;
021
022import org.apache.commons.codec.EncoderException;
023import org.apache.commons.codec.StringEncoderAbstractTest;
024import org.junit.Test;
025
026/**
027 * Tests RefinedSoundex.
028 *
029 * @version $Id: RefinedSoundexTest.html 891688 2013-12-24 20:49:46Z ggregory $
030 */
031public class RefinedSoundexTest extends StringEncoderAbstractTest<RefinedSoundex> {
032
033    @Override
034    protected RefinedSoundex createStringEncoder() {
035        return new RefinedSoundex();
036    }
037
038    @Test
039    public void testDifference() throws EncoderException {
040        // Edge cases
041        assertEquals(0, this.getStringEncoder().difference(null, null));
042        assertEquals(0, this.getStringEncoder().difference("", ""));
043        assertEquals(0, this.getStringEncoder().difference(" ", " "));
044        // Normal cases
045        assertEquals(6, this.getStringEncoder().difference("Smith", "Smythe"));
046        assertEquals(3, this.getStringEncoder().difference("Ann", "Andrew"));
047        assertEquals(1, this.getStringEncoder().difference("Margaret", "Andrew"));
048        assertEquals(1, this.getStringEncoder().difference("Janet", "Margaret"));
049        // Examples from
050        // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_de-dz_8co5.asp
051        assertEquals(5, this.getStringEncoder().difference("Green", "Greene"));
052        assertEquals(1, this.getStringEncoder().difference("Blotchet-Halls", "Greene"));
053        // Examples from
054        // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_setu-sus_3o6w.asp
055        assertEquals(6, this.getStringEncoder().difference("Smith", "Smythe"));
056        assertEquals(8, this.getStringEncoder().difference("Smithers", "Smythers"));
057        assertEquals(5, this.getStringEncoder().difference("Anothers", "Brothers"));
058    }
059
060    @Test
061    public void testEncode() {
062        assertEquals("T6036084", this.getStringEncoder().encode("testing"));
063        assertEquals("T6036084", this.getStringEncoder().encode("TESTING"));
064        assertEquals("T60", this.getStringEncoder().encode("The"));
065        assertEquals("Q503", this.getStringEncoder().encode("quick"));
066        assertEquals("B1908", this.getStringEncoder().encode("brown"));
067        assertEquals("F205", this.getStringEncoder().encode("fox"));
068        assertEquals("J408106", this.getStringEncoder().encode("jumped"));
069        assertEquals("O0209", this.getStringEncoder().encode("over"));
070        assertEquals("T60", this.getStringEncoder().encode("the"));
071        assertEquals("L7050", this.getStringEncoder().encode("lazy"));
072        assertEquals("D6043", this.getStringEncoder().encode("dogs"));
073
074        // Testing CODEC-56
075        assertEquals("D6043", RefinedSoundex.US_ENGLISH.encode("dogs"));
076    }
077
078    @Test
079    public void testGetMappingCodeNonLetter() {
080        final char code = this.getStringEncoder().getMappingCode('#');
081        assertEquals("Code does not equals zero", 0, code);
082    }
083
084    @Test
085    public void testNewInstance() {
086        assertEquals("D6043", new RefinedSoundex().soundex("dogs"));
087    }
088
089    @Test
090    public void testNewInstance2() {
091        assertEquals("D6043", new RefinedSoundex(RefinedSoundex.US_ENGLISH_MAPPING_STRING.toCharArray()).soundex("dogs"));
092    }
093
094    @Test
095    public void testNewInstance3() {
096        assertEquals("D6043", new RefinedSoundex(RefinedSoundex.US_ENGLISH_MAPPING_STRING).soundex("dogs"));
097    }
098}