Skip to content

Commit 9869cfe

Browse files
committed
More tests.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1298599 13f79535-47bb-0310-9956-ffa450edef68
1 parent 28cb227 commit 9869cfe

1 file changed

Lines changed: 133 additions & 27 deletions

File tree

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

Lines changed: 133 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,19 @@
3333
*/
3434
public class NysiisTest extends StringEncoderAbstractTest {
3535

36+
private final Nysiis fullNysiis = new Nysiis(false);
37+
3638
@Override
3739
protected StringEncoder createStringEncoder() {
3840
return new Nysiis();
3941
}
4042

41-
protected StringEncoder createStringEncoder(boolean trueLength) {
42-
return new Nysiis(trueLength);
43-
}
44-
4543
private void encodeAll(String[] strings, String expectedEncoding) throws EncoderException {
4644
for (int i = 0; i < strings.length; i++) {
4745
Assert.assertEquals("Problem with " + strings[i], expectedEncoding, getStringEncoder().encode(strings[i]));
4846
}
4947
}
5048

51-
@Test
52-
public void testTrueVariant() {
53-
Nysiis encoder = new Nysiis(true);
54-
55-
String encoded = encoder.encode("WESTERLUND");
56-
Assert.assertTrue(encoded.length() <= 6);
57-
Assert.assertEquals("WASTAR", encoded);
58-
}
59-
6049
@Test
6150
public void testBran() throws EncoderException {
6251
encodeAll(new String[] { "Brian", "Brown", "Brun" }, "BRAN");
@@ -79,17 +68,6 @@ public void testDan() throws EncoderException {
7968
this.encodeAll(new String[] { "Dane", "Dean", "Dionne" }, "DAN");
8069
}
8170

82-
@Test
83-
public void testSpecialBranches() throws EncoderException {
84-
this.encodeAll(new String[] { "Kobwick" }, "CABWAC");
85-
this.encodeAll(new String[] { "Kocher" }, "CACAR");
86-
this.encodeAll(new String[] { "Fesca" }, "FASC");
87-
this.encodeAll(new String[] { "Shom" }, "SAN");
88-
this.encodeAll(new String[] { "Ohlo" }, "OL");
89-
this.encodeAll(new String[] { "Uhu" }, "UH");
90-
this.encodeAll(new String[] { "Um" }, "UN");
91-
}
92-
9371
@Test
9472
public void testDropBy() throws EncoderException {
9573
List<String[]> testValues =
@@ -121,7 +99,7 @@ public void testDropBy() throws EncoderException {
12199
new String[] { "YAMADA", "YANAD" });
122100

123101
for (String[] arr : testValues) {
124-
Assert.assertEquals("Problem with " + arr[0], arr[1], createStringEncoder(false).encode(arr[0]));
102+
Assert.assertEquals("Problem with " + arr[0], arr[1], this.fullNysiis.encode(arr[0]));
125103
}
126104
}
127105

@@ -223,7 +201,7 @@ public void testDropBy2() throws EncoderException {
223201
new String[] { "YAMADA", "YANAD" });
224202

225203
for (String[] arr : testValues) {
226-
Assert.assertEquals("Problem with " + arr[0], arr[1], createStringEncoder(false).encode(arr[0]));
204+
Assert.assertEquals("Problem with " + arr[0], arr[1], this.fullNysiis.encode(arr[0]));
227205
}
228206
}
229207

@@ -250,10 +228,118 @@ public void testOthers() throws EncoderException {
250228
new String[] { "FUZZY", "FASY" });
251229

252230
for (String[] arr : testValues) {
253-
Assert.assertEquals("Problem with " + arr[0], arr[1], createStringEncoder(false).encode(arr[0]));
231+
Assert.assertEquals("Problem with " + arr[0], arr[1], this.fullNysiis.encode(arr[0]));
232+
}
233+
}
234+
235+
/**
236+
* Tests rule 1: Translate first characters of name: MAC → MCC, KN → N, K → C, PH, PF → FF, SCH → SSS
237+
*
238+
* @throws EncoderException
239+
*/
240+
@Test
241+
public void testRule1() throws EncoderException {
242+
List<String[]> testValues =
243+
Arrays.asList(
244+
new String[] { "MACX", "MCX" },
245+
new String[] { "KNX", "NX" },
246+
new String[] { "KX", "CX" },
247+
new String[] { "PHX", "FX" },
248+
new String[] { "PFX", "FX" },
249+
new String[] { "SCHX", "SX" });
250+
for (String[] arr : testValues) {
251+
Assert.assertEquals("Problem with " + arr[0], arr[1], this.fullNysiis.encode(arr[0]));
252+
}
253+
}
254+
255+
/**
256+
* Tests rule 2: Translate last characters of name: EE → Y, IE → Y, DT, RT, RD, NT, ND → D
257+
*
258+
* @throws EncoderException
259+
*/
260+
@Test
261+
public void testRule2() throws EncoderException {
262+
List<String[]> testValues =
263+
Arrays.asList(
264+
new String[] { "XEE", "XY" },
265+
new String[] { "XIE", "XY" },
266+
new String[] { "XDT", "XD" },
267+
new String[] { "XRT", "XD" },
268+
new String[] { "XRD", "XD" },
269+
new String[] { "XNT", "XD" },
270+
new String[] { "XND", "XD" });
271+
for (String[] arr : testValues) {
272+
Assert.assertEquals("Problem with " + arr[0], arr[1], this.fullNysiis.encode(arr[0]));
254273
}
255274
}
256275

