1 64 65 package com.jcorporate.expresso.core.misc.tests; 66 67 import com.jcorporate.expresso.core.misc.HexEncoder; 68 import com.jcorporate.expresso.services.test.CommandLineParser; 69 import com.jcorporate.expresso.services.test.ExpressoTestCase; 70 import junit.framework.TestSuite; 71 72 73 80 public class HexEncoderTest 81 extends ExpressoTestCase { 82 public HexEncoderTest(String testName) 83 throws Exception { 84 super(testName); 85 } 86 87 public static void main(String [] args) 88 throws Exception { 89 90 CommandLineParser.parseCommandLine(args); 92 junit.textui.TestRunner.run(suite()); 93 } 94 95 98 public static junit.framework.Test suite() { 99 return new TestSuite(HexEncoderTest.class); 100 } 101 102 109 private static boolean arraysAreEqual(byte[] array1, byte[] array2) { 110 if (array1.length != array2.length) { 111 return false; 112 } 113 114 int len = array1.length; 115 116 for (int i = 0; i < len; i++) { 117 if (array1[i] != array2[i]) { 118 return false; 119 } 120 } 121 122 return true; 123 } 124 125 131 public void testHexEncode() 132 throws Exception { 133 byte[][] testArray = { 134 {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {5, 6, 7, 8, 9, 0, 35, 62, 33}, 135 {(byte) 0x0A, (byte) 0xFF, (byte) 0xCA, (byte) 0xB1, (byte) 0x00, (byte) 0x42} 136 }; 137 138 for (int i = 0; i < testArray.length; i++) { 139 byte[] test = testArray[i]; 140 String encoded = HexEncoder.encode(test); 141 byte[] decoded = HexEncoder.decode(encoded); 142 assertTrue("Arrays Must Be Equal: i=" + i, 143 arraysAreEqual(test, decoded)); 144 } 145 } 146 147 151 public void testBadInputs() 152 throws Exception { 153 String [] breakDecode = {"ABA", "", "ABCDEFHLKAMABACD"}; 154 155 for (int i = 0; i < breakDecode.length; i++) { 156 try { 157 byte[] decoded = HexEncoder.decode(breakDecode[i]); 158 fail("Didn't handle bad input " + breakDecode[i]); 159 } catch (IllegalArgumentException ex) { 160 } 161 } 162 } 163 } | Popular Tags |