01: /*
02: * Copyright 2001-2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
20: import java.util.Collections;
21: import java.util.List;
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 name) {
36: super (name);
37: }
38:
39: public void testComparatorNoArgCon() throws Exception {
40: new StringEncoderComparator();
41: }
42:
43: public void testComparatorWithSoundex() throws Exception {
44: StringEncoderComparator sCompare = new StringEncoderComparator(
45: 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 {
53: StringEncoderComparator sCompare = new StringEncoderComparator(
54: new DoubleMetaphone());
55:
56: String[] testArray = { "Jordan", "Sosa", "Prior", "Pryor" };
57: List testList = Arrays.asList(testArray);
58:
59: String[] controlArray = { "Jordan", "Prior", "Pryor", "Sosa" };
60:
61: Collections.sort(testList, sCompare);
62:
63: String[] resultArray = (String[]) testList
64: .toArray(new String[0]);
65:
66: for (int i = 0; i < resultArray.length; i++) {
67: assertEquals(
68: "Result Array not Equal to Control Array at index: "
69: + i, controlArray[i], resultArray[i]);
70: }
71: }
72:
73: public void testComparatorWithDoubleMetaphoneAndInvalidInput()
74: throws Exception {
75: StringEncoderComparator sCompare = new StringEncoderComparator(
76: new DoubleMetaphone());
77:
78: int compare = sCompare.compare(new Double(3.0), new Long(3));
79: assertEquals(
80: "Trying to compare objects that make no sense to the underlying encoder should return a zero compare code",
81: 0, compare);
82:
83: }
84: }
|