Skip to content

Commit d99d0bd

Browse files
committed
Add a test to make sure getEncoding and getDefaultChar set match.
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/codec/trunk@130324 13f79535-47bb-0310-9956-ffa450edef68
1 parent d308d51 commit d99d0bd

1 file changed

Lines changed: 74 additions & 59 deletions

File tree

src/test/org/apache/commons/codec/net/URLCodecTest.java

Lines changed: 74 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public URLCodecTest(String name) {
4040
super(name);
4141
}
4242

43+
private void validateState(URLCodec urlCodec) {
44+
assertEquals(urlCodec.getEncoding(), urlCodec.getDefaultCharset());
45+
}
46+
4347
private String constructString(int [] unicodeChars) {
4448
StringBuffer buffer = new StringBuffer();
4549
if (unicodeChars != null) {
@@ -55,99 +59,107 @@ public void testUTF8RoundTrip() throws Exception {
5559
String ru_msg = constructString(RUSSIAN_STUFF_UNICODE);
5660
String ch_msg = constructString(SWISS_GERMAN_STUFF_UNICODE);
5761

58-
URLCodec codec = new URLCodec();
62+
URLCodec urlCodec = new URLCodec();
63+
this.validateState(urlCodec);
5964

6065
assertEquals(
6166
"%D0%92%D1%81%D0%B5%D0%BC_%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82",
62-
codec.encode(ru_msg, "UTF-8")
67+
urlCodec.encode(ru_msg, "UTF-8")
6368
);
64-
assertEquals("Gr%C3%BCezi_z%C3%A4m%C3%A4", codec.encode(ch_msg, "UTF-8"));
69+
assertEquals("Gr%C3%BCezi_z%C3%A4m%C3%A4", urlCodec.encode(ch_msg, "UTF-8"));
6570

66-
assertEquals(ru_msg, codec.decode(codec.encode(ru_msg, "UTF-8"), "UTF-8"));
67-
assertEquals(ch_msg, codec.decode(codec.encode(ch_msg, "UTF-8"), "UTF-8"));
71+
assertEquals(ru_msg, urlCodec.decode(urlCodec.encode(ru_msg, "UTF-8"), "UTF-8"));
72+
assertEquals(ch_msg, urlCodec.decode(urlCodec.encode(ch_msg, "UTF-8"), "UTF-8"));
73+
this.validateState(urlCodec);
6874
}
6975

7076
public void testBasicEncodeDecode() throws Exception {
71-
URLCodec urlcodec = new URLCodec();
77+
URLCodec urlCodec = new URLCodec();
7278
String plain = "Hello there!";
73-
String encoded = urlcodec.encode(plain);
79+
String encoded = urlCodec.encode(plain);
7480
assertEquals("Basic URL encoding test",
7581
"Hello+there%21", encoded);
7682
assertEquals("Basic URL decoding test",
77-
plain, urlcodec.decode(encoded));
83+
plain, urlCodec.decode(encoded));
84+
this.validateState(urlCodec);
7885
}
7986

8087

8188
public void testSafeCharEncodeDecode() throws Exception {
82-
URLCodec urlcodec = new URLCodec();
89+
URLCodec urlCodec = new URLCodec();
8390
String plain = "abc123_-.*";
84-
String encoded = urlcodec.encode(plain);
91+
String encoded = urlCodec.encode(plain);
8592
assertEquals("Safe chars URL encoding test",
8693
plain, encoded);
8794
assertEquals("Safe chars URL decoding test",
88-
plain, urlcodec.decode(encoded));
95+
plain, urlCodec.decode(encoded));
96+
this.validateState(urlCodec);
8997
}
9098

9199

92100
public void testUnsafeEncodeDecode() throws Exception {
93-
URLCodec urlcodec = new URLCodec();
101+
URLCodec urlCodec = new URLCodec();
94102
String plain = "~!@#$%^&()+{}\"\\;:`,/[]";
95-
String encoded = urlcodec.encode(plain);
103+
String encoded = urlCodec.encode(plain);
96104
assertEquals("Unsafe chars URL encoding test",
97105
"%7E%21%40%23%24%25%5E%26%28%29%2B%7B%7D%22%5C%3B%3A%60%2C%2F%5B%5D", encoded);
98106
assertEquals("Unsafe chars URL decoding test",
99-
plain, urlcodec.decode(encoded));
107+
plain, urlCodec.decode(encoded));
108+
this.validateState(urlCodec);
100109
}
101110

