1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.codec.net;
19
20 import junit.framework.TestCase;
21
22 import org.apache.commons.codec.DecoderException;
23 import org.apache.commons.codec.EncoderException;
24
25
26
27
28
29
30
31 public class BCodecTest extends TestCase {
32
33 static final int SWISS_GERMAN_STUFF_UNICODE[] =
34 { 0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4 };
35
36 static final int RUSSIAN_STUFF_UNICODE[] =
37 { 0x412, 0x441, 0x435, 0x43C, 0x5F, 0x43F, 0x440, 0x438, 0x432, 0x435, 0x442 };
38
39 public BCodecTest(String name) {
40 super(name);
41 }
42
43 private String constructString(int[] unicodeChars) {
44 StringBuffer buffer = new StringBuffer();
45 if (unicodeChars != null) {
46 for (int i = 0; i < unicodeChars.length; i++) {
47 buffer.append((char) unicodeChars[i]);
48 }
49 }
50 return buffer.toString();
51 }
52
53 public void testNullInput() throws Exception {
54 BCodec bcodec = new BCodec();
55 assertNull(bcodec.doDecoding(null));
56 assertNull(bcodec.doEncoding(null));
57 }
58
59 public void testUTF8RoundTrip() throws Exception {
60
61 String ru_msg = constructString(RUSSIAN_STUFF_UNICODE);
62 String ch_msg = constructString(SWISS_GERMAN_STUFF_UNICODE);
63
64 BCodec bcodec = new BCodec("UTF-8");
65
66 assertEquals("=?UTF-8?B?0JLRgdC10Lxf0L/RgNC40LLQtdGC?=", bcodec.encode(ru_msg));
67 assertEquals("=?UTF-8?B?R3LDvGV6aV96w6Rtw6Q=?=", bcodec.encode(ch_msg));
68
69 assertEquals(ru_msg, bcodec.decode(bcodec.encode(ru_msg)));
70 assertEquals(ch_msg, bcodec.decode(bcodec.encode(ch_msg)));
71 }
72
73 public void testBasicEncodeDecode() throws Exception {
74 BCodec bcodec = new BCodec();
75 String plain = "Hello there";
76 String encoded = bcodec.encode(plain);
77 assertEquals("Basic B encoding test", "=?UTF-8?B?SGVsbG8gdGhlcmU=?=", encoded);
78 assertEquals("Basic B decoding test", plain, bcodec.decode(encoded));
79 }
80
81 public void testEncodeDecodeNull() throws Exception {
82 BCodec bcodec = new BCodec();
83 assertNull("Null string B encoding test", bcodec.encode((String) null));
84 assertNull("Null string B decoding test", bcodec.decode((String) null));
85 }
86
87 public void testEncodeStringWithNull() throws Exception {
88 BCodec bcodec = new BCodec();
89 String test = null;
90 String result = bcodec.encode(test, "charset");
91 assertEquals("Result should be null", null, result);
92 }
93
94 public void testDecodeStringWithNull() throws Exception {
95 BCodec bcodec = new BCodec();
96 String test = null;
97 String result = bcodec.decode(test);
98 assertEquals("Result should be null", null, result);
99 }
100
101 public void testEncodeObjects() throws Exception {
102 BCodec bcodec = new BCodec();
103 String plain = "what not";
104 String encoded = (String) bcodec.encode((Object) plain);
105
106 assertEquals("Basic B encoding test", "=?UTF-8?B?d2hhdCBub3Q=?=", encoded);
107
108 Object result = bcodec.encode((Object) null);
109 assertEquals("Encoding a null Object should return null", null, result);
110
111 try {
112 Object dObj = new Double(3.0);
113 bcodec.encode(dObj);
114 fail("Trying to url encode a Double object should cause an exception.");
115 } catch (EncoderException ee) {
116
117 }
118 }
119
120 public void testInvalidEncoding() {
121 BCodec bcodec = new BCodec("NONSENSE");
122 try {
123 bcodec.encode("Hello there!");
124 fail("We set the encoding to a bogus NONSENSE value, this shouldn't have worked.");
125 } catch (EncoderException ee) {
126
127 }
128 try {
129 bcodec.decode("=?NONSENSE?B?Hello there!?=");
130 fail("We set the encoding to a bogus NONSENSE value, this shouldn't have worked.");
131 } catch (DecoderException ee) {
132
133 }
134 }
135
136 public void testDecodeObjects() throws Exception {
137 BCodec bcodec = new BCodec();
138 String decoded = "=?UTF-8?B?d2hhdCBub3Q=?=";
139 String plain = (String) bcodec.decode((Object) decoded);
140 assertEquals("Basic B decoding test", "what not", plain);
141
142 Object result = bcodec.decode((Object) null);
143 assertEquals("Decoding a null Object should return null", null, result);
144
145 try {
146 Object dObj = new Double(3.0);
147 bcodec.decode(dObj);
148 fail("Trying to url encode a Double object should cause an exception.");
149 } catch (DecoderException ee) {
150
151 }
152 }
153 }