KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > codec > language > RefinedSoundexTest


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.language;
18
19 import junit.framework.Test;
20 import junit.framework.TestSuite;
21 import org.apache.commons.codec.EncoderException;
22 import org.apache.commons.codec.StringEncoder;
23 import org.apache.commons.codec.StringEncoderAbstractTest;
24
25 /**
26  * Tests RefinedSoundex.
27  *
28  * @version $Id: RefinedSoundexTest.java,v 1.11 2004/05/24 00:17:24 ggregory Exp $
29  * @author Apache Software Foundation
30  */

31 public class RefinedSoundexTest extends StringEncoderAbstractTest {
32
33     public static Test suite() {
34         return (new TestSuite(RefinedSoundexTest.class));
35     }
36
37     private RefinedSoundex encoder = null;
38
39     public RefinedSoundexTest(String JavaDoc name) {
40         super(name);
41     }
42
43     /**
44      * @return Returns the encoder.
45      */

46     private RefinedSoundex getEncoder() {
47         return this.encoder;
48     }
49
50     protected StringEncoder makeEncoder() {
51         return new RefinedSoundex();
52     }
53
54     /**
55      * @param encoder
56      * The encoder to set.
57      */

58     private void setEncoder(RefinedSoundex encoder) {
59         this.encoder = encoder;
60     }
61
62     public void setUp() throws Exception JavaDoc {
63         super.setUp();
64         this.setEncoder(new RefinedSoundex());
65     }
66
67     public void tearDown() throws Exception JavaDoc {
68         super.tearDown();
69         this.setEncoder(null);
70     }
71
72     public void testDifference() throws EncoderException {
73         // Edge cases
74
assertEquals(0, this.getEncoder().difference(null, null));
75         assertEquals(0, this.getEncoder().difference("", ""));
76         assertEquals(0, this.getEncoder().difference(" ", " "));
77         // Normal cases
78
assertEquals(6, this.getEncoder().difference("Smith", "Smythe"));
79         assertEquals(3, this.getEncoder().difference("Ann", "Andrew"));
80         assertEquals(1, this.getEncoder().difference("Margaret", "Andrew"));
81         assertEquals(1, this.getEncoder().difference("Janet", "Margaret"));
82         // Examples from
83
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_de-dz_8co5.asp
84
assertEquals(5, this.getEncoder().difference("Green", "Greene"));
85         assertEquals(1, this.getEncoder().difference("Blotchet-Halls", "Greene"));
86         // Examples from
87
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_setu-sus_3o6w.asp
88
assertEquals(6, this.getEncoder().difference("Smith", "Smythe"));
89         assertEquals(8, this.getEncoder().difference("Smithers", "Smythers"));
90         assertEquals(5, this.getEncoder().difference("Anothers", "Brothers"));
91     }
92
93     public void testEncode() {
94         assertEquals("T6036084", this.getEncoder().encode("testing"));
95         assertEquals("T6036084", this.getEncoder().encode("TESTING"));
96         assertEquals("T60", this.getEncoder().encode("The"));
97         assertEquals("Q503", this.getEncoder().encode("quick"));
98         assertEquals("B1908", this.getEncoder().encode("brown"));
99         assertEquals("F205", this.getEncoder().encode("fox"));
100         assertEquals("J408106", this.getEncoder().encode("jumped"));
101         assertEquals("O0209", this.getEncoder().encode("over"));
102         assertEquals("T60", this.getEncoder().encode("the"));
103         assertEquals("L7050", this.getEncoder().encode("lazy"));
104         assertEquals("D6043", this.getEncoder().encode("dogs"));
105     }
106
107     public void testGetMappingCodeNonLetter() {
108         char code = this.getEncoder().getMappingCode('#');
109         assertEquals("Code does not equals zero", 0, code);
110     }
111 }
Popular Tags