|
61 | 61 | import java.util.Random; |
62 | 62 |
|
63 | 63 | import junit.framework.TestCase; |
| 64 | +import org.apache.commons.codec.DecoderException; |
| 65 | +import org.apache.commons.codec.EncoderException; |
64 | 66 |
|
65 | 67 | /** |
| 68 | + * Tests {@link org.apache.commons.codec.binary.Hex}. |
66 | 69 | * |
67 | 70 | * @author <a href="mailto:siege@preoccupied.net">Christopher O'Brien</a> |
68 | 71 | * @author Tim O'Brien |
| 72 | + * @author Gary Gregory |
| 73 | + * @version $Id: HexTest.java,v 1.6 2003/11/03 19:04:34 ggregory Exp $ |
69 | 74 | */ |
70 | 75 |
|
71 | 76 | public class HexTest extends TestCase { |
72 | 77 |
|
73 | 78 | public HexTest(String name) { |
74 | 79 | super(name); |
75 | 80 | } |
| 81 | + |
| 82 | + public void testDecodeArrayOddCharacters() { |
| 83 | + try { |
| 84 | + new Hex().decode(new byte[] { 65 }); |
| 85 | + fail("An exception wasn't thrown when trying to decode an odd number of characters"); |
| 86 | + } |
| 87 | + catch (DecoderException e) { |
| 88 | + // Expected exception |
| 89 | + } |
| 90 | + } |
76 | 91 |
|
77 | | - public void testEncodeEmpty() throws Exception { |
78 | | - char[] c = Hex.encodeHex(new byte[0]); |
79 | | - assertTrue(Arrays.equals(new char[0], c)); |
| 92 | + public void testDecodeClassCastException() { |
| 93 | + try { |
| 94 | + new Hex().decode(new int[] { 65 }); |
| 95 | + fail("An exception wasn't thrown when trying to decode."); |
| 96 | + } |
| 97 | + catch (DecoderException e) { |
| 98 | + // Expected exception |
| 99 | + } |
80 | 100 | } |
81 | 101 |
|
82 | | - public void testEncodeZeroes() throws Exception { |
83 | | - char[] c = Hex.encodeHex(new byte[36]); |
84 | | - assertEquals( |
85 | | - "000000000000000000000000000000000000" |
86 | | - + "000000000000000000000000000000000000", |
87 | | - new String(c)); |
| 102 | + public void testDecodeHexOddCharacters() { |
| 103 | + try { |
| 104 | + Hex.decodeHex(new char[] { 'A' }); |
| 105 | + fail("An exception wasn't thrown when trying to decode an odd number of characters"); |
| 106 | + } |
| 107 | + catch (DecoderException e) { |
| 108 | + // Expected exception |
| 109 | + } |
88 | 110 | } |
89 | 111 |
|
90 | | - public void testHelloWorld() throws Exception { |
91 | | - byte[] b = "Hello World".getBytes(); |
92 | | - char[] c = Hex.encodeHex(b); |
93 | | - assertEquals("48656c6c6f20576f726c64", new String(c)); |
| 112 | + public void testDecodeStringOddCharacters() { |
| 113 | + try { |
| 114 | + new Hex().decode("6"); |
| 115 | + fail("An exception wasn't thrown when trying to decode an odd number of characters"); |
| 116 | + } |
| 117 | + catch (DecoderException e) { |
| 118 | + // Expected exception |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + public void testDencodeEmpty() throws DecoderException { |
| 123 | + assertTrue(Arrays.equals(new byte[0], Hex.decodeHex(new char[0]))); |
| 124 | + assertTrue(Arrays.equals(new byte[0], new Hex().decode(new byte[0]))); |
| 125 | + assertTrue(Arrays.equals(new byte[0], (byte[])new Hex().decode(""))); |
| 126 | + } |
| 127 | + |
| 128 | + public void testEncodeClassCastException() { |
| 129 | + try { |
| 130 | + new Hex().encode(new int[] { 65 }); |
| 131 | + fail("An exception wasn't thrown when trying to encode."); |
| 132 | + } |
| 133 | + catch (EncoderException e) { |
| 134 | + // Expected exception |
| 135 | + } |
94 | 136 | } |
95 | 137 |
|
96 | | - public void testEncodeDecodeRandom() throws Exception { |
| 138 | + public void testEncodeDecodeRandom() throws DecoderException, EncoderException { |
97 | 139 | Random random = new Random(); |
98 | 140 |
|
| 141 | + Hex hex = new Hex(); |
99 | 142 | for (int i = 5; i > 0; i--) { |
100 | 143 | byte[] data = new byte[random.nextInt(10000) + 1]; |
101 | 144 | random.nextBytes(data); |
102 | 145 |
|
103 | | - char[] enc = Hex.encodeHex(data); |
104 | | - byte[] data2 = Hex.decodeHex(enc); |
105 | | - |
106 | | - assertTrue(Arrays.equals(data, data2)); |
| 146 | + // static API |
| 147 | + char[] encodedChars = Hex.encodeHex(data); |
| 148 | + byte[] decodedBytes = Hex.decodeHex(encodedChars); |
| 149 | + assertTrue(Arrays.equals(data, decodedBytes)); |
| 150 | + |
| 151 | + // instance API with array parameter |
| 152 | + byte[] encodedStringBytes = hex.encode(data); |
| 153 | + decodedBytes = hex.decode(encodedStringBytes); |
| 154 | + assertTrue(Arrays.equals(data, decodedBytes)); |
| 155 | + |
| 156 | + // instance API with char[] (Object) parameter |
| 157 | + String dataString = new String(encodedChars); |
| 158 | + char[] encodedStringChars = (char[])hex.encode(dataString); |
| 159 | + decodedBytes = (byte[])hex.decode(encodedStringChars); |
| 160 | + assertTrue(Arrays.equals(dataString.getBytes(), decodedBytes)); |
| 161 | + |
| 162 | + // instance API with String (Object) parameter |
| 163 | + dataString = new String(encodedChars); |
| 164 | + encodedStringChars = (char[])hex.encode(dataString); |
| 165 | + decodedBytes = (byte[])hex.decode(new String(encodedStringChars)); |
| 166 | + assertTrue(Arrays.equals(dataString.getBytes(), decodedBytes)); |
107 | 167 | } |
108 | 168 | } |
109 | 169 |
|
110 | | - public void testOddCharacters() throws Exception { |
111 | | - |
112 | | - boolean exceptionThrown = false; |
113 | | - |
114 | | - try { |
115 | | - char[] singleChar = new char[1]; |
116 | | - singleChar[0] = 'a'; |
117 | | - |
118 | | - Hex.decodeHex( singleChar ); |
119 | | - } |
120 | | - catch (Exception e) { |
121 | | - exceptionThrown = true; |
122 | | - } |
123 | | - |
124 | | - assertTrue( "An exception wasn't thrown when trying to " + |
125 | | - "decode an odd number of characters", exceptionThrown ); |
126 | | - |
| 170 | + public void testEncodeEmpty() throws EncoderException { |
| 171 | + assertTrue(Arrays.equals(new char[0], Hex.encodeHex(new byte[0]))); |
| 172 | + assertTrue(Arrays.equals(new byte[0], new Hex().encode(new byte[0]))); |
| 173 | + assertTrue(Arrays.equals(new char[0], (char[])new Hex().encode(""))); |
| 174 | + } |
127 | 175 |
|
| 176 | + public void testEncodeZeroes() { |
| 177 | + char[] c = Hex.encodeHex(new byte[36]); |
| 178 | + assertEquals( |
| 179 | + "000000000000000000000000000000000000" |
| 180 | + + "000000000000000000000000000000000000", |
| 181 | + new String(c)); |
128 | 182 | } |
129 | 183 |
|
| 184 | + public void testHelloWorld() { |
| 185 | + byte[] b = "Hello World".getBytes(); |
| 186 | + char[] c = Hex.encodeHex(b); |
| 187 | + assertEquals("48656c6c6f20576f726c64", new String(c)); |
| 188 | + } |
130 | 189 | } |
0 commit comments