Skip to content

Commit 22b7bd4

Browse files
committed
Refactor encoding tests.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1075410 13f79535-47bb-0310-9956-ffa450edef68
1 parent 9e2945f commit 22b7bd4

3 files changed

Lines changed: 43 additions & 46 deletions

File tree

src/test/org/apache/commons/codec/StringEncoderAbstractTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.util.Locale;
2121

22+
import junit.framework.Assert;
2223
import junit.framework.TestCase;
2324

2425
/**
@@ -27,6 +28,8 @@
2728
*/
2829
public abstract class StringEncoderAbstractTest extends TestCase {
2930

31+
protected StringEncoder stringEncoder = createEncoder();
32+
3033
public StringEncoderAbstractTest(String name) {
3134
super(name);
3235
}
@@ -97,4 +100,20 @@ public void testLocaleIndependence() throws Exception {
97100
}
98101
}
99102

103+
protected void checkEncodings(String[][] data) throws EncoderException {
104+
for (int i = 0; i < data.length; i++) {
105+
this.checkEncoding(data[i][1], data[i][0]);
106+
}
107+
}
108+
109+
protected void checkEncodingVariations(String expected, String data[]) throws EncoderException {
110+
for (int i = 0; i < data.length; i++) {
111+
this.checkEncoding(expected, data[i]);
112+
}
113+
}
114+
115+
public void checkEncoding(String expected, String source) throws EncoderException {
116+
Assert.assertEquals("Source: " + source, expected, this.stringEncoder.encode(source));
117+
}
118+
100119
}

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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

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

20+
import org.apache.commons.codec.EncoderException;
2021
import org.apache.commons.codec.StringEncoder;
2122
import org.apache.commons.codec.StringEncoderAbstractTest;
2223

@@ -34,24 +35,19 @@ protected StringEncoder createEncoder() {
3435
return new Caverphone();
3536
}
3637

37-
public void testSpecificationExamples() {
38-
Caverphone caverphone = new Caverphone();
38+
public void testSpecificationExamples() throws EncoderException {
3939
String[][] data = {
40-
{"Stevenson", "STFNSN1111"},
41-
{"Peter", "PTA1111111"},
42-
{"ready", "RTA1111111"},
43-
{"social", "SSA1111111"},
44-
{"able", "APA1111111"},
45-
{"Tedder", "TTA1111111"},
46-
{"Karleen", "KLN1111111"},
47-
{"Dyun", "TN11111111"},
48-
};
49-
50-
for(int i=0; i<data.length; i++) {
51-
assertEquals( data[i][1], caverphone.caverphone(data[i][0]) );
52-
}
40+
{"Peter", "PTA1111111"},
41+
{"ready", "RTA1111111"},
42+
{"social", "SSA1111111"},
43+
{"able", "APA1111111"},
44+
{"Tedder", "TTA1111111"},
45+
{"Karleen", "KLN1111111"},
46+
{"Dyun", "TN11111111"},};
47+
this.checkEncodings(data);
5348
}
5449

50+
// Caverphone Revisited
5551
public void testIsCaverphoneEquals() {
5652
Caverphone caverphone = new Caverphone();
5753
assertFalse("Caverphone encodings should not be equal", caverphone.isCaverphoneEqual("Peter", "Stevenson"));

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

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,48 +17,30 @@
1717

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

20-
import junit.framework.Assert;
2120

21+
import org.apache.commons.codec.EncoderException;
2222
import org.apache.commons.codec.StringEncoder;
2323
import org.apache.commons.codec.StringEncoderAbstractTest;
2424

2525
public class ColognePhoneticTest extends StringEncoderAbstractTest {
2626

27-
private ColognePhonetic colognePhonetic = new ColognePhonetic();
28-
2927
public ColognePhoneticTest(String name) {
3028
super(name);
3129
}
3230

33-
public void checkEncoding(String expected, String source) {
34-
Assert.assertEquals("Source: " + source, expected, this.colognePhonetic.encode(source));
35-
}
36-
37-
private void checkEncodings(String[][] data) {
38-
for (int i = 0; i < data.length; i++) {
39-
this.checkEncoding(data[i][1], data[i][0]);
40-
}
41-
}
42-
43-
private void checkEncodingVariations(String expected, String data[]) {
44-
for (int i = 0; i < data.length; i++) {
45-
this.checkEncoding(expected, data[i]);
46-
}
47-
}
48-
4931
protected StringEncoder createEncoder() {
5032
return new ColognePhonetic();
5133
}
5234

53-
public void testAabjoe() {
35+
public void testAabjoe() throws EncoderException {
5436
this.checkEncoding("01", "Aabjoe");
5537
}
5638

57-
public void testAaclan() {
39+
public void testAaclan() throws EncoderException {
5840
this.checkEncoding("0856", "Aaclan");
5941
}
6042

61-
public void testEdgeCases() {
43+
public void testEdgeCases() throws EncoderException {
6244
String[][] data = {
6345
{"a", "0"},
6446
{"e", "0"},
@@ -92,7 +74,7 @@ public void testEdgeCases() {
9274
this.checkEncodings(data);
9375
}
9476

95-
public void testExamples() {
77+
public void testExamples() throws EncoderException {
9678
String[][] data = {
9779
{"m\u00DCller", "657"},
9880
{"schmidt", "862"},
@@ -125,7 +107,7 @@ public void testExamples() {
125107
this.checkEncodings(data);
126108
}
127109

128-
public void testHyphen() {
110+
public void testHyphen() throws EncoderException {
129111
String[][] data = {{"bergisch-gladbach", "174845214"}, {"Müller-Lüdenscheidt", "65752682"},
130112
// From the Javadoc example:
131113
{"Müller-Lüdenscheidt", "65752682"}};
@@ -143,17 +125,17 @@ public void testIsEncodeEquals() {
143125
{"ganz", "Gänse"},
144126
{"Miyagi", "Miyako"}};
145127
for (int i = 0; i < data.length; i++) {
146-
this.colognePhonetic.isEncodeEqual(data[i][1], data[i][0]);
128+
((ColognePhonetic) this.stringEncoder).isEncodeEqual(data[i][1], data[i][0]);
147129
}
148130
}
149131

150-
public void testVariationsMeyer() {
151-
String data[] = {"Meier", "Maier", "Mair", "Meyer", "Meyr", "Mejer", "Major"};
152-
this.checkEncodingVariations("67", data);
153-
}
154-
155-
public void testVariationsMella() {
132+
public void testVariationsMella() throws EncoderException {
156133
String data[] = {"mella", "milah", "moulla", "mellah", "muehle", "mule"};
157134
this.checkEncodingVariations("65", data);
158135
}
136+
137+
public void testVariationsMeyer() throws EncoderException {
138+
String data[] = {"Meier", "Maier", "Mair", "Meyer", "Meyr", "Mejer", "Major"};
139+
this.checkEncodingVariations("67", data);
140+
}
159141
}

0 commit comments

Comments
 (0)