01: /*
02: * Copyright (C) 2004 Joe Walnes.
03: * Copyright (C) 2006, 2007 XStream Committers.
04: * All rights reserved.
05: *
06: * The software in this package is published under the terms of the BSD
07: * style license a copy of which has been included with this distribution in
08: * the LICENSE.txt file.
09: *
10: * Created on 06. August 2004 by Joe Walnes
11: */
12: package com.thoughtworks.xstream.core.util;
13:
14: import com.thoughtworks.acceptance.AbstractAcceptanceTest;
15:
16: public class Base64EncoderTest extends AbstractAcceptanceTest {
17:
18: private Base64Encoder encoder = new Base64Encoder();
19:
20: public void testEncodesEntireByteArrayAsString() {
21: byte input[] = "hello world".getBytes();
22: String expected = "aGVsbG8gd29ybGQ=";
23: assertEquals(expected, encoder.encode(input));
24: assertByteArrayEquals(input, encoder.decode(expected));
25: }
26:
27: public void testWrapsLinesAt76Chars() {
28: byte input[] = ("hello world. hello world. hello world. hello world. hello world. hello world. hello world. "
29: + "hello world. hello world. hello world. hello world. hello world. hello world. hello world. ")
30: .getBytes();
31: String expected = "aGVsbG8gd29ybGQuIGhlbGxvIHdvcmxkLiBoZWxsbyB3b3JsZC4gaGVsbG8gd29ybGQuIGhlbGxv\n"
32: + "IHdvcmxkLiBoZWxsbyB3b3JsZC4gaGVsbG8gd29ybGQuIGhlbGxvIHdvcmxkLiBoZWxsbyB3b3Js\n"
33: + "ZC4gaGVsbG8gd29ybGQuIGhlbGxvIHdvcmxkLiBoZWxsbyB3b3JsZC4gaGVsbG8gd29ybGQuIGhl\n"
34: + "bGxvIHdvcmxkLiA=";
35: assertEquals(expected, encoder.encode(input));
36: assertByteArrayEquals(input, encoder.decode(expected));
37: }
38:
39: public void testPadsSingleMissingByteWhenNotMultipleOfThree() {
40: byte input[] = { 1, 2, 3, 4, 5 };
41: String expected = "AQIDBAU=";
42: assertEquals(expected, encoder.encode(input));
43: assertByteArrayEquals(input, encoder.decode(expected));
44: }
45:
46: public void testPadsDoubleMissingByteWhenNotMultipleOfThree() {
47: byte input[] = { 1, 2, 3, 4 };
48: String expected = "AQIDBA==";
49: assertEquals(expected, encoder.encode(input));
50: assertByteArrayEquals(input, encoder.decode(expected));
51: }
52:
53: public void testDoesNotPadWhenMultipleOfThree() {
54: byte input[] = { 1, 2, 3, 4, 5, 6 };
55: String expected = "AQIDBAUG";
56: assertEquals(expected, encoder.encode(input));
57: assertByteArrayEquals(input, encoder.decode(expected));
58: }
59:
60: public void testHandlesAllPositiveBytes() {
61: byte input[] = new byte[127];
62: for (int i = 0; i < 126; i++)
63: input[i] = (byte) (i + 1);
64: String expected = "AQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyAhIiMkJSYnKCkqKywtLi8wMTIzNDU2Nzg5\n"
65: + "Ojs8PT4/QEFCQ0RFRkdISUpLTE1OT1BRUlNUVVZXWFlaW1xdXl9gYWJjZGVmZ2hpamtsbW5vcHFy\n"
66: + "c3R1dnd4eXp7fH1+AA==";
67: assertEquals(expected, encoder.encode(input));
68: assertByteArrayEquals(input, encoder.decode(expected));
69: }
70:
71: public void testHandlesAllNegativeBytes() {
72: byte input[] = new byte[128];
73: for (int i = 0; i < 127; i++)
74: input[i] = (byte) (-1 - i);
75: String expected = "//79/Pv6+fj39vX08/Lx8O/u7ezr6uno5+bl5OPi4eDf3t3c29rZ2NfW1dTT0tHQz87NzMvKycjH\n"
76: + "xsXEw8LBwL++vby7urm4t7a1tLOysbCvrq2sq6qpqKempaSjoqGgn56dnJuamZiXlpWUk5KRkI+O\n"
77: + "jYyLiomIh4aFhIOCgQA=";
78: assertEquals(expected, encoder.encode(input));
79: assertByteArrayEquals(input, encoder.decode(expected));
80: }
81:
82: public void testHandlesZeroByte() {
83: byte input[] = { 0, 0, 0, 0 };
84: String expected = "AAAAAA==";
85: assertEquals(expected, encoder.encode(input));
86: assertByteArrayEquals(input, encoder.decode(expected));
87: }
88:
89: public void testProducesEmptyStringWhenNoBytesGiven() {
90: byte input[] = new byte[0];
91: String expected = "";
92: assertEquals(expected, encoder.encode(input));
93: assertByteArrayEquals(input, encoder.decode(expected));
94: }
95:
96: }
|