KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > codec > net > QuotedPrintableCodecTest


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 package org.apache.commons.codec.net;
18
19 import org.apache.commons.codec.DecoderException;
20 import org.apache.commons.codec.EncoderException;
21
22 import junit.framework.TestCase;
23
24 /**
25  * Quoted-printable codec test cases
26  *
27  * @author <a HREF="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
28  */

29 public class QuotedPrintableCodecTest extends TestCase {
30     
31     static final int SWISS_GERMAN_STUFF_UNICODE [] = {
32         0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4
33     };
34     
35     static final int RUSSIAN_STUFF_UNICODE [] = {
36         0x412, 0x441, 0x435, 0x43C, 0x5F, 0x43F, 0x440, 0x438,
37         0x432, 0x435, 0x442
38     };
39
40     public QuotedPrintableCodecTest(String JavaDoc name) {
41         super(name);
42     }
43
44     private String JavaDoc constructString(int [] unicodeChars) {
45         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
46         if (unicodeChars != null) {
47             for (int i = 0; i < unicodeChars.length; i++) {
48                 buffer.append((char)unicodeChars[i]);
49             }
50         }
51         return buffer.toString();
52     }
53
54     public void testUTF8RoundTrip() throws Exception JavaDoc {
55
56         String JavaDoc ru_msg = constructString(RUSSIAN_STUFF_UNICODE);
57         String JavaDoc ch_msg = constructString(SWISS_GERMAN_STUFF_UNICODE);
58         
59         QuotedPrintableCodec qpcodec = new QuotedPrintableCodec();
60         
61         assertEquals(
62             "=D0=92=D1=81=D0=B5=D0=BC_=D0=BF=D1=80=D0=B8=D0=B2=D0=B5=D1=82",
63         qpcodec.encode(ru_msg, "UTF-8")
64         );
65         assertEquals("Gr=C3=BCezi_z=C3=A4m=C3=A4", qpcodec.encode(ch_msg, "UTF-8"));
66         
67         assertEquals(ru_msg, qpcodec.decode(qpcodec.encode(ru_msg, "UTF-8"), "UTF-8"));
68         assertEquals(ch_msg, qpcodec.decode(qpcodec.encode(ch_msg, "UTF-8"), "UTF-8"));
69     }
70
71     public void testBasicEncodeDecode() throws Exception JavaDoc {
72         QuotedPrintableCodec qpcodec = new QuotedPrintableCodec();
73         String JavaDoc plain = "= Hello there =\r\n";
74         String JavaDoc encoded = qpcodec.encode(plain);
75         assertEquals("Basic quoted-printable encoding test",
76             "=3D Hello there =3D=0D=0A", encoded);
77         assertEquals("Basic quoted-printable decoding test",
78             plain, qpcodec.decode(encoded));
79     }
80
81     public void testSafeCharEncodeDecode() throws Exception JavaDoc {
82         QuotedPrintableCodec qpcodec = new QuotedPrintableCodec();
83         String JavaDoc plain = "abc123_-.*~!@#$%^&()+{}\"\\;:`,/[]";
84         String JavaDoc encoded = qpcodec.encode(plain);
85         assertEquals("Safe chars quoted-printable encoding test",
86             plain, encoded);
87         assertEquals("Safe chars quoted-printable decoding test",
88             plain, qpcodec.decode(encoded));
89     }
90
91
92     public void testUnsafeEncodeDecode() throws Exception JavaDoc {
93         QuotedPrintableCodec qpcodec = new QuotedPrintableCodec();
94         String JavaDoc plain = "=\r\n";
95         String JavaDoc encoded = qpcodec.encode(plain);
96         assertEquals("Unsafe chars quoted-printable encoding test",
97             "=3D=0D=0A", encoded);
98         assertEquals("Unsafe chars quoted-printable decoding test",
99             plain, qpcodec.decode(encoded));
100     }
101
102     public void testEncodeDecodeNull() throws Exception JavaDoc {
103         QuotedPrintableCodec qpcodec = new QuotedPrintableCodec();
104         assertNull("Null string quoted-printable encoding test",
105             qpcodec.encode((String JavaDoc)null));
106         assertNull("Null string quoted-printable decoding test",
107             qpcodec.decode((String JavaDoc)null));
108     }
109
110
111     public void testDecodeInvalid() throws Exception JavaDoc {
112         QuotedPrintableCodec qpcodec = new QuotedPrintableCodec();
113         try {
114             qpcodec.decode("=");
115             fail("DecoderException should have been thrown");
116         } catch(DecoderException e) {
117             // Expected. Move on
118
}
119         try {
120             qpcodec.decode("=A");
121             fail("DecoderException should have been thrown");
122         } catch(DecoderException e) {
123             // Expected. Move on
124
}
125         try {
126             qpcodec.decode("=WW");
127             fail("DecoderException should have been thrown");
128         } catch(DecoderException e) {
129             // Expected. Move on
130
}
131     }
132
133     public void testEncodeNull() throws Exception JavaDoc {
134         QuotedPrintableCodec qpcodec = new QuotedPrintableCodec();
135         byte[] plain = null;
136         byte[] encoded = qpcodec.encode(plain);
137         assertEquals("Encoding a null string should return null",
138             null, encoded);
139     }
140     
141     public void testEncodeUrlWithNullBitSet() throws Exception JavaDoc {
142         QuotedPrintableCodec qpcodec = new QuotedPrintableCodec();
143         String JavaDoc plain = "1+1 = 2";
144         String JavaDoc encoded = new String JavaDoc(QuotedPrintableCodec.
145             encodeQuotedPrintable(null, plain.getBytes()));
146         assertEquals("Basic quoted-printable encoding test",
147             "1+1 =3D 2", encoded);
148         assertEquals("Basic quoted-printable decoding test",
149             plain, qpcodec.decode(encoded));
150         
151     }
152
153     public void testDecodeWithNullArray() throws Exception JavaDoc {
154         byte[] plain = null;
155         byte[] result = QuotedPrintableCodec.decodeQuotedPrintable( plain );
156         assertEquals("Result should be null", null, result);
157     }
158
159     public void testEncodeStringWithNull() throws Exception JavaDoc {
160         QuotedPrintableCodec qpcodec = new QuotedPrintableCodec();
161         String JavaDoc test = null;
162         String JavaDoc result = qpcodec.encode( test, "charset" );
163         assertEquals("Result should be null", null, result);
164     }
165
166     public void testDecodeStringWithNull() throws Exception JavaDoc {
167         QuotedPrintableCodec qpcodec = new QuotedPrintableCodec();
168         String JavaDoc test = null;
169         String JavaDoc result = qpcodec.decode( test, "charset" );
170         assertEquals("Result should be null", null, result);
171     }
172     
173     public void testEncodeObjects() throws Exception JavaDoc {
174         QuotedPrintableCodec qpcodec = new QuotedPrintableCodec();
175         String JavaDoc plain = "1+1 = 2";
176         String JavaDoc encoded = (String JavaDoc) qpcodec.encode((Object JavaDoc) plain);
177         assertEquals("Basic quoted-printable encoding test",
178             "1+1 =3D 2", encoded);
179
180         byte[] plainBA = plain.getBytes();
181         byte[] encodedBA = (byte[]) qpcodec.encode((Object JavaDoc) plainBA);
182         encoded = new String JavaDoc(encodedBA);
183         assertEquals("Basic quoted-printable encoding test",
184             "1+1 =3D 2", encoded);
185             
186         Object JavaDoc result = qpcodec.encode((Object JavaDoc) null);
187         assertEquals( "Encoding a null Object should return null", null, result);
188         
189         try {
190             Object JavaDoc dObj = new Double JavaDoc(3.0);
191             qpcodec.encode( dObj );
192             fail( "Trying to url encode a Double object should cause an exception.");
193         } catch( EncoderException ee ) {
194             // Exception expected, test segment passes.
195
}
196     }
197     
198     public void testInvalidEncoding() {
199         QuotedPrintableCodec qpcodec = new QuotedPrintableCodec("NONSENSE");
200            String JavaDoc plain = "Hello there!";
201             try {
202                qpcodec.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                qpcodec.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
}
213     }
214
215     public void testDecodeObjects() throws Exception JavaDoc {
216         QuotedPrintableCodec qpcodec = new QuotedPrintableCodec();
217         String JavaDoc plain = "1+1 =3D 2";
218         String JavaDoc decoded = (String JavaDoc) qpcodec.decode((Object JavaDoc) plain);
219         assertEquals("Basic quoted-printable decoding test",
220             "1+1 = 2", decoded);
221
222         byte[] plainBA = plain.getBytes();
223         byte[] decodedBA = (byte[]) qpcodec.decode((Object JavaDoc) plainBA);
224         decoded = new String JavaDoc(decodedBA);
225         assertEquals("Basic quoted-printable decoding test",
226             "1+1 = 2", decoded);
227             
228         Object JavaDoc result = qpcodec.decode((Object JavaDoc) null);
229         assertEquals( "Decoding a null Object should return null", null, result);
230         
231         try {
232             Object JavaDoc dObj = new Double JavaDoc(3.0);
233             qpcodec.decode( dObj );
234             fail( "Trying to url encode a Double object should cause an exception.");
235         } catch( DecoderException ee ) {
236             // Exception expected, test segment passes.
237
}
238     }
239
240     public void testDefaultEncoding() throws Exception JavaDoc {
241         String JavaDoc plain = "Hello there!";
242         QuotedPrintableCodec qpcodec = new QuotedPrintableCodec("UnicodeBig");
243         qpcodec.encode(plain); // To work around a weird quirk in Java 1.2.2
244
String JavaDoc encoded1 = qpcodec.encode(plain, "UnicodeBig");
245         String JavaDoc encoded2 = qpcodec.encode(plain);
246         assertEquals(encoded1, encoded2);
247     }
248 }
249
Popular Tags