All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.Ostermiller.util.Base64Tests Maven / Gradle / Ivy

Go to download

Open source (GPL) Java utilities maintained by Stephen Ostermiller with help from many contributors.

The newest version!
/*
 * Copyright (C) 2004-2007 Stephen Ostermiller
 * http://ostermiller.org/contact.pl?regarding=Java+Utilities
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * See COPYING.TXT for details.
 */
package com.Ostermiller.util;

import java.util.*;

/**
 * Test cases for Base64
 */
class Base64Tests {

	private static class TestCase {
		private String encoded;
		private byte[] decoded;
		/**
		 * @param encoded
		 * @param decoded
		 */
		public TestCase(String encoded, byte[] decoded){
			this.encoded = encoded;
			this.decoded = decoded;
		}
		private void test() throws Exception {
			String enc = Base64.encodeToString(decoded);
			if (!encoded.equals(enc)){
				throw new Exception("Decoding problem, expected '" + encoded + "' got '" + enc + "'.");
			}
			byte[] b = Base64.decodeToBytes(encoded);
			if (!byteArraysEqual(b, decoded)){
				throw new Exception("Encoding problem, started with '" + encoded + "'.");
			}
		}
	}

	private static boolean byteArraysEqual(byte[] b1, byte[] b2){
		if (b1.length != b2.length) return false;
		for (int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy