1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.codec.net;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertNull;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import java.nio.charset.UnsupportedCharsetException;
28
29 import org.apache.commons.codec.CharEncoding;
30 import org.apache.commons.codec.DecoderException;
31 import org.apache.commons.codec.EncoderException;
32 import org.junit.Test;
33
34
35
36
37
38
39 public class QCodecTest {
40
41 static final int SWISS_GERMAN_STUFF_UNICODE [] = {
42 0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4
43 };
44
45 static final int RUSSIAN_STUFF_UNICODE [] = {
46 0x412, 0x441, 0x435, 0x43C, 0x5F, 0x43F, 0x440, 0x438,
47 0x432, 0x435, 0x442
48 };
49
50 private String constructString(final int [] unicodeChars) {
51 final StringBuilder buffer = new StringBuilder();
52 if (unicodeChars != null) {
53 for (final int unicodeChar : unicodeChars) {
54 buffer.append((char)unicodeChar);
55 }
56 }
57 return buffer.toString();
58 }
59
60 @Test
61 public void testNullInput() throws Exception {
62 final QCodec qcodec = new QCodec();
63 assertNull(qcodec.doDecoding(null));
64 assertNull(qcodec.doEncoding(null));
65 }
66
67 @Test
68 public void testUTF8RoundTrip() throws Exception {
69
70 final String ru_msg = constructString(RUSSIAN_STUFF_UNICODE);
71 final String ch_msg = constructString(SWISS_GERMAN_STUFF_UNICODE);
72
73 final QCodec qcodec = new QCodec(CharEncoding.UTF_8);
74
75 assertEquals(
76 "=?UTF-8?Q?=D0=92=D1=81=D0=B5=D0=BC=5F=D0=BF=D1=80=D0=B8=D0=B2=D0=B5=D1=82?=",
77 qcodec.encode(ru_msg)
78 );
79 assertEquals("=?UTF-8?Q?Gr=C3=BCezi=5Fz=C3=A4m=C3=A4?=", qcodec.encode(ch_msg));
80
81 assertEquals(ru_msg, qcodec.decode(qcodec.encode(ru_msg)));
82 assertEquals(ch_msg, qcodec.decode(qcodec.encode(ch_msg)));
83 }
84
85
86 @Test
87 public void testBasicEncodeDecode() throws Exception {
88 final QCodec qcodec = new QCodec();
89 final String plain = "= Hello there =\r\n";
90 final String encoded = qcodec.encode(plain);
91 assertEquals("Basic Q encoding test",
92 "=?UTF-8?Q?=3D Hello there =3D=0D=0A?=", encoded);
93 assertEquals("Basic Q decoding test",
94 plain, qcodec.decode(encoded));
95 }
96
97 @Test
98 public void testUnsafeEncodeDecode() throws Exception {
99 final QCodec qcodec = new QCodec();
100 final String plain = "?_=\r\n";
101 final String encoded = qcodec.encode(plain);
102 assertEquals("Unsafe chars Q encoding test",
103 "=?UTF-8?Q?=3F=5F=3D=0D=0A?=", encoded);
104 assertEquals("Unsafe chars Q decoding test",
105 plain, qcodec.decode(encoded));
106 }
107
108 @Test
109 public void testEncodeDecodeNull() throws Exception {
110 final QCodec qcodec = new QCodec();
111 assertNull("Null string Q encoding test",
112 qcodec.encode((String)null));
113 assertNull("Null string Q decoding test",
114 qcodec.decode((String)null));
115 }
116
117 @Test
118 public void testEncodeStringWithNull() throws Exception {
119 final QCodec qcodec = new QCodec();
120 final String test = null;
121 final String result = qcodec.encode( test, "charset" );
122 assertEquals("Result should be null", null, result);
123 }
124
125 @Test
126 public void testDecodeStringWithNull() throws Exception {
127 final QCodec qcodec = new QCodec();
128 final String test = null;
129 final String result = qcodec.decode( test );
130 assertEquals("Result should be null", null, result);
131 }
132
133
134 @Test
135 public void testEncodeObjects() throws Exception {
136 final QCodec qcodec = new QCodec();
137 final String plain = "1+1 = 2";
138 final String encoded = (String) qcodec.encode((Object) plain);
139 assertEquals("Basic Q encoding test",
140 "=?UTF-8?Q?1+1 =3D 2?=", encoded);
141
142 final Object result = qcodec.encode((Object) null);
143 assertEquals( "Encoding a null Object should return null", null, result);
144
145 try {
146 final Object dObj = new Double(3.0);
147 qcodec.encode( dObj );
148 fail( "Trying to url encode a Double object should cause an exception.");
149 } catch (final EncoderException ee) {
150
151 }
152 }
153
154
155 @Test(expected=UnsupportedCharsetException.class)
156 public void testInvalidEncoding() {
157 new QCodec("NONSENSE");
158 }
159
160 @Test
161 public void testDecodeObjects() throws Exception {
162 final QCodec qcodec = new QCodec();
163 final String decoded = "=?UTF-8?Q?1+1 =3D 2?=";
164 final String plain = (String) qcodec.decode((Object) decoded);
165 assertEquals("Basic Q decoding test",
166 "1+1 = 2", plain);
167
168 final Object result = qcodec.decode((Object) null);
169 assertEquals( "Decoding a null Object should return null", null, result);
170
171 try {
172 final Object dObj = new Double(3.0);
173 qcodec.decode( dObj );
174 fail( "Trying to url encode a Double object should cause an exception.");
175 } catch (final DecoderException ee) {
176
177 }
178 }
179
180
181 @Test
182 public void testEncodeDecodeBlanks() throws Exception {
183 final String plain = "Mind those pesky blanks";
184 final String encoded1 = "=?UTF-8?Q?Mind those pesky blanks?=";
185 final String encoded2 = "=?UTF-8?Q?Mind_those_pesky_blanks?=";
186 final QCodec qcodec = new QCodec();
187 qcodec.setEncodeBlanks(false);
188 String s = qcodec.encode(plain);
189 assertEquals("Blanks encoding with the Q codec test", encoded1, s);
190 qcodec.setEncodeBlanks(true);
191 s = qcodec.encode(plain);
192 assertEquals("Blanks encoding with the Q codec test", encoded2, s);
193 s = qcodec.decode(encoded1);
194 assertEquals("Blanks decoding with the Q codec test", plain, s);
195 s = qcodec.decode(encoded2);
196 assertEquals("Blanks decoding with the Q codec test", plain, s);
197 }
198
199
200 @Test
201 public void testLetUsMakeCloverHappy() throws Exception {
202 final QCodec qcodec = new QCodec();
203 qcodec.setEncodeBlanks(true);
204 assertTrue(qcodec.isEncodeBlanks());
205 qcodec.setEncodeBlanks(false);
206 assertFalse(qcodec.isEncodeBlanks());
207 }
208
209 }