Skip to content

Commit 878d96c

Browse files
committed
Add missing tests
1 parent c1bcf38 commit 878d96c

1 file changed

Lines changed: 46 additions & 3 deletions

File tree

src/test/java/org/apache/commons/codec/binary/HexTest.java

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.apache.commons.codec.binary;
1919

20+
import static java.nio.charset.StandardCharsets.UTF_8;
2021
import static org.junit.Assert.assertArrayEquals;
2122
import static org.junit.Assert.assertEquals;
2223
import static org.junit.Assert.assertFalse;
@@ -69,7 +70,7 @@ protected ByteBuffer allocate(final int capacity) {
6970
* @return the byte buffer
7071
*/
7172
private ByteBuffer getByteBufferUtf8(final String string) {
72-
final byte[] bytes = string.getBytes(StandardCharsets.UTF_8);
73+
final byte[] bytes = string.getBytes(UTF_8);
7374
final ByteBuffer bb = allocate(bytes.length);
7475
bb.put(bytes);
7576
bb.flip();
@@ -583,6 +584,48 @@ public void testEncodeHex_ByteBufferWithLimit() {
583584
}
584585
}
585586

587+
@Test
588+
public void testEncodeHexPartialInput() {
589+
final byte data[] = "hello world".getBytes(UTF_8);
590+
591+
char[] hex = Hex.encodeHex(data, 0, 0, true);
592+
assertArrayEquals(new char[0], hex);
593+
594+
hex = Hex.encodeHex(data, 0, 1, true);
595+
assertArrayEquals("68".toCharArray(), hex);
596+
597+
hex = Hex.encodeHex(data, 0, 1, false);
598+
assertArrayEquals("68".toCharArray(), hex);
599+
600+
hex = Hex.encodeHex(data, 2, 4, true);
601+
assertArrayEquals("6c6c6f20".toCharArray(), hex);
602+
603+
hex = Hex.encodeHex(data, 2, 4, false);
604+
assertArrayEquals("6C6C6F20".toCharArray(), hex);
605+
606+
hex = Hex.encodeHex(data, 10, 1, true);
607+
assertArrayEquals("64".toCharArray(), hex);
608+
609+
hex = Hex.encodeHex(data, 10, 1, false);
610+
assertArrayEquals("64".toCharArray(), hex);
611+
}
612+
613+
@Test(expected=ArrayIndexOutOfBoundsException.class)
614+
public void testEncodeHexPartialInputUnderbounds() {
615+
final byte data[] = "hello world".getBytes(UTF_8);
616+
617+
final char[] hex = Hex.encodeHex(data, -2, 10, true);
618+
assertArrayEquals("64".toCharArray(), hex);
619+
}
620+
621+
@Test(expected=ArrayIndexOutOfBoundsException.class)
622+
public void testEncodeHexPartialInputOverbounds() {
623+
final byte data[] = "hello world".getBytes(UTF_8);
624+
625+
final char[] hex = Hex.encodeHex(data, 9, 10, true);
626+
assertArrayEquals("64".toCharArray(), hex);
627+
}
628+
586629
@Test
587630
public void testEncodeHexByteString_ByteBufferOfZeroes() {
588631
final String c = Hex.encodeHexString(allocate(36));
@@ -670,12 +713,12 @@ public void testEncodeStringEmpty() throws EncoderException {
670713

671714
@Test
672715
public void testGetCharset() {
673-
Assert.assertEquals(StandardCharsets.UTF_8, new Hex(StandardCharsets.UTF_8).getCharset());
716+
Assert.assertEquals(UTF_8, new Hex(UTF_8).getCharset());
674717
}
675718

676719
@Test
677720
public void testGetCharsetName() {
678-
Assert.assertEquals(StandardCharsets.UTF_8.name(), new Hex(StandardCharsets.UTF_8).getCharsetName());
721+
Assert.assertEquals(UTF_8.name(), new Hex(UTF_8).getCharsetName());
679722
}
680723

681724
@Test

0 commit comments

Comments
 (0)