Skip to content

Commit 921a971

Browse files
committed
Refactor encoding tests.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1075426 13f79535-47bb-0310-9956-ffa450edef68
1 parent 1a0de7f commit 921a971

8 files changed

Lines changed: 1551 additions & 1628 deletions

File tree

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

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,54 +28,70 @@
2828
*/
2929
public abstract class StringEncoderAbstractTest extends TestCase {
3030

31-
protected StringEncoder stringEncoder = createEncoder();
31+
protected StringEncoder stringEncoder = this.createStringEncoder();
3232

3333
public StringEncoderAbstractTest(String name) {
3434
super(name);
3535
}
3636

37-
protected abstract StringEncoder createEncoder();
37+
public void checkEncoding(String expected, String source) throws EncoderException {
38+
Assert.assertEquals("Source: " + source, expected, this.getStringEncoder().encode(source));
39+
}
3840

39-
// ------------------------------------------------------------------------
41+
protected void checkEncodings(String[][] data) throws EncoderException {
42+
for (int i = 0; i < data.length; i++) {
43+
this.checkEncoding(data[i][1], data[i][0]);
44+
}
45+
}
46+
47+
protected void checkEncodingVariations(String expected, String data[]) throws EncoderException {
48+
for (int i = 0; i < data.length; i++) {
49+
this.checkEncoding(expected, data[i]);
50+
}
51+
}
52+
53+
protected abstract StringEncoder createStringEncoder();
54+
55+
public StringEncoder getStringEncoder() {
56+
return this.stringEncoder;
57+
}
4058

4159
public void testEncodeEmpty() throws Exception {
42-
Encoder encoder = createEncoder();
60+
Encoder encoder = this.getStringEncoder();
4361
encoder.encode("");
4462
encoder.encode(" ");
4563
encoder.encode("\t");
46-
}
64+
}
4765

4866
public void testEncodeNull() throws Exception {
49-
StringEncoder encoder = createEncoder();
50-
67+
StringEncoder encoder = this.getStringEncoder();
5168
try {
5269
encoder.encode(null);
5370
} catch (EncoderException ee) {
5471
// An exception should be thrown
5572
}
56-
}
73+
}
5774

5875
public void testEncodeWithInvalidObject() throws Exception {
5976

6077
boolean exceptionThrown = false;
6178
try {
62-
StringEncoder encoder = createEncoder();
63-
encoder.encode( new Float( 3.4 ) );
79+
StringEncoder encoder = this.getStringEncoder();
80+
encoder.encode(new Float(3.4));
6481
} catch (Exception e) {
6582
exceptionThrown = true;
6683
}
6784

68-
assertTrue( "An exception was not thrown when we tried to encode " +
69-
"a Float object", exceptionThrown );
85+
Assert.assertTrue("An exception was not thrown when we tried to encode " + "a Float object", exceptionThrown);
7086
}
7187

7288
public void testLocaleIndependence() throws Exception {
73-
StringEncoder encoder = createEncoder();
89+
StringEncoder encoder = this.getStringEncoder();
7490

75-
String[] data = { "I", "i", };
91+
String[] data = {"I", "i",};
7692

7793
Locale orig = Locale.getDefault();
78-
Locale[] locales = { Locale.ENGLISH, new Locale("tr"), Locale.getDefault() };
94+
Locale[] locales = {Locale.ENGLISH, new Locale("tr"), Locale.getDefault()};
7995

8096
try {
8197
for (int i = 0; i < data.length; i++) {
@@ -89,9 +105,9 @@ public void testLocaleIndependence() throws Exception {
89105
try {
90106
cur = encoder.encode(data[i]);
91107
} catch (Exception e) {
92-
fail(Locale.getDefault().toString() + ": " + e.getMessage());
108+
Assert.fail(Locale.getDefault().toString() + ": " + e.getMessage());
93109
}
94-
assertEquals(Locale.getDefault().toString() + ": ", ref, cur);
110+
Assert.assertEquals(Locale.getDefault().toString() + ": ", ref, cur);
95111
}
96112
}
97113
}
@@ -100,20 +116,4 @@ public void testLocaleIndependence() throws Exception {
100116
}
101117
}
102118

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-
119119
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public CaverphoneTest(String name) {
3131
super(name);
3232
}
3333

34-
protected StringEncoder createEncoder() {
34+
protected StringEncoder createStringEncoder() {
3535
return new Caverphone();
3636
}
3737

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public ColognePhoneticTest(String name) {
2828
super(name);
2929
}
3030

31-
protected StringEncoder createEncoder() {
31+
protected StringEncoder createStringEncoder() {
3232
return new ColognePhonetic();
3333
}
3434

@@ -125,7 +125,7 @@ public void testIsEncodeEquals() {
125125
{"ganz", "Gänse"},
126126
{"Miyagi", "Miyako"}};
127127
for (int i = 0; i < data.length; i++) {
128-
((ColognePhonetic) this.stringEncoder).isEncodeEqual(data[i][1], data[i][0]);
128+
((ColognePhonetic) this.getStringEncoder()).isEncodeEqual(data[i][1], data[i][0]);
129129
}
130130
}
131131

0 commit comments

Comments
 (0)