KickJava   Java API By Example, From Geeks To Geeks.

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


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 BCodecTest 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     static final int RUSSIAN_STUFF_UNICODE[] =
35         { 0x412, 0x441, 0x435, 0x43C, 0x5F, 0x43F, 0x440, 0x438, 0x432, 0x435, 0x442 };
36
37     public BCodecTest(String JavaDoc name) {
38         super(name);
39     }
40
41     private String JavaDoc constructString(int[] unicodeChars) {
42         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
43         if (unicodeChars != null) {
44             for (int i = 0; i < unicodeChars.length; i++) {
45                 buffer.append((char) unicodeChars[i]);
46             }
47         }
48         return buffer.toString();
49     }
50
51     public void testNullInput() throws Exception JavaDoc {
52         BCodec bcodec = new BCodec();
53         assertNull(bcodec.doDecoding(null));
54         assertNull(bcodec.doEncoding(null));
55     }
56
57     public void testUTF8RoundTrip() throws Exception JavaDoc {
58
59         String JavaDoc ru_msg = constructString(RUSSIAN_STUFF_UNICODE);
60         String JavaDoc ch_msg = constructString(SWISS_GERMAN_STUFF_UNICODE);
61
62         BCodec bcodec = new BCodec("UTF-8");
63
64         assertEquals("=?UTF-8?B?0JLRgdC10Lxf0L/RgNC40LLQtdGC?=", bcodec.encode(ru_msg));
65         assertEquals("=?UTF-8?B?R3LDvGV6aV96w6Rtw6Q=?=", bcodec.encode(ch_msg));
66
67         assertEquals(ru_msg, bcodec.decode(bcodec.encode(ru_msg)));
68         assertEquals(ch_msg, bcodec.decode(bcodec.encode(ch_msg)));
69     }
70
71     public void testBasicEncodeDecode() throws Exception JavaDoc {
72         BCodec bcodec = new BCodec();
73         String JavaDoc plain = "Hello there";
74         String JavaDoc encoded = bcodec.encode(plain);
75         assertEquals("Basic B encoding test", "=?UTF-8?B?SGVsbG8gdGhlcmU=?=", encoded);
76         assertEquals("Basic B decoding test", plain, bcodec.decode(encoded));
77     }
78
79     public void testEncodeDecodeNull() throws Exception JavaDoc {
80         BCodec bcodec = new BCodec();
81         assertNull("Null string B encoding test", bcodec.encode((String JavaDoc) null));
82         assertNull("Null string B decoding test", bcodec.decode((String JavaDoc) null));
83     }
84
85     public void testEncodeStringWithNull() throws Exception JavaDoc {
86         BCodec bcodec = new BCodec();
87         String JavaDoc test = null;
88         String JavaDoc result = bcodec.encode(test, "charset");
89         assertEquals("Result should be null", null, result);
90     }
91
92     public void testDecodeStringWithNull() throws Exception JavaDoc {
93         BCodec bcodec = new BCodec();
94         String JavaDoc test = null;
95         String JavaDoc result = bcodec.decode(test);
96         assertEquals("Result should be null", null, result);
97     }
98
99     public void testEncodeObjects() throws Exception JavaDoc {
100         BCodec bcodec = new BCodec();
101         String JavaDoc plain = "what not";
102         String JavaDoc encoded = (String JavaDoc) bcodec.encode((Object JavaDoc) plain);
103
104         assertEquals("Basic B encoding test", "=?UTF-8?B?d2hhdCBub3Q=?=", encoded);
105
106         Object JavaDoc result = bcodec.encode((Object JavaDoc) null);
107         assertEquals("Encoding a null Object should return null", null, result);
108
109         try {
110             Object JavaDoc dObj = new Double JavaDoc(3.0);
111             bcodec.encode(dObj);
112             fail("Trying to url encode a Double object should cause an exception.");
113         } catch (EncoderException ee) {
114             // Exception expected, test segment passes.
115
}
116     }
117
118     public void testInvalidEncoding() {
119         BCodec bcodec = new BCodec("NONSENSE");
120         try {
121             bcodec.encode("Hello there!");
122             fail("We set the encoding to a bogus NONSENSE value, this shouldn't have worked.");
123         } catch (EncoderException ee) {
124             // Exception expected, test segment passes.
125
}
126         try {
127             bcodec.decode("=?NONSENSE?B?Hello there!?=");
128             fail("We set the encoding to a bogus NONSENSE value, this shouldn't have worked.");
129         } catch (DecoderException ee) {
130             // Exception expected, test segment passes.
131
}
132     }
133
134     public void testDecodeObjects() throws Exception JavaDoc {
135         BCodec bcodec = new BCodec();
136         String JavaDoc decoded = "=?UTF-8?B?d2hhdCBub3Q=?=";
137         String JavaDoc plain = (String JavaDoc) bcodec.decode((Object JavaDoc) decoded);
138         assertEquals("Basic B decoding test", "what not", plain);
139
140         Object JavaDoc result = bcodec.decode((Object JavaDoc) null);
141         assertEquals("Decoding a null Object should return null", null, result);
142
143         try {
144             Object JavaDoc dObj = new Double JavaDoc(3.0);
145             bcodec.decode(dObj);
146             fail("Trying to url encode a Double object should cause an exception.");
147         } catch (DecoderException ee) {
148             // Exception expected, test segment passes.
149
}
150     }
151 }
152
Popular Tags