Skip to content

Commit e398498

Browse files
committed
http://nagoya.apache.org/bugzilla/show_bug.cgi?id=29080
Throw an exception with a clear message. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/codec/trunk@130373 13f79535-47bb-0310-9956-ffa450edef68
1 parent 3ac0571 commit e398498

2 files changed

Lines changed: 39 additions & 15 deletions

File tree

src/java/org/apache/commons/codec/language/Soundex.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* with similar phonemes.
2626
*
2727
* @author Apache Software Foundation
28-
* @version $Id: Soundex.java,v 1.21 2004/03/17 18:30:59 ggregory Exp $
28+
* @version $Id: Soundex.java,v 1.22 2004/06/02 00:55:29 ggregory Exp $
2929
*/
3030
public class Soundex implements StringEncoder {
3131

@@ -117,17 +117,10 @@ public Soundex(char[] mapping) {
117117
* if the parameter supplied is not of type java.lang.String
118118
*/
119119
public Object encode(Object pObject) throws EncoderException {
120-
121-
Object result;
122-
123-
if (!(pObject instanceof java.lang.String)) {
120+
if (!(pObject instanceof String)) {
124121
throw new EncoderException("Parameter supplied to Soundex encode is not of type java.lang.String");
125-
} else {
126-
result = soundex((String) pObject);
127-
}
128-
129-
return result;
130-
122+
}
123+
return soundex((String) pObject);
131124
}
132125

133126
/**
@@ -196,7 +189,11 @@ private char[] getSoundexMapping() {
196189
* @return A Soundex code.
197190
*/
198191
private char map(char c) {
199-
return this.getSoundexMapping()[c - 'A'];
192+
int index = c - 'A';
193+
if (index < 0 || index >= this.getSoundexMapping().length) {
194+
throw new IllegalArgumentException("The character is not mapped: " + c);
195+
}
196+
return this.getSoundexMapping()[index];
200197
}
201198

202199
/**

src/test/org/apache/commons/codec/language/SoundexTest.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/**
2727
* Tests {@link Soundex}
2828
*
29-
* @version $Id: SoundexTest.java,v 1.17 2004/04/19 01:14:29 ggregory Exp $
29+
* @version $Id: SoundexTest.java,v 1.18 2004/06/02 00:55:38 ggregory Exp $
3030
* @author Apache Software Foundation
3131
*/
3232
public class SoundexTest extends StringEncoderAbstractTest {
@@ -219,8 +219,6 @@ public void testEncodeIgnoreApostrophes() {
219219

220220
/**
221221
* Test data from http://www.myatt.demon.co.uk/sxalg.htm
222-
*
223-
* @throws EncoderException
224222
*/
225223
public void testEncodeIgnoreHyphens() {
226224
this.encodeAll(
@@ -339,4 +337,33 @@ public void testMsSqlServer3() {
339337
assertEquals("A500", this.getEncoder().encode("Anne"));
340338
}
341339

340+
/**
341+
* Fancy characters are not mapped by the default US mapping.
342+
*
343+
* http://nagoya.apache.org/bugzilla/show_bug.cgi?id=29080
344+
*/
345+
public void testUsMappingOWithDiaeresis() {
346+
assertEquals("O000", this.getEncoder().encode("o"));
347+
try {
348+
assertEquals("Ö000", this.getEncoder().encode("ö"));
349+
fail("Expected IllegalArgumentException not thrown");
350+
} catch (IllegalArgumentException e) {
351+
// expected
352+
}
353+
}
354+
355+
/**
356+
* Fancy characters are not mapped by the default US mapping.
357+
*
358+
* http://nagoya.apache.org/bugzilla/show_bug.cgi?id=29080
359+
*/
360+
public void testUsMappingEWithAcute() {
361+
assertEquals("E000", this.getEncoder().encode("e"));
362+
try {
363+
assertEquals("É000", this.getEncoder().encode("é"));
364+
fail("Expected IllegalArgumentException not thrown");
365+
} catch (IllegalArgumentException e) {
366+
// expected
367+
}
368+
}
342369
}

0 commit comments

Comments
 (0)