1 16 17 package org.apache.commons.codec.binary; 18 19 import java.util.Arrays ; 20 import java.util.Random ; 21 22 import junit.framework.TestCase; 23 import org.apache.commons.codec.DecoderException; 24 import org.apache.commons.codec.EncoderException; 25 26 32 33 public class HexTest extends TestCase { 34 35 public HexTest(String name) { 36 super(name); 37 } 38 39 public void testDecodeArrayOddCharacters() { 40 try { 41 new Hex().decode(new byte[] { 65 }); 42 fail("An exception wasn't thrown when trying to decode an odd number of characters"); 43 } 44 catch (DecoderException e) { 45 } 47 } 48 49 public void testDecodeBadCharacterPos0() { 50 try { 51 new Hex().decode("q0"); 52 fail("An exception wasn't thrown when trying to decode an illegal character"); 53 } 54 catch (DecoderException e) { 55 } 57 } 58 59 public void testDecodeBadCharacterPos1() { 60 try { 61 new Hex().decode("0q"); 62 fail("An exception wasn't thrown when trying to decode an illegal character"); 63 } 64 catch (DecoderException e) { 65 } 67 } 68 69 public void testDecodeClassCastException() { 70 try { 71 new Hex().decode(new int[] { 65 }); 72 fail("An exception wasn't thrown when trying to decode."); 73 } 74 catch (DecoderException e) { 75 } 77 } 78 79 public void testDecodeHexOddCharacters() { 80 try { 81 Hex.decodeHex(new char[] { 'A' }); 82 fail("An exception wasn't thrown when trying to decode an odd number of characters"); 83 } 84 catch (DecoderException e) { 85 } 87 } 88 89 public void testDecodeStringOddCharacters() { 90 try { 91 new Hex().decode("6"); 92 fail("An exception wasn't thrown when trying to decode an odd number of characters"); 93 } 94 catch (DecoderException e) { 95 } 97 } 98 99 public void testDencodeEmpty() throws DecoderException { 100 assertTrue(Arrays.equals(new byte[0], Hex.decodeHex(new char[0]))); 101 assertTrue(Arrays.equals(new byte[0], new Hex().decode(new byte[0]))); 102 assertTrue(Arrays.equals(new byte[0], (byte[])new Hex().decode(""))); 103 } 104 105 public void testEncodeClassCastException() { 106 try { 107 new Hex().encode(new int[] { 65 }); 108 fail("An exception wasn't thrown when trying to encode."); 109 } 110 catch (EncoderException e) { 111 } 113 } 114 115 public void testEncodeDecodeRandom() throws DecoderException, EncoderException { 116 Random random = new Random (); 117 118 Hex hex = new Hex(); 119 for (int i = 5; i > 0; i--) { 120 byte[] data = new byte[random.nextInt(10000) + 1]; 121 random.nextBytes(data); 122 123 char[] encodedChars = Hex.encodeHex(data); 125 byte[] decodedBytes = Hex.decodeHex(encodedChars); 126 assertTrue(Arrays.equals(data, decodedBytes)); 127 128 byte[] encodedStringBytes = hex.encode(data); 130 decodedBytes = hex.decode(encodedStringBytes); 131 assertTrue(Arrays.equals(data, decodedBytes)); 132 133 String dataString = new String (encodedChars); 135 char[] encodedStringChars = (char[])hex.encode(dataString); 136 decodedBytes = (byte[])hex.decode(encodedStringChars); 137 assertTrue(Arrays.equals(dataString.getBytes(), decodedBytes)); 138 139 dataString = new String (encodedChars); 141 encodedStringChars = (char[])hex.encode(dataString); 142 decodedBytes = (byte[])hex.decode(new String (encodedStringChars)); 143 assertTrue(Arrays.equals(dataString.getBytes(), decodedBytes)); 144 } 145 } 146 147 public void testEncodeEmpty() throws EncoderException { 148 assertTrue(Arrays.equals(new char[0], Hex.encodeHex(new byte[0]))); 149 assertTrue(Arrays.equals(new byte[0], new Hex().encode(new byte[0]))); 150 assertTrue(Arrays.equals(new char[0], (char[])new Hex().encode(""))); 151 } 152 153 public void testEncodeZeroes() { 154 char[] c = Hex.encodeHex(new byte[36]); 155 assertEquals( 156 "000000000000000000000000000000000000" 157 + "000000000000000000000000000000000000", 158 new String (c)); 159 } 160 161 public void testHelloWorld() { 162 byte[] b = "Hello World".getBytes(); 163 char[] c = Hex.encodeHex(b); 164 assertEquals("48656c6c6f20576f726c64", new String (c)); 165 } 166 } 167 | Popular Tags |