import static org.testng.Assert.assertEquals; import java.nio.ByteBuffer; import java.nio.charset.Charset; import static java.nio.charset.StandardCharsets.UTF_8; import org.testng.annotations.Test; /* @test * @summary Test creating a String from a bytebuffer * @run testng StringByteBufferConversions */ public class StringByteBufferConversions { @Test(expectedExceptions = NullPointerException.class) public void nullByteBufferShouldThrowException() { // given ByteBuffer buffer = null; // when new String(buffer, 0, 1, UTF_8); } @Test(expectedExceptions = StringIndexOutOfBoundsException.class) public void undersizedByteBufferShouldThrowException() { // given ByteBuffer buffer = ByteBuffer.allocateDirect(5); // when new String(buffer, 0, 10, UTF_8); } @Test(expectedExceptions = NullPointerException.class) public void nullCharsetShouldThrowException() { // given ByteBuffer buffer = ByteBuffer.allocateDirect(20); // when new String(buffer, 0, 10, null); } @Test public void heapByteBuffersCanBeConvertedToStrings() { // given String expected = "Hello World"; ByteBuffer buffer = ByteBuffer.wrap(expected.getBytes(UTF_8)); // when String str = new String(buffer, 0, buffer.capacity(), UTF_8); // then assertEquals(str, expected); } @Test public void directByteBuffersCanBeConvertedToStrings() { // given String expected = "Hello World"; byte[] bytes = expected.getBytes(UTF_8); ByteBuffer buffer = ByteBuffer.allocateDirect(bytes.length) .put(bytes, 0, bytes.length); buffer.position(0); // when String str = new String(buffer, 0, buffer.capacity(), UTF_8); // then assertEquals(str, expected); } @Test public void chunksOfByteBuffersCanBeConvertedToStrings() { // given String expected = "Hello World"; byte[] bytes = expected.getBytes(UTF_8); int offset = 5; int length = offset + bytes.length + 5; ByteBuffer buffer = ByteBuffer.allocateDirect(length); buffer.position(offset); buffer.put(bytes, 0, bytes.length); buffer.position(0); // when String str = new String(buffer, offset, bytes.length, UTF_8); // then assertEquals(str, expected); } @Test(expectedExceptions = NullPointerException.class) public void getBytesThrowsExceptionWithNullBuffer() { "".getBytes((ByteBuffer)null, 0, UTF_8); } @Test(expectedExceptions = NullPointerException.class) public void getBytesThrowsExceptionWithNullByteArray() { "".getBytes((byte[])null, 0, UTF_8); } @Test(expectedExceptions = NullPointerException.class) public void getBytesThrowsExceptionWithNullCharset() { byte[] bytes = new byte[16]; "".getBytes(bytes, 0, null); } @Test public void shouldEncodeStringsOntoByteArrays() { byte[] bytes = new byte[11]; int written = "Hello World".getBytes(bytes, 0, UTF_8); assertEquals(written, 11); assertEquals(new String(bytes, UTF_8), "Hello World"); } @Test public void shouldEncodeStringsOntoByteBuffer() { ByteBuffer bytes = ByteBuffer.allocateDirect(12); int written = "Hello World".getBytes(bytes, 0, UTF_8); assertEquals(written, 11); assertEquals(new String(bytes, 0, written, UTF_8), "Hello World"); } @Test public void shouldEncodeStringsOntoChunkOfByteArray() { byte[] bytes = new byte[20]; final int offset = 5; int written = "Hello World".getBytes(bytes, offset, UTF_8); assertEquals(written, 11); assertEquals(new String(bytes, offset, written, UTF_8), "Hello World"); } @Test public void shouldEncodeStringsOntoChunkOfByteBuffer() { ByteBuffer bytes = ByteBuffer.allocateDirect(20); final int offset = 5; int written = "Hello World".getBytes(bytes, offset, UTF_8); assertEquals(written, 11); assertEquals(new String(bytes, offset, written, UTF_8), "Hello World"); } }