1 64 65 package com.jcorporate.expresso.core.misc.tests; 66 67 import com.jcorporate.expresso.core.misc.Base64; 68 import com.jcorporate.expresso.services.test.CommandLineParser; 69 import com.jcorporate.expresso.services.test.ExpressoTestCase; 70 import junit.framework.TestSuite; 71 72 73 79 public class Base64Test 80 extends ExpressoTestCase { 81 final String [] testString = { 82 "a", "ab", "abc", "abcd", "abcde", "ABC", "ABCDE", "abcdef", "abcdefg", 83 "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ!@", 84 "#$%^&*()_+1234567890<>,.:\"\\" 85 }; 86 final byte[] test3 = { 87 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -1, 120, 54, -125, 30, (byte) 0xFF 88 }; 89 90 public Base64Test(String testName) 91 throws Exception { 92 super(testName); 93 } 94 95 public static void main(String [] args) 96 throws Exception { 97 98 CommandLineParser.parseCommandLine(args); 100 junit.textui.TestRunner.run(suite()); 101 } 102 103 106 public static junit.framework.Test suite() { 107 return new TestSuite(Base64Test.class); 108 } 109 110 113 public void testBase64WithPadding() { 114 int i; 115 116 for (i = 0; i < testString.length; i++) { 117 String arrayPartEncode = Base64.encode(testString[i].getBytes()); 118 String arrayPartDecode = new String (Base64.decode(arrayPartEncode)); 119 assertTrue("Base64 Test: Test Data " + testString[i], 120 arrayPartDecode.equals(testString[i])); 121 assertTrue("Greater Than Zero Length Return " + testString[i], 122 arrayPartEncode.length() > 0); 123 } 124 125 String encode3 = Base64.encode(test3); 127 byte[] decode3 = Base64.decode(encode3); 128 129 for (i = 0; i < decode3.length; i++) { 130 assertTrue("Binary Input Data i=" + Integer.toString(i), 131 test3[i] == decode3[i]); 132 } 133 } 134 135 138 public void testBase64NoPadding() { 139 int i; 140 141 for (i = 0; i < testString.length; i++) { 142 String arrayPartEncode = Base64.encodeNoPadding(testString[i].getBytes()); 143 String arrayPartDecode = new String (Base64.decodeNoPadding(arrayPartEncode)); 144 assertTrue("Base64 Test: Test Data " + testString[i], 145 arrayPartDecode.equals(testString[i])); 146 assertTrue("Greater Than Zero Length Return " + testString[i], 147 arrayPartEncode.length() > 0); 148 assertTrue("No Padding", arrayPartEncode.indexOf('=') == -1); 149 } 150 151 String encode3 = Base64.encodeNoPadding(test3); 153 byte[] decode3 = Base64.decodeNoPadding(encode3); 154 assertTrue("Greater Than Zero Length Return [binary]", 155 decode3.length > 0); 156 157 for (i = 0; i < decode3.length; i++) { 158 assertTrue("Binary Input Data i=" + Integer.toString(i), 159 test3[i] == decode3[i]); 160 } 161 } 162 } | Popular Tags |