|
17 | 17 |
|
18 | 18 | package org.apache.commons.codec.binary; |
19 | 19 |
|
| 20 | +import static java.nio.charset.StandardCharsets.UTF_8; |
20 | 21 | import static org.junit.Assert.assertArrayEquals; |
21 | 22 | import static org.junit.Assert.assertEquals; |
22 | 23 | import static org.junit.Assert.assertFalse; |
@@ -69,7 +70,7 @@ protected ByteBuffer allocate(final int capacity) { |
69 | 70 | * @return the byte buffer |
70 | 71 | */ |
71 | 72 | private ByteBuffer getByteBufferUtf8(final String string) { |
72 | | - final byte[] bytes = string.getBytes(StandardCharsets.UTF_8); |
| 73 | + final byte[] bytes = string.getBytes(UTF_8); |
73 | 74 | final ByteBuffer bb = allocate(bytes.length); |
74 | 75 | bb.put(bytes); |
75 | 76 | bb.flip(); |
@@ -583,6 +584,48 @@ public void testEncodeHex_ByteBufferWithLimit() { |
583 | 584 | } |
584 | 585 | } |
585 | 586 |
|
| 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 | + |
586 | 629 | @Test |
587 | 630 | public void testEncodeHexByteString_ByteBufferOfZeroes() { |
588 | 631 | final String c = Hex.encodeHexString(allocate(36)); |
@@ -670,12 +713,12 @@ public void testEncodeStringEmpty() throws EncoderException { |
670 | 713 |
|
671 | 714 | @Test |
672 | 715 | 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()); |
674 | 717 | } |
675 | 718 |
|
676 | 719 | @Test |
677 | 720 | 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()); |
679 | 722 | } |
680 | 723 |
|
681 | 724 | @Test |
|
0 commit comments