Skip to content

Commit c10a2d7

Browse files
committed
PR: [Codec][Patch] RFC 1522 codecs: Q-codec & B-codec
http://issues.apache.org/bugzilla/show_bug.cgi?id=28002 Submitted by: Oleg Kalnichevski Reviewed by: Gary Gregory git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/codec/trunk@130349 13f79535-47bb-0310-9956-ffa450edef68
1 parent 2013a1e commit c10a2d7

3 files changed

Lines changed: 128 additions & 69 deletions

File tree

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

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ private String constructString(int[] unicodeChars) {
4848
return buffer.toString();
4949
}
5050

51+
public void testNullInput() throws Exception {
52+
BCodec bcodec = new BCodec();
53+
assertNull(bcodec.doDecoding(null));
54+
assertNull(bcodec.doEncoding(null));
55+
}
56+
5157
public void testUTF8RoundTrip() throws Exception {
5258

5359
String ru_msg = constructString(RUSSIAN_STUFF_UNICODE);
@@ -76,40 +82,6 @@ public void testEncodeDecodeNull() throws Exception {
7682
assertNull("Null string B decoding test", bcodec.decode((String) null));
7783
}
7884

79-
public void testDecodeInvalid() throws Exception {
80-
BCodec bcodec = new BCodec();
81-
try {
82-
bcodec.decode("whatever");
83-
fail("DecoderException should have been thrown");
84-
} catch (DecoderException e) {
85-
// Expected. Move on
86-
}
87-
try {
88-
bcodec.decode("=?UTF-8?B?stuff");
89-
fail("DecoderException should have been thrown");
90-
} catch (DecoderException e) {
91-
// Expected. Move on
92-
}
93-
try {
94-
bcodec.decode("=??B?stuff?=");
95-
fail("DecoderException should have been thrown");
96-
} catch (DecoderException e) {
97-
// Expected. Move on
98-
}
99-
try {
100-
bcodec.decode("=?UTF-8??stuff?=");
101-
fail("DecoderException should have been thrown");
102-
} catch (DecoderException e) {
103-
// Expected. Move on
104-
}
105-
try {
106-
bcodec.decode("=?UTF-8?W?stuff?=");
107-
fail("DecoderException should have been thrown");
108-
} catch (DecoderException e) {
109-
// Expected. Move on
110-
}
111-
}
112-
11385
public void testEncodeStringWithNull() throws Exception {
11486
BCodec bcodec = new BCodec();
11587
String test = null;
@@ -152,7 +124,7 @@ public void testInvalidEncoding() {
152124
// Exception expected, test segment passes.
153125
}
154126
try {
155-
bcodec.decode("=?NONSENSE?Q?Hello there!?=");
127+
bcodec.decode("=?NONSENSE?B?Hello there!?=");
156128
fail("We set the encoding to a bogus NONSENSE value, this shouldn't have worked.");
157129
} catch (DecoderException ee) {
158130
// Exception expected, test segment passes.

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

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ private String constructString(int [] unicodeChars) {
5252
return buffer.toString();
5353
}
5454

55+
public void testNullInput() throws Exception {
56+
QCodec qcodec = new QCodec();
57+
assertNull(qcodec.doDecoding(null));
58+
assertNull(qcodec.doEncoding(null));
59+
}
60+
5561
public void testUTF8RoundTrip() throws Exception {
5662

5763
String ru_msg = constructString(RUSSIAN_STUFF_UNICODE);
@@ -98,40 +104,6 @@ public void testEncodeDecodeNull() throws Exception {
98104
qcodec.decode((String)null));
99105
}
100106

101-
public void testDecodeInvalid() throws Exception {
102-
QCodec qcodec = new QCodec();
103-
try {
104-
qcodec.decode("whatever");
105-
fail("DecoderException should have been thrown");
106-
} catch(DecoderException e) {
107-
// Expected. Move on
108-
}
109-
try {
110-
qcodec.decode("=?UTF-8?Q?stuff");
111-
fail("DecoderException should have been thrown");
112-
} catch(DecoderException e) {
113-
// Expected. Move on
114-
}
115-
try {
116-
qcodec.decode("=??Q?stuff?=");
117-
fail("DecoderException should have been thrown");
118-
} catch(DecoderException e) {
119-
// Expected. Move on
120-
}
121-
try {
122-
qcodec.decode("=?UTF-8??stuff?=");
123-
fail("DecoderException should have been thrown");
124-
} catch(DecoderException e) {
125-
// Expected. Move on
126-
}
127-
try {
128-
qcodec.decode("=?UTF-8?W?stuff?=");
129-
fail("DecoderException should have been thrown");
130-
} catch(DecoderException e) {
131-
// Expected. Move on
132-
}
133-
}
134-
135107
public void testEncodeStringWithNull() throws Exception {
136108
QCodec qcodec = new QCodec();
137109
String test = null;
@@ -219,4 +191,14 @@ public void testEncodeDecodeBlanks() throws Exception {
219191
s = qcodec.decode(encoded2);
220192
assertEquals("Blanks decoding with the Q codec test", plain, s);
221193
}
194+
195+
196+
public void testLetUsMakeCloverHappy() throws Exception {
197+
QCodec qcodec = new QCodec();
198+
qcodec.setEncodeBlanks(true);
199+
assertTrue(qcodec.isEncodeBlanks());
200+
qcodec.setEncodeBlanks(false);
201+
assertFalse(qcodec.isEncodeBlanks());
202+
}
203+
222204
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2001-2004 The Apache Software Foundation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
18+
package org.apache.commons.codec.net;
19+
20+
import org.apache.commons.codec.DecoderException;
21+
import org.apache.commons.codec.EncoderException;
22+
23+
import junit.framework.TestCase;
24+
25+
/**
26+
* RFC 1522 compliant codec test cases
27+
*
28+
* @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
29+
*/
30+
public class RFC1522CodecTest extends TestCase {
31+
32+
public RFC1522CodecTest(String name) {
33+
super(name);
34+
}
35+
36+
37+
class RFC1522TestCodec extends RFC1522Codec {
38+
39+
protected byte[] doDecoding(byte[] bytes) throws DecoderException {
40+
return bytes;
41+
}
42+
43+
protected byte[] doEncoding(byte[] bytes) throws EncoderException {
44+
return bytes;
45+
}
46+
47+
protected String getEncoding() {
48+
return "T";
49+
}
50+
51+
}
52+
53+
public void testNullInput() throws Exception {
54+
RFC1522TestCodec testcodec = new RFC1522TestCodec();
55+
assertNull(testcodec.decodeText(null));
56+
assertNull(testcodec.encodeText(null, "UTF-8"));
57+
}
58+
59+
public void testDecodeInvalid() throws Exception {
60+
RFC1522TestCodec testcodec = new RFC1522TestCodec();
61+
try {
62+
testcodec.decodeText("whatever");
63+
fail("DecoderException should have been thrown");
64+
} catch(DecoderException e) {
65+
// Expected. Move on
66+
}
67+
try {
68+
testcodec.decodeText("=?stuff?=");
69+
fail("DecoderException should have been thrown");
70+
} catch(DecoderException e) {
71+
// Expected. Move on
72+
}
73+
try {
74+
testcodec.decodeText("=?UTF-8?stuff?=");
75+
fail("DecoderException should have been thrown");
76+
} catch(DecoderException e) {
77+
// Expected. Move on
78+
}
79+
try {
80+
testcodec.decodeText("=?UTF-8?T?stuff");
81+
fail("DecoderException should have been thrown");
82+
} catch(DecoderException e) {
83+
// Expected. Move on
84+
}
85+
try {
86+
testcodec.decodeText("=??T?stuff?=");
87+
fail("DecoderException should have been thrown");
88+
} catch(DecoderException e) {
89+
// Expected. Move on
90+
}
91+
try {
92+
testcodec.decodeText("=?UTF-8??stuff?=");
93+
fail("DecoderException should have been thrown");
94+
} catch(DecoderException e) {
95+
// Expected. Move on
96+
}
97+
try {
98+
testcodec.decodeText("=?UTF-8?W?stuff?=");
99+
fail("DecoderException should have been thrown");
100+
} catch(DecoderException e) {
101+
// Expected. Move on
102+
}
103+
}
104+
105+
}

0 commit comments

Comments
 (0)