KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Quoted-printable codec test cases
27  *
28  * @author <a HREF="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
29  */

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