KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > codec > StringEncoderComparatorTest


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.codec;
18
19 import java.util.Arrays JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.List JavaDoc;
22
23 import junit.framework.TestCase;
24 import org.apache.commons.codec.language.DoubleMetaphone;
25 import org.apache.commons.codec.language.Soundex;
26
27 /**
28  * Test cases for the StingEncoderComparator.
29  *
30  * @version $Id: StringEncoderComparatorTest.java,v 1.11 2004/04/19 01:14:29 ggregory Exp $
31  * @author Apache Software Foundation
32  */

33 public class StringEncoderComparatorTest extends TestCase {
34
35     public StringEncoderComparatorTest(String JavaDoc name) {
36         super(name);
37     }
38
39     public void testComparatorNoArgCon() throws Exception JavaDoc {
40         new StringEncoderComparator();
41     }
42
43     public void testComparatorWithSoundex() throws Exception JavaDoc {
44         StringEncoderComparator sCompare =
45             new StringEncoderComparator( new Soundex() );
46
47         assertTrue( "O'Brien and O'Brian didn't come out with " +
48                     "the same Soundex, something must be wrong here",
49                     0 == sCompare.compare( "O'Brien", "O'Brian" ) );
50     }
51     
52     public void testComparatorWithDoubleMetaphone() throws Exception JavaDoc {
53         StringEncoderComparator sCompare =
54             new StringEncoderComparator( new DoubleMetaphone() );
55             
56         String JavaDoc[] testArray = { "Jordan", "Sosa", "Prior", "Pryor" };
57         List JavaDoc testList = Arrays.asList( testArray );
58         
59         String JavaDoc[] controlArray = { "Jordan", "Prior", "Pryor", "Sosa" };
60
61         Collections.sort( testList, sCompare);
62         
63         String JavaDoc[] resultArray = (String JavaDoc[]) testList.toArray(new String JavaDoc[0]);
64         
65         for( int i = 0; i < resultArray.length; i++) {
66             assertEquals( "Result Array not Equal to Control Array at index: " + i, controlArray[i], resultArray[i] );
67         }
68     }
69
70     public void testComparatorWithDoubleMetaphoneAndInvalidInput() throws Exception JavaDoc {
71         StringEncoderComparator sCompare =
72             new StringEncoderComparator( new DoubleMetaphone() );
73            
74         int compare = sCompare.compare(new Double JavaDoc(3.0), new Long JavaDoc(3));
75         assertEquals( "Trying to compare objects that make no sense to the underlying encoder should return a zero compare code",
76                                 0, compare);
77         
78     }
79 }
80
Popular Tags