276+
/**
277+
* Tests rule 4.1: EV → AF else A, E, I, O, U → A
278+
*
279+
* @throws EncoderException
280+
*/
281+
@Test
282+
public void testRule4Dot1() throws EncoderException {
283+
List<String[]> testValues =
284+
Arrays.asList(
285+
new String[] { "XEV", "XAF" },
286+
new String[] { "XAX", "XAX" },
287+
new String[] { "XEX", "XAX" },
288+
new String[] { "XIX", "XAX" },
289+
new String[] { "XOX", "XAX" },
290+
new String[] { "XUX", "XAX" });
291+
for (String[] arr : testValues) {
292+
Assert.assertEquals("Problem with " + arr[0], arr[1], this.fullNysiis.encode(arr[0]));
293+
}
294+
}
295+
296+
/**
297+
* Tests rule 5: If last character is S, remove it.
298+
*
299+
* @throws EncoderException
300+
*/
301+
@Test
302+
public void testRule5() throws EncoderException {
303+
List<String[]> testValues =
304+
Arrays.asList(
305+
new String[] { "XS", "X" },
306+
new String[] { "XSS", "X" });
307+
for (String[] arr : testValues) {
308+
Assert.assertEquals("Problem with " + arr[0], arr[1], this.fullNysiis.encode(arr[0]));
309+
}
310+
}
311+
312+
/**
313+
* Tests rule 6: If last characters are AY, replace with Y.
314+
*
315+
* @throws EncoderException
316+
*/
317+
@Test
318+
public void testRule6() throws EncoderException {
319+
List<String[]> testValues =
320+
Arrays.asList(
321+
new String[] { "XAY", "XY" },
322+
new String[] { "XAYS", "XY" }); // Rules 5, 6
323+
for (String[] arr : testValues) {
324+
Assert.assertEquals("Problem with " + arr[0], arr[1], this.fullNysiis.encode(arr[0]));
325+
}
326+
}
327+
328+
/**
329+
* Tests rule 7: If last character is A, remove it.
330+
*
331+
* @throws EncoderException
332+
*/
333+
@Test
334+
public void testRule7() throws EncoderException {
335+
List<String[]> testValues =
336+
Arrays.asList(
337+
new String[] { "XA", "X" },
338+
new String[] { "XAS", "X" }); // Rules 5, 7
339+
for (String[] arr : testValues) {
340+
Assert.assertEquals("Problem with " + arr[0], arr[1], this.fullNysiis.encode(arr[0]));
341+
}
342+
}
257343
@Test
258344
public void testSnad() throws EncoderException {
259345
// Data Quality and Record Linkage Techniques P.121 claims this is SNAT,
@@ -266,9 +352,29 @@ public void testSnat() throws EncoderException {
266352
this.encodeAll(new String[] { "Smith", "Schmit" }, "SNAT");
267353
}
268354

355+
@Test
356+
public void testSpecialBranches() throws EncoderException {
357+
this.encodeAll(new String[] { "Kobwick" }, "CABWAC");
358+
this.encodeAll(new String[] { "Kocher" }, "CACAR");
359+
this.encodeAll(new String[] { "Fesca" }, "FASC");
360+
this.encodeAll(new String[] { "Shom" }, "SAN");
361+
this.encodeAll(new String[] { "Ohlo" }, "OL");
362+
this.encodeAll(new String[] { "Uhu" }, "UH");
363+
this.encodeAll(new String[] { "Um" }, "UN");
364+
}
365+
269366
@Test
270367
public void testTranan() throws EncoderException {
271368
this.encodeAll(new String[] { "Trueman", "Truman" }, "TRANAN");
272369
}
273370

371+
@Test
372+
public void testTrueVariant() {
373+
Nysiis encoder = new Nysiis(true);
374+
375+
String encoded = encoder.encode("WESTERLUND");
376+
Assert.assertTrue(encoded.length() <= 6);
377+
Assert.assertEquals("WASTAR", encoded);
378+
}
379+
274380
}

0 commit comments

Comments
 (0)