1 package com.thoughtworks.xstream.core.util; 2 3 import com.thoughtworks.acceptance.AbstractAcceptanceTest; 4 5 public class Base64EncoderTest extends AbstractAcceptanceTest { 6 7 private Base64Encoder encoder = new Base64Encoder(); 8 9 public void testEncodesEntireByteArrayAsString() { 10 byte input[] = "hello world".getBytes(); 11 String expected = "aGVsbG8gd29ybGQ="; 12 assertEquals(expected, encoder.encode(input)); 13 assertByteArrayEquals(input, encoder.decode(expected)); 14 } 15 16 public void testWrapsLinesAt76Chars() { 17 byte input[] = ("hello world. hello world. hello world. hello world. hello world. hello world. hello world. " 18 + "hello world. hello world. hello world. hello world. hello world. hello world. hello world. ").getBytes(); 19 String expected = "aGVsbG8gd29ybGQuIGhlbGxvIHdvcmxkLiBoZWxsbyB3b3JsZC4gaGVsbG8gd29ybGQuIGhlbGxv\n" 20 + "IHdvcmxkLiBoZWxsbyB3b3JsZC4gaGVsbG8gd29ybGQuIGhlbGxvIHdvcmxkLiBoZWxsbyB3b3Js\n" 21 + "ZC4gaGVsbG8gd29ybGQuIGhlbGxvIHdvcmxkLiBoZWxsbyB3b3JsZC4gaGVsbG8gd29ybGQuIGhl\n" 22 + "bGxvIHdvcmxkLiA="; 23 assertEquals(expected, encoder.encode(input)); 24 assertByteArrayEquals(input, encoder.decode(expected)); 25 } 26 27 public void testPadsSingleMissingByteWhenNotMultipleOfThree() { 28 byte input[] = { 1, 2, 3, 4, 5 }; 29 String expected = "AQIDBAU="; 30 assertEquals(expected, encoder.encode(input)); 31 assertByteArrayEquals(input, encoder.decode(expected)); 32 } 33 34 public void testPadsDoubleMissingByteWhenNotMultipleOfThree() { 35 byte input[] = { 1, 2, 3, 4 }; 36 String expected = "AQIDBA=="; 37 assertEquals(expected, encoder.encode(input)); 38 assertByteArrayEquals(input, encoder.decode(expected)); 39 } 40 41 public void testDoesNotPadWhenMultipleOfThree() { 42 byte input[] = { 1, 2, 3, 4, 5, 6 }; 43 String expected = "AQIDBAUG"; 44 assertEquals(expected, encoder.encode(input)); 45 assertByteArrayEquals(input, encoder.decode(expected)); 46 } 47 48 public void testHandlesAllPositiveBytes() { 49 byte input[] = new byte[127]; 50 for (int i = 0; i < 126; i++) input[i] = (byte) (i + 1); 51 String expected = "AQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyAhIiMkJSYnKCkqKywtLi8wMTIzNDU2Nzg5\n" 52 + "Ojs8PT4/QEFCQ0RFRkdISUpLTE1OT1BRUlNUVVZXWFlaW1xdXl9gYWJjZGVmZ2hpamtsbW5vcHFy\n" 53 + "c3R1dnd4eXp7fH1+AA=="; 54 assertEquals(expected, encoder.encode(input)); 55 assertByteArrayEquals(input, encoder.decode(expected)); 56 } 57 58 public void testHandlesAllNegativeBytes() { 59 byte input[] = new byte[128]; 60 for (int i = 0; i < 127; i++) input[i] = (byte) (-1 - i); 61 String expected = "//79/Pv6+fj39vX08/Lx8O/u7ezr6uno5+bl5OPi4eDf3t3c29rZ2NfW1dTT0tHQz87NzMvKycjH\n" 62 + "xsXEw8LBwL++vby7urm4t7a1tLOysbCvrq2sq6qpqKempaSjoqGgn56dnJuamZiXlpWUk5KRkI+O\n" 63 + "jYyLiomIh4aFhIOCgQA="; 64 assertEquals(expected, encoder.encode(input)); 65 assertByteArrayEquals(input, encoder.decode(expected)); 66 } 67 68 public void testHandlesZeroByte() { 69 byte input[] = { 0, 0, 0, 0 }; 70 String expected = "AAAAAA=="; 71 assertEquals(expected, encoder.encode(input)); 72 assertByteArrayEquals(input, encoder.decode(expected)); 73 } 74 75 public void testProducesEmptyStringWhenNoBytesGiven() { 76 byte input[] = new byte[0]; 77 String expected = ""; 78 assertEquals(expected, encoder.encode(input)); 79 assertByteArrayEquals(input, encoder.decode(expected)); 80 } 81 82 } 83 | Popular Tags |