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;
019    
020    import static org.junit.Assert.assertEquals;
021    import static org.junit.Assert.assertTrue;
022    
023    import java.util.Arrays;
024    import java.util.Collections;
025    import java.util.List;
026    
027    import org.apache.commons.codec.language.DoubleMetaphone;
028    import org.apache.commons.codec.language.Soundex;
029    import org.junit.Test;
030    
031    /**
032     * Test cases for the StingEncoderComparator.
033     *
034     * @version $Id: StringEncoderComparatorTest.html 889935 2013-12-11 05:05:13Z ggregory $
035     */
036    public class StringEncoderComparatorTest {
037    
038        @Test
039        public void testComparatorWithSoundex() throws Exception {
040            final StringEncoderComparator sCompare =
041                new StringEncoderComparator( new Soundex() );
042    
043            assertTrue( "O'Brien and O'Brian didn't come out with " +
044                        "the same Soundex, something must be wrong here",
045                        0 == sCompare.compare( "O'Brien", "O'Brian" ) );
046        }
047    
048        @Test
049        public void testComparatorWithDoubleMetaphone() throws Exception {
050            final StringEncoderComparator sCompare = new StringEncoderComparator(new DoubleMetaphone());
051    
052            final String[] testArray = { "Jordan", "Sosa", "Prior", "Pryor" };
053            final List<String> testList = Arrays.asList(testArray);
054    
055            final String[] controlArray = { "Jordan", "Prior", "Pryor", "Sosa" };
056    
057            Collections.sort(testList, sCompare);
058    
059            final String[] resultArray = testList.toArray(new String[0]);
060    
061            for (int i = 0; i < resultArray.length; i++) {
062                assertEquals("Result Array not Equal to Control Array at index: " + i, controlArray[i], resultArray[i]);
063            }
064        }
065    
066        @Test
067        public void testComparatorWithDoubleMetaphoneAndInvalidInput() throws Exception {
068            final StringEncoderComparator sCompare =
069                new StringEncoderComparator( new DoubleMetaphone() );
070    
071            final int compare = sCompare.compare(new Double(3.0), Long.valueOf(3));
072            assertEquals( "Trying to compare objects that make no sense to the underlying encoder should return a zero compare code",
073                                    0, compare);
074        }
075    }