102111

103112
public void testEncodeDecodeNull() throws Exception {
104-
URLCodec urlcodec = new URLCodec();
113+
URLCodec urlCodec = new URLCodec();
105114
assertNull("Null string URL encoding test",
106-
urlcodec.encode((String)null));
115+
urlCodec.encode((String)null));
107116
assertNull("Null string URL decoding test",
108-
urlcodec.decode((String)null));
117+
urlCodec.decode((String)null));
118+
this.validateState(urlCodec);
109119
}
110120

111121

112122
public void testDecodeInvalid() throws Exception {
113-
URLCodec urlcodec = new URLCodec();
123+
URLCodec urlCodec = new URLCodec();
114124
try {
115-
urlcodec.decode("%");
125+
urlCodec.decode("%");
116126
fail("DecoderException should have been thrown");
117127
} catch(DecoderException e) {
118128
// Expected. Move on
119129
}
120130
try {
121-
urlcodec.decode("%A");
131+
urlCodec.decode("%A");
122132
fail("DecoderException should have been thrown");
123133
} catch(DecoderException e) {
124134
// Expected. Move on
125135
}
126136
try {
127-
urlcodec.decode("%WW");
137+
urlCodec.decode("%WW");
128138
fail("DecoderException should have been thrown");
129139
} catch(DecoderException e) {
130140
// Expected. Move on
131141
}
142+
this.validateState(urlCodec);
132143
}
133144

134145
public void testEncodeNull() throws Exception {
135-
URLCodec urlcodec = new URLCodec();
146+
URLCodec urlCodec = new URLCodec();
136147
byte[] plain = null;
137-
byte[] encoded = urlcodec.encode(plain);
148+
byte[] encoded = urlCodec.encode(plain);
138149
assertEquals("Encoding a null string should return null",
139150
null, encoded);
151+
this.validateState(urlCodec);
140152
}
141153

142154
public void testEncodeUrlWithNullBitSet() throws Exception {
143-
URLCodec urlcodec = new URLCodec();
155+
URLCodec urlCodec = new URLCodec();
144156
String plain = "Hello there!";
145157
String encoded = new String( URLCodec.encodeUrl(null, plain.getBytes()));
146158
assertEquals("Basic URL encoding test",
147159
"Hello+there%21", encoded);
148160
assertEquals("Basic URL decoding test",
149-
plain, urlcodec.decode(encoded));
150-
161+
plain, urlCodec.decode(encoded));
162+
this.validateState(urlCodec);
151163
}
152164

153165
public void testDecodeWithNullArray() throws Exception {
@@ -157,93 +169,96 @@ public void testDecodeWithNullArray() throws Exception {
157169
}
158170

159171
public void testEncodeStringWithNull() throws Exception {
160-
URLCodec urlcodec = new URLCodec();
172+
URLCodec urlCodec = new URLCodec();
161173
String test = null;
162-
String result = urlcodec.encode( test, "charset" );
174+
String result = urlCodec.encode( test, "charset" );
163175
assertEquals("Result should be null", null, result);
164176
}
165177

166178
public void testDecodeStringWithNull() throws Exception {
167-
URLCodec urlcodec = new URLCodec();
179+
URLCodec urlCodec = new URLCodec();
168180
String test = null;
169-
String result = urlcodec.decode( test, "charset" );
181+
String result = urlCodec.decode( test, "charset" );
170182
assertEquals("Result should be null", null, result);
171183
}
172184

173185
public void testEncodeObjects() throws Exception {
174-
URLCodec urlcodec = new URLCodec();
186+
URLCodec urlCodec = new URLCodec();
175187
String plain = "Hello there!";
176-
String encoded = (String) urlcodec.encode((Object) plain);
188+
String encoded = (String) urlCodec.encode((Object) plain);
177189
assertEquals("Basic URL encoding test",
178190
"Hello+there%21", encoded);
179191

180192
byte[] plainBA = plain.getBytes();
181-
byte[] encodedBA = (byte[]) urlcodec.encode((Object) plainBA);
193+
byte[] encodedBA = (byte[]) urlCodec.encode((Object) plainBA);
182194
encoded = new String(encodedBA);
183195
assertEquals("Basic URL encoding test",
184196
"Hello+there%21", encoded);
185197

186-
Object result = urlcodec.encode((Object) null);
198+
Object result = urlCodec.encode((Object) null);
187199
assertEquals( "Encoding a null Object should return null", null, result);
188200

189201
try {
190202
Object dObj = new Double(3.0);
191-
urlcodec.encode( dObj );
203+
urlCodec.encode( dObj );
192204
fail( "Trying to url encode a Double object should cause an exception.");
193205
} catch( EncoderException ee ) {
194206
// Exception expected, test segment passes.
195207
}
208+
this.validateState(urlCodec);
196209
}
197210

198211
public void testInvalidEncoding() {
199-
URLCodec urlcodec = new URLCodec("NONSENSE");
200-
String plain = "Hello there!";
201-
try {
202-
urlcodec.encode(plain);
203-
fail( "We set the encoding to a bogus NONSENSE vlaue, this shouldn't have worked.");
204-
} catch( EncoderException ee ) {
205-
// Exception expected, test segment passes.
206-
}
207-
try {
208-
urlcodec.decode(plain);
209-
fail( "We set the encoding to a bogus NONSENSE vlaue, this shouldn't have worked.");
210-
} catch( DecoderException ee ) {
211-
// Exception expected, test segment passes.
212-
}
212+
URLCodec urlCodec = new URLCodec("NONSENSE");
213+
String plain = "Hello there!";
214+
try {
215+
urlCodec.encode(plain);
216+
fail("We set the encoding to a bogus NONSENSE vlaue, this shouldn't have worked.");
217+
} catch (EncoderException ee) {
218+
// Exception expected, test segment passes.
219+
}
220+
try {
221+
urlCodec.decode(plain);
222+
fail("We set the encoding to a bogus NONSENSE vlaue, this shouldn't have worked.");
223+
} catch (DecoderException ee) {
224+
// Exception expected, test segment passes.
225+
}
226+
this.validateState(urlCodec);
213227
}
214228

215229
public void testDecodeObjects() throws Exception {
216-
URLCodec urlcodec = new URLCodec();
230+
URLCodec urlCodec = new URLCodec();
217231
String plain = "Hello+there%21";
218-
String decoded = (String) urlcodec.decode((Object) plain);
232+
String decoded = (String) urlCodec.decode((Object) plain);
219233
assertEquals("Basic URL decoding test",
220234
"Hello there!", decoded);
221235

222236
byte[] plainBA = plain.getBytes();
223-
byte[] decodedBA = (byte[]) urlcodec.decode((Object) plainBA);
237+
byte[] decodedBA = (byte[]) urlCodec.decode((Object) plainBA);
224238
decoded = new String(decodedBA);
225239
assertEquals("Basic URL decoding test",
226240
"Hello there!", decoded);
227241

228-
Object result = urlcodec.decode((Object) null);
242+
Object result = urlCodec.decode((Object) null);
229243
assertEquals( "Decoding a null Object should return null", null, result);
230244

231245
try {
232246
Object dObj = new Double(3.0);
233-
urlcodec.decode( dObj );
247+
urlCodec.decode( dObj );
234248
fail( "Trying to url encode a Double object should cause an exception.");
235249
} catch( DecoderException ee ) {
236250
// Exception expected, test segment passes.
237251
}
252+
this.validateState(urlCodec);
238253
}
239254

240255
public void testDefaultEncoding() throws Exception {
241256
String plain = "Hello there!";
242-
URLCodec urlcodec = new URLCodec("UnicodeBig");
243-
urlcodec.encode(plain); // To work around a weird quirk in Java 1.2.2
244-
String encoded1 = urlcodec.encode(plain, "UnicodeBig");
245-
String encoded2 = urlcodec.encode(plain);
257+
URLCodec urlCodec = new URLCodec("UnicodeBig");
258+
urlCodec.encode(plain); // To work around a weird quirk in Java 1.2.2
259+
String encoded1 = urlCodec.encode(plain, "UnicodeBig");
260+
String encoded2 = urlCodec.encode(plain);
246261
assertEquals(encoded1, encoded2);
262+
this.validateState(urlCodec);
247263
}
248-
249264
}

0 commit comments

Comments
 (0)