|
17 | 17 |
|
18 | 18 | package org.apache.commons.codec.language; |
19 | 19 |
|
| 20 | +import java.util.HashSet; |
| 21 | +import java.util.Locale; |
| 22 | +import java.util.Set; |
| 23 | + |
20 | 24 | import org.apache.commons.codec.EncoderException; |
21 | 25 | import org.apache.commons.codec.StringEncoderAbstractTest; |
| 26 | +import org.junit.AfterClass; |
22 | 27 | import org.junit.Assert; |
23 | 28 | import org.junit.Test; |
24 | 29 |
|
|
30 | 35 | */ |
31 | 36 | public class ColognePhoneticTest extends StringEncoderAbstractTest<ColognePhonetic> { |
32 | 37 |
|
| 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 | + |
33 | 95 | @Override |
34 | 96 | protected ColognePhonetic createStringEncoder() { |
35 | 97 | return new ColognePhonetic(); |
36 | 98 | } |
37 | 99 |
|
| 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 | + |
38 | 106 | @Test |
39 | 107 | public void testAabjoe() throws EncoderException { |
40 | 108 | this.checkEncoding("01", "Aabjoe"); |
@@ -123,6 +191,8 @@ public void testExamples() throws EncoderException { |
123 | 191 | {"Arbeitsamt", "071862"}, |
124 | 192 | {"Eberhard", "01772"}, |
125 | 193 | {"Eberhardt", "01772"}, |
| 194 | + {"Celsius", "8588"}, |
| 195 | + {"Ace", "08"}, |
126 | 196 | {"heithabu", "021"}}; |
127 | 197 | this.checkEncodings(data); |
128 | 198 | } |
|
0 commit comments