Skip to content

Commit a9e682d

Browse files
committed
[CODEC-259] getByteBufferUtf8() method to use allocate(int).
This allows the AllocateDirectHexTest class to test the methods that require a ByteBuffer allocated with a UTF-8 string with a direct byte buffer.
1 parent 2311c5e commit a9e682d

1 file changed

Lines changed: 30 additions & 3 deletions

File tree

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.io.UnsupportedEncodingException;
2626
import java.nio.ByteBuffer;
2727
import java.nio.charset.Charset;
28+
import java.nio.charset.StandardCharsets;
2829
import java.nio.charset.UnsupportedCharsetException;
2930
import java.util.Arrays;
3031
import java.util.Random;
@@ -45,10 +46,36 @@ public class HexTest {
4546

4647
private final static boolean LOG = false;
4748

49+
/**
50+
* Allocate a ByteBuffer.
51+
*
52+
* <p>The default implementation uses {@link ByteBuffer#allocate(int)}.
53+
* The method is overridden in AllocateDirectHexTest to use
54+
* {@link ByteBuffer#allocateDirect(int)}
55+
*
56+
* @param capacity the capacity
57+
* @return the byte buffer
58+
*/
4859
protected ByteBuffer allocate(final int capacity) {
4960
return ByteBuffer.allocate(capacity);
5061
}
5162

63+
/**
64+
* Encodes the given string into a byte buffer using the UTF-8 charset.
65+
*
66+
* <p>The buffer is allocated using {@link #allocate(int)}.
67+
*
68+
* @param string the String to encode
69+
* @return the byte buffer
70+
*/
71+
private ByteBuffer getByteBufferUtf8(String string) {
72+
final byte[] bytes = string.getBytes(StandardCharsets.UTF_8);
73+
final ByteBuffer bb = allocate(bytes.length);
74+
bb.put(bytes);
75+
bb.flip();
76+
return bb;
77+
}
78+
5279
private boolean charsetSanityCheck(final String name) {
5380
final String source = "the quick brown dog jumped over the lazy fox";
5481
try {
@@ -331,7 +358,7 @@ public void testDecodeStringEmpty() throws DecoderException {
331358

332359
@Test
333360
public void testDecodeByteBufferWithLimit() throws DecoderException {
334-
final ByteBuffer bb = StringUtils.getByteBufferUtf8("000102030405060708090a0b0c0d0e0f");
361+
final ByteBuffer bb = getByteBufferUtf8("000102030405060708090a0b0c0d0e0f");
335362
final byte[] expected = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
336363
// Test pairs of bytes
337364
for (int i = 0; i < 15; i++) {
@@ -460,7 +487,7 @@ public void testEncodeHexByteBufferEmpty() {
460487

461488
@Test
462489
public void testEncodeHexByteBufferHelloWorldLowerCaseHex() {
463-
final ByteBuffer b = StringUtils.getByteBufferUtf8("Hello World");
490+
final ByteBuffer b = getByteBufferUtf8("Hello World");
464491
final String expected = "48656c6c6f20576f726c64";
465492
char[] actual;
466493
// Default lower-case
@@ -481,7 +508,7 @@ public void testEncodeHexByteBufferHelloWorldLowerCaseHex() {
481508

482509
@Test
483510
public void testEncodeHexByteBufferHelloWorldUpperCaseHex() {
484-
final ByteBuffer b = StringUtils.getByteBufferUtf8("Hello World");
511+
final ByteBuffer b = getByteBufferUtf8("Hello World");
485512
final String expected = "48656C6C6F20576F726C64";
486513
char[] actual;
487514
// Default lower-case

0 commit comments

Comments
 (0)