Skip to content

Commit e9a2edc

Browse files
committed
Add test to try and check all possible combinations are covered.
Add tests for two missing combinations git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1842102 13f79535-47bb-0310-9956-ffa450edef68
1 parent a9690db commit e9a2edc

1 file changed

Lines changed: 70 additions & 0 deletions

File tree

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@
1717

1818
package org.apache.commons.codec.language;
1919

20+
import java.util.HashSet;
21+
import java.util.Locale;
22+
import java.util.Set;
23+
2024
import org.apache.commons.codec.EncoderException;
2125
import org.apache.commons.codec.StringEncoderAbstractTest;
26+
import org.junit.AfterClass;
2227
import org.junit.Assert;
2328
import org.junit.Test;
2429

@@ -30,11 +35,74 @@
3035
*/
3136
public class ColognePhoneticTest extends StringEncoderAbstractTest<ColognePhonetic> {
3237

38+
private static final Set<String> TESTSET = new HashSet<String>();
39+
40+
private static boolean hasTestCase(String re) {
41+
for(String s : TESTSET) {
42+
if (s.matches(re)) {
43+
return true;
44+
}
45+
}
46+
return false;
47+
}
48+
49+
// Character sequences to be tested by the code
50+
private static final String MATCHES[] = {
51+
".*[AEIOUJY].*", // A, E, I, J, O, U, Y
52+
".*H.*", // H
53+
".*B.*", // B
54+
".*P[^H].*", // P not before H
55+
".*[DT][^CSZ].*", // D,T not before C,S,Z
56+
".*[FVW].*", // F,V,W
57+
".*PH.*", // P before H
58+
".*[GKQ].*", // G,K,Q
59+
"C[AHKLOQRUX].*", // Initial C before A, H, K, L, O, Q, R, U, X
60+
".*[^SZ]C[AHKLOQRUX].*", // C before A, H, K, L, O, Q, R, U, X but not after S, Z
61+
".*[^CKQ]X.*", // X not after C,K,Q
62+
".*L.*", // L
63+
".*[MN].*", // M,N
64+
".*R.*", // R
65+
".*[SZ].*", // S,Z
66+
".*[SZ]C.*", // C after S,Z
67+
"C[^AHKLOQRUX].*", // Initial C except before A, H, K, L, O, Q, R, U, X
68+
".+C[^AHKLOQRUX].*", // C except before A, H, K, L, O, Q, R, U, X
69+
".*[DT][CSZ].*", // D,T before C,S,Z
70+
".*[CKQ]X.*", // X after C,K,Q
71+
};
72+
73+
@AfterClass
74+
// Check that all possible input sequence conditions are represented
75+
public static void finishTests() {
76+
boolean ok = true;
77+
int errors = 0;
78+
for(String m : MATCHES) {
79+
if (!hasTestCase(m)) {
80+
System.out.println(m + " has no test case");
81+
errors++;
82+
}
83+
}
84+
Assert.assertEquals("Not expecting any missing test cases", 0, errors);
85+
}
86+
87+
@Override
88+
// Capture test strings for later checking
89+
public void checkEncoding(String expected, String source) throws EncoderException {
90+
// Note that the German letter Eszett is converted to SS by toUpperCase, so we don't need to replace it
91+
TESTSET.add(source.toUpperCase(Locale.GERMAN).replace('Ä', 'A').replace('Ö', 'O').replace('Ü', 'U'));
92+
super.checkEncoding(expected, source);
93+
}
94+
3395
@Override
3496
protected ColognePhonetic createStringEncoder() {
3597
return new ColognePhonetic();
3698
}
3799

100+
@Test(expected=org.junit.ComparisonFailure.class)
101+
// Ensure that override still allows tests to work
102+
public void testCanFail() throws EncoderException {
103+
this.checkEncoding("/", "Fehler");
104+
}
105+
38106
@Test
39107
public void testAabjoe() throws EncoderException {
40108
this.checkEncoding("01", "Aabjoe");
@@ -123,6 +191,8 @@ public void testExamples() throws EncoderException {
123191
{"Arbeitsamt", "071862"},
124192
{"Eberhard", "01772"},
125193
{"Eberhardt", "01772"},
194+
{"Celsius", "8588"},
195+
{"Ace", "08"},
126196
{"heithabu", "021"}};
127197
this.checkEncodings(data);
128198
}

0 commit comments

Comments
 (0)