/** Tests edge conditions: null or empty arguments */ @Test public void testEdge() { assertNull(EncodingUtils.encodeBase64(null)); assertNull(EncodingUtils.decodeBase64(null)); String encoded = EncodingUtils.encodeBase64(decoded4); assertNotNull(encoded); assertEquals(encoded4, encoded); byte[] decoded = EncodingUtils.decodeBase64(encoded); compareArrays(decoded4, decoded); }
/** Tests encoding using hand-calculated examples. */ @Test public void testHandCoded() { String encoded = EncodingUtils.encodeBase64(decoded1.getBytes()); assertEquals(encoded1, encoded); byte[] bytes = EncodingUtils.decodeBase64(encoded2); String decoded = new String(bytes); assertEquals(decoded2, decoded); String testZeroes = EncodingUtils.encodeBase64(decoded3); assertEquals(encoded3, testZeroes); byte[] roundtripBytes = EncodingUtils.decodeBase64(testZeroes); compareArrays(decoded3, roundtripBytes); byte[] bytesInvalidChars = EncodingUtils.decodeBase64(encoded5); String decodedInvalidChars = new String(bytesInvalidChars); assertEquals(decoded5, decodedInvalidChars); String shortSample = EncodingUtils.encodeBase64(decoded6.getBytes()); assertEquals(encoded6, shortSample); assertEquals(decoded6, new String(EncodingUtils.decodeBase64(shortSample))); }
/** Tests round trip using large random sequences */ @Test public void testRandom() { Random generator = new Random(System.currentTimeMillis()); for (int i = 0; i < RANDOM_ITERATIONS; i++) { // length of array is random in [100, 1000) int length = 100 + generator.nextInt(900); byte[] bytes = new byte[length]; generator.nextBytes(bytes); // round trip String encoded = EncodingUtils.encodeBase64(bytes); byte[] decoded = EncodingUtils.decodeBase64(encoded); compareArrays(bytes, decoded); } }