KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > TestBase64


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/Attic/TestBase64.java,v 1.8.2.1 2004/02/22 18:21:16 olegk Exp $
3  * $Revision: 1.8.2.1 $
4  * $Date: 2004/02/22 18:21:16 $
5  * ====================================================================
6  *
7  * Copyright 1999-2004 The Apache Software Foundation
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ====================================================================
21  *
22  * This software consists of voluntary contributions made by many
23  * individuals on behalf of the Apache Software Foundation. For more
24  * information on the Apache Software Foundation, please see
25  * <http://www.apache.org/>.
26  *
27  * [Additional notices, if required by prior licensing conditions]
28  *
29  */

30
31 package org.apache.commons.httpclient;
32
33 import junit.framework.*;
34 import java.util.Random JavaDoc;
35 import org.apache.commons.httpclient.util.Base64;
36
37 /**
38  * Simple tests of {@link Base64}.
39  *
40  * @deprecated The commons-codec Base64 class will be used in HttpClient 2.1
41  * @author Rodney Waldhoff
42  * @version $Id: TestBase64.java,v 1.8.2.1 2004/02/22 18:21:16 olegk Exp $
43  */

44 public class TestBase64 extends TestCase {
45
46     // ------------------------------------------------------------ Constructor
47
public TestBase64(String JavaDoc testName) {
48         super(testName);
49     }
50
51     // ------------------------------------------------------------------- Main
52
public static void main(String JavaDoc args[]) {
53         String JavaDoc[] testCaseName = { TestBase64.class.getName() };
54         junit.textui.TestRunner.main(testCaseName);
55     }
56
57     // ------------------------------------------------------- TestCase Methods
58
public static Test suite() {
59         return new TestSuite(TestBase64.class);
60     }
61
62     public void setUp() throws Exception JavaDoc {
63     }
64
65     private Random JavaDoc _rand = new Random JavaDoc();
66
67     // encode/decode random arrays from size 0 to size 11
68
public void testEncodeDecodeSmall() {
69         for(int i=0;i<12;i++) {
70             byte[] data = new byte[i];
71             _rand.nextBytes(data);
72             byte[] enc = Base64.encode(data);
73             assertTrue("\"" + (HttpConstants.getAsciiString(enc)) + "\" is Base64 data.",Base64.isBase64((HttpConstants.getAsciiString(enc))));
74             byte[] data2 = Base64.decode(enc);
75             assertTrue(toString(data) + " equals " + toString(data2),isEqual(data,data2));
76         }
77     }
78
79     // encode/decode a large random array
80
public void testEncodeDecodeRandom() {
81         for(int i=1;i<5;i++) {
82             byte[] data = new byte[_rand.nextInt(10000)+1];
83             _rand.nextBytes(data);
84             byte[] enc = Base64.encode(data);
85             assertTrue(Base64.isBase64(HttpConstants.getAsciiString(enc)));
86             byte[] data2 = Base64.decode(enc);
87             assertTrue(isEqual(data,data2));
88         }
89     }
90
91     public void testSingletons() {
92         assertEquals("AA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0 })));
93         assertEquals("AQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)1 })));
94         assertEquals("Ag==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)2 })));
95         assertEquals("Aw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)3 })));
96         assertEquals("BA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)4 })));
97         assertEquals("BQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)5 })));
98         assertEquals("Bg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)6 })));
99         assertEquals("Bw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)7 })));
100         assertEquals("CA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)8 })));
101         assertEquals("CQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)9 })));
102         assertEquals("Cg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)10 })));
103         assertEquals("Cw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)11 })));
104         assertEquals("DA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)12 })));
105         assertEquals("DQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)13 })));
106         assertEquals("Dg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)14 })));
107         assertEquals("Dw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)15 })));
108         assertEquals("EA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)16 })));
109         assertEquals("EQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)17 })));
110         assertEquals("Eg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)18 })));
111         assertEquals("Ew==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)19 })));
112         assertEquals("FA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)20 })));
113         assertEquals("FQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)21 })));
114         assertEquals("Fg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)22 })));
115         assertEquals("Fw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)23 })));
116         assertEquals("GA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)24 })));
117         assertEquals("GQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)25 })));
118         assertEquals("Gg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)26 })));
119         assertEquals("Gw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)27 })));
120         assertEquals("HA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)28 })));
121         assertEquals("HQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)29 })));
122         assertEquals("Hg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)30 })));
123         assertEquals("Hw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)31 })));
124         assertEquals("IA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)32 })));
125         assertEquals("IQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)33 })));
126         assertEquals("Ig==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)34 })));
127         assertEquals("Iw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)35 })));
128         assertEquals("JA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)36 })));
129         assertEquals("JQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)37 })));
130         assertEquals("Jg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)38 })));
131         assertEquals("Jw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)39 })));
132         assertEquals("KA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)40 })));
133         assertEquals("KQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)41 })));
134         assertEquals("Kg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)42 })));
135         assertEquals("Kw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)43 })));
136         assertEquals("LA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)44 })));
137         assertEquals("LQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)45 })));
138         assertEquals("Lg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)46 })));
139         assertEquals("Lw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)47 })));
140         assertEquals("MA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)48 })));
141         assertEquals("MQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)49 })));
142         assertEquals("Mg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)50 })));
143         assertEquals("Mw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)51 })));
144         assertEquals("NA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)52 })));
145         assertEquals("NQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)53 })));
146         assertEquals("Ng==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)54 })));
147         assertEquals("Nw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)55 })));
148         assertEquals("OA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)56 })));
149         assertEquals("OQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)57 })));
150         assertEquals("Og==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)58 })));
151         assertEquals("Ow==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)59 })));
152         assertEquals("PA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)60 })));
153         assertEquals("PQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)61 })));
154         assertEquals("Pg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)62 })));
155         assertEquals("Pw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)63 })));
156         assertEquals("QA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)64 })));
157         assertEquals("QQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)65 })));
158         assertEquals("Qg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)66 })));
159         assertEquals("Qw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)67 })));
160         assertEquals("RA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)68 })));
161         assertEquals("RQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)69 })));
162         assertEquals("Rg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)70 })));
163         assertEquals("Rw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)71 })));
164         assertEquals("SA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)72 })));
165         assertEquals("SQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)73 })));
166         assertEquals("Sg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)74 })));
167         assertEquals("Sw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)75 })));
168         assertEquals("TA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)76 })));
169         assertEquals("TQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)77 })));
170         assertEquals("Tg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)78 })));
171         assertEquals("Tw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)79 })));
172         assertEquals("UA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)80 })));
173         assertEquals("UQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)81 })));
174         assertEquals("Ug==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)82 })));
175         assertEquals("Uw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)83 })));
176         assertEquals("VA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)84 })));
177         assertEquals("VQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)85 })));
178         assertEquals("Vg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)86 })));
179         assertEquals("Vw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)87 })));
180         assertEquals("WA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)88 })));
181         assertEquals("WQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)89 })));
182         assertEquals("Wg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)90 })));
183         assertEquals("Ww==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)91 })));
184         assertEquals("XA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)92 })));
185         assertEquals("XQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)93 })));
186         assertEquals("Xg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)94 })));
187         assertEquals("Xw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)95 })));
188         assertEquals("YA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)96 })));
189         assertEquals("YQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)97 })));
190         assertEquals("Yg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)98 })));
191         assertEquals("Yw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)99 })));
192         assertEquals("ZA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)100 })));
193         assertEquals("ZQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)101 })));
194         assertEquals("Zg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)102 })));
195         assertEquals("Zw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)103 })));
196         assertEquals("aA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)104 })));
197     }
198
199     public void testTriplets() {
200         assertEquals("AAAA",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)0 })));
201         assertEquals("AAAB",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)1 })));
202         assertEquals("AAAC",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)2 })));
203         assertEquals("AAAD",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)3 })));
204         assertEquals("AAAE",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)4 })));
205         assertEquals("AAAF",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)5 })));
206         assertEquals("AAAG",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)6 })));
207         assertEquals("AAAH",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)7 })));
208         assertEquals("AAAI",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)8 })));
209         assertEquals("AAAJ",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)9 })));
210         assertEquals("AAAK",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)10 })));
211         assertEquals("AAAL",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)11 })));
212         assertEquals("AAAM",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)12 })));
213         assertEquals("AAAN",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)13 })));
214         assertEquals("AAAO",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)14 })));
215         assertEquals("AAAP",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)15 })));
216         assertEquals("AAAQ",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)16 })));
217         assertEquals("AAAR",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)17 })));
218         assertEquals("AAAS",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)18 })));
219         assertEquals("AAAT",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)19 })));
220         assertEquals("AAAU",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)20 })));
221         assertEquals("AAAV",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)21 })));
222         assertEquals("AAAW",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)22 })));
223         assertEquals("AAAX",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)23 })));
224         assertEquals("AAAY",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)24 })));
225         assertEquals("AAAZ",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)25 })));
226         assertEquals("AAAa",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)26 })));
227         assertEquals("AAAb",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)27 })));
228         assertEquals("AAAc",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)28 })));
229         assertEquals("AAAd",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)29 })));
230         assertEquals("AAAe",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)30 })));
231         assertEquals("AAAf",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)31 })));
232         assertEquals("AAAg",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)32 })));
233         assertEquals("AAAh",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)33 })));
234         assertEquals("AAAi",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)34 })));
235         assertEquals("AAAj",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)35 })));
236         assertEquals("AAAk",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)36 })));
237         assertEquals("AAAl",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)37 })));
238         assertEquals("AAAm",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)38 })));
239         assertEquals("AAAn",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)39 })));
240         assertEquals("AAAo",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)40 })));
241         assertEquals("AAAp",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)41 })));
242         assertEquals("AAAq",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)42 })));
243         assertEquals("AAAr",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)43 })));
244         assertEquals("AAAs",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)44 })));
245         assertEquals("AAAt",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)45 })));
246         assertEquals("AAAu",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)46 })));
247         assertEquals("AAAv",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)47 })));
248         assertEquals("AAAw",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)48 })));
249         assertEquals("AAAx",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)49 })));
250         assertEquals("AAAy",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)50 })));
251         assertEquals("AAAz",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)51 })));
252         assertEquals("AAA0",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)52 })));
253         assertEquals("AAA1",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)53 })));
254         assertEquals("AAA2",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)54 })));
255         assertEquals("AAA3",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)55 })));
256         assertEquals("AAA4",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)56 })));
257         assertEquals("AAA5",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)57 })));
258         assertEquals("AAA6",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)58 })));
259         assertEquals("AAA7",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)59 })));
260         assertEquals("AAA8",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)60 })));
261         assertEquals("AAA9",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)61 })));
262         assertEquals("AAA+",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)62 })));
263         assertEquals("AAA/",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)63 })));
264     }
265
266     public void testKnownEncodings() {
267         assertEquals("VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2dzLg==",HttpConstants.getAsciiString(Base64.encode(HttpConstants.getAsciiBytes("The quick brown fox jumped over the lazy dogs."))));
268         assertEquals("SXQgd2FzIHRoZSBiZXN0IG9mIHRpbWVzLCBpdCB3YXMgdGhlIHdvcnN0IG9mIHRpbWVzLg==",HttpConstants.getAsciiString(Base64.encode(HttpConstants.getAsciiBytes("It was the best of times, it was the worst of times."))));
269         assertEquals("aHR0cDovL2pha2FydGEuYXBhY2hlLm9yZy9jb21tbW9ucw==",HttpConstants.getAsciiString(Base64.encode(HttpConstants.getAsciiBytes("http://jakarta.apache.org/commmons"))));
270         assertEquals("QWFCYkNjRGRFZUZmR2dIaElpSmpLa0xsTW1Obk9vUHBRcVJyU3NUdFV1VnZXd1h4WXlaeg==",HttpConstants.getAsciiString(Base64.encode(HttpConstants.getAsciiBytes("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"))));
271         assertEquals("eyAwLCAxLCAyLCAzLCA0LCA1LCA2LCA3LCA4LCA5IH0=",HttpConstants.getAsciiString(Base64.encode(HttpConstants.getAsciiBytes("{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }"))));
272         assertEquals("eHl6enkh",HttpConstants.getAsciiString(Base64.encode(HttpConstants.getAsciiBytes("xyzzy!"))));
273     }
274
275     public void testKnownDecodings() {
276         assertEquals("The quick brown fox jumped over the lazy dogs.",HttpConstants.getAsciiString(Base64.decode(HttpConstants.getAsciiBytes("VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2dzLg=="))));
277         assertEquals("It was the best of times, it was the worst of times.",HttpConstants.getAsciiString(Base64.decode(HttpConstants.getAsciiBytes("SXQgd2FzIHRoZSBiZXN0IG9mIHRpbWVzLCBpdCB3YXMgdGhlIHdvcnN0IG9mIHRpbWVzLg=="))));
278         assertEquals("http://jakarta.apache.org/commmons",HttpConstants.getAsciiString(Base64.decode(HttpConstants.getAsciiBytes("aHR0cDovL2pha2FydGEuYXBhY2hlLm9yZy9jb21tbW9ucw=="))));
279         assertEquals("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz",HttpConstants.getAsciiString(Base64.decode(HttpConstants.getAsciiBytes("QWFCYkNjRGRFZUZmR2dIaElpSmpLa0xsTW1Obk9vUHBRcVJyU3NUdFV1VnZXd1h4WXlaeg=="))));
280         assertEquals("{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }",HttpConstants.getAsciiString(Base64.decode(HttpConstants.getAsciiBytes("eyAwLCAxLCAyLCAzLCA0LCA1LCA2LCA3LCA4LCA5IH0="))));
281         assertEquals("xyzzy!",HttpConstants.getAsciiString(Base64.decode(HttpConstants.getAsciiBytes("eHl6enkh"))));
282     }
283
284     // -------------------------------------------------------- Private Methods
285
private boolean isEqual(byte[] a, byte[] b) {
286         if(a.length != b.length) { return false; }
287         for(int i=0;i<a.length;i++) {
288             if(a[i] != b[i]) { return false; }
289         }
290         return true;
291     }
292
293     private String JavaDoc toString(byte[] data) {
294         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
295         for(int i=0;i<data.length;i++) {
296             buf.append(data[i]);
297             if(i != data.length-1) {
298                 buf.append(",");
299             }
300         }
301         return buf.toString();
302     }
303 }
304
Popular Tags