|
| 1 | +package com.baeldung.algorithms.conversion; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertArrayEquals; |
| 4 | +import static org.junit.Assert.assertEquals; |
| 5 | +import static org.junit.Assert.assertThat; |
| 6 | + |
| 7 | +import org.apache.commons.codec.DecoderException; |
| 8 | +import org.hamcrest.text.IsEqualIgnoringCase; |
| 9 | +import org.junit.Before; |
| 10 | +import org.junit.Test; |
| 11 | + |
| 12 | +import com.baeldung.algorithms.conversion.HexStringConverter; |
| 13 | + |
| 14 | +public class ByteArrayConverterUnitTest { |
| 15 | + |
| 16 | + private HexStringConverter hexStringConverter; |
| 17 | + |
| 18 | + @Before |
| 19 | + public void setup() { |
| 20 | + hexStringConverter = new HexStringConverter(); |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + public void shouldEncodeByteArrayToHexStringUsingBigIntegerToString() { |
| 25 | + byte[] bytes = getSampleBytes(); |
| 26 | + String hexString = getSampleHexString(); |
| 27 | + if(hexString.charAt(0) == '0') { |
| 28 | + hexString = hexString.substring(1); |
| 29 | + } |
| 30 | + String output = hexStringConverter.encodeUsingBigIntegerToString(bytes); |
| 31 | + assertThat(output, IsEqualIgnoringCase.equalToIgnoringCase(hexString)); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void shouldEncodeByteArrayToHexStringUsingBigIntegerStringFormat() { |
| 36 | + byte[] bytes = getSampleBytes(); |
| 37 | + String hexString = getSampleHexString(); |
| 38 | + String output = hexStringConverter.encodeUsingBigIntegerStringFormat(bytes); |
| 39 | + assertThat(output, IsEqualIgnoringCase.equalToIgnoringCase(hexString)); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void shouldDecodeHexStringToByteArrayUsingBigInteger() { |
| 44 | + byte[] bytes = getSampleBytes(); |
| 45 | + String hexString = getSampleHexString(); |
| 46 | + byte[] output = hexStringConverter.decodeUsingBigInteger(hexString); |
| 47 | + assertArrayEquals(bytes, output); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void shouldEncodeByteArrayToHexStringUsingCharacterConversion() { |
| 52 | + byte[] bytes = getSampleBytes(); |
| 53 | + String hexString = getSampleHexString(); |
| 54 | + String output = hexStringConverter.encodeHexString(bytes); |
| 55 | + assertThat(output, IsEqualIgnoringCase.equalToIgnoringCase(hexString)); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void shouldDecodeHexStringToByteArrayUsingCharacterConversion() { |
| 60 | + byte[] bytes = getSampleBytes(); |
| 61 | + String hexString = getSampleHexString(); |
| 62 | + byte[] output = hexStringConverter.decodeHexString(hexString); |
| 63 | + assertArrayEquals(bytes, output); |
| 64 | + } |
| 65 | + |
| 66 | + @Test(expected=IllegalArgumentException.class) |
| 67 | + public void shouldDecodeHexToByteWithInvalidHexCharacter() { |
| 68 | + hexStringConverter.hexToByte("fg"); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void shouldEncodeByteArrayToHexStringDataTypeConverter() { |
| 73 | + byte[] bytes = getSampleBytes(); |
| 74 | + String hexString = getSampleHexString(); |
| 75 | + String output = hexStringConverter.encodeUsingDataTypeConverter(bytes); |
| 76 | + assertThat(output, IsEqualIgnoringCase.equalToIgnoringCase(hexString)); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void shouldDecodeHexStringToByteArrayUsingDataTypeConverter() { |
| 81 | + byte[] bytes = getSampleBytes(); |
| 82 | + String hexString = getSampleHexString(); |
| 83 | + byte[] output = hexStringConverter.decodeUsingDataTypeConverter(hexString); |
| 84 | + assertArrayEquals(bytes, output); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void shouldEncodeByteArrayToHexStringUsingGuava() { |
| 89 | + byte[] bytes = getSampleBytes(); |
| 90 | + String hexString = getSampleHexString(); |
| 91 | + String output = hexStringConverter.encodeUsingGuava(bytes); |
| 92 | + assertThat(output, IsEqualIgnoringCase.equalToIgnoringCase(hexString)); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void shouldDecodeHexStringToByteArrayUsingGuava() { |
| 97 | + byte[] bytes = getSampleBytes(); |
| 98 | + String hexString = getSampleHexString(); |
| 99 | + byte[] output = hexStringConverter.decodeUsingGuava(hexString); |
| 100 | + assertArrayEquals(bytes, output); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void shouldEncodeByteArrayToHexStringUsingApacheCommons() throws DecoderException { |
| 105 | + byte[] bytes = getSampleBytes(); |
| 106 | + String hexString = getSampleHexString(); |
| 107 | + String output = hexStringConverter.encodeUsingApacheCommons(bytes); |
| 108 | + assertThat(output, IsEqualIgnoringCase.equalToIgnoringCase(hexString)); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void shouldDecodeHexStringToByteArrayUsingApacheCommons() throws DecoderException { |
| 113 | + byte[] bytes = getSampleBytes(); |
| 114 | + String hexString = getSampleHexString(); |
| 115 | + byte[] output = hexStringConverter.decodeUsingApacheCommons(hexString); |
| 116 | + assertArrayEquals(bytes, output); |
| 117 | + } |
| 118 | + |
| 119 | + private String getSampleHexString() { |
| 120 | + return "0af50c0e2d10"; |
| 121 | + } |
| 122 | + |
| 123 | + private byte[] getSampleBytes() { |
| 124 | + return new byte[] { 10, -11, 12, 14, 45, 16 }; |
| 125 | + } |
| 126 | + |
| 127 | +} |
0 commit